-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
276 lines (257 loc) · 8.53 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import java.util.Scanner;
/**
* Player.java
*
* @author: Zachary, Anand, Jason (Group 7)
* Assignment #: Blackjack Project
*
* Brief Program Description:
*
*
*/
public class Player
{
private Hand playerHand;
private double bankroll = 0;
private double bet = 0;
private Scanner scan;
private boolean dealerPlay = true; //if the dealer will play, or not
//if the player wins by 5 card charlie or busts, this variable is set to false
private boolean play = true; //if the play cards loop will keep running or not
//if the player wins by 5 card charlie, busts, stands, or double downs
//then this variable is set to false
private boolean doubleDown = false; //if the player selects double down, this variable
//is set to true to indicate that
public Player()
{
playerHand = new Hand();
scan = new Scanner(System.in);
initializeBankroll();
}
/**
* @return - boolean dealerPlay
*/
public boolean getDealerPlay()
{
return dealerPlay;
}
/**
* @return - boolean play
*/
public boolean getPlay()
{
return play;
}
/**
* @param - boolean to be the new value of play
*/
public void setPlay(boolean x)
{
play = x;
}
/**
* Resets booleans to default value after each round
*/
public void reset()
{
play = true;
dealerPlay = true;
doubleDown = false;
}
/**
* @return - boolean doubleDown
*/
public boolean getDoubleDown()
{
return doubleDown;
}
/**
* @return - double bankroll
*/
public double getBankroll()
{
return bankroll;
}
/**
* @return - Hand playerHand
*/
public Hand getHand()
{
return playerHand;
}
/**
* @return - double bet
*/
public double getBet()
{
return bet;
}
/**
* Gives the player money or to take it away
* @param - int num to be added to bankroll
*/
public void addToBankroll(double num)
{
bankroll += num;
}
/**
* Appears once at the start of the game, sets the value of bankroll
*/
public void initializeBankroll()
{
double playerMoney;
boolean valid = false;
do //Just a sentry loop in case the player puts in a stupid value
{
System.out.println("-----------------------------------------------");
System.out.println("How much is your bankroll in dollars?"); //Player inputs the bankroll at the start of the game
String input = scan.nextLine();
try
{
playerMoney = Double.parseDouble(input);
if(playerMoney <= 0)
throw new Exception();
bankroll = playerMoney;
valid = true;
}
catch(Exception e)
{
System.out.println("Invalid input!");
continue;
}
} while(!valid);
}
/**
* called at the start of each round, determines the amount of money the player will bet
*/
public void betMoney()
{
double playerBet;
boolean valid = false;
do
{
System.out.println("-----------------------------------------------");
System.out.println("Starting round...");
System.out.println("Enter your bet:"); //Player enters the bet
String input = scan.nextLine();
try
{
playerBet = Double.parseDouble(input);
if(playerBet <=0 || playerBet > bankroll)
throw new Exception();
bet = playerBet;
valid = true;
}
catch(Exception e)
{
System.out.println("Invalid input!");
continue;
}
}while(!valid);
}
/**
* main method for player gameplay
*/
public void playCards() //
{
dealerPlay = true;
int playerChoice = 0;
boolean valid = false;
do
{
System.out.println("-----------------------------------------------");
System.out.println("Select your action by entering a number:");
if(playerHand.getHandSize() == 2)
System.out.println("1 to hit, 2 to stand, 3 to double down.");
else
System.out.println("1 to hit, 2 to stand.");
String input = scan.nextLine();
try
{
playerChoice = Integer.parseInt(input);
if(playerChoice != 1 && playerChoice != 2 && playerChoice != 3)
throw new Exception();
if(playerHand.getHandSize() != 2 && playerChoice == 3)
throw new Exception();
valid = true;
}
catch(Exception e)
{
System.out.println("Invalid input!");
continue;
}
} while(!valid); //Just a sentry loop to prevent stupid input value
if(playerChoice == 1) //if the player hits
{
hit(); //just a helper method for adding a card to the hand and recalculating the sum
System.out.println("-----------------------------------------------");
System.out.println("You have selected: Hit");
System.out.println("Your cards are: ");
System.out.println(getHand());
if(isBusted()) //if the player busts
{
System.out.println("-----------------------------------------------");
System.out.println("BUST! Dealer wins.");
addToBankroll(-1 * bet); //player loses the bet
System.out.println(this);
dealerPlay = false; //dealer doesn't play because the player already lost
}
}
if(playerChoice == 2)//if the player stands
{
System.out.println("-----------------------------------------------");
System.out.println("You have selected: Stand");
System.out.println("Your final score is: "+getHand().getValue());
play = false; //player's turn stops
}
if(playerChoice == 3 && bet * 2 <= bankroll && playerHand.getHandSize() == 2) //if the player double-downs
{
bet *= 2; //player's bet is doubled
hit(); //the rest of the implementation is identical to when the player hits
System.out.println("-----------------------------------------------");
System.out.println("You have selected: Double Down");
System.out.println("Your bet has been doubled to $"+bet+".");
System.out.println("Your cards are: ");
System.out.println(getHand());
if(isBusted())
{
System.out.println("-----------------------------------------------");
System.out.println("BUST! Dealer wins.");
addToBankroll(-1 * bet); //player loses the bet
System.out.println(this);
dealerPlay = false; //dealer doesn't play because the player already lost
}
play = false; //You can double down ONLY ONCE
doubleDown = true;
}
else if(playerChoice == 3 && bet * 2 > bankroll)
{
System.out.println("-----------------------------------------------");
System.out.println("Double down unavailable because\nyour bet exceeds half of your bankroll.");
}
else if(playerChoice == 3 && playerHand.getHandSize() != 2)
{
System.out.println("-----------------------------------------------");
System.out.println("Invalid input!");
System.out.println("Double down is unavailable after taking a hit.");
}
}
/**
* add card to the player's hand when they hit
*/
public void hit()
{
playerHand.addCard(Dealer.dealCard());
}
/**
* @return - boolean, true if player has busted, false otherwise.
*/
public boolean isBusted()
{
return playerHand.getValue() > 21;
}
public String toString()
{
return "You currently have $"+String.format("%.2f",bankroll)+".";
}
}