-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHandRank.java
128 lines (116 loc) · 3.24 KB
/
HandRank.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
/*
* HandRank tells what kind of hand
* 5 poker cards are
* It currently works for 5 cards, but it is limited
* such that it assumes high ranks have been checked
*
* For example, a hand of 5, 5, 5, 5 (different suits) and 4 (hearts)
* can qualify as a Pair although a Four of a Kind
* is better suited and is the actual
*/
public abstract class HandRank {
public abstract boolean qualifies(PlayingCard[] hand);
}
class HighCard extends HandRank {
public boolean qualifies(PlayingCard[] hand) {
return hand.length != 0;
}
}
class Pair extends HandRank {
public boolean qualifies(PlayingCard[] hand) {
for (int i = 0; i < hand.length - 1; i++) {
if (hand[i].getRankValue() == hand[i + 1].getRankValue()) {
return true;
}
}
return false;
}
}
class TwoPair extends HandRank {
public boolean qualifies(PlayingCard[] hand) {
int numOfPairs = 0;
for (int i = 0; i < hand.length - 1; i++) {
if (hand[i].getRankValue() == hand[i + 1].getRankValue()) {
numOfPairs++;
}
}
return numOfPairs > 1;
}
}
class ThreeOfAKind extends HandRank {
public boolean qualifies(PlayingCard[] hand) {
for (int i = 0; i < hand.length - 2; i++) {
if (hand[i].getRankValue() == hand[i + 1].getRankValue() &&
hand[i].getRankValue() == hand[i + 2].getRankValue()) {
return true;
}
}
return false;
}
}
class Straight extends HandRank {
public boolean qualifies(PlayingCard[] hand) {
for (int i = 0; i < hand.length - 1; i++) {
if (hand[i].getRankValue() + 1 != hand[i + 1].getRankValue()) {
return false;
}
}
return true;
}
}
class Flush extends HandRank {
public boolean qualifies(PlayingCard[] hand) {
for (int i = 1; i < hand.length; i++) {
if (hand[i].getSuit() != hand[0].getSuit()) {
return false;
}
}
return true;
}
}
class FullHouse extends HandRank {
public boolean qualifies(PlayingCard[] hand) {
if (hand[0].getRank() == hand[1].getRank()) {
return (hand[2].getRank() == hand[3].getRank()) &&
hand[2].getRank() == hand[4].getRank();
} else if (hand[3].getRank() == hand[4].getRank()) {
return (hand[0].getRank() == hand[1].getRank()) &&
hand[0].getRank() == hand[2].getRank();
} else {
return false;
}
}
}
class FourOfAKind extends HandRank {
public boolean qualifies(PlayingCard[] hand) {
boolean firstFour = (hand[0].getRank() == hand[1].getRank()) &&
(hand[0].getRank() == hand[2].getRank()) &&
(hand[0].getRank() == hand[3].getRank());
if (!firstFour) {
boolean lastFour = (hand[1].getRank() == hand[2].getRank()) &&
(hand[1].getRank() == hand[3].getRank()) &&
(hand[1].getRank() == hand[4].getRank());
return lastFour;
}
return true;
}
}
// Unsure how to utilize Straight and Flush classes
// Static vs non static reference
class StraightFlush extends HandRank {
public boolean qualifies(PlayingCard[] hand) {
return (new Straight()).qualifies(hand) && (new Flush()).qualifies(hand);
}
}
class RoyalFlush extends HandRank {
public boolean qualifies(PlayingCard[] hand) {
if (hand[0].getRankValue() != 1) {
return false;
}
return hand[1].getRankValue() == 10 &&
hand[2].getRankValue() == 11 &&
hand[3].getRankValue() == 12 &&
hand[4].getRankValue() == 13 &&
(new Flush()).qualifies(hand);
}
}