-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPoker Game
783 lines (605 loc) · 21 KB
/
Poker Game
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
//package pj4;
package PJ4;
import java.util.*;
/*
* Ref: http://en.wikipedia.org/wiki/Video_poker
* http://www.google.com/ig/directory?type=gadgets&url=www.labpixies.com/campaigns/videopoker/videopoker.xml
*
*
* Short Description and Poker rules:
*
* Video poker is also known as draw poker.
* The dealer uses a 52-card deck, which is played fresh after each playerHand.
* The player is dealt one five-card poker playerHand.
* After the first draw, which is automatic, you may hold any of the cards and draw
* again to replace the cards that you haven't chosen to hold.
* Your cards are compared to a table of winning combinations.
* The object is to get the best possible combination so that you earn the highest
* payout on the bet you placed.
*
* Winning Combinations
*
* 1. Jacks or Better: a pair pays out only if the cards in the pair are Jacks,
* Queens, Kings, or Aces. Lower pairs do not pay out.
* 2. Two Pair: two sets of pairs of the same card denomination.
* 3. Three of a Kind: three cards of the same denomination.
* 4. Straight: five consecutive denomination cards of different suit.
* 5. Flush: five non-consecutive denomination cards of the same suit.
* 6. Full House: a set of three cards of the same denomination plus
* a set of two cards of the same denomination.
* 7. Four of a kind: four cards of the same denomination.
* 8. Straight Flush: five consecutive denomination cards of the same suit.
* 9. Royal Flush: five consecutive denomination cards of the same suit,
* starting from 10 and ending with an ace
*
*/
/* This is the main poker game class.
* It uses Deck and Card objects to implement poker game.
* Please do not modify any data fields or defined methods
* You may add new data fields and methods
* Note: You must implement defined methods
*/
public class PokerGame {
// default constant values
private static final int startingBalance=100;
private static final int numberOfCards=5;
// default constant payout value and playerHand types
private static final int[] multipliers={1,2,3,5,6,9,25,50,250};
private static final String[] goodHandTypes={
"Royal Pair" , "Two Pairs" , "Three of a Kind", "Straight", "Flush ",
"Full House", "Four of a Kind", "Straight Flush", "Royal Flush" };
// must use only one deck
private static final Deck oneDeck = new Deck(1);
// holding current poker 5-card hand, balance, bet
private List<Card> playerHand;
private int playerBalance;
private int playerBet;
private boolean playing=true;
private static List<Integer> discardArray;
private int handValue;
/** default constructor, set balance = startingBalance */
public PokerGame()
{
this(startingBalance);
}
/** constructor, set given balance */
public PokerGame(int balance)
{
this.playerBalance= balance;
}
/** This display the payout table based on multipliers and goodHandTypes arrays */
private void showPayoutTable()
{
System.out.println("\n\n");
System.out.println("Payout Table Multiplier ");
System.out.println("=======================================");
int size = multipliers.length;
for (int i=size-1; i >= 0; i--) {
System.out.println(goodHandTypes[i]+"\t|\t"+multipliers[i]);
}
System.out.println("\n\n");
}
/** Check current playerHand using multipliers and goodHandTypes arrays
* Must print yourHandType (default is "Sorry, you lost") at the end of function.
* This can be checked by testCheckHands() and main() method.
*/
private void checkHands()
{
handValue=9;
if (isRoyalFlush()) handValue=8;
else if (isStraightFlush()) handValue=7;
else if (isFourOfAKind()) handValue=6;
else if (isFullHouse()) handValue=5;
else if (isFlush()) handValue=4;
else if (isStraight()) handValue=3;
else if (isThreeOfAKind()) handValue=2;
else if (isTwoPair()) handValue=1;
else if (isRoyalPair()) handValue=0;
if (handValue < 9)
System.out.println(goodHandTypes[handValue] + "!");
else
System.out.println("Sorry, you lost !");
}
/*************************************************
* add new private methods here ....
*
*************************************************/
//sort ranks of the cards
public static void SortRanks(int[] array) {
for (int j = 0; j < array.length ; j++) {
int a = array[j];
int i = j - 1;
while( i >=0 && array[i] > a){
array[i+1] = array[i];
i = i - 1;
}
array[i+1] = a;
}
}
private boolean isRoyalPair(){
if ( playerHand.size() != 5 ) return(false);
Card[] tempHand = new Card[playerHand.size()];
for (int i = 0; i < tempHand.length; i++) {
tempHand[i]=(Card)playerHand.get(i);
}
int [] tempRank = new int[5];
for(int i =0; i < 5; i++){
tempRank[i] = tempHand[i].getRank();
}
SortRanks(tempRank);
boolean isPair = false;
for (int i=0; i<5; i++){
if (tempRank[i] ==1 ){
for (int j=0; j<5; j++){
if (j != i) {
if (tempRank[j] == 1 )
isPair = true;
}
}
}
}
for (int i=0; i<5; i++){
if (tempRank[i] ==11 ){
for (int j=0; j<5; j++){
if (j != i) {
if (tempRank[j] == 11 )
isPair = true;
}
}
}
}
for (int i=0; i<5; i++){
if (tempRank[i] ==12 ){
for (int j=0; j<5; j++){
if (j != i) {
if (tempRank[j] == 12 )
isPair = true;
}
}
}
}
for (int i=0; i<5; i++){
if (tempRank[i] ==13){
for (int j=0; j<5; j++){
if (j != i) {
if (tempRank[j] == 13 )
isPair = true;
}
}
}
}
if (isPair)
return true;
else
return false;
}
private boolean isTwoPair(){
if ( playerHand.size() != 5 ) return(false);
Card[] tempHand = new Card[playerHand.size()];
for (int i = 0; i < tempHand.length; i++) {
tempHand[i]=(Card)playerHand.get(i);
}
int [] tempRank = new int[5];
for(int i =0; i < 5; i++){
tempRank[i] = tempHand[i].getRank();
}
SortRanks(tempRank);
int totalPairs = 0;
if (tempRank[0] == tempRank[1] &&
tempRank[0] != tempRank[2] )
totalPairs += 1;
if (tempRank[1] == tempRank[2] &&
tempRank[1] != tempRank[3] )
totalPairs += 1;
if (tempRank[2] == tempRank[3] &&
tempRank[2] != tempRank[4] )
totalPairs += 1;
if (tempRank[3] == tempRank[4] )
totalPairs += 1;
if (totalPairs == 2)
return true;
else
return false;
}
private boolean isThreeOfAKind(){
if ( playerHand.size() != 5 ) return(false);
Card[] tempHand = new Card[playerHand.size()];
for (int i = 0; i < tempHand.length; i++) {
tempHand[i]=(Card)playerHand.get(i);
}
int [] tempRank = new int[5];
for(int i =0; i < 5; i++){
tempRank[i] = tempHand[i].getRank();
}
SortRanks(tempRank);
if (tempRank[0] == tempRank[1] &&
tempRank[1] == tempRank[2] && tempRank[3] != tempRank[4])
return true;
else
return false;
}
private boolean isStraight(){
if ( playerHand.size() != 5 ) return(false);
Card[] tempHand = new Card[playerHand.size()];
for (int i = 0; i < tempHand.length; i++) {
tempHand[i]=(Card)playerHand.get(i);
}
int [] tempRank = new int[5];
for(int i =0; i < 5; i++){
tempRank[i] = tempHand[i].getRank();
}
SortRanks(tempRank);
if (tempRank[1] == (tempRank[0]+1) &&
tempRank[2] == (tempRank[1]+1) &&
tempRank[3] == (tempRank[2]+1) &&
tempRank[4] == (tempRank[3]+1)) return true;
else
return false;
}
private boolean isFlush(){
if ( playerHand.size() != 5 ) return(false);
Card[] tempHand = new Card[playerHand.size()];
for (int i = 0; i < tempHand.length; i++) {
tempHand[i]=(Card)playerHand.get(i);
}
for (int i=0; i<4; i++){
if (tempHand[i].getSuit() != tempHand[i+1].getSuit())
return false;
}
return true;
}
private boolean isFullHouse(){
if ( playerHand.size() != 5 ) return(false);
Card[] tempHand = new Card[playerHand.size()];
for (int i = 0; i < tempHand.length; i++) {
tempHand[i]=(Card)playerHand.get(i);
}
int [] tempRank = new int[5];
for(int i =0; i < 5; i++){
tempRank[i] = tempHand[i].getRank();
}
SortRanks(tempRank);
if (tempRank[0] == tempRank[1] &&
tempRank[1] == tempRank[2] && tempRank[3] == tempRank[4])
return true;
else
return false;
}
private boolean isFourOfAKind(){
if ( playerHand.size() != 5 ) return(false);
Card[] tempHand = new Card[playerHand.size()];
for (int i = 0; i < tempHand.length; i++) {
tempHand[i]=(Card)playerHand.get(i);
}
int [] tempRank = new int[5];
for(int i =0; i < 5; i++){
tempRank[i] = tempHand[i].getRank();
}
SortRanks(tempRank);
if (tempRank[0] == tempRank[1] &&
tempRank[1] == tempRank[2] &&
tempRank[2] == tempRank[3] )
return true;
else
return false;
}
private boolean isStraightFlush(){
if ( playerHand.size() != 5 ) return(false);
Card[] tempHand = new Card[playerHand.size()];
for (int i = 0; i < tempHand.length; i++) {
tempHand[i]=(Card)playerHand.get(i);
}
int [] tempRank = new int[5];
for(int i =0; i < 5; i++){
tempRank[i] = tempHand[i].getRank();
}
SortRanks(tempRank);
for (int i=0; i<5; i++){
for (int j=i+1; j<5; j++){
if (tempHand[i].getSuit() != tempHand[j].getSuit())
return false;
}
}
if (tempRank[1] == (tempRank[0]+1) &&
tempRank[2] == (tempRank[1]+1) &&
tempRank[3] == (tempRank[2]+1) &&
tempRank[4] == (tempRank[3]+1)) return true;
else
return false;
}
private boolean isRoyalFlush(){
if ( playerHand.size() != 5 ) return(false);
Card[] tempHand = new Card[playerHand.size()];
for (int i = 0; i < tempHand.length; i++) {
tempHand[i]=(Card)playerHand.get(i);
}
int [] tempRank = new int[5];
for(int i =0; i < 5; i++){
tempRank[i] = tempHand[i].getRank();
}
SortRanks(tempRank);
for (int i=0; i<5; i++){
for (int j=i+1; j<5; j++){
if (tempHand[i].getSuit() != tempHand[j].getSuit())
return false;
}
}
if (tempRank[0] ==1 && tempRank[1]==10 &&
tempRank[2]==11 && tempRank[3]==12 &&
tempRank[4]==13) return true;
else
return false;
}
List<Card> aHand = new ArrayList<Card>();
private List<Card> discard(){
discardArray = new ArrayList<Integer>();
Scanner in = new Scanner(System.in);
String inStr=null;
boolean doLoop = true;
while (doLoop){
doLoop = false;
System.out.println("Enter positions of cards to keep (e.g. 1 4 5): ");
inStr = in.nextLine();
if (!inStr.isEmpty()){
String splStr[] = inStr.split(" ");
if (splStr.length>5){ // check if user selected more than 5 cards
System.out.println("Error, too many cards. Choose up to 5 cards. ");
doLoop = true;
}
for (int i=0; i < splStr.length; i++) {
int k = Integer.parseInt(splStr[i]);
discardArray.add(i, k);
}
//Check if each card number is between 1 and 5
for (int i=0; i < discardArray.size(); i++) {
if (discardArray.get(i)<1 || discardArray.get(i)>=6){
System.out.println("Position number out of range. Choose 1 to 5");
doLoop = true;
}
}
if(!doLoop){
//remove cards and add them to new array
for (int i=0; i<discardArray.size(); i++){
int x =discardArray.get(i);
aHand.add(playerHand.get(x-1));
}
System.out.println("Held Cards: "+aHand);
try { //deal more cards
aHand.addAll(oneDeck.deal(5-discardArray.size()));
} catch (PlayingCardException exc) {
exc.printStackTrace();
}
}
discardArray.clear();
}
}
if (inStr.isEmpty()){
aHand.clear();
System.out.println("No cards in hands.");
try {
aHand.addAll(oneDeck.deal(5));
}
catch (PlayingCardException exc) {
exc.printStackTrace();
}
}
return aHand;
}
//*************************************************/
public void play()
{
/** The main algorithm for single player poker game
*
* Steps:
* showPayoutTable()
*
* ++
* show balance, get bet
* verify bet value, update balance
* reset deck, shuffle deck,
* deal cards and display cards
* ask for positions of cards to keep
* get positions in one input line
* update cards
* check hands, display proper messages
* update balance if there is a payout
* if balance = O:
* end of program
* else
* ask if the player wants to play a new game
* if the answer is "no" : end of program
* else : showPayoutTable() if user wants to see it
* goto ++
*/
//The algorithm for single player
showPayoutTable();
//Create a hand arrayList
playerHand = new ArrayList<Card>();
while (playing) {
System.out.println("Balance: $" +getBalance());
placeBet();
oneDeck.shuffle();
//Dealing the hand
try {
playerHand = new ArrayList<Card>(oneDeck.deal(5));
} catch (PlayingCardException exc) {
exc.printStackTrace();
}
System.out.println("Hand: "+playerHand);
playerHand=discard();
System.out.println("Hand: "+playerHand);
//Check hands
checkHands();
payOut(handValue);
playerHand.clear();
System.out.println("\nYour balance: $"+playerBalance);
// play more?
if ((playerBalance > 0)&&(askIfPlayAgain())) {;
askIfShowPayoutTable();
}
else {
System.out.println("Your balance is: $" + getBalance() );
System.out.println("Bye!");
playing=false;
}
}
}
//Adjust the balance
private void payOut(int payOut){
if (payOut==9){
playerBalance = playerBalance-playerBet; //deducting bet if lost game
}
else if (payOut!=9){
playerBalance=playerBalance+(playerBet*multipliers[payOut]); //pay if winning game
}
}
//Get the balance
private int getBalance(){
return this.playerBalance;
}
//Get the bet
private int getBet(){
return playerBet;
}
//Place a bet and check for funds
private void placeBet(){
Scanner in = new Scanner(System.in);
boolean hasFunds = true;
do {
System.out.print("Enter bet: ");
playerBet=in.nextInt();
if (playerBet > playerBalance){
hasFunds = false;
System.out.println("Not enough funds. Enter a smaller amount.");
}
else
hasFunds = true;
}while(!hasFunds);
}
//Ask if play again
public static boolean askIfPlayAgain(){
boolean askAgain=true;
boolean playAgain=true;
Scanner in = new Scanner(System.in);
while (askAgain){
System.out.println("One more game (y or n)?");
String continuePlaying=in.next();
if (continuePlaying.equalsIgnoreCase("y")){
playAgain=true;
askAgain=false;
}
else if (continuePlaying.equalsIgnoreCase("n")){
playAgain=false;
askAgain=false;
}
else
System.out.println("Please answer y/n");
}
return playAgain;
}
// ask if show payout table
private void askIfShowPayoutTable()
{
boolean askAgain=true;
boolean playAgain=true;
Scanner in = new Scanner(System.in);
while (askAgain){
System.out.println("Show payout table? (y or n)?");
String continuePlaying=in.next();
if (continuePlaying.equalsIgnoreCase("y")){
playAgain=true;
askAgain=false;
}
else if (continuePlaying.equalsIgnoreCase("n")){
playAgain=false;
askAgain=false;
}
else
System.out.println("Please answer y/n");
}
if (playAgain) showPayoutTable();
}
/*************************************************
* Do not modify methods below
/*************************************************
/** testCheckHands() is used to test checkHands() method
* checkHands() should print your current hand type
*/
public void testCheckHands()
{
try {
playerHand = new ArrayList<Card>();
// set Royal Flush
playerHand.add(new Card(1,4));
playerHand.add(new Card(10,4));
playerHand.add(new Card(12,4));
playerHand.add(new Card(11,4));
playerHand.add(new Card(13,4));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Straight Flush
playerHand.set(0,new Card(9,4));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Straight
playerHand.set(4, new Card(8,2));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Flush
playerHand.set(4, new Card(5,4));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// "Royal Pair" , "Two Pairs" , "Three of a Kind", "Straight", "Flush ",
// "Full House", "Four of a Kind", "Straight Flush", "Royal Flush" };
// set Four of a Kind
playerHand.clear();
playerHand.add(new Card(8,4));
playerHand.add(new Card(8,1));
playerHand.add(new Card(12,4));
playerHand.add(new Card(8,2));
playerHand.add(new Card(8,3));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Three of a Kind
playerHand.set(4, new Card(11,4));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Full House
playerHand.set(2, new Card(11,2));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Two Pairs
playerHand.set(1, new Card(9,2));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// set Royal Pair
playerHand.set(0, new Card(3,2));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
// non Royal Pair
playerHand.set(2, new Card(3,4));
System.out.println(playerHand);
checkHands();
System.out.println("-----------------------------------");
}
catch (Exception e)
{
System.out.println(e.getMessage());
}
}
/* Quick testCheckHands() */
public static void main(String args[])
{
PokerGame pokergame = new PokerGame();
pokergame.testCheckHands();
}
}