Repository navigation
Expand file tree
/
Copy pathPokerTest.java
More file actions
381 lines (342 loc) · 14.3 KB
/
Copy pathPokerTest.java
File metadata and controls
381 lines (342 loc) · 14.3 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
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
public class PokerTest {
@Test
@DisplayName("single hand always wins")
public void oneHand() {
String hand = "4S 5S 7H 8D JC";
assertThat(new Poker(Collections.singletonList(hand)).getBestHands())
.containsExactly(hand);
}
@Disabled("Remove to run test")
@Test
@DisplayName("highest card out of all hands wins")
public void highestCardWins() {
String highest8 = "4D 5S 6S 8D 3C";
String highest10 = "2S 4C 7S 9H 10H";
String highestJ = "3S 4S 5D 6H JH";
assertThat(new Poker(Arrays.asList(highest8, highest10, highestJ)).getBestHands())
.containsExactly(highestJ);
}
@Disabled("Remove to run test")
@Test
@DisplayName("a tie has multiple winners")
public void tieHasMultipleWinners() {
String highest8 = "4D 5S 6S 8D 3C";
String highest10 = "2S 4C 7S 9H 10H";
String highestJh = "3S 4S 5D 6H JH";
String highestJd = "3H 4H 5C 6C JD";
assertThat(new Poker(Arrays.asList(highest8, highest10, highestJh, highestJd)).getBestHands())
.containsExactly(highestJh, highestJd);
}
@Disabled("Remove to run test")
@Test
@DisplayName("multiple hands with the same high cards, tie compares next highest ranked, down to last card")
public void sameHighCards() {
String nextHighest3 = "3S 5H 6S 8D 7H";
String nextHighest2 = "2S 5D 6D 8C 7S";
assertThat(new Poker(Arrays.asList(nextHighest3, nextHighest2)).getBestHands())
.containsExactly(nextHighest3);
}
@Disabled("Remove to run test")
@Test
@DisplayName("winning high card hand also has the lowest card")
public void winningWithLowestCard() {
String lowest2 = "2S 5H 6S 8D 7H";
String lowest3 = "3S 4D 6D 8C 7S";
assertThat(new Poker(Arrays.asList(lowest2, lowest3)).getBestHands())
.containsExactly(lowest2);
}
@Disabled("Remove to run test")
@Test
@DisplayName("one pair beats high card")
public void nothingVsOnePair() {
String nothing = "4S 5H 6C 8D KH";
String pairOf4 = "2S 4H 6S 4D JH";
assertThat(new Poker(Arrays.asList(nothing, pairOf4)).getBestHands())
.containsExactly(pairOf4);
}
@Disabled("Remove to run test")
@Test
@DisplayName("highest pair wins")
public void twoPairs() {
String pairOf2 = "4S 2H 6S 2D JH";
String pairOf4 = "2S 4H 6C 4D JD";
assertThat(new Poker(Arrays.asList(pairOf2, pairOf4)).getBestHands())
.containsExactly(pairOf4);
}
@Disabled("Remove to run test")
@Test
@DisplayName("both hands have the same pair, high card wins")
public void samePair() {
String pairOf4Lower = "4H 4S AH JC 3D";
String pairOf4Higher = "4C 4D AS 5D 6C";
assertThat(new Poker(Arrays.asList(pairOf4Lower, pairOf4Higher)).getBestHands())
.containsExactly(pairOf4Lower);
}
@Disabled("Remove to run test")
@Test
@DisplayName("two pairs beats one pair")
public void onePairVsDoublePair() {
String pairOf8 = "2S 8H 6S 8D JH";
String doublePair = "4S 5H 4C 8C 5C";
assertThat(new Poker(Arrays.asList(pairOf8, doublePair)).getBestHands())
.containsExactly(doublePair);
}
@Disabled("Remove to run test")
@Test
@DisplayName("both hands have two pairs, highest ranked pair wins")
public void twoDoublePairs() {
String doublePair2And8 = "2S 8H 2D 8D 3H";
String doublePair4And5 = "4S 5H 4C 8S 5D";
assertThat(new Poker(Arrays.asList(doublePair2And8, doublePair4And5)).getBestHands())
.containsExactly(doublePair2And8);
}
@Disabled("Remove to run test")
@Test
@DisplayName("both hands have two pairs, with the same highest ranked pair, tie goes to low pair")
public void sameHighestPair() {
String doublePair2AndQ = "2S QS 2C QD JH";
String doublePairJAndQ = "JD QH JS 8D QC";
assertThat(new Poker(Arrays.asList(doublePairJAndQ, doublePair2AndQ)).getBestHands())
.containsExactly(doublePairJAndQ);
}
@Disabled("Remove to run test")
@Test
@DisplayName("both hands have two identically ranked pairs, tie goes to remaining card (kicker)")
public void identicallyRankedPairs() {
String kicker8 = "JD QH JS 8D QC";
String kicker2 = "JS QS JC 2D QD";
assertThat(new Poker(Arrays.asList(kicker8, kicker2)).getBestHands())
.containsExactly(kicker8);
}
@Disabled("Remove to run test")
@Test
@DisplayName("both hands have two pairs that add to the same value, win goes to highest pair")
public void twoPairsAddingToSameValue() {
String doublePair6And3 = "6S 6H 3S 3H AS";
String doublePair7And2 = "7H 7S 2H 2S AC";
assertThat(new Poker(Arrays.asList(doublePair6And3, doublePair7And2)).getBestHands())
.containsExactly(doublePair7And2);
}
@Disabled("Remove to run test")
@Test
@DisplayName("two pairs first ranked by largest pair")
public void rankedByLargestPair() {
String doublePairs5And4 = "5C 2S 5S 4H 4C";
String doublePairs6And2 = "6S 2S 6H 7C 2C";
assertThat(new Poker(Arrays.asList(doublePairs5And4, doublePairs6And2)).getBestHands())
.containsExactly(doublePairs6And2);
}
@Disabled("Remove to run test")
@Test
@DisplayName("three of a kind beats two pair")
public void doublePairVsThree() {
String doublePair2And8 = "2S 8H 2H 8D JH";
String threeOf4 = "4S 5H 4C 8S 4H";
assertThat(new Poker(Arrays.asList(doublePair2And8, threeOf4)).getBestHands())
.containsExactly(threeOf4);
}
@Disabled("Remove to run test")
@Test
@DisplayName("both hands have three of a kind, tie goes to highest ranked triplet")
public void twoThrees() {
String threeOf2 = "2S 2H 2C 8D JH";
String threeOf1 = "4S AH AS 8C AD";
assertThat(new Poker(Arrays.asList(threeOf2, threeOf1)).getBestHands())
.containsExactly(threeOf1);
}
@Disabled("Remove to run test")
@Test
@DisplayName("with multiple decks, two players can have same three of a kind, ties go to highest remaining cards")
public void sameThreesMultipleDecks() {
String remainingCard7 = "5S AH AS 7C AD";
String remainingCard8 = "4S AH AS 8C AD";
assertThat(new Poker(Arrays.asList(remainingCard7, remainingCard8)).getBestHands())
.containsExactly(remainingCard8);
}
@Disabled("Remove to run test")
@Test
@DisplayName("a straight beats three of a kind")
public void threeVsStraight() {
String threeOf4 = "4S 5H 4C 8D 4H";
String straight = "3S 4D 2S 6D 5C";
assertThat(new Poker(Arrays.asList(threeOf4, straight)).getBestHands())
.containsExactly(straight);
}
@Disabled("Remove to run test")
@Test
@DisplayName("aces can end a straight (10 J Q K A)")
public void acesCanEndAStraight() {
String hand = "4S 5H 4C 8D 4H";
String straightEndsA = "10D JH QS KD AC";
assertThat(new Poker(Arrays.asList(hand, straightEndsA)).getBestHands())
.containsExactly(straightEndsA);
}
@Disabled("Remove to run test")
@Test
@DisplayName("aces can start a straight (A 2 3 4 5)")
public void acesCanStartAStraight() {
String hand = "4S 5H 4C 8D 4H";
String straightStartA = "4D AH 3S 2D 5C";
assertThat(new Poker(Arrays.asList(hand, straightStartA)).getBestHands())
.containsExactly(straightStartA);
}
@Disabled("Remove to run test")
@Test
@DisplayName("aces cannot be in the middle of a straight (Q K A 2 3)")
public void acesCannotBeInMiddleOfStraight() {
String hand = "2C 3D 7H 5H 2S";
String straightMiddleA = "QS KH AC 2D 3S";
assertThat(new Poker(Arrays.asList(hand, straightMiddleA)).getBestHands())
.containsExactly(hand);
}
@Disabled("Remove to run test")
@Test
@DisplayName("both hands with a straight, tie goes to highest ranked card")
public void twoStraights() {
String straightTo8 = "4S 6C 7S 8D 5H";
String straightTo9 = "5S 7H 8S 9D 6H";
assertThat(new Poker(Arrays.asList(straightTo8, straightTo9)).getBestHands())
.containsExactly(straightTo9);
}
@Disabled("Remove to run test")
@Test
@DisplayName("even though an ace is usually high, a 5-high straight is the lowest-scoring straight")
public void theLowestStraightStartsWithAce() {
String straight = "2H 3C 4D 5D 6H";
String straightStartA = "4S AH 3S 2D 5H";
assertThat(new Poker(Arrays.asList(straight, straightStartA)).getBestHands())
.containsExactly(straight);
}
@Disabled("Remove to run test")
@Test
@DisplayName("flush beats a straight")
public void straightVsFlush() {
String straightTo8 = "4C 6H 7D 8D 5H";
String flushTo7 = "2S 4S 5S 6S 7S";
assertThat(new Poker(Arrays.asList(straightTo8, flushTo7)).getBestHands())
.containsExactly(flushTo7);
}
@Disabled("Remove to run test")
@Test
@DisplayName("both hands have a flush, tie goes to high card, down to the last one if necessary")
public void twoFlushs() {
String flushTo9 = "2H 7H 8H 9H 6H";
String flushTo7 = "3S 5S 6S 7S 8S";
assertThat(new Poker(Arrays.asList(flushTo9, flushTo7)).getBestHands())
.containsExactly(flushTo9);
}
@Disabled("Remove to run test")
@Test
@DisplayName("full house beats a flush")
public void flushVsFull() {
String flushTo8 = "3H 6H 7H 8H 5H";
String full = "4S 5H 4C 5D 4H";
assertThat(new Poker(Arrays.asList(full, flushTo8)).getBestHands())
.containsExactly(full);
}
@Disabled("Remove to run test")
@Test
@DisplayName("both hands have a full house, tie goes to highest-ranked triplet")
public void twoFulls() {
String fullOf4By9 = "4H 4S 4D 9S 9D";
String fullOf5By8 = "5H 5S 5D 8S 8D";
assertThat(new Poker(Arrays.asList(fullOf4By9, fullOf5By8)).getBestHands())
.containsExactly(fullOf5By8);
}
@Disabled("Remove to run test")
@Test
@DisplayName("with multiple decks, both hands have a full house with the same triplet, tie goes to the pair")
public void twoFullssameThripletMultipleDecks() {
String fullOf5By9 = "5H 5S 5D 9S 9D";
String fullOf5By8 = "5H 5S 5D 8S 8D";
assertThat(new Poker(Arrays.asList(fullOf5By9, fullOf5By8)).getBestHands())
.containsExactly(fullOf5By9);
}
@Disabled("Remove to run test")
@Test
@DisplayName("four of a kind beats a full house")
public void fullVsSquare() {
String full = "4S 5H 4D 5D 4H";
String squareOf3 = "3S 3H 2S 3D 3C";
assertThat(new Poker(Arrays.asList(full, squareOf3)).getBestHands())
.containsExactly(squareOf3);
}
@Disabled("Remove to run test")
@Test
@DisplayName("both hands have four of a kind, tie goes to high quad")
public void twoSquares() {
String squareOf2 = "2S 2H 2C 8D 2D";
String squareOf5 = "4S 5H 5S 5D 5C";
assertThat(new Poker(Arrays.asList(squareOf2, squareOf5)).getBestHands())
.containsExactly(squareOf5);
}
@Disabled("Remove to run test")
@Test
@DisplayName("with multiple decks, both hands with identical four of a kind, tie determined by kicker")
public void sameSquaresMultipleDecks() {
String kicker2 = "3S 3H 2S 3D 3C";
String kicker4 = "3S 3H 4S 3D 3C";
assertThat(new Poker(Arrays.asList(kicker2, kicker4)).getBestHands())
.containsExactly(kicker4);
}
@Disabled("Remove to run test")
@Test
@DisplayName("straight flush beats four of a kind")
public void squareVsStraightFlush() {
String squareOf5 = "4S 5H 5S 5D 5C";
String straightFlushTo9 = "7S 8S 9S 6S 10S";
assertThat(new Poker(Arrays.asList(squareOf5, straightFlushTo9)).getBestHands())
.containsExactly(straightFlushTo9);
}
@Disabled("Remove to run test")
@Test
@DisplayName("aces can end a straight flush (10 J Q K A)")
public void acesEndingStraightFlush() {
String hand = "KC AH AS AD AC";
String straightFlushEndingWithA = "10C JC QC KC AC";
assertThat(new Poker(Arrays.asList(hand, straightFlushEndingWithA)).getBestHands())
.containsExactly(straightFlushEndingWithA);
}
@Disabled("Remove to run test")
@Test
@DisplayName("aces can start a straight flush (A 2 3 4 5)")
public void acesStartingStraightFlush() {
String straightFlushStartingWithA = "4H AH 3H 2H 5H";
String hand = "KS AH AS AD AC";
assertThat(new Poker(Arrays.asList(straightFlushStartingWithA, hand)).getBestHands())
.containsExactly(straightFlushStartingWithA);
}
@Disabled("Remove to run test")
@Test
@DisplayName("aces cannot be in the middle of a straight flush (Q K A 2 3)")
public void acesCannotBeInMiddleOfStraightFlush() {
String straightFlushWithAInMiddle = "QH KH AH 2H 3H";
String hand = "2C AC QC 10C KC";
assertThat(new Poker(Arrays.asList(straightFlushWithAInMiddle, hand)).getBestHands())
.containsExactly(hand);
}
@Disabled("Remove to run test")
@Test
@DisplayName("both hands have a straight flush, tie goes to highest-ranked card")
public void twoStraightFlushes() {
String straightFlushTo8 = "4H 6H 7H 8H 5H";
String straightFlushTo9 = "5S 7S 8S 9S 6S";
assertThat(new Poker(Arrays.asList(straightFlushTo8, straightFlushTo9)).getBestHands())
.containsExactly(straightFlushTo9);
}
@Disabled("Remove to run test")
@Test
@DisplayName("even though an ace is usually high, a 5-high straight flush is the lowest-scoring straight flush")
public void straightFlushTo5IsTheLowestScoring() {
String straightFlushTo6 = "2H 3H 4H 5H 6H";
String straightFlushTo5 = "4D AD 3D 2D 5D";
assertThat(new Poker(Arrays.asList(straightFlushTo6, straightFlushTo5)).getBestHands())
.containsExactly(straightFlushTo6);
}
}