-
Notifications
You must be signed in to change notification settings - Fork 0
/
Player.java
209 lines (179 loc) · 5.1 KB
/
Player.java
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import java.io.Console;
import java.security.InvalidParameterException;
import java.util.Scanner;
public class Player {
private Console console;
private Scanner scanner;
private Hand hand;
public Round.blind blind;
public double money, personalPotValue;
private String name, password;
public boolean allIn, called, folded;
private int wins;
public Player(String name) {
this.name = name;
hand = new Hand();
for (Card card : hand.cards) {
card.setPlayerHash(this.hashCode());
}
money = Constants.startingMoney;
scanner = new Scanner(System.in);
console = System.console();
}
@Override
public String toString() {
return name;
}
public Hand getHand() {
return hand;
}
public void setMoney(double money) {
this.money = money;
}
public double getMoney() {
return money;
}
public double requestBet() {
double betValue;
System.out.print("How much would you like to bet: $");
betValue = scanner.nextDouble();
if (betValue > money) {
System.out.println("You don't have that much money!");
betValue = requestBet();
} else if (betValue < 0) {
System.out.println("You can't bet negative money!\n");
betValue = requestBet();
} else {
money -= betValue;
}
personalPotValue += betValue;
if (money == 0) {
allIn();
}
return betValue;
}
public double call(double callValue) {
double returnVal = callValue - personalPotValue;
money -= returnVal;
personalPotValue = callValue;
if (money == 0) {
allIn();
}
return returnVal;
}
public int folded() {
if (folded) {
return 1;
} else {
return 0;
}
}
/** Will show the full hand, Only use for testing and showdown */
public void showCards() {
System.out.println(this.name + "'s Cards:");
for (Card card : hand.cards) {
System.out.println(card.toString());
}
}
public void showMoney() {
System.out.println(name + ", you have $" + money);
}
/**
* This is to create a password and will overwrite the current
* password if called
*/
public void setPassword() {
char[] password = null;
try {
password = console.readPassword("Enter Password: ");
if (password.equals("")) {
throw new InvalidParameterException(name + " tried to set a blank password");
}
} catch (Exception e) {
System.out.println("Invalid Input");
setPassword();
}
this.password = parsePassword(password);
}
public void setPassword(String password) {
this.password = password;
}
/**
* @deprecated Use getAndCheckPW() instead
*/
public String getPassword() {
String pw = null;
try {
pw = parsePassword(console.readPassword("Enter Password: "));
} catch (Exception e) {
System.out.println("Invalid Input");
pw = getPassword();
}
return pw;
}
/**
* Use this most of the time for passwords, will get, check and repeat function
* until correct password is given
*/
public void getAndCheckPW() {
String pwInput;
char[] pw = null;
try {
pw = console.readPassword("Enter Password: ");
} catch (Exception e) {
System.out.println("Invalid Input");
getAndCheckPW();
}
pwInput = parsePassword(pw);
if (!this.password.equals(pwInput)) {
System.out.println("Incorrect Password");
getAndCheckPW();
}
}
/**
* True if password matches, false if password is wrong
*
* @deprecated Use getAndCheckPW() instead
*/
public boolean checkPassword(char[] password) {
return (this.password.equals(parsePassword(password)));
}
public void printPassword() {
System.out.println(password);
}
/** Returns string of password */
private String parsePassword(char[] password) {
String pass = "";
for (char character : password) {
pass += character;
}
return pass;
}
public void win(double pot) {
wins++;
money += pot;
}
public int getWins() {
return wins;
}
public void setBlind(Round.blind blind) {
this.blind = blind;
}
public Round.blind getBlind() {
return blind;
}
public void allIn() {
allIn = true;
System.out.println("You are all in!");
}
public boolean isAllIn() {
return allIn;
}
public void printCycleStats() {
System.out.println("You have $" + money);
System.out.println("You have bet $" + personalPotValue + " this round");
}
public ValueOfHand getHandValue() {
return new ValueOfHand(this);
}
}