-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
312 lines (209 loc) · 8.6 KB
/
main.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
/*
Write a program that reads HAND_SIZE cards from the user, then analyzes the cards
and prints out the type of poker hand that they represent. (HAND_SIZE will be a
global constant, typically 5, but the program must still work if it is NOT 5.)
To simplify the program we will ignore card suits, and face cards. The values that
the user inputs will be integer values from LOWEST_NUM to HIGHEST_NUM.
*/
#include <iostream>
using namespace std;
//required
const int HAND_SIZE = 8;
const int LOWEST_NUM = 1; //1; //1;
const int HIGHEST_NUM = 13; //13; //13;
//required functions and parameters
bool containsPair(const int hand[]); //3 lines
bool containsTwoPair(const int hand[]);//3 lines
bool containsThreeOfaKind(const int hand[]);//3 lines
bool containsStraight(const int hand[]);//3 lines
bool containsFullHouse(const int hand[]);//3 lines
bool containsFourOfaKind(const int hand[]);//3 lines
//helper functions for this long version HW by using 2 arrays.
//array hand stores orgional user input cards, in case for other purposes.
//array arrCounts stores counted cards for identifying the hand in this project
void inputCards(int hand[], int iHandSize);//5 lines
void countCardsbyType(const int hand[], int arrCounts[]);//2 lines
void countCardbyType(const int hand[], int arrCounts[], int iIndex);//5 lines
int findMatches(const int arrCounts[], const int iHandType);//5 lines
bool isStraight(const int hand[], const int arrCounts[]);//4 lines
int main()
{
int hand[HAND_SIZE];
inputCards(hand, HAND_SIZE);//call function to collect cards input
if (containsFourOfaKind(hand))
{
cout << "Four of a kind!" << endl;
}
else if (containsFullHouse(hand))
{
cout << "Full house!" << endl;
}
else if (containsStraight(hand))
{
cout << "Straight!" << endl;
}
else if (containsThreeOfaKind(hand))
{
cout << "Three of a kind" << endl;
}
else if (containsTwoPair(hand))
{
cout << "Two pair!" << endl;
}
else if (containsPair(hand))
{
cout << "Pair!" << endl;
}
else
{
cout << "High card!" << endl;
}
}
//This function asks user input cards as integer numbers. hand size, low, high
//numbers are all fixed. Then the cards are stored in array hand.
void inputCards(int hand[], int iHandSize)
{
cout << "Enter " << iHandSize << " numeric cards, no face cards. Use ";
cout << LOWEST_NUM << " - " << HIGHEST_NUM << "." << endl;
for (int i = 0; i < iHandSize; i++)
{
cout << "Card " << i + 1 << ": ";
cin >> hand[i];
}
}
//This function stores all cards' repeating times. eg, A hand of 5 5 5 8 8 is
//stored as arrCounts[5]=3 arrCounts[8]=2 arrCounts[2]=0....arrCounts[9]=0
//After this, arrCounts will be ready to be used for identifying the hand.
void countCardsbyType(const int hand[], int arrCounts[])
{
//the largest card number is HIGHEST_NUM, arrCounts's index ends at HIGHEST_NUM
for (int i = 0; i < HIGHEST_NUM + 1; i++)
{
countCardbyType(hand, arrCounts, i);//to save each card's repeating times
}
}
//This function saves the number of how many times of one card repeats
//eg, if hand is 5 5 5 8 8 and iIndex=5, it will set arrCounts[5] to 3
void countCardbyType(const int hand[], int arrCounts[], int iIndex)
{
for (int i = 0; i < HAND_SIZE; i++)
{
if (hand[i] == iIndex)
{
arrCounts[iIndex] = arrCounts[iIndex] + 1;//increment repeating card
if (arrCounts[iIndex] > 4)
{
//if a card repeats 5 or 6 or 7...times, it's still 4-of-a-kind
arrCounts[iIndex] = 4;
}
}
}
}
//the numbers of repeated cards are stored in arrCounts. eg, a hand of 5 5 5 8 8
//is stored as arrCounts[5]=3 arrCounts[8]=2 arrCounts[2]=0 arrCounts[9]=0
//...
//this function, if passing iHandType=2, it will find how many pairs are there
//if passing iHandType=3, it will find how many 3-of-a-kind are there...so on
int findMatches(const int arrCounts[], int iHandType)
{
int count = 0;
for (int i = 0; i < HIGHEST_NUM + 1; i++)
{
if (arrCounts[i] == iHandType)
{
count = count + 1;
}
}
return count;
}
//the numbers of repeated cards are stored in arrCounts. eg, a hand of 5 9 7 8 6
//is stored as arrCounts[5]=1 arrCounts[6]=1 ... arrCounts[9]=1
//
//this function is to check if 5 cards are consecutive. if so, return true
bool isStraight(const int hand[], const int arrCounts[])
{
//To avoid out of bound, loop from 0 to HIGHEST_NUM + 1 - HAND_SIZE.
for (int i = 0; i <= HIGHEST_NUM + 1 - HAND_SIZE; i++)
{
//look for five consecutive numbers in hand
if (arrCounts[i] > 0 && arrCounts[i + 1] > 0 && arrCounts[i + 2] > 0
&& arrCounts[i + 3] > 0 && arrCounts[i + 4] > 0)
{
return true;
}
}
return false;
}
//the numbers of repeated cards are stored in arrCounts. eg, a hand of 5 5 9 8 6
//is stored as arrCounts[5]=2 arrCounts[6]=1...arrCounts[9]=1 arrCounts[2]=0
//then call findMatches
//post: returns true if and only if there are one or more pairs in the hand. Note that
//this function returns false if there are more than two of the same card(and no other pairs).
bool containsPair(const int hand[])
{
//the largest card number is HIGHEST_NUM, arrCounts's index ends at HIGHEST_NUM
int arrCounts[HIGHEST_NUM + 1] = { 0 };
countCardsbyType(hand, arrCounts);//call function to count
//pairs, pass value 2 to check. If there is at least 1 pair, return true
return ( findMatches(arrCounts, 2) > 0 );
}
//the numbers of repeated cards are stored in arrCounts. eg, a hand of 5 5 8 8 6
//is stored as arrCounts[5]=2 arrCounts[6]=1 arrCounts[8]=2 arrCounts[2]=0...
//then call findMatches
//post: returns true if and only if there are two or more pairs in the hand.
bool containsTwoPair(const int hand[])
{
//the largest card number is HIGHEST_NUM, arrCounts's index ends at HIGHEST_NUM
int arrCounts[HIGHEST_NUM + 1] = { 0 };
countCardsbyType(hand, arrCounts);//call function to count
//pairs, pass value 2 to check. If there are at least 2 pairs, return true
return ( findMatches(arrCounts, 2) > 1 );
}
//the numbers of repeated cards are stored in arrCounts. eg, a hand of 5 8 8 8 6
//is stored as arrCounts[5]=1 arrCounts[6]=1 arrCounts[8]=3 arrCounts[2]=0...
//
//post: returns true if and only if there are one or more three-of-a-kind's in the hand.
bool containsThreeOfaKind(const int hand[])
{
//the largest card number is HIGHEST_NUM, arrCounts's index ends at HIGHEST_NUM
int arrCounts[HIGHEST_NUM + 1] = { 0 };
countCardsbyType(hand, arrCounts);//call function to count
//3-of-a-kind, pass value 3 to check. If there is at least one, return true
return ( findMatches(arrCounts, 3) > 0 );
}
//the numbers of repeated cards are stored in arrCounts. eg, a hand of 8 8 8 8 6
//is stored as arrCounts[6]=1 arrCounts[8]=4 arrCounts[2]=0...arrCounts[9]=0
//then call findMatches
//post: returns true if and only if there are one or more 4-of-a-kind's in the hand.
bool containsFourOfaKind(const int hand[])
{
//the largest card number is HIGHEST_NUM, arrCounts's index ends at HIGHEST_NUM
int arrCounts[HIGHEST_NUM + 1] = { 0 };
countCardsbyType(hand, arrCounts);//call function to count
//4-of-a-kind, pass value 4 to check. If there is at least one, return true
return ( findMatches(arrCounts, 4) > 0 );
}
//the numbers of repeated cards are stored in arrCounts. eg, a hand of 5 9 7 8 6
//is stored as arrCounts[5]=1 arrCounts[6]=1...arrCounts[9]=1 arrCounts[2]=0...
//then call findMatches
//post: returns true if there are 5 consecutive cards in the hand.
bool containsStraight(const int hand[])
{
//the largest card number is HIGHEST_NUM, arrCounts's index ends at HIGHEST_NUM
int arrCounts[HIGHEST_NUM + 1] = { 0 };
countCardsbyType(hand, arrCounts);//call function to count
return ( isStraight(hand, arrCounts) );
}
//the numbers of repeated cards are stored in arrCounts. eg, hand 5 5 8 8 8
//is stored as arrCounts[5]=2 arrCounts[8]=3 ... arrCounts[9]=0
//then call findMatches
//post:returns true if there is are one or more pairs and one or more 3-of-a-kind's in the hand.
bool containsFullHouse(const int hand[])
{
//the largest card number is HIGHEST_NUM, arrCounts's index ends at HIGHEST_NUM
int arrCounts[HIGHEST_NUM + 1] = { 0 };
countCardsbyType(hand, arrCounts);//call function to count
//pair, pass value 2 to check; 3-Of-a-Kind, pass value 3 to check.
//If it has at least 1 pair and at least one 3-of-a-kind, it's a full house.
return ( findMatches(arrCounts, 2) > 0) && (findMatches(arrCounts, 3) > 0 );
}