-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPokerDeck.java
More file actions
306 lines (289 loc) · 10.9 KB
/
PokerDeck.java
File metadata and controls
306 lines (289 loc) · 10.9 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
import java.util.*;
class PokerDeck extends Deck { // a deck specifically designed to facilitate a game of poker
private Card[] board;
public PokerDeck() {
super();
board = new Card[5];
}
public Card[] getBoard() {
return board.clone();
}
public void setBoard(Card[] board) {
this.board = board;
}
public void reset() { // resets for new hand
super.reset();
board = new Card[5];
}
public Card[] deal() { // deals out the cards required for the board
for (int i = 0; i < 5; i++) {
board[i] = super.deal()[0];
}
return board.clone();
}
public Card[][] deal(int players) { // deals out cards to int players players
Card[][] hands = new Card[players][2];
for (int i = 0; i < players; i++) {
for (int j = 0; j < 2; j++) {
hands[i][j] = super.deal()[0];
}
}
return hands;
}
public int getRanking(Card[] hand) { // gets the ranking of a 5 card poker hand
/*
* Rankings:
* 1 - straight flush
* 2 - 4 of a kind
* 3 - full house
* 4 - flush
* 5 - straight
* 6 - 3 of a kind
* 7 - 2 pair
* 8 - 1 pair
* 9 - high card
*/
int[] numhand = Deck.cardToInt(hand);
int rank = 0;
for (int i = 0; i < 5; i++) {
numhand[i] = hand[i].getNum();
}
Arrays.sort(numhand);
int[] modes = new int[5];
int currMode = 0;
modes[0]++;
boolean containsDupes = false;
for (int i = 1; i < 5; i++) { // check duplicates
if (numhand[i] == numhand[i - 1]) {
modes[currMode]++;
containsDupes = true;
} else {
currMode++;
modes[currMode]++;
}
}
Arrays.sort(modes);
if (containsDupes) {
switch (modes[4]) {
case 4:
rank = 2;
break;
case 3:
if (modes[3] == 2) {
rank = 3;
} else
rank = 6;
break;
case 2:
if (modes[3] == 2) {
rank = 7;
} else
rank = 8;
break;
}
} else {
boolean flush = true;
boolean straight = true;
for (int i = 1; i < 5; i++) {
if (numhand[i] != numhand[i - 1] + 1)
straight = false;
if (hand[i].getValue().charAt(1) != hand[i - 1].getValue().charAt(1))
flush = false;
}
// Special logic for the "Wheel" straight (A-2-3-4-5)
boolean wheel = (numhand[0] == 2 && numhand[1] == 3 && numhand[2] == 4 && numhand[3] == 5 && numhand[4] == 14);
if (wheel) straight = true;
rank = (flush && straight) ? 1 : ((flush || straight) ? ((flush) ? 4 : 5) : 9);
}
return rank;
}
public Card[] getBestHand(Card[] h) { // gets the best possible hand given a players hand and the community cards
// use board and player's hand
ArrayList<Card> hand = new ArrayList<Card>(Arrays.asList(h));
hand.addAll(Arrays.asList(board));
int currRank = Integer.MAX_VALUE;
ArrayList<ArrayList<Card>> currHand = new ArrayList<ArrayList<Card>>();
// We need to choose 5 cards out of 7 (C(7,5) = 21 combinations) (brute force XD)
for (int i = 0; i < hand.size(); i++) {
for (int j = i + 1; j < hand.size(); j++) {
for (int k = j + 1; k < hand.size(); k++) {
for (int l = k + 1; l < hand.size(); l++) {
for (int m = l + 1; m < hand.size(); m++) {
ArrayList<Card> combo = new ArrayList<Card>();
combo.add(hand.get(i));
combo.add(hand.get(j));
combo.add(hand.get(k));
combo.add(hand.get(l));
combo.add(hand.get(m));
if (getRanking(combo.toArray(new Card[5])) < currRank) {
currHand = new ArrayList<ArrayList<Card>>();
currHand.add(combo);
currRank = getRanking(combo.toArray(new Card[5]));
} else if (getRanking(combo.toArray(new Card[5])) == currRank) currHand.add(combo);
}
}
}
}
}
// the above code finds hands only by rank, but does not necessarily compare within the same rank, that's why we have this to compare them within the same rank because there can only be one possible best hand
// i.e. the above code might return all the possible full houses if that's the best, but we need to find the best full house out of all of the full houses, thats what this does
Card[] bestHand = currHand.get(0).toArray(new Card[5]);
for (int i = 1; i < currHand.size(); i++) {
if (compareHands(bestHand, currHand.get(i).toArray(new Card[5])) == 2) bestHand = currHand.get(i).toArray(new Card[5]);
}
return bestHand;
}
public Card[] getBestHand(Card[] h, Card[] b) { // gets the best possible hand given a players hand and custom board
ArrayList<Card> hand = new ArrayList<Card>(Arrays.asList(h));
hand.addAll(Arrays.asList(b));
int currRank = Integer.MAX_VALUE;
ArrayList<ArrayList<Card>> currHand = new ArrayList<ArrayList<Card>>();
for (int i = 0; i < hand.size(); i++) {
for (int j = i + 1; j < hand.size(); j++) {
for (int k = j + 1; k < hand.size(); k++) {
for (int l = k + 1; l < hand.size(); l++) {
for (int m = l + 1; m < hand.size(); m++) {
ArrayList<Card> combo = new ArrayList<Card>();
combo.add(hand.get(i));
combo.add(hand.get(j));
combo.add(hand.get(k));
combo.add(hand.get(l));
combo.add(hand.get(m));
if (getRanking(combo.toArray(new Card[5])) < currRank) {
currHand = new ArrayList<ArrayList<Card>>();
currHand.add(combo);
currRank = getRanking(combo.toArray(new Card[5]));
} else if (getRanking(combo.toArray(new Card[5])) == currRank) currHand.add(combo);
}
}
}
}
}
Card[] bestHand = currHand.get(0).toArray(new Card[5]);
for (int i = 1; i < currHand.size(); i++) {
if (compareHands(bestHand, currHand.get(i).toArray(new Card[5])) == 2) bestHand = currHand.get(i).toArray(new Card[5]);
}
return bestHand;
}
public int compareHands(Card[] h1, Card[] h2) { // compares two poker hands
// returns 1 if the first one is better, 2 if the second one better, 0 if theyre the are the exact same
int H1 = this.getRanking(h1);
int H2 = this.getRanking(h2);
int[] num1 = Deck.cardToInt(h1);
int[] num2 = Deck.cardToInt(h2);
Arrays.sort(num1);
Arrays.sort(num2);
Integer[] useMode = { 2, 3, 6, 7, 8 }; // see the rankings for getRankings
// ^ these are the types of hands that require checking the most reoccuring card
if (H1 < H2) // these are just simple comparisons by rank
return 1;
else if (H2 < H1)
return 2;
else {
if (!Arrays.equals(num1, num2)) { // checks if the arrays are equal to immediately determine if needs to return 0
if (Arrays.asList(useMode).contains((Integer) H1)) {
if (mode(num1) != mode(num2))
return (mode(num1) > mode(num2)) ? 1 : 2;
else {
if (H1 == 7) { // compares two 2-pair hands
int[] mode1 = new int[2]; // stores the pairs for hand 1
int[] mode2 = new int[2]; // stores the pairs for hand 2
int[] sNum1 = new int[3]; // stores hand 1 after removing the first pair, temporary
int[] sNum2 = new int[3]; // stores hand 2 after removing the first pair, temporary
// below code acquires the 2 pairs in each hand
int i1 = 0;
int i2 = 0;
int m1 = mode(num1);
int m2 = mode(num2);
mode1[0] = m1;
mode2[0] = m2;
for (int i = 0; i < 5; i++) {
if (num1[i] != m1) {
sNum1[i1] = num1[i];
i1++;
}
if (num2[i] != m2) {
sNum2[i2] = num2[i];
i2++;
}
}
mode1[1] = mode(sNum1);
mode2[1] = mode(sNum2);
//sorts mode arrays to check if theyre equal
Arrays.sort(mode1);
Arrays.sort(mode2);
if (!Arrays.equals(mode1, mode2)) {
if (mode1[1] != mode2[1])
return (mode1[1] > mode2[1]) ? 1 : 2;
return (mode1[0] > mode2[0]) ? 1 : 2;
} else { // else, check the "kicker" card, or the last card to determine equality
int last1 = 0;
int last2 = 0;
for (int i = 0; i < 3; i++) {
if (sNum1[i] != mode1[0] && sNum1[i] != mode1[1])
last1 = sNum1[i];
if (sNum2[i] != mode2[0] && sNum2[i] != mode2[1])
last2 = sNum2[i];
}
return (last1 != last2) ? ((last1 > last2) ? 1 : 2) : 0;
}
} else { // compares any other pair/set hands (1 pair, 3 of a kind, 4 of a kind, full house)
int m1 = mode(num1);
int m2 = mode(num2);
if (m1 == m2) {
int r = 0;
for (int i = 4; i >= 0; i--) {
if (num1[i] > num2[i]) {
r = 1;
break;
} else if (num1[i] < num2[i]) {
r = 2;
break;
}
}
return r;
} else {
return (m1 > m2) ? 1 : 2;
}
}
}
} else { // compares any non pair/set related hands, like flushes, straights, straight flush, high card
// Special Case: Wheel Straight check (A-2-3-4-5) - Treat Ace (14) as 1 for ranking
boolean wheel1 = (num1[0] == 2 && num1[1] == 3 && num1[2] == 4 && num1[3] == 5 && num1[4] == 14);
boolean wheel2 = (num2[0] == 2 && num2[1] == 3 && num2[2] == 4 && num2[3] == 5 && num2[4] == 14);
if (wheel1 && !wheel2) return (H2 == 5 || H2 == 1) ? 2 : 1; // Any other straight beats a wheel
if (wheel2 && !wheel1) return (H1 == 5 || H1 == 1) ? 1 : 2;
int r = 0;
for (int i = 4; i >= 0; i--) {
if (num1[i] > num2[i]) {
r = 1;
break;
} else if (num1[i] < num2[i]) {
r = 2;
break;
}
}
return r;
}
} else
return 0;
}
}
private int mode(int[] j) { // returns the mode of int list, assuming j is already sorted
int mode = j[0];
int maxFreq = 1;
int currFreq = 1;
for (int i = 1; i < j.length; i++) {
if (j[i] == j[i - 1]) {
currFreq++;
} else {
currFreq = 1;
}
if (currFreq >= maxFreq) {
maxFreq = currFreq;
mode = j[i];
}
}
return mode;
}
}