-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHandEvaluator.java
More file actions
403 lines (341 loc) · 9.41 KB
/
HandEvaluator.java
File metadata and controls
403 lines (341 loc) · 9.41 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
import hsa.Console;
import java.awt.*;
public class HandEvaluator
{
VectorDeckClass AIDeck = new VectorDeckClass (); // AIDeck consists of 2 cards dealt by dealer
CardClass AIPotDeck[] = new CardClass [5]; // Potdeck has the 5 community cards
public CardClass globalHand[] = new CardClass [7];
// Get all Values
int[] AIValue = new int [7];
// Get all Suits
int[] AISuit = new int [7];
HandEvaluator ()
{
for (int i = 0 ; i < 7 ; i++)
{
globalHand [i] = new CardClass ();
}
for (int i = 0 ; i < 7 ; i++)
{
AIValue [i] = 0;
}
for (int i = 0 ; i < 7 ; i++)
{
AISuit [i] = 0;
}
}
public void setAIDeck (VectorDeckClass nAIDeck)
{
AIDeck = nAIDeck;
}
public void setAIPotDeck (CardClass nCard, int position)
{
AIPotDeck [position] = nCard;
}
public void setCards ()
{
// Dealt Cards
for (int i = 0 ; i < 2 ; i++)
{
globalHand [i] = AIDeck.dealCard (i);
// Store in Array
AISuit [i] = globalHand [i].getSuit ();
AIValue [i] = globalHand [i].getValue ();
}
for (int j = 0 ; j < 5 ; j++)
{
globalHand [(j + 2)] = AIPotDeck [j];
// Store in Array
AISuit [(j + 2)] = globalHand [(j + 2)].getSuit ();
AIValue [(j + 2)] = globalHand [(j + 2)].getValue ();
}
}
//OK
public int[] Hand (int Combination)
{
int HandValue[] = new int [10];
CardClass currentHand[] = new CardClass [5]; // 5 taken from the 7 in the globalhand array
int[] currentHandValue = new int [14]; // 1 for Ace, 0 always empty, increment by 1 everytime a card appears
int sameCards = 1; // to record how many of the cards are of the same value
int sameCards2 = 1; // to record how many of the cards are of the same value
int largeGroupValue = 0; // to hold the value of the largest pair that is found
int smallGroupValue = 0; // to hold the value of the smaller pair that is found
int index = 0;
int topStraightValue = 0; // Value of straight is calculated from the topmost card
boolean isFlush = true; // Assume there is a flush until proven otherwise
boolean isStraight = false; //Flag for striaghts
//int HandValue[] = new int [6];
int orderedValues[] = new int [6];
//FILL THE HAND WITH A SPECIFIC COMBO
//ALL COMBOS
// 12345
// 13456
// 14567
// 23456
// 24567
// 34567
//12456
//12567
//12356
//12367
//23567
if (Combination == 1) //12345
{
for (int i = 0 ; i < 5 ; i++)
{
currentHand [i] = globalHand [i]; //fill up cards array with cards from global
}
}
if (Combination == 2) //23456
{
for (int i = 1 ; i < 6 ; i++)
{
currentHand [i - 1] = globalHand [i]; //fill up cards array with cards from global
}
}
if (Combination == 3) //34567
{
for (int i = 2 ; i < 7 ; i++)
{
currentHand [i - 2] = globalHand [i]; //fill up cards array with cards from global
}
}
if (Combination == 4) //13456
{
currentHand [0] = globalHand [0];
for (int i = 2 ; i < 6 ; i++)
{
currentHand [i - 1] = globalHand [i]; //fill up cards array with cards from global
}
}
if (Combination == 5) //14567
{
currentHand [0] = globalHand [0];
for (int i = 3 ; i < 7 ; i++)
{
currentHand [i - 2] = globalHand [i]; //fill up cards array with cards from global
}
}
if (Combination == 6) //24567
{
currentHand [0] = globalHand [1];
for (int i = 3 ; i < 7 ; i++)
{
currentHand [i - 2] = globalHand [i]; //fill up cards array with cards from global
}
}
if (Combination == 7) //12456
{
currentHand [0] = globalHand [0];
currentHand [1] = globalHand [1];
for (int i = 3 ; i < 6 ; i++)
{
currentHand [i - 1] = globalHand [i]; //fill up cards array with cards from global
}
}
if (Combination == 8) //12567
{
currentHand [0] = globalHand [0];
currentHand [1] = globalHand [1];
for (int i = 4 ; i < 7 ; i++)
{
currentHand [i - 2] = globalHand [i]; //fill up cards array with cards from global
}
}
if (Combination == 9) //12356
{
currentHand [0] = globalHand [0];
currentHand [1] = globalHand [1];
currentHand [2] = globalHand [2];
currentHand [3] = globalHand [4];
currentHand [4] = globalHand [5];
}
if (Combination == 10) //12367
{
currentHand [0] = globalHand [0];
currentHand [1] = globalHand [1];
currentHand [2] = globalHand [2];
currentHand [3] = globalHand [5];
currentHand [4] = globalHand [6];
}
if (Combination == 11) //23567
{
currentHand [0] = globalHand [1];
currentHand [1] = globalHand [2];
currentHand [2] = globalHand [4];
currentHand [3] = globalHand [5];
currentHand [4] = globalHand [6];
}
//Array of 13 values to count the number of occurances
for (int i = 0 ; i <= 13 ; i++)
{
currentHandValue [i] = 0;
}
for (int i = 0 ; i <= 4 ; i++)
{
currentHandValue [currentHand [i].getValue ()]++;
}
//===============CHECK FOR POKER HANDS HERE
//Confirm that there is a flush
for (int i = 0 ; i < 4 ; i++)
{
if (currentHand [i].getSuit () != currentHand [i + 1].getSuit ())
{
isFlush = false;
}
}
//Check for doubles, triples and quads
for (int i = 13 ; i >= 1 ; i--) //Loop startng at 13 (king) to 1 (Ace)
{
if (currentHandValue [i] > sameCards)
{
if (sameCards != 1) // if sameCards's default value has changed
{
sameCards2 = sameCards;
smallGroupValue = largeGroupValue;
}
sameCards = currentHandValue [i];
largeGroupValue = i;
}
else if (currentHandValue [i] > sameCards2)
{
sameCards2 = currentHandValue [i];
smallGroupValue = i;
}
}
if (currentHandValue [1] == 1)
{
orderedValues [index] = 14;
index++;
}
for (int i = 13 ; i >= 2 ; i--)
{
if (currentHandValue [i] == 1)
{
orderedValues [index] = i; //if ace
index++;
}
}
//Figure out if there is a straight
for (int i = 1 ; i <= 9 ; i++)
{
if (currentHandValue [i] == 1 &&
currentHandValue [i + 1] == 1 &&
currentHandValue [i + 2] == 1 &&
currentHandValue [i + 3] == 1 &&
currentHandValue [i + 4] == 1)
{
isStraight = true;
topStraightValue = i + 4;
break;
}
}
// Special case for 10, Jack, Queen, King, Ace
if (currentHandValue [10] == 1 &&
currentHandValue [11] == 1 &&
currentHandValue [12] == 1 &&
currentHandValue [13] == 1 &&
currentHandValue [1] == 1)
{
isStraight = true;
topStraightValue = 14; //ranked higher than king
}
//===================Rank Hands=============================
if (currentHandValue [1] == 1) //if ace, run this before because ace is highest card
{
//record an ace as 14 instead of one, as its the highest card
orderedValues [index] = 14;
index++; //increment position
}
for (int i = 13 ; i >= 2 ; i--)
{
if (currentHandValue [i] == 1)
//we have already written code to handle the case
//of their being two cards of the same rank
{
// orderedVes [index] = i;
index++;
}
}
// currentHandValue
for (int i = 0 ; i <= 5 ; i++)
{
HandValue [i] = 0;
}
//START HAND EVALUATION
if (sameCards == 1)
{ //if we have no pair...
HandValue [0] = 1; //this is the lowest type of hand, so it gets the lowest value
HandValue [1] = orderedValues [0]; //the first determining factor is the highest card,
HandValue [2] = orderedValues [1]; //then the next highest card,
HandValue [3] = orderedValues [2]; //and so on
HandValue [4] = orderedValues [3];
HandValue [5] = orderedValues [4];
}
if (sameCards == 2 && sameCards2 == 1) //if 1 pair
{
HandValue [0] = 2; //pair ranked higher than high card
HandValue [1] = largeGroupValue; //rank of pair
HandValue [2] = orderedValues [0]; //next highest cards.
HandValue [3] = orderedValues [1];
HandValue [4] = orderedValues [2];
}
if (sameCards == 2 && sameCards2 == 2) //two pair
{
HandValue [0] = 3;
//rank of greater pair
HandValue [1] = largeGroupValue > smallGroupValue ? largeGroupValue:
smallGroupValue;
//rank of smaller pair
HandValue [2] = largeGroupValue < smallGroupValue ? largeGroupValue:
smallGroupValue;
HandValue [3] = orderedValues [0]; //extra card
}
if (sameCards == 3 && sameCards2 != 2)
//three of a kind (not full house)
{
HandValue [0] = 4;
HandValue [1] = largeGroupValue;
HandValue [2] = orderedValues [0];
HandValue [3] = orderedValues [1];
}
if (isStraight == true)
{
HandValue [0] = 5;
HandValue [1] = topStraightValue;
//if we have two straights,
//the one with the highest top cards wins
}
if (isFlush == true)
{
HandValue [0] = 6;
HandValue [1] = orderedValues [0]; //tie determined by ranks of cards
HandValue [2] = orderedValues [1];
HandValue [3] = orderedValues [2];
HandValue [4] = orderedValues [3];
HandValue [5] = orderedValues [4];
}
if (sameCards == 3 && sameCards2 == 2) //full house
{
HandValue [0] = 7;
HandValue [1] = largeGroupValue;
HandValue [2] = smallGroupValue;
}
if (sameCards == 4) //four of a kind
{
HandValue [0] = 8;
HandValue [1] = largeGroupValue;
HandValue [2] = orderedValues [0];
}
if (isFlush == true && isFlush == true) //straight flush
{
HandValue [0] = 9;
HandValue [1] = orderedValues [0];
HandValue [2] = orderedValues [1];
HandValue [3] = orderedValues [2];
HandValue [4] = orderedValues [3];
HandValue [5] = orderedValues [4];
}
return HandValue;
}
}