-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokerHand.java
More file actions
602 lines (557 loc) · 18.5 KB
/
PokerHand.java
File metadata and controls
602 lines (557 loc) · 18.5 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
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
import java.util.Scanner;
/* author: rajee ganesan
login: cs11wki
date: jan 30 2019
file: pokerhand.java
sources of help: various tutors, starter cod
description: creates methods for inputting a hand, determining the
poker hand and best possible option, and comparing hands.
note: the words rank & card are used interchangeable in the project.
*/
/**
* Class to store a hand of cards
*/
public class PokerHand implements IPokerHand{
// variable declarations
private final int SIZE = 5;
private String hand;
int[] numberVal = {1,2,3,4,5,6,7,8,9,10,11,12,13};
String[] cardVal = {"A", "1", "2", "3", "4", "5", "6", "7",
"8", "9", "10","J", "Q", "K"};
Card [] cards = new Card[SIZE];
/**
*Constructor for the class
*@param hand, String entered by the user
*Use the constructor to initialze the card array
**/
PokerHand(String hand){
this.hand = hand;
}
/**
* method to format inputted hands
* @param null
* formats hand in rank+suit format
**/
public String formattedHand(){
String[] inputHand = hand.split(" "); // splits hand into array
if (inputHand.length != 10){ // checks for garbage input
return ""; // if so, returns empty string
}
int[] cardNumber = new int[SIZE]; // creates array for ranks
String[] suit = new String[SIZE]; // creates array for suits
for (int i = 0; i < inputHand.length; i+= 2){
cardNumber[i/2] = Integer.parseInt(inputHand[i]); // gets rank
} // inputs suits into suit array
for (int i = 1; i < inputHand.length; i+=2){
suit[(i/2)] = inputHand[i]; // gets suit
} // inputs cards into card array
for (int i = 0; i < SIZE; i++){
cards[i] = new Card(cardNumber[i],suit[i]); // puts rank & suit together
} // inputs values into formatted card array
String playerHand = ""; // creates empty string
for (Card given: cards ){
playerHand = playerHand + given.cardInfo() + " ";
} //creates string for the formatted output
String newPlayerHand = playerHand.substring(0, playerHand.length()-1);
return newPlayerHand; // returns formatted string after deleting space
}
/**
* method to determine for straight flush
* @param null
* returns true/false based on output for straightFlush
**/
public boolean straightFlush(){
boolean straightFlush = true; // initializes variable
String[] inputHand = hand.split(" "); //splits stiring
int[] cards = new int[5]; // cards array
for (int i = 0; i < inputHand.length; i += 2){
cards[i/2] = Integer.parseInt(inputHand[i]);
} //inputs rank into card array
String[] suit = new String[5]; // suit array
for (int i = 1; i < inputHand.length; i += 2){
suit[i/2] = inputHand[i];
} // inputs suits into suit array
String flushSuit = suit[2]; // sets suit for flush
if (cards[0] == 1 && cards[4] == 13){ // checks for aces
if (suit[0].equals(flushSuit) && suit[4].equals(flushSuit)){
for (int j = 1; j < 4; j++){
if(cards[j] + 1 == cards[j+1] && suit[j].equals(suit[j+1])){
straightFlush = true; // ensures suits are equal & incrementing
}
else{
straightFlush = false;
break;
}
}
}
else{
straightFlush = false;
}
}
else{
for (int i = 1; i <5; i++){ // checks for incrementing
if (cards[i-1] == cards[i] -1 && suit[i].equals(suit[i-1])){
straightFlush = true; // ensures suits are equal & incrementing
}
else if (cards[4]-1 == cards[0] && suit[i].equals(suit[i-1])){
straightFlush = true; // checks incrementing
}
else{
straightFlush = false;
break;
}
}
}
return straightFlush; // returns true/false based on output
}
/**
* helper method for fourOfAKind determination
* @param null
* returns t/f value based on output for fourofakind
**/
public boolean fourOfAKind(){
boolean fourOfAKind = true; // initializes variable
String[] inputHand = hand.split(" "); // splits hand
int[] cards = new int[5]; // creates rank array
for (int i = 0; i < inputHand.length; i +=2){
cards[i/2] = Integer.parseInt(inputHand[i]);
} // inputs ranks into card array
String[] suit = new String[5];
for (int i = 1; i < inputHand.length; i += 2){ // creates suit array
suit[i/2] = inputHand[i];
} // inputs suits into suit array
int fourOfAKindRank = cards[2]; // rank of middle card
for(int i = 0; i < 4; i++){ // fourofakind loop
if (cards[0] == fourOfAKindRank){ // checks if four starts @ 0
for(int g= 1; g < 4; g++){
if(suit[g+1] != suit[g] && cards[g]== fourOfAKindRank){
fourOfAKind = true; // checks if ranks are equal
}
else{
fourOfAKind = false;
break;
}
}
}
else if (cards[4] == fourOfAKindRank){ //checks if four starts @ 1
for (int j = 1; j < 4; j++){
if(suit[j+1] != suit[j] && cards[j] == fourOfAKindRank){
fourOfAKind = true; // checks if ranks are equal
}
else{
fourOfAKind = false;
break;
}
}
}
else{
fourOfAKind = false;
break;
}
}
return fourOfAKind; // returns true/false based on output
}
/** helper method for full house determination
* @param null
* returns t/f value based on output for full house
**/
public boolean fullHouse(){
boolean fullHouse = false; //initializes variable
String[] inputHand = hand.split(" "); // splits
int[] cards = new int[5];
for (int i = 0; i < inputHand.length; i +=2){ // cards array
cards[i/2] = Integer.parseInt(inputHand[i]);
} // inputs rank into cards array
String[] suit = new String[5]; // suit array
for (int i = 1; i < inputHand.length; i += 2){
suit[i/2] = inputHand[i];
} // inputs suit into suit array
int fullHouseRank = cards[2]; // rank of three series
if (cards[0] == fullHouseRank){ // checks if three is first
for (int g = 0; g < 2; g++){
if (suit[g+1]!= suit[g] && cards[g] == fullHouseRank){
for (int j = 3; j < 4; j++){ // checks if ranks are equal
if (suit[j+1] != suit[j] && cards[j] == cards[j+1]){
fullHouse = true; // checks for two
}
else{
fullHouse = false;
break;
}
}
}
else{
fullHouse = false;
break;
}
}
}
else if (cards[4] == fullHouseRank){ // checks if two is first
for(int k = 2; k < 4; k++){
if((!suit[k+1].equals(suit[k])) && cards[k] == fullHouseRank){
for (int r = 0; r < 1; r++){ // checks if ranks are equal
if((!suit[r+1].equals(suit[r])) && cards[r] == cards[r+1]){
fullHouse = true; // checks for two
}
else{
fullHouse = false;
break;
}
}
}
else{
fullHouse = false; // if otherwise
break;
}
}
}
else{
fullHouse = false;
}
return fullHouse; // returns true or false based on output
}
/** helper method for flush
* @param null
* returns t/f value based on output for flush
**/
public boolean flush(){
boolean flush = false; // initializes flush variable
String[] inputHand = hand.split(" "); // splits string
int[] cards = new int[5];
for (int i = 0; i < inputHand.length;i+= 2){ // cards array
cards[i/2] = Integer.parseInt(inputHand[i]);
} // inputs ranks into array
String[] suit = new String[5];
for (int i = 1; i < inputHand.length; i+= 2){ // suit array
suit[i/2] = inputHand[i];
} // inputs suits into array
String flushSuit = suit[2]; // finds suit of all cards
for(int j = 0; j < 4; j++){
if(suit[j].equals(flushSuit)){ // checks for same suit
flush = true;
}
else{ // returns otherwise
flush = false;
break;
}
}
return flush; // returns true or false based on output
}
/** helper method for straight determination
* @param null
* returns t/f value based on output for straight
**/
public boolean straight(){
boolean straight = false; //initializes return value
String[] inputHand = hand.split(" "); // splits hand
int[] cards = new int[5];
for (int i = 0; i < inputHand.length; i+= 2){ // cards array
cards[i/2] = Integer.parseInt(inputHand[i]);
} // inputs ranks into array
String[] suit = new String[5]; // suit array
for (int i =1; i < inputHand.length; i+=2){
suit[i/2] = inputHand[i];
} // inputs suits into array
if(cards[0] == 1 && cards[4] == 13){ // checks for aces
for(int g = 1; g < 4; g++){
if(cards[g]+1 == cards[g+1]){ // checks increment
straight = true;
}
else{
straight = false;
break;
}
}
}
else{ // not aces
for(int g = 0; g < 4; g++){
if (cards[g]+1 == cards[g+1]){ // checks increment
straight = true;
}
else{
straight = false;
break;
}
}
}
return straight; // returns true or false based on output
}
/**
* helper method for threeofakind determination
* @param null
* returns t/f value based on output for three of a kind
**/
public boolean threeOfAKind(){
boolean threeOfAKind = true; // initializes variable
String[] inputHand = hand.split(" "); // splits string
int[] cards = new int[5];
for(int i = 0; i < inputHand.length; i += 2){ // cards array
cards[i/2] = Integer.parseInt(inputHand[i]);
} // inputs ranks into card array
String[] suit = new String[5]; // suit array
for(int i = 1; i < inputHand.length; i += 2){
suit[i/2] = inputHand[i];
} // inputs suits into suit array
int three = cards[2]; // finds rank for three of a kind
for(int j = 0; j < 4; j++){
if (cards[0] == three){ // checks threeofakind start @ zero
for(int g = 0; g < 2; g++){
if (cards[g] == cards[g+1]){ // checks for incrementing
threeOfAKind = true;
}
else{
threeOfAKind = false;
break;
}
}
}
else if (cards[1] == three){ // checks if threeofakind start @ 1
for (int k = 1; k < 3; k++){
if (cards[k] == cards[k+1]){ // checks for incrementing
threeOfAKind = true;
}
else{
threeOfAKind = false;
break;
}
}
}
else if (cards[4] == three){ // checks if threeofakind start @ 2
for(int r = 2; r < 4; r++){
if (cards[r] == cards[r+1]){ // checks for incrementing
threeOfAKind = true;
}
else{
threeOfAKind = false;
break;
}
}
}
else{ // if otherwise
threeOfAKind = false;
}
}
return threeOfAKind; // returns true or false based on output
}
/**
* helper method for two pair determination
* @param null
* returns t/f value based on output for two pair
**/
public boolean twoPair(){
boolean twoPair = false; // initializes variable
String[] inputHand = hand.split(" "); // splits string
int[] cards = new int[5]; // cards array
for(int i = 0; i < inputHand.length; i +=2){
cards[i/2] = Integer.parseInt(inputHand[i]);
} // inputs ranks into cards array
String[] suit = new String[5]; // suit array
for(int i = 1; i < inputHand.length; i += 2){
suit[i/2] = inputHand[i];
} // inputs suits into suit array
if (cards[0] == cards[1]){ // checks if first two are a pair
for(int j = 2; j < 3; j++){ // checks for second pair
if( cards[j] == cards[j+1]){
twoPair = true;
}
}
for (int g = 3; g <4; g++){ // checks for second pair if not found
if(cards[g] == cards[g+1]){
twoPair = true;
}
}
}
else if (cards[3] == cards[4]){ // checks if last two are a pair
for(int k = 1; k < 2; k++){ // checks if first two were a pair
if(cards[k] == cards[k+1]){
twoPair = true;
}
else{
twoPair = false; // otherwise
break;
}
}
}
else{
twoPair = false; // no pairs
}
return twoPair; // reutrns true or false based on output
}
/**
* helper method for one pair determination
* @param null
* returns t/f value based on output of one pair
**/
public boolean onePair(){
boolean onePair = true; // initializes variable
String[] inputHand = hand.split(" "); // splits hand
int[] cards = new int[5]; // cards array
for(int i = 0; i< inputHand.length; i +=2){
cards[i/2] = Integer.parseInt(inputHand[i]);
} // inputs ranks into cards array
String[] suit = new String[5]; // suit array
for(int i = 1; i < inputHand.length; i +=2){
suit[i/2] = inputHand[i];
} // inputs suits into suit array
if (cards[0] == cards[1]){ // checks for pair in first two
for(int k = 2; k < 4; k++){ // checks for other pairs
if (cards[k] != cards[k+1]){
onePair = true;
}
else{
onePair = false;
break;
}
}
}
else if (cards[1] == cards[2]){ // checks for pair in 1 and 2
for (int j = 3; j < 4; j++){
if(cards[j] != cards[j+1]){ // checks for other pairs
onePair = true;
}
else{
onePair = false;
break;
}
}
}
else if (cards[2] == cards[3]){ // checks for pair in 2 and 3
onePair = true;
}
else if (cards[3] == cards[4]){ // checks for pair in 3 and 4
onePair = true;
}
else{ // otherwise
onePair = false;
}
return onePair; // returns true or false based on output
}
/**
* besthand method
* @param null
* evaluates and returns best hand possible
**/
public String bestHand(){
String retVal = ""; //empty string
PokerHand givenHand = new PokerHand(hand); // creates new hand
String[] givenHandInput = hand.split(" ");
if(givenHandInput.length != 10){ // checks for garbage input
return ""; // if so, returns empty string
}
if (givenHand.straightFlush() == true){ // checks for hands in order
retVal += "STRAIGHT FLUSH";
}
else if (givenHand.fourOfAKind() == true){
retVal += "FOUR OF A KIND";
}
else if (givenHand.fullHouse() == true){
retVal += "FULL HOUSE";
}
else if (givenHand.flush() == true){
retVal += "FLUSH";
}
else if (givenHand.straight() == true){
retVal += "STRAIGHT";
}
else if (givenHand.threeOfAKind() == true){
retVal += "THREE OF A KIND";
}
else if(givenHand.twoPair() == true){
retVal += "TWO PAIR";
}
else if(givenHand.onePair() == true){
retVal += "ONE PAIR";
}
else{
retVal += "HIGH CARD";
}
return (retVal); // returns highest value hand
}
/**
* comparehands method for class
* @param p, pokerhand entered by user
* compares hands of two players and returns 0 for tie, 1 for player
* 1 winning, and 2 for player 2 winning.
**/
public int compareHands(PokerHand p){
String[] listOfHands = {"STRAIGHT FLUSH", "FOUR OF A KIND", "FULL HOUSE",
"FLUSH", "STRAIGHT", "THREE OF A KIND",
"TWO PAIR", "ONE PAIR", "HIGH CARD"};
int retVal = 0; // initializes return value
int[] rankOfHand = {9,8,7,6,5,4,3,2,1,0}; // point value system
String bestHand2 = p.bestHand(); // gets string of first pokerHand
PokerHand dos = new PokerHand(hand); // gets second pokerhand
String bestHand1 = dos.bestHand(); // gets string of second pokerhand
int index = 0;
int index2 = 0;
for(int i = 0; i < listOfHands.length; i++){
if (bestHand1.equals("")){ // checks for garbage input
index = 9; // if so, returns empty string
break;
}
if (bestHand1.equals(listOfHands[i])){
index = i; // finds index
break;
}
else index = 0;
}
for(int g = 0; g < listOfHands.length; g++){
if (bestHand2.equals("")){ // checks for garbage input
index2 = 9; // if so, returns empty string
break;
}
if (bestHand2.equals(listOfHands[g])){
index2 = g; // finds index
break;
}
else index = 0;
}
int rankIndex = rankOfHand[index]; // finds point
int rankIndex2 = rankOfHand[index2] ;
if (rankIndex > rankIndex2){ // checks which is higher
retVal = 1; // player one wins
}
else if (rankIndex < rankIndex2){
retVal = 2; // player two wins
}
else if (rankIndex == rankIndex2){
retVal = 0; // tie
}
else{
retVal = -1; // null (something is wrong)
}
return retVal; // returns winner
}
//examples & testing
public static void main(String[] args) {
System.out.println("Enter Player 1's cards:");
Scanner scanner = new Scanner(System.in);
String givenHand = scanner.nextLine();
PokerHand newHand = new PokerHand(givenHand);
System.out.println(newHand.formattedHand());
System.out.println("STRAIGHT FLUSH:" + newHand.straightFlush());
System.out.println("FOUROFAKIND: " + newHand.fourOfAKind());
System.out.println("FULL HOUSE: " + newHand.fullHouse());
System.out.println("FLUSH: " + newHand.flush());
System.out.println("STRAIGHT " + newHand.straight());
System.out.println("THREE OF A KIND: " + newHand.threeOfAKind());
System.out.println("TWO PAIR: " + newHand.twoPair());
System.out.println("ONE PAIR: " + newHand.onePair());
System.out.println( newHand.bestHand());
System.out.println("Enter Player 2's cards:");
Scanner sc = new Scanner(System.in);
String playerTwoHand = scanner.nextLine();
PokerHand newHand2 = new PokerHand(playerTwoHand);
System.out.println(newHand2.formattedHand());
System.out.println("STRAIGHT FLUSH: " + newHand2.straightFlush());
System.out.println("FOUR OF A KIND: " + newHand2.fourOfAKind());
System.out.println("FULL HOUSE: " + newHand2.fullHouse());
System.out.println("FLUSH: " + newHand2.flush());
System.out.println("STRAIGHT: " + newHand2.straight());
System.out.println("THREE OF A KIND: " + newHand2.threeOfAKind());
System.out.println("TWO PAIR: " + newHand2.twoPair());
System.out.println("ONE PAIR: " + newHand2.onePair());
System.out.println(newHand2.bestHand());
System.out.println(newHand.compareHands(newHand2));
}
}