-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCardCheck.java
More file actions
234 lines (213 loc) · 5.26 KB
/
Copy pathCardCheck.java
File metadata and controls
234 lines (213 loc) · 5.26 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
/**
* CardCheck.java
* performs checks on card objects to find their poker values,
* to be used with PokerTest.java
*
* COMS W1004
* @author Trevor Rukwava (ttr2107)
*
* Friday, November 3, 2017
*/
import java.util.ArrayList;
public class CardCheck {
private ArrayList<Card> hand;
public final int HAND_SIZE;
// constructor to test 5 card hand
public CardCheck(ArrayList<Card> testHand){
HAND_SIZE = 5; // 5 cards per hand
hand = new ArrayList<Card>();
hand = testHand;
}
// performs a series of tests on hand
public String checkHand(){
if (royalFlush()){
return "You have a royal flush!";
}
else if (straight() && flush()){
return "You have a straight flush!";
}
else if (fourOfaKind()){
return "You have four of a kind!";
}
else if (fullHouse()){
return "You have a full house!";
}
else if (flush()){
return "You have a flush!";
}
else if (straight()){
return "You have a straight!";
}
else if (triple()){
return "You have three of a kind!";
}
else if (twoPairs()){
return "You have two pairs!";
}
else if (pair()){
return "You have a pair!";
}
else{
return "Unfortunately, you have no pair.";
}
}
// checks for a royal flush
int check=0;
public boolean royalFlush(){
// checks if ace, then 10, Jack, Queen, then King
if (!(((hand.get(0)).getRank()) == 1 && (hand.get(1)).getRank() == 10
&& (hand.get(2)).getRank() == 11 && (hand.get(3)).getRank() == 12
&& (hand.get(4)).getRank() == 13)){
return false;
}
//checks if all are spades
/* for (Card test: hand){
if (test.getSuit() == 4){
check++;
}
}
if (check == 5){
return true;
} */
return true;
}
public boolean fourOfaKind(){
int check = 0;
for (int j=0; j < HAND_SIZE; j++){
check = 0;
for(int i = 0; i < HAND_SIZE; i++){
if ((hand.get(j)).getRank() == (hand.get(i)).getRank()){
check++;
} // checks if there are
}
if (check==4){
return true;
}
}
int comparison=0;
if ((hand.get(0)).getRank() == (hand.get(1)).getRank()){
for (int i = 1; i < HAND_SIZE; i++){
if ((hand.get(0)).getRank() == (hand.get(i)).getRank())
comparison++;
}
}
else {
for (int i = 1; i < HAND_SIZE-1; i++){
if ((hand.get(HAND_SIZE-1)).getRank()==(hand.get(i)).getRank())
comparison++;
}
}
if (comparison == 4){
return true;
}
else{
return false;
}
}
// checks for full house
public boolean fullHouse(){
return (((hand.get(0)).getRank() == (hand.get(1)).getRank() &&
(hand.get(1)).getRank() == (hand.get(2)).getRank() &&
(hand.get(3)).getRank()== (hand.get(4)).getRank())||
((hand.get(4)).getRank() == (hand.get(3)).getRank() &&
(hand.get(3)).getRank() == (hand.get(2)).getRank() &&
(hand.get(1)).getRank()== (hand.get(0)).getRank()));
}
// checks for flush
public boolean flush(){
for (int i = 1; i < HAND_SIZE; i++){
if ((hand.get(0)).getSuit() != (hand.get(i)).getSuit()){
return false;
}
}
return true;
}
// check for straight
public boolean straight(){
for (int i = 0; i < HAND_SIZE-1; i++){
if (hand.get(0).getRank()==0 && hand.get(1).getRank()==10 &&
hand.get(2).getRank()==11 && hand.get(3).getRank()==12 &&
hand.get(4).getRank()==13){
return true;
}
if ((hand.get(i)).getRank() != ((hand.get(i+1)).getRank() - 1)){
return false;
}
}
return true;
}
// checks for triple
public boolean triple(){
for (int i=0; i<3; i++){
if (hand.get(i).getRank()==hand.get(i+1).getRank() &&
hand.get(i+1).getRank()==hand.get(i+2).getRank()){
return true;
}
}
return false;
}
// checks for two pairs
public boolean twoPairs(){
int check = 0;
for(int i = 0; i < HAND_SIZE - 1; i++){
if ((hand.get(i)).getRank() == (hand.get(i+1)).getRank()){
check++;
}
}
if (check == 2){
return true;
}
else{
return false;
}
}
// check for one pair
public boolean pair(){
int check = 0;
for(int i = 0; i < HAND_SIZE-1; i++){
if ((hand.get(i)).getRank() == (hand.get(i+1)).getRank()){
check++;
}
}
if (check == 1){
return true;
}
else{
return false;
}
}
public int odds(){
// returns the odds associated with each hand
// in order to calculate winnings
if (royalFlush()){
return 250;
}
else if (straight() && flush()){
return 50;
}
else if (fourOfaKind()){
return 25;
}
else if (fullHouse()){
return 6;
}
else if (flush()){
return 5;
}
else if (straight()){
return 4;
}
else if (triple()){
return 3;
}
else if (twoPairs()){
return 2;
}
else if (pair()){
return 1;
}
else{
return 0;
}
}
}