-
Notifications
You must be signed in to change notification settings - Fork 0
/
FiveCardDraw.java
193 lines (177 loc) · 8.41 KB
/
FiveCardDraw.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
package com.cbschenk.poker.fivecarddraw;
import com.cbschenk.poker.Card;
public class FiveCardDraw {
//Winning hand is returned, otherwise null if tie
public static Hand play(Hand hand1, Hand hand2) {
int hand1Rank = hand1.getRank();
int hand2Rank = hand2.getRank();
if (hand1Rank > hand2Rank) {
return hand1;
} else if (hand1Rank < hand2Rank) {
return hand2;
} else {
//Tie!
return null;
}
}
/**
* Let's make some examples and print them out to the screen
* @param args
*/
public static void main(String[] args) {
//Straight Flush
Hand straightFlush = new Hand();
straightFlush.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_2));
straightFlush.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_3));
straightFlush.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_4));
straightFlush.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_5));
straightFlush.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_6));
//Four of a Kind
Hand fourOfAKind = new Hand();
fourOfAKind.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_10));
fourOfAKind.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_10));
fourOfAKind.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_10));
fourOfAKind.addCard(new Card(Card.SUIT_SPADES, Card.CARD_10));
fourOfAKind.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_2));
//Full House
Hand fullHouse = new Hand();
fullHouse.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_10));
fullHouse.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_10));
fullHouse.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_ACE));
fullHouse.addCard(new Card(Card.SUIT_SPADES, Card.CARD_ACE));
fullHouse.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_ACE));
//Two Pair (first)
Hand twoPairFirst = new Hand();
twoPairFirst.addCard(new Card(Card.SUIT_SPADES, Card.CARD_8));
twoPairFirst.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_8));
twoPairFirst.addCard(new Card(Card.SUIT_SPADES, Card.CARD_9));
twoPairFirst.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_9));
twoPairFirst.addCard(new Card(Card.SUIT_SPADES, Card.CARD_10));
//Two Pair (second, tie)
Hand twoPairSecond = new Hand();
twoPairSecond.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_8));
twoPairSecond.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_8));
twoPairSecond.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_9));
twoPairSecond.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_9));
twoPairSecond.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_10));
//Two Pair (high card)
Hand twoPairHighCard = new Hand();
twoPairHighCard.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_8));
twoPairHighCard.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_8));
twoPairHighCard.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_9));
twoPairHighCard.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_9));
twoPairHighCard.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_JACK));
//Two Pair (high second pair)
Hand twoPairSecondPair = new Hand();
twoPairSecondPair.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_4));
twoPairSecondPair.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_8));
twoPairSecondPair.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_8));
twoPairSecondPair.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_JACK));
twoPairSecondPair.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_JACK));
//Flush high
Hand flushHigh = new Hand();
flushHigh.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_3));
flushHigh.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_5));
flushHigh.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_8));
flushHigh.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_9));
flushHigh.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_KING));
//Flush low
Hand flushLow = new Hand();
flushLow.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_3));
flushLow.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_4));
flushLow.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_8));
flushLow.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_9));
flushLow.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_KING));
//One Pair
Hand onePair = new Hand();
onePair.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_7));
onePair.addCard(new Card(Card.SUIT_SPADES, Card.CARD_8));
onePair.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_8));
onePair.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_9));
onePair.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_10));
//High Card
Hand highCard = new Hand();
highCard.addCard(new Card(Card.SUIT_CLUBS, Card.CARD_3));
highCard.addCard(new Card(Card.SUIT_SPADES, Card.CARD_6));
highCard.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_7));
highCard.addCard(new Card(Card.SUIT_DIAMONDS, Card.CARD_9));
highCard.addCard(new Card(Card.SUIT_HEARTS, Card.CARD_ACE));
//Play
playAndPrintResult(straightFlush, fourOfAKind);
playAndPrintResult(fullHouse, fourOfAKind);
playAndPrintResult(flushHigh, flushLow);
playAndPrintResult(onePair, highCard);
playAndPrintResult(twoPairFirst, twoPairSecond);
playAndPrintResult(twoPairFirst, twoPairHighCard);
playAndPrintResult(twoPairFirst, twoPairSecondPair);
playAndPrintResult(fullHouse, twoPairFirst);
System.out.println();
System.out.println("*************************");
System.out.println("*** For more examples *** => src/test/com/cbschenk/poker/fivecarddraw/FiveCardDrawTest.java");
System.out.println("*************************");
}
private static void playAndPrintResult(Hand hand1, Hand hand2) {
Hand winner = play(hand1, hand2);
String hand1Prefix = "", hand2Prefix = "";
if (winner == null) {
hand1Prefix = "TIE";
hand2Prefix = "TIE";
} else if (winner.equals(hand1)) {
hand1Prefix = "WIN";
} else {
hand2Prefix = "WIN";
}
System.out.println("***************************************************************************************");
System.out.println(" Result Cards Hand Integer Hand Tie Breaker");
printHand(hand1Prefix, hand1, true);
printHand(hand2Prefix, hand2, false);
}
/**
* Prints a hand with decoded rank either above or below for better visualization
* @param resultPrefix 3-character max string stating win/loss/tie in some manner
* @param hand Hand to be printed
* @param top display decoded rank either above or below hand row
*/
private static void printHand(String resultPrefix, Hand hand, boolean top) {
String handRow = String.format(" %-3s %-37s %10d %s", resultPrefix, hand, hand.getRank(), formatBits(hand.getRank()));
String rankRow = String.format("%87s", decodeInt(hand.getRank()));
if (top) {
System.out.println(rankRow);
System.out.println(handRow);
} else {
System.out.println(handRow);
System.out.println(rankRow);
}
}
/**
* Pretty-printing of rank value to visualize the encoding
* @param rank
* @return Binary representation of rank with spaces every 4 bits
*/
private static String formatBits(int rank) {
char[] bits = String.format("%24s", Integer.toBinaryString(rank)).replace(' ', '0').toCharArray();
//Add spaces every four bits
StringBuffer sb = new StringBuffer(24/4 - 1);
for(int i = 0; i < bits.length; i++) {
sb.append(bits[i]);
if ((i+1) % 4 == 0) {
sb.append(' ');
}
}
return sb.toString();
}
/**
* Creates a string representation of the integer values for every 4 bits
* i.e.
* 0010 1001 1000 1010 0000 0000
* 2 9 8 10 0 0
*/
private static String decodeInt(int rank) {
StringBuffer sb = new StringBuffer(29);
for(int i = 20; i >= 0; i -= 4) {
int value = (rank >> i) & 0xF;
sb.append(String.format("%3d ", value));
}
return sb.toString();
}
}