-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEuler54.java
356 lines (303 loc) · 7.46 KB
/
Euler54.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
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
import java.util.*;
import java.io.*;
class Euler54 {
public static void main(String[] arg) {
File file = new File("poker.txt");
Scanner scan;
try {
scan = new Scanner(file);
}
catch(FileNotFoundException e) {
System.err.println("File not found");
return;
}
int count = 0;
while(scan.hasNextLine()) {
String line = scan.nextLine();
String[] tokens = line.split(" ");
Card[] c1 = new Card[5];
Card[] c2 = new Card[5];
int tokenCount = 0;
for(int i = 0; i < 5; i++, tokenCount++)
c1[i] = new Card(tokens[tokenCount]);
for(int i = 0; i < 5; i++, tokenCount++)
c2[i] = new Card(tokens[tokenCount]);
Poker poker = new Poker(c1, c2);
count = poker.determineWinner() ? count + 1 : count;
}
System.out.println(count);
}
}
class Card implements Comparable<Card> {
public int value;
public int suit;
public static final int HEARTS = 1;
public static final int CLUBS = 2;
public static final int SPADES = 3;
public static final int DIAMONDS = 4;
public static final int TWO = 2;
public static final int THREE = 3;
public static final int FOUR = 4;
public static final int FIVE = 5;
public static final int SIX = 6;
public static final int SEVEN = 7;
public static final int EIGHT = 8;
public static final int NINE = 9;
public static final int TEN = 10;
public static final int JACK = 11;
public static final int QUEEN = 12;
public static final int KING = 13;
public static final int ACE = 14;
public Card(String card) {
char a1 = card.charAt(0);
char a2 = card.charAt(1);
if(a1 >= '2' && a1 <= '9')
value = a1 - 48;
else if(a1 == 'T')
value = TEN;
else if(a1 == 'J')
value = JACK;
else if(a1 == 'Q')
value = QUEEN;
else if(a1 == 'K')
value = KING;
else if(a1 == 'A')
value = ACE;
switch(a2) {
case 'H': suit = HEARTS;
break;
case 'C': suit = CLUBS;
break;
case 'S': suit = SPADES;
break;
case 'D': suit = DIAMONDS;
break;
}
}
public int compareTo(Card c) {
if(this.value < c.value)
return -1;
else if(this.value > c.value)
return 1;
else
return 0;
}
public String toString() {
return "{" + value + ", " + suit + "}";
}
}
class Hand {
public Card[] c;
public int p1, p2;
public Hand(Card[] c) {
this.c = c;
}
}
class Poker {
Hand h1;
Hand h2;
public Poker(Card[] c1, Card[] c2) {
Arrays.sort(c1);
Arrays.sort(c2);
h1 = new Hand(c1);
h2 = new Hand(c2);
}
public boolean determineWinner() {
boolean case1, case2;
case1 = straightFlush(h1);
case2 = straightFlush(h2);
if(case1 && case2)
return winner();
if(case1)
return true;
if(case2)
return false;
case1 = fourOfAKind(h1);
case2 = fourOfAKind(h2);
if(case1 && case2) {
if(h1.c[2].value > h2.c[2].value)
return true;
else if(h1.c[2].value < h2.c[2].value)
return false;
else
return winner();
}
if(case1)
return true;
if(case2)
return false;
case1 = fullHouse(h1);
case2 = fullHouse(h2);
if(case1 && case2) {
if(h1.c[2].value > h2.c[2].value)
return true;
else if(h1.c[2].value < h2.c[2].value)
return false;
else if(h1.p1 > h2.p2)
return true;
else return false;
}
if(case1)
return true;
if(case2)
return false;
case1 = flush(h1);
case2 = flush(h2);
if(case1 && case2) {
return winner();
}
if(case1)
return true;
if(case2)
return false;
case1 = straight(h1);
case2 = straight(h2);
if(case1 && case2) {
return winner();
}
if(case1)
return true;
if(case2)
return false;
case1 = threeOfAKind(h1);
case2 = threeOfAKind(h2);
if(case1 && case2) {
if(h1.c[2].value > h2.c[2].value)
return true;
else
return winner();
}
if(case1)
return true;
if(case2)
return false;
case1 = twoPair(h1);
case2 = twoPair(h2);
if(case1 && case2) {
if(h1.p1 > h2.p1)
return true;
else if(h1.p1 < h2.p1)
return false;
else if(h1.p2 > h2.p2)
return true;
else if(h1.p2 < h2.p2)
return false;
else
return winner();
}
if(case1)
return true;
if(case2)
return false;
case1 = pair(h1);
case2 = pair(h2);
if(case1 && case2) {
if(h1.p1 > h2.p1)
return true;
else if(h1.p1 < h2.p1)
return false;
else
return winner();
}
if(case1)
return true;
if(case2)
return false;
return winner();
}
public boolean flush(Hand h) {
return (h.c[0].suit == h.c[1].suit &&
h.c[0].suit == h.c[2].suit &&
h.c[0].suit == h.c[3].suit &&
h.c[0].suit == h.c[4].suit);
}
private boolean straight(Hand h) {
if(h.c[0].value == Card.TWO &&
h.c[1].value == Card.THREE &&
h.c[2].value == Card.FOUR &&
h.c[3].value == Card.FIVE &&
h.c[4].value == Card.ACE)
return true;
return (h.c[4].value - h.c[3].value == 1 &&
h.c[3].value - h.c[2].value == 1 &&
h.c[2].value - h.c[1].value == 1 &&
h.c[1].value - h.c[0].value == 1);
}
private boolean fourOfAKind(Hand h) {
return( (h.c[0].value == h.c[1].value &&
h.c[1].value == h.c[2].value &&
h.c[2].value == h.c[3].value) ||
(h.c[1].value == h.c[2].value &&
h.c[2].value == h.c[3].value &&
h.c[3].value == h.c[4].value) );
}
private boolean threeOfAKind(Hand h) {
return( (h.c[0].value == h.c[1].value &&
h.c[1].value == h.c[2].value) ||
(h.c[1].value == h.c[2].value &&
h.c[2].value == h.c[3].value) ||
(h.c[2].value == h.c[3].value &&
h.c[3].value == h.c[4].value) );
}
private boolean pair(Hand h) {
if(h.c[0].value == h.c[1].value &&
h.c[1].value != h.c[2].value) {
h.p1 = h.c[0].value;
return true;
}
if(h.c[0].value != h.c[1].value &&
h.c[1].value == h.c[2].value &&
h.c[2].value != h.c[3].value) {
h.p1 = h.c[1].value;
return true;
}
if(h.c[1].value != h.c[2].value &&
h.c[2].value == h.c[3].value &&
h.c[3].value != h.c[4].value) {
h.p1 = h.c[2].value;
return true;
}
if(h.c[2].value != h.c[3].value &&
h.c[3].value == h.c[4].value) {
h.p1 = h.c[3].value;
return true;
}
return false;
}
private boolean twoPair(Hand h) {
int count = 0;
int index = 0;
int temp;
while(index < 4) {
if(h.c[index++].value == h.c[index].value) {
if(h.p1 == 0)
h.p1 = h.c[index].value;
else
h.p2 = h.c[index].value;
index++;
count++;
}
}
if(h.p2 > h.p1) {
temp = h.p1;
h.p1 = h.p2;
h.p2 = temp;
}
return (count == 2);
}
private boolean fullHouse(Hand h) {
return threeOfAKind(h) && pair(h);
}
private boolean straightFlush(Hand h) {
return straight(h) && flush(h);
}
private boolean winner() {
for(int i = 4; i >= 0; i--) {
if(h1.c[i].value > h2.c[i].value)
return true;
else if(h1.c[i].value < h2.c[i].value)
return false;
}
return false;
}
}