-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
497 lines (336 loc) · 9.88 KB
/
Copy pathGame.java
File metadata and controls
497 lines (336 loc) · 9.88 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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/* Alua Zhanuzakova
* az2879
* Game.java utilizes the Deck, Player, and Card
* classes to simulate a game of video poker.
*/
import java.util.Scanner;
import java.util.ArrayList;
public class Game {
private Player p; // Human player
private Deck cards; // Deck of 52 cards
private boolean playingGame; // to keep playing
private double payout; // base # of tokens won
private int tokens; // # of tokens player bets
private int[] suitCounts; // tracks suits in hand
private int[] rankCounts; // tracks ranks in hand
/**
* Initializes a Game with a new player and deck.
* The user specifies which cards to add to the hand.
*/
public Game(String[] testHand){
p = new Player();
cards = new Deck();
playingGame = true;
payout = 0.0;
tokens = 0;
for (int i = 0; i < testHand.length; i++) {
if (p.getHand().size() == 5) {
System.out.println("First five cards have been added to hand");
break;
}
p.addCard(Card.toCard(testHand[i]));
}
}
/**
* Initializes a Game with a new player and deck.
*/
public Game(){
p = new Player();
cards = new Deck();
playingGame = true;
payout = 0.0;
tokens = 0;
}
/**
* Starts a game of video poker. Game will continue until
* player says they wish to stop.
*/
public void play(){
// Instantiate scanner to read in player's inputs
Scanner playerInput = new Scanner(System.in);
//The deck is shuffled prior to the start of the game
cards.shuffle();
while (playingGame) {
System.out.println("Welcome to Video Poker!");
// The player is made aware how many tokens they start with
System.out.println("Your current balance is " +
p.getBankroll() + " tokens");
// Player can print out paytable before betting
System.out.print("Would you like to see the paytable " +
"before you bet (y/n)? ");
if (playerInput.next().charAt(0) == 'y') {
displayPaytable();
}
// The player makes a bet (1-5 tokens)
System.out.print("How many tokens (1-5) would you like to bet? ");
tokens = playerInput.nextInt();
while (tokens < 1 | tokens > 5) {
System.out.print("Must pick at least 1 and at most 5 tokens. " +
"Try again: ");
tokens = playerInput.nextInt();
}
p.bets(tokens);
// The player is dealt up to 5 starting cards
while (p.getHand().size() < 5) {
p.addCard(cards.deal());
}
System.out.println("Your hand is: " + p.handToString());
// The player selects which cards they want to discard
System.out.print("Do you wish to discard any of these cards (y/n)? ");
if (playerInput.next().charAt(0) == 'y') {
System.out.println("Input the cards you wish to discard one-by-one");
System.out.println("Input cards using the same format shown to you");
System.out.println("When you wish to stop discarding, input 'stop'");
// Allow the player to discard up to 5 cards
while (p.getHand().size() > 0) {
String nextCard = playerInput.next();
if (nextCard.equals("stop")) {
break;
}
try {
p.removeCard(Card.toCard(nextCard));
// If .toCard() is unable to convert the player's
// input properly, let the user know and allow
// them to try again with the correct format
} catch (IllegalArgumentException e) {
System.out.println("Invalid card format: " +
e.getMessage() + ". Try again.");
continue;
}
}
}
// Rejected cards replaced with cards from top of deck
while (p.getHand().size() < 5) {
p.addCard(cards.deal());
}
System.out.println("Here is the new hand: " +
p.handToString());
// The player's hand is evaluated
System.out.println(checkHand(p.getHand()));
// Bankroll is adjusted accordingly
p.winnings(tokens * payout);
System.out.println("Your current balance is " +
p.getBankroll() + " tokens");
// Empty the hand before next round starts
p.clearHand();
// The player is asked if they want to bet again
System.out.print("Would you like to keep betting (y/n)? ");
if (playerInput.next().charAt(0) == 'n') {
playingGame = false;
}
}
System.out.println("Game over. Final balance: " +
p.getBankroll() + " tokens");
}
/**
* Evaluates the player's hand to determine payout
*
* @param hand The player's current hand
* @return A string announcing how hand was scored
*/
public String checkHand(ArrayList<Card> hand){
// this method should take an ArrayList of cards
// as input and then determine what evaluates to and
// return that as a String
suitCounts = p.getSuitCounts();
rankCounts = p.getRankCounts();
if (isRoyalFlush()) {
payout = 250;
return "Royal flush";
} else if (isStraightFlush()) {
payout = 50;
return "Straight flush";
} else if (isFourOfAKind()) {
payout = 25;
return "Four of a kind";
} else if (isFullHouse()) {
payout = 6;
return "Full House";
} else if (isFlush()) {
payout = 5;
return "Flush";
} else if (isStraight()) {
payout = 4;
return "Straight";
} else if (isThreeOfAKind()) {
payout = 3;
return "Three of a kind";
} else if (isTwoPairs()) {
payout = 2;
return "Two pairs";
} else if (isOnePair()) {
payout = 1;
return "One pair";
} else {
payout = 0;
return "No pair";
}
}
/**
* Determines if player's hand has a pair
*
* @return True if the hand has a pair
* False otherwise
*/
public boolean isOnePair() {
for (int i : rankCounts) {
// A value of 2 means there are
// 2 cards with the same rank
if (i == 2) {
return true;
}
}
return false;
}
/**
* Determines if player's hand has two pairs
*
* @return True if the hand has two pairs
* False otherwise
*/
public boolean isTwoPairs() {
int pairs = 0;
for (int i : rankCounts) {
if (i==2) {
pairs++;
}
}
return (pairs==2);
}
/**
* Determines if player's hand has 3 cards
* of the same rank
*
* @return True if the hand is 3 of a kind
* False otherwise
*/
public boolean isThreeOfAKind() {
for (int i : rankCounts) {
// A value of 3 means there are
// 3 cards of the same rank
if (i == 3) {
return true;
}
}
return false;
}
/**
* Determines if player's hand is a straight
*
* @return True if the hand is a straight
* False otherwise
*/
public boolean isStraight() {
// check if the order is 10-J-Q-K-A
if (rankCounts[10] == 1 &&
rankCounts[11] == 1 &&
rankCounts[12] == 1 &&
rankCounts[13] == 1 &&
rankCounts[1] == 1) {
return true;
}
// check for all other sequential orders
for (int i = 0; i < rankCounts.length - 4; i++) {
if (rankCounts[i] == 1 &&
rankCounts[i+1] == 1 &&
rankCounts[i+2] == 1 &&
rankCounts[i+3] == 1 &&
rankCounts[i+4] == 1) {
return true;
}
}
return false;
}
/**
* Determines if player's hand is a flush
*
* @return True if the hand is a flush
* False otherwise
*/
public boolean isFlush() {
for (int i : suitCounts) {
// A value of 5 means there are
// 5 cards of the same suit
if (i == 5) {
return true;
}
}
return false;
}
/**
* Determines if player's hand is a full house
*
* @return True if the hand is a full house
* False otherwise
*/
public boolean isFullHouse() {
// A full house has 3 of a kind and 1 pair
return isThreeOfAKind() && isOnePair();
}
/**
* Determines if player's hand has 4 cards of
* the same rank
*
* @return True if the hand is 4 of a kind
* False otherwise
*/
public boolean isFourOfAKind() {
for (int i : rankCounts) {
// A value of 4 means there are
// 4 cards of the same rank
if (i == 4) {
return true;
}
}
return false;
}
/**
* Determines if player's hand is a straight flush
*
* @return True if the hand is a straight flush
* False otherwise
*/
public boolean isStraightFlush() {
// Straight flush is both a straight and flush
return isStraight() && isFlush();
}
/**
* Determines if player's hand is a royal flush
*
* @return True if the hand is royal flush
* False otherwise
*/
public boolean isRoyalFlush() {
// a royal flush must also be a regular flush
if (!isFlush()) {
return false;
}
// check that an ace is present
if (rankCounts[1] != 1) {
return false;
}
// checks that the other 4 card ranks are 10, J, Q, K
for (int i = 10; i < rankCounts.length; i++) {
if (rankCounts[i] != 1) {
return false;
}
}
return true;
}
/**
* Displays the video poker paytable
*/
public void displayPaytable() {
System.out.println(" | 1 token | 2 tokens | 3 tokens | 4 tokens | 5 tokens");
System.out.println("----------------+----------+------------+------------+------------+----------");
System.out.println("One pair | 1 | 2 | 3 | 4 | 5 ");
System.out.println("Two pairs | 2 | 4 | 6 | 8 | 10 ");
System.out.println("Three of a kind | 3 | 6 | 9 | 12 | 15 ");
System.out.println("Straight | 4 | 8 | 12 | 16 | 20 ");
System.out.println("Flush | 5 | 10 | 15 | 20 | 25 ");
System.out.println("Full house | 6 | 12 | 18 | 24 | 30 ");
System.out.println("Four of a kind | 25 | 50 | 75 | 100 | 125 ");
System.out.println("Straight flush | 50 | 100 | 150 | 200 | 250 ");
System.out.println("Royal flush | 250 | 500 | 750 | 1000 | 1250 ");
}
}