-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rank.cpp
344 lines (285 loc) · 10.4 KB
/
Rank.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
341
342
343
344
#include "prospect.h"
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define FACES 13
#define KCARDS 5
#define MAXPLAYERS 10
bool _CompareFaces(Cards lhs, Cards rhs)
{
return lhs.Faces > rhs.Faces;
}
char _ConvertChar(int Value)
{
if (Value >= 0 && Value <= 9)
return (char)Value + 48;
else if (Value == 10)
return 'A'; //A dai dien cho J, mang ma 65
else if (Value == 11)
return 'B'; //B dai dien cho Q
else if (Value == 12)
return 'C'; //C dai dien cho K
else //co the Value == 13
return 'D'; //D dai dien cho Ace
}
//Prototype de tai su dung ham
void RankListThreeOfAKind(Rank RankList[], Cards EachPlayer[][KCARDS], std::vector<int> IndexThreeOfAKind, int &IndexStart, int NoPlayer);
void RankListNormal(Rank RankList[], Cards EachPlayer[][KCARDS], std::vector<int> IndexNormal, int &IndexStart, int NoPlayer);
//Ket thuc prototype
//Xep hang nhung hand la cai sanh va cung chat
void RankListStraightFlush(Rank RankList[], Cards EachPlayer[][KCARDS], std::vector<int> IndexStraightFlush, int &IndexStart, int NoPlayer)
{
std::vector<std::string> StringHand;
for (int i = 0; i < IndexStraightFlush.size(); i++)
{
std::string CurrentStringHand = "";
CurrentStringHand += _ConvertChar(EachPlayer[IndexStraightFlush.at(i)][0].Faces);
CurrentStringHand += _ConvertChar(IndexStraightFlush.at(i));
StringHand.push_back(CurrentStringHand);
}
std::sort(StringHand.begin(), StringHand.end());
int Count = 0;
while (Count < IndexStraightFlush.size())
{
RankList[StringHand[Count].at(1) - 48].Ranks = IndexStart;
RankList[StringHand[Count].at(1) - 48].Scores = NoPlayer - IndexStart - 1;
IndexStart++;
Count++;
}
}
//Xep hang nhung hand la tu qui
void RankListFourOfAKind(Rank RankList[], Cards EachPlayer[][KCARDS], std::vector<int> IndexFourOfAKind, int &IndexStart, int NoPlayer)
{
int CountFace[MAXPLAYERS][FACES + 1] = { 0 };//FACES + 1 vi luu them so 13 <=> Ace
for (int i = 0; i < IndexFourOfAKind.size(); i++)
for (int j = 0; j < KCARDS; j++)
CountFace[i][EachPlayer[IndexFourOfAKind.at(i)][j].Faces]++;
std::vector<std::string> StringThree;
for (int i = 0; i < IndexFourOfAKind.size(); i++)
{
std::string CurrentStringHand = "";
for (int j = FACES - 1 + 1; j >= 0; j--)//FACES + 1 vi luu them so 13 <=> Ace
if (CountFace[i][j] == 4)
{
CurrentStringHand += _ConvertChar(j);
break;
}
CurrentStringHand += _ConvertChar(IndexFourOfAKind.at(i));
StringThree.push_back(CurrentStringHand);
}
std::sort(StringThree.rbegin(), StringThree.rend());
int Count = 0;
while (Count < IndexFourOfAKind.size())
{
RankList[StringThree[Count].at(1) - 48].Ranks = IndexStart;
RankList[StringThree[Count].at(1) - 48].Scores = NoPlayer - IndexStart - 1;
IndexStart++;
Count++;
}
}
//Xep hang nhung hand gom mot sam co va mot doi
void RankListFullHouse(Rank RankList[], Cards EachPlayer[][KCARDS], std::vector<int> IndexFullHouse, int &IndexStart, int NoPlayer)
{
RankListThreeOfAKind(RankList, EachPlayer, IndexFullHouse, IndexStart, NoPlayer);
}
//Xep hang nhung hand co cung chat
void RankListFlush(Rank RankList[], Cards EachPlayer[][KCARDS], std::vector<int> IndexFlush, int &IndexStart, int NoPlayer)
{
RankListNormal(RankList, EachPlayer, IndexFlush, IndexStart, NoPlayer);
}
//Xep hang nhung hand la nhung cai sanh
void RankListStraight(Rank RankList[], Cards EachPlayer[][KCARDS], std::vector<int> IndexStraight, int &IndexStart, int NoPlayer)
{
std::vector<std::string> StringMaxCard;
for (int i = 0; i < IndexStraight.size(); i++)
{
std::string CurrentStringHand = "";
CurrentStringHand += _ConvertChar(EachPlayer[IndexStraight.at(i)][0].Faces);
CurrentStringHand += _ConvertChar(IndexStraight.at(i));
StringMaxCard.push_back(CurrentStringHand);
}
std::sort(StringMaxCard.rbegin(), StringMaxCard.rend());
int Count = 0;
while (Count < IndexStraight.size())
{
RankList[StringMaxCard[Count].at(1) - 48].Ranks = IndexStart;
RankList[StringMaxCard[Count].at(1) - 48].Scores = NoPlayer - IndexStart - 1;
IndexStart++;
Count++;
}
}
//Xep hang nhung hand chi la sam co
void RankListThreeOfAKind(Rank RankList[], Cards EachPlayer[][KCARDS], std::vector<int> IndexThreeOfAKind, int &IndexStart, int NoPlayer)
{
int CountFace[MAXPLAYERS][FACES + 1] = { 0 };//FACES + 1 vi luu them so 13 <=> Ace
for (int i = 0; i < IndexThreeOfAKind.size(); i++)
for (int j = 0; j < KCARDS; j++)
CountFace[i][EachPlayer[IndexThreeOfAKind.at(i)][j].Faces]++;
std::vector<std::string> StringThree;
for (int i = 0; i < IndexThreeOfAKind.size(); i++)
{
std::string CurrentStringHand = "";
for (int j = FACES - 1 + 1; j >= 0; j--)//FACES + 1 vi luu them so 13 <=> Ace
if (CountFace[i][j] == 3)
{
CurrentStringHand += _ConvertChar(j);
break;
}
CurrentStringHand += _ConvertChar(IndexThreeOfAKind.at(i));
StringThree.push_back(CurrentStringHand);
}
std::sort(StringThree.rbegin(), StringThree.rend());
int Count = 0;
while (Count < IndexThreeOfAKind.size())
{
RankList[StringThree[Count].at(1) - 48].Ranks = IndexStart;
RankList[StringThree[Count].at(1) - 48].Scores = NoPlayer - IndexStart - 1;
IndexStart++;
Count++;
}
}
//Xep hang nhung hand gom hai doi
void RankListTwoPairs(Rank RankList[], Cards EachPlayer[][KCARDS], std::vector<int> IndexTwoPairs, int &IndexStart, int NoPlayer)
{
int CountFace[MAXPLAYERS][FACES + 1] = { 0 };
for (int i = 0; i < IndexTwoPairs.size(); i++)
for (int j = 0; j < KCARDS; j++)
CountFace[i][EachPlayer[IndexTwoPairs.at(i)][j].Faces]++;
std::vector<std::string> StringHand;
for (int i = 0; i < IndexTwoPairs.size(); i++)
{
std::string CurrentStringHand = "";
for (int j = FACES - 1 + 1; j >= 0; j--)
if (CountFace[i][j] == 2)
CurrentStringHand += _ConvertChar(j);
for (int j = 0; j < FACES + 1; j++)
if (CountFace[i][j] == 1)
{
CurrentStringHand += _ConvertChar(j);
CurrentStringHand += _ConvertChar(IndexTwoPairs.at(i));
break;
}
StringHand.push_back(CurrentStringHand);
}
std::sort(StringHand.rbegin(), StringHand.rend());
int Count = 0;
while (Count < IndexTwoPairs.size())
{
RankList[StringHand[Count].at(KCARDS - 2) - 48].Ranks = IndexStart;
RankList[StringHand[Count].at(KCARDS - 2) - 48].Scores = NoPlayer - IndexStart - 1;
IndexStart++;
Count++;
}
}
//Xep hang nhung hand chi mot doi
void RankListPair(Rank RankList[], Cards EachPlayer[][KCARDS], std::vector<int> IndexPair, int &IndexStart, int NoPlayer)
{
int CountFace[MAXPLAYERS][FACES + 1] = { 0 };//FACES + 1 vi luu them so 13 <=> Ace
for (int i = 0; i < IndexPair.size(); i++)
for (int j = 0; j < KCARDS; j++)
CountFace[i][EachPlayer[IndexPair.at(i)][j].Faces]++;
std::vector<std::string> StringHand;
for (int i = 0; i < IndexPair.size(); i++)
{
std::string CurrentStringHand = "";
for (int j = 0; j < FACES + 1; j++)//FACES + 1 vi luu them so 13 <=> Ace
if (CountFace[i][j] == 2)
{
CurrentStringHand += _ConvertChar(j);
break;
}
for (int j = FACES - 1 + 1; j >= 0; j--)//FACES + 1 vi luu them so 13 <=> Ace
if (CountFace[i][j] == 1)
CurrentStringHand += _ConvertChar(j);
CurrentStringHand += _ConvertChar(IndexPair.at(i));
StringHand.push_back(CurrentStringHand);
}
std::sort(StringHand.rbegin(), StringHand.rend());
int Count = 0;
while (Count < IndexPair.size())
{
RankList[StringHand[Count].at(KCARDS - 1) - 48].Ranks = IndexStart;
RankList[StringHand[Count].at(KCARDS - 1) - 48].Scores = NoPlayer - IndexStart - 1;
IndexStart++;
Count++;
}
}
//Xep hang nhung hand la dan den
void RankListNormal(Rank RankList[], Cards EachPlayer[][KCARDS], std::vector<int> IndexNormal, int &IndexStart, int NoPlayer)
{
std::vector<std::string> StringHand;
for (int i = 0; i < IndexNormal.size(); i++)
{
std::string CurrentStringHand = "";
for (int j = 0; j < KCARDS; j++)
CurrentStringHand += _ConvertChar(EachPlayer[i][j].Faces);
CurrentStringHand += _ConvertChar(IndexNormal.at(i));
StringHand.push_back(CurrentStringHand);
}
std::sort(StringHand.rbegin(), StringHand.rend());
int Count = 0;
while (Count < IndexNormal.size())
{
RankList[StringHand[Count].at(KCARDS) - 48].Ranks = IndexStart;
RankList[StringHand[Count].at(KCARDS) - 48].Scores = NoPlayer - IndexStart - 1;
IndexStart++;
Count++;
}
}
void StatisticStatus(Cards EachPlayer[][KCARDS], Rank RankList[], int NoPlayer)
{
std::vector<int> IndexStraightFlush;
std::vector<int> IndexFourOfAKind;
std::vector<int> IndexFullHouse;
std::vector<int> IndexFlush;
std::vector<int> IndexStraight;
std::vector<int> IndexThreeOfAKind;
std::vector<int> IndexTwoPairs;
std::vector<int> IndexPair;
std::vector<int> IndexNormal;
for (int i = 0; i < NoPlayer; i++)
if (IsStraightFlush(EachPlayer[i]))
IndexStraightFlush.push_back(i);
else if (IsFourOfAKind(EachPlayer[i]))
IndexFourOfAKind.push_back(i);
else if (IsFullHouse(EachPlayer[i]))
IndexFullHouse.push_back(i);
else if (IsFlush(EachPlayer[i]))
IndexFlush.push_back(i);
else if (IsStraight(EachPlayer[i]))
IndexStraight.push_back(i);
else if (IsThreeOfAKind(EachPlayer[i]))
IndexThreeOfAKind.push_back(i);
else if (IsTwoPairs(EachPlayer[i]))
IndexTwoPairs.push_back(i);
else if (IsPair(EachPlayer[i]))
IndexPair.push_back(i);
else
IndexNormal.push_back(i);
for (int i = 0; i < NoPlayer; i++)
{
for (int j = 0; j < KCARDS; j++)
if (EachPlayer[i][j].Faces == 0)
EachPlayer[i][j].Faces = 13;
std::sort(EachPlayer[i], EachPlayer[i] + KCARDS, _CompareFaces);
}
int IndexStart = 0;
RankListStraightFlush(RankList, EachPlayer, IndexStraightFlush, IndexStart, NoPlayer);
RankListFourOfAKind(RankList, EachPlayer, IndexFourOfAKind, IndexStart, NoPlayer);
RankListFullHouse(RankList, EachPlayer, IndexFullHouse, IndexStart, NoPlayer);
RankListFlush(RankList, EachPlayer, IndexFlush, IndexStart, NoPlayer);
RankListStraight(RankList, EachPlayer, IndexStraight, IndexStart, NoPlayer);
RankListThreeOfAKind(RankList, EachPlayer, IndexThreeOfAKind, IndexStart, NoPlayer);
RankListTwoPairs(RankList, EachPlayer, IndexTwoPairs, IndexStart, NoPlayer);
RankListPair(RankList, EachPlayer, IndexPair, IndexStart, NoPlayer);
RankListNormal(RankList, EachPlayer, IndexNormal, IndexStart, NoPlayer);
}
void ShowRankList(Cards EachPlayer[][KCARDS], Rank RankList[], int NoPlayer)
{
StatisticStatus(EachPlayer, RankList, NoPlayer);
std::cout << "Player\tScores\tRank" << std::endl;
for (int i = 0; i < NoPlayer; i++)
std::cout << i + 1 << "\t" << RankList[i].Scores << "\t" << RankList[i].Ranks + 1 << std::endl;
std::cout << std::endl;
}