-
Notifications
You must be signed in to change notification settings - Fork 3
/
Hand.cpp
340 lines (325 loc) · 9.07 KB
/
Hand.cpp
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
//******************************************************************************
//
// File Name: Hand.cpp
//
// File Overview: Represents a Hand containing five cards
//
//******************************************************************************
//
// Revision History:
//
// Date Author Description
// 11/27/2017 Jason Chen Added Class
//******************************************************************************
#include <iostream>
#include "Hand.h"
#include <algorithm>
Hand::Hand(Card card0, Card card1, Card card2, Card card3, Card card4)
{
first = card0;
second = card1;
third = card2;
fourth = card3;
fifth = card4;
type = this->getType();
suit = this->getSuit();
quad = this->getQuad();
trip = this->getTrip();
doub = this->getDoub();
conn = this->getConn();
high = this->getHigh();
}
//***************************************************************************
// Function : getQuad
// Process : checks if the hand is suited
// return 1 for suited, return 0 for non-suited
// Notes : None
//***************************************************************************
int Hand::getSuit()
{
int suits[] = {first.getSuit(), second.getSuit(), third.getSuit(), fourth.getSuit(), fifth.getSuit()};
vector<int> vsuits (suits, suits+5);
sort(vsuits.begin(),vsuits.begin()+5);
vector<int>::iterator it;
it = std::unique(vsuits.begin(), vsuits.end());
vsuits.resize(std::distance(vsuits.begin(), it));
if(vsuits.size() == 1)
{
return 1;
}
else
{
return 0;
}
}
//***************************************************************************
// Function : getQuad
// Process : checks for quads
// return a positive number in base 100 with the most significant bit representing the quadrupled value
// the least significant bit represents kicker's value
// return 0 if no quadruplet is detected
// Notes : The kicker here is optional, notice that comparing quads can be simplified by invoking the high card method
//***************************************************************************
int Hand::getQuad()
{
int numbers[] = {first.getNumber(), second.getNumber(), third.getNumber(), fourth.getNumber(), fifth.getNumber()};
vector<int> vnumbers (numbers, numbers+5);
sort(vnumbers.begin(),vnumbers.begin()+5);
int count = 1, temp = 1, quad = 0, kicker = 0;
for(unsigned int i = 0; i < vnumbers.size() - 1; i++)
{
if(vnumbers[i] == vnumbers[i + 1])
{
if(temp == 1)
{
count++;
quad = 100 * vnumbers[i];
}
else
{
temp = 1;
count = 2;
}
}
else
{
if(i == 0)
{
kicker = vnumbers[0];
}
if(i == 3)
{
kicker = vnumbers[4];
}
temp = 0;
}
}
if(count == 4)
{
return quad + kicker;
}
else
{
return 0;
}
}
//***************************************************************************
// Function : getDoub
// Process : checks for the any three of a kind or full house in a hand
// return a positive number in base 100 with the most significant bit representing the pair value
// the least significant bit represents the triplet's value
// return 0 if no triplet is detected
// Notes : None
//***************************************************************************
int Hand::getTrip()
{
int numbers[] = {first.getNumber(), second.getNumber(), third.getNumber(), fourth.getNumber(), fifth.getNumber()};
vector<int> vnumbers (numbers, numbers+5);
sort(vnumbers.begin(),vnumbers.begin()+5);
int maxcount = 0, count = 1, temp = 1, triple = 0, full = 0;
for(unsigned int i = 0; i < vnumbers.size() - 1; i++)
{
if(vnumbers[i] == vnumbers[i + 1])
{
if(temp == 1)
{
count++;
maxcount = count;
triple = vnumbers[i];
}
else
{
temp = 1;
count = 2;
}
}
else
{
//AABBB
if(i == 1)
{
if(vnumbers[0] == vnumbers[1])
{
full = vnumbers[0] * 100;
}
}
//AAABB
if(i == 2)
{
if(vnumbers[3] == vnumbers[4])
{
full = vnumbers[3] * 100;
}
}
temp = 0;
}
}
if(maxcount == 3)
{
return full + triple;
}
else
{
return 0;
}
}
//***************************************************************************
// Function : getDoub
// Process : checks for the any pairs in a hand
// return a positive number for two pairs or one pair
// return 0 for non-paired
// Notes : None
//***************************************************************************
int Hand::getDoub()
{
int numbers[] = {first.getNumber(), second.getNumber(), third.getNumber(), fourth.getNumber(), fifth.getNumber()};
vector<int> vnumbers (numbers, numbers+5);
sort(vnumbers.begin(),vnumbers.begin()+5);
int count = 1, doub1 = 0, doub2 = 0;
for(unsigned int i = 0; i < vnumbers.size() - 1; i++)
{
if(vnumbers[i] == vnumbers[i + 1])
{
if(count == 1)
{
count++;
doub2 = vnumbers[i];
}
else
{
doub1 = vnumbers[i] * 100;
}
}
}
return doub1 + doub2;
}
//***************************************************************************
// Function : getConn
// Process : checks the connectivity of a hand
// return the largest number within a straight
// return 0 for non-straight
// Notes : None
//***************************************************************************
int Hand::getConn()
{
int numbers[] = {first.getNumber(), second.getNumber(), third.getNumber(), fourth.getNumber(), fifth.getNumber()};
vector<int> vnumbers (numbers, numbers+5);
sort(vnumbers.begin(),vnumbers.begin()+5);
int straight = 0;
for(unsigned int i = 0; i < vnumbers.size() - 1; i++)
{
if(vnumbers[i] + 1 == vnumbers[i + 1])
{
straight = vnumbers[i + 1];
}
else
{
return 0;
}
}
return straight;
}
//***************************************************************************
// Function : getHigh
// Process : return a base 100, encoded 10 digits number
// representing the high card value of a hand
// Notes : None
//***************************************************************************
int Hand::getHigh()
{
int numbers[] = {first.getNumber(), second.getNumber(), third.getNumber(), fourth.getNumber(), fifth.getNumber()};
vector<int> vnumbers (numbers, numbers+5);
sort(vnumbers.begin(),vnumbers.begin()+5);
return vnumbers[4] * 100000000 + vnumbers[3] * 1000000 + vnumbers[2] * 10000 + vnumbers[1] * 100 + vnumbers[0];
}
//***************************************************************************
// Function : printHand
// Process : Print the hand by calling each card's print method
// Print the hand type
// Notes : None
//***************************************************************************
void Hand::printHand()
{
Card card; // Current card, used to iterate the hand and print each card
first.printCard();
second.printCard();
third.printCard();
fourth.printCard();
fifth.printCard();
this->printType();
}
//***************************************************************************
// Function : getType
// Process : return values corresponds to the type of hand from 0 to 8
// Notes : None
//***************************************************************************
int Hand::getType()
{
if(suit == 1 && conn > 1)
return 8;
else if(quad > 0)
return 7;
else if(trip > 100)
return 6;
else if(suit == 1)
return 5;
else if(conn > 0)
return 4;
else if(trip > 0)
return 3;
else if(doub > 100)
return 2;
else if(doub > 0)
return 1;
else
return 0;
}
//***************************************************************************
// Function : printType
// Process : Print the hand type (straight flush, full house, etc)
// Notes : None
//***************************************************************************
void Hand::printType()
{
int type = this->getType();
if( type == 0)
{
cout << "(High Card)";
}
else if( type == 1)
{
cout << "(Pair)";
}
else if( type == 2)
{
cout << "(Two Pair)";
}
else if( type == 3)
{
cout << "(Three of a Kind)";
}
else if( type == 4)
{
cout << "(Straight)";
}
else if( type == 5)
{
cout << "(Flush)";
}
else if( type == 6)
{
cout << "(Full House)";
}
else if( type == 7)
{
cout << "(Four of a Kind)";
}
else if( type == 8)
{
cout << "(Straight Flush)";
}
else
{
cout << "(Invalid Type)";
}
}