-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHand.java
More file actions
executable file
·320 lines (260 loc) · 6.38 KB
/
Hand.java
File metadata and controls
executable file
·320 lines (260 loc) · 6.38 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
/**
* Class to handle hands
*
* @author Herb Wolfe Jr
*
*/
package maverick;
import static java.lang.System.*;
import java.io.*;
import java.util.Scanner;
public class Hand {
private Card[] aHand;
private byte cardsInHand;
private byte handSize;
/**
* Creates a hand with a default size of HANDSIZE
*/
public Hand() {
new Hand(Maverick.HANDSIZE);
}
/**
* Creates a hand with the passed size
* @param byte - the size of the hand to create
*/
public Hand(byte size) {
if ((size > Maverick.MAXHANDSIZE) || (size < 1)) {
// throw IllegalHandSize
out.println("Illegal hand size!");
System.exit(-1);
}
this.handSize = size;
this.aHand = new Card[handSize];
this.cardsInHand = 0;
}
/**
* Adds a card to the hand
* @param card the card to add
*/
public void addCard(Card aCard) {
// change to exception!
if (aCard == null) {
out.println("Error! null card passed to addCard!");
System.exit(-1);
}
if (this.cardsInHand < this.handSize) {
//this.aHand[cardsInHand] = new Card(aCard);
this.aHand[cardsInHand] = aCard;
//this.aHand[cardsInHand] = aCard.clone();
//this.aHand[cardsInHand].setCard(aCard.getCard());
cardsInHand++;
} else {
/* hand is full! */
out.println("Hand is full!");
}
}
/**
* Returns an array of 5 card arrays, assuming this is a 6 or 7 card hand
*
* TODO: Modify/update to return an array of Hand objects
*/
private Hand[] get5CardHands() {
Hand[] allHands = null;
byte aSize, length = this.getHandSize();
if (length == 6) {
aSize = 6;
allHands = new Hand[aSize];
byte c1 = 0; // one card gets dropped
/* build the hands here */
for (byte i = 0; i < aSize; i++) {
byte p = 0; // placeholder in aHand
for (byte j = 0; j < Maverick.HANDSIZE; j++) {
if (p == c1) p++;
allHands[i].addCard(this.aHand[p]);
p++;
}
c1++;
}
} else if (length == 7) {
aSize = 21;
allHands = new Hand[aSize];
byte c1 = 0, c2 = 1; // 2 cards get dropped
/* build the hands here */
for (byte i = 0; i < aSize; i++) {
byte p = 0; // placeholder in aHand
for (byte j = 0; j < Maverick.HANDSIZE; j++) {
if (p == c1) p++;
if (p == c2) p++;
allHands[i].addCard(this.aHand[p]);
p++;
}
if (c2 == length) {
c1++; c2 = (byte)(c1 + 1);
} else {
c2++;
}
}
} else {
// we have an error
}
return allHands;
}
/**
* Returns a string representation of the hand
*/
public String getHand() {
String aHandStr = "";
for (int i = 0; i < cardsInHand; i++) {
aHandStr += this.aHand[i] + " ";
}
return aHandStr;
}
/**
* Returns the number of cards in the hand
* @return byte - the number of cards in the hand
*/
public byte getCardCount() {
return this.cardsInHand;
}
/**
* Returns the size of the hand
* @return byte - the number of cards the hand can hold
*/
public byte getHandSize() {
return this.handSize;
}
/**
* Returns the name of the hand
* For example, 8 high straight, Ace high flush, Aces full of Treys, etc.
*
* @return The name of the hand.
*/
public String getHandName() {
String name = "a hand";
boolean flush = isFlush();
int aRank = getHandRank(flush);
/* lookup code goes here */
if (flush) {
name += " flush";
}
return name;
}
/**
* Returns the ranking of the hand
* Currently 1 for lowest hand, 75432 of mixed suits, 7462 for a royal
* flush
*
* @param boolean - whether or not the hand is a flush
* @return the rank of the hand
*/
public short getHandRank(boolean flush) {
// insert error checking! cardsInHand should == handSize
// Need to change to exception
if (this.getCardCount() < this.getHandSize()) {
out.println("Error, hand is not complete, cannot look up rank!");
System.exit(-1);
}
int aVal = 1;
int aRank;
for (Card c: this.aHand) {
aRank = c.getRank() >> 3;
aVal *= aRank;
}
return (this.lookup(aVal, flush));
//return aVal;
}
/**
* Determine if the hand has a (5-card) flush.
*
* @return true if the hand has a flush
*/
private boolean hasFlush() {
boolean flush = false;
switch (handSize) {
case 5:
flush = isFlush();
break;
case 6:
case 7:
// generate possible 5 card combinations
Hand fiveCardHands[] = this.get5CardHands();
for(byte i = 0; i < fiveCardHands.length && !flush; i++) {
flush = fiveCardHands[i].isFlush();
}
break;
default:
// throw error?
}
return flush;
}
/**
* Determines if the 5 card hand is a flush
*
* @return true if the hand is a flush.
*/
private boolean isFlush() {
short aSuit = Maverick.SUITMASK;
boolean flush;
/*
* add error checking?
* if this.cardsInHand <> 5 {
* throw invalidHandException;
* }
*/
for (Card c : this.aHand) {
aSuit = (short)(aSuit & c.getSuitByte());
}
flush = (aSuit == this.aHand[1].getCard()) ? true : false;
return flush;
}
/**
* Performs the actual lookup in the hand rank table
* @param int - the value to look for
* @param boolean - whether or not to return the flush rank
*/
private short lookup(int aVal, boolean flush) {
short aRank = 0;
int inRank, inVal;
String token;
/* insert lookup code here:
*
* open file
* seek aval
* if flush
* read flushrank
* else
* read rank
* endif
*/
out.println(aVal);
Scanner in = null;
try {
in = new Scanner(new File("maverick/data/5cardranks.txt"));
//in.useDelimiter(",");
} catch (FileNotFoundException ex) {
out.println(ex.getMessage());
out.println("in " + System.getProperty("user.dir"));
System.exit(1);
}
boolean found = false;
while (in.hasNextLine() && !found) {
inRank = in.nextInt();
//inRank = Integer.parseInt(in.next());
//out.print(inRank);
//inVal = Integer.parseInt(in.next());
inVal = in.nextInt();
//out.println(inVal);
//in.nextLine();
if (aVal == inVal) {
found = true;
aRank = (short)inRank;
out.println("value found!");
break;
}
}
if (!found) {
out.println("Error, hand value not found in lookup file!");
}
return aRank;
}
}