-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokerGame.cpp
More file actions
342 lines (285 loc) · 7.66 KB
/
PokerGame.cpp
File metadata and controls
342 lines (285 loc) · 7.66 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
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
#include <bits/stdc++.h>
using namespace std;
map<string, int> CharToNumber;
vector<string> Map {"Bich", "Chuon", "Ro", "Co"};
vector<string> Poker {"High Card", "One Pair", "Two Pair", "Three of a kind", "Straight", "Flush", "Full House", "Four of a kind", "Straight Flush", "Royal Flush"};
void CharMap() {
for(int i = 0; i < Map.size(); i++) {
CharToNumber[Map[i]] = i + 1;
}
// for(int i = 0; i < Map.size(); i++) {
// cout << Map[i];
// cout << CharToNumber[Map[i]];
// cout << "\n ================ \n";
// }
}
class Card {
private:
string ch;
int number;
public:
Card(string cha, int num) {
ch = cha;
number = num;
}
Card() {
ch = "Bich";
number = 3;
}
void Input() {
while(1) {
string tmp;
cout << "Enter the value from ( Bich, Chuon, Ro, Co ) \n";
cin >> tmp;
if(tmp == "Bich" || tmp == "Chuon" || tmp == "Ro" || tmp == "Co") {
ch = tmp;
break;
}
else {
cout << "Wrong value. Please try again";
}
}
while(1) {
int x;
cout << "Enter the value from 1 to 13 ( 1: A, 11: J, 12: Q, 13: K ) \n";
cin >> x;
if(x >= 1 && x <= 13) {
number = x;
return;
}
else {
cout << "Wrong value. Please try again";
}
}
}
void Output() {
string cha = "";
if(number == 11) cha = "J";
else if(number == 12) cha = "Q";
else if(number == 13) cha = "K";
cout << ((number <= 10) ? to_string(number) : cha) << " " << ch << " ";
}
int TrongSo() const {
return number == 1 ? 14 : (number == 2 ? 15 : number);
}
bool SmallerCard(Card &a) {
if(number != a.number)
return number < a.number;
return CharToNumber[ch] < CharToNumber[a.ch];
}
static bool SortSmaller(Card &a, Card &b) {
return a.TrongSo() < b.TrongSo();
}
int getNumber() {
return number;
}
string getChar() {
return ch;
}
};
class Hand {
private:
Card cards[5];
public:
int Pair() {
int result = 1;
map<int, int> Map;
for(int i = 0; i < 5; i++) {
Map[cards[i].getNumber()]++;
}
for(int i = 0; i < 5; i++) {
if(Map[cards[i].getNumber()] == 2) {
result++;
Map[cards[i].getNumber()] = 0;
} else if(Map[cards[i].getNumber()] == 3) {
result = 4;
Map[cards[i].getNumber()] = 0;
} else if(Map[cards[i].getNumber()] == 4) {
result = 8;
Map[cards[i].getNumber()] = 0;
} else if((Map[cards[i].getNumber()] == 3 && result == 2) ||
(Map[cards[i].getNumber()] == 2 && result == 4)) {
result = 7;
Map[cards[i].getNumber()] = 0;
}
}
return result;
}
bool Respectively() {
for(int i = 0; i < 4; i++) {
if(cards[i].getNumber() + 1 != cards[i+1].getNumber()) return 0;
}
return 1;
}
bool DiffChar() {
int count = 0;
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5; j++) {
if(cards[i].getChar() == cards[j].getChar()) count++;
}
if(count < 5) count = 0;
else return 0;
}
return count < 5;
}
int Straight() {
if(Respectively() && DiffChar()) return 5;
return 0;
}
int Flush() {
if(!Respectively() && !DiffChar()) return 6;
return 0;
}
int StraightFlush() {
if(Respectively() && !DiffChar()) return 9;
return 0;
}
void setCard() {
for(int i = 0; i < 5; i++) {
cards[i].Input();
}
}
void SortCardFromHand() {
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 5 - i - 1; j++) {
if(!cards[j].SmallerCard(cards[j+1])) {
swap(cards[j], cards[j+1]);
}
}
}
// cout << "After sort:\n";
// for(int i = 0; i < 5; i++) {
// cout << cards[i].getChar() << " " << cards[i].getNumber()
// << " (strength: " << cards[i].TrongSo() << ")\n";
// }
}
void OutPutFromHand() {
for(int i = 0; i < 5; i++) {
cards[i].Output();
}
}
int DetectHand() {
int pair = Pair();
if(pair > 1) return pair;
int straight = Straight();
if(straight) return straight;
int flush = Flush();
if(flush) return flush;
int straightFlush = StraightFlush();
if(straightFlush) return straightFlush;
return 1; // High card
}
};
void ThucHanhB() {
CharMap();
int n;
cout << "Enter the number of card: "; cin >> n;
Card *c = new Card[n];
for(int i = 0; i < n; i++) {
cout << "Enter the " << i + 1 << " card \n";
c[i].Input();
}
Card BiggestCard;
for(int i = 0; i < n - 1; i++) {
cout << "Loop " << i << endl;
c[i].Output();
c[i + 1].Output();
if(c[i].SmallerCard(c[i+1])) {
BiggestCard = c[i+1];
}
cout << "Biggest card is: ";
BiggestCard.Output();
cout << "========= \n";
}
cout << "The card having biggest value is: ";
BiggestCard.Output();
}
/*
Bich 3
Chuon 10
Co 2
*/
void ThucHanhC() {
int n = 5;
Hand player1;
Hand player2;
cout << "Input for " << 1 << " player \n";
player1.setCard();
cout << "Input for " << 2 << " player \n";
player2.setCard();
cout << "\n====================================\n\n";
player1.SortCardFromHand();
cout << "Player 1 after sort: \n";
player1.OutPutFromHand(); cout << endl;
player2.SortCardFromHand();
cout << "Player 2 after sort: \n";
player2.OutPutFromHand();
cout << "\n\n============== RESULT ==============\n\n";
if(player1.DetectHand() > player2.DetectHand()) {
cout << "Player 1 Won - " << Poker[player1.DetectHand() - 1] << endl;
cout << "Player 2 Lost - " << Poker[player2.DetectHand() - 1] << endl;
} else {
cout << "Player 1 Lost - " << Poker[player1.DetectHand() - 1] << endl;
cout << "Player 2 Won - " << Poker[player2.DetectHand() - 1] << endl;
}
}
/*
High card
Bich 1
Chuon 3
Co 2
Ro 6
Ro 9
One pair
Bich 1
Co 1
Chuon 5
Ro 9
Bich 10
Two pair
Bich 6
Co 6
Ro 7
Bich 7
Co 10
Three of a kind
Bich 7
Co 2
Chuon 2
Bich 2
Chuon 9
Straight
Bich 13
Chuon 12
Co 11
Chuon 10
Co 9
Flush
Bich 6
Bich 1
Bich 9
Bich 7
Bich 10
Full House
Ro 3
Co 3
Chuon 3
Bich 7
Ro 7
Four of a kind
Bich 8
Co 8
Ro 8
Chuon 8
Co 5
Straight Flush
Co 5
Co 6
Co 7
Co 8
Co 9
*/
int main() {
// ThucHanhB();
ThucHanhC();
}