-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPlayer.java
More file actions
157 lines (140 loc) · 5.1 KB
/
Player.java
File metadata and controls
157 lines (140 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import java.util.*;
public class Player {
private String name;
private int chips;
private boolean in;
public static Scanner sc = new Scanner(System.in);
public Player(String name) {
if (name.length() > 0)
this.name = name.substring(0, 1).toUpperCase() + name.substring(1);
else
this.name = Names.getUniqueName(null);
chips = 1000;
in = true;
}
public int[] action() { // length 2
return null;
}
public int getChips() {
return chips;
}
public void setChips(int chips) {
this.chips = chips;
}
public String getName() {
return name;
}
public boolean inHand() {
return in;
}
public void addChips(int chips) {
this.chips += chips;
}
public int removeChips(int chips) {
if (chips > this.chips) {
int taken = this.chips;
this.chips = 0;
return taken;
}
this.chips -= chips;
return chips;
}
public void setName(String name) {
this.name = name;
}
public void setInHand(boolean in) {
this.in = in;
}
public static int getValidInt(String message, int min, int max) {
return getValidInt(message, min, max, false, false);
}
public static int getValidInt(String message, int min, int max, boolean allowBack) {
return getValidInt(message, min, max, allowBack, false);
}
public static int getValidInt(String message, int min, int max, boolean allowBack, boolean lastManStanding) { // continuously
// prompt
// user
// for
// valid
// int
// given
// range
// and
// message
// to
// keep
// prompting
// with
int x;
if (!allowBack)
Utils.flushInput();
while (true) {
System.out.println(message + (allowBack ? " [B] to go back" : ""));
try {
String z = sc.nextLine().trim();
if (z.toLowerCase().trim().equals("q"))
System.exit(0);
if (allowBack && z.toLowerCase().trim().equals("b"))
return -1;
if (z.isEmpty() || !z.matches("-?\\d+")) {
if (lastManStanding)
return 0;
throw new NumberFormatException();
}
x = Integer.parseInt(z);
if (x >= min && x <= max)
break;
else
System.out.print("Not within specified bounds! ");
} catch (Exception e) {
System.out.print("Not an integer! ");
continue;
}
}
return x;
}
public static String getValidStr(String message, int min, int max) { // continusoly prompt user for valid string
// between certain length
int x;
String r;
while (true) {
System.out.println(message);
try {
String z = sc.nextLine().trim();
if (z.toLowerCase().trim().equals("q"))
System.exit(0);
x = z.length();
if (x >= min && x <= max) {
r = z;
break;
} else
System.out.print("Not within specified length! ");
} catch (Exception e) {
continue;
}
}
return r;
}
public static double getValidDouble(String message, double min, double max, double defaultVal) {
while (true) {
System.out.println(message + " [default: " + defaultVal + "]");
try {
String z = sc.nextLine().trim();
if (z.toLowerCase().equals("q"))
System.exit(0);
if (z.isEmpty())
return defaultVal;
double x = Double.parseDouble(z);
if (x >= min && x <= max)
return x;
else
System.out.print("Not within specified bounds (" + min + " - " + max + ")! ");
} catch (Exception e) {
System.out.print("Not a valid number! ");
}
}
}
public String toString() {
return name + ": ✨" + chips;
}
}