forked from Diusrex/UVA-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10315 Poker Hands.cpp
256 lines (196 loc) · 7.98 KB
/
10315 Poker Hands.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
#include <cstdio>
#include <map>
bool BetterThanOther(int *firstCount, int *secondCount, bool& secondWon)
{
for (int i = 12; i >= 0; --i)
{
if (firstCount[i] && !secondCount[i])
return true;
else if (!firstCount[i] && secondCount[i])
{
secondWon = true;
return false;
}
}
return false;
}
using namespace std;
int main()
{
map<char, int> cardConversions;
cardConversions['2'] = 0; cardConversions['3'] = 1; cardConversions['4'] = 2;
cardConversions['5'] = 3; cardConversions['6'] = 4; cardConversions['7'] = 5;
cardConversions['8'] = 6; cardConversions['9'] = 7; cardConversions['T'] = 8;
cardConversions['J'] = 9; cardConversions['Q'] = 10; cardConversions['K'] = 11;
cardConversions['A'] = 12;
int count[2][13], suitPos, suitPosStart, cardPos, checking, start;
// [p][0 = most, 1 = second]
// Ordered first by number, then by value
int topTwoMostCommon[2][2];
bool straight[2], flush[2];
char line[34], suit;
while (gets(line))
{
flush[0] = flush[1] = true;
straight[0] = straight[1] = true;
suitPos = 1;
cardPos = 0;
topTwoMostCommon[0][0] = topTwoMostCommon[0][1] = topTwoMostCommon[1][0] = topTwoMostCommon[1][1] = -1;
for (int pos(0); pos < 13; ++pos)
count[0][pos] = count[1][pos] = 0;
for (int p = 0; p < 2; ++p)
{
suitPosStart = suitPos;
suit = line[suitPos];
suitPos += 3;
for (; suitPos <= suitPosStart + 12; suitPos += 3)
if (line[suitPos] != suit)
flush[p] = false;
for (; cardPos < suitPosStart + 12; cardPos += 3)
{
++count[p][cardConversions[line[cardPos]]];
}
start = -1;
for (checking = 12; checking >= 0; --checking)
{
// Fails if count > 1
if (count[p][checking] > 1)
straight[p] = false;
if (count[p][checking] == 1 && start == -1)
start = checking;
else if (count[p][checking] == 0 && (start != -1 && start - checking <= 4))
straight[p] = false;
if (topTwoMostCommon[p][0] == -1 || count[p][topTwoMostCommon[p][0]] < count[p][checking])
{
topTwoMostCommon[p][1] = topTwoMostCommon[p][0];
topTwoMostCommon[p][0] = checking;
}
else if (topTwoMostCommon[p][1] == -1 || count[p][topTwoMostCommon[p][1]] < count[p][checking])
topTwoMostCommon[p][1] = checking;
}
}
int firstMostCommon(topTwoMostCommon[0][0]), firstSecondCommon(topTwoMostCommon[0][1]), secondMostCommon(topTwoMostCommon[1][0]), secondSecondCommon(topTwoMostCommon[1][1]);
bool firstWin(false), secondWin(false);
if (flush[0] && straight[0])
{
if (flush[1] && straight[1])
{
if (firstMostCommon > secondMostCommon)
firstWin = true;
else if (firstMostCommon < secondMostCommon)
secondWin = true;
}
else
firstWin = true;
}
else if (flush[1] && straight[1])
secondWin = true;
else if (count[0][firstMostCommon] == 4)
{
if (count[1][secondMostCommon] == 4)
{
if (firstMostCommon > secondMostCommon)
firstWin = true;
else if (firstMostCommon < secondMostCommon)
secondWin = true;
}
else
firstWin = true;
}
else if (count[1][secondMostCommon] == 4)
secondWin = true;
else if (count[0][firstMostCommon] == 3 && count[0][firstSecondCommon] == 2)
{
if (count[1][secondMostCommon] == 3 && count[1][secondSecondCommon] == 2)
{
if (firstMostCommon > topTwoMostCommon[1][0] || (firstMostCommon == secondMostCommon && firstSecondCommon > secondSecondCommon))
firstWin = true;
else if (firstMostCommon < topTwoMostCommon[1][0] || (firstMostCommon == secondMostCommon && firstSecondCommon < secondSecondCommon))
secondWin = true;
}
else
firstWin = true;
}
else if (count[1][secondMostCommon] && count[1][secondSecondCommon] == 2)
secondWin = true;
else if (flush[0])
{
if (flush[1])
{
firstWin = BetterThanOther(count[0], count[1], secondWin);
}
else
firstWin = true;
}
else if (flush[1])
secondWin = true;
else if (straight[0])
{
if (straight[1])
{
if (firstMostCommon > secondMostCommon)
firstWin = true;
else if (firstMostCommon < secondMostCommon)
secondWin = true;
}
else
firstWin = true;
}
else if (straight[1])
secondWin = true;
else if (count[0][firstMostCommon] == 3)
{
if (count[1][secondMostCommon] == 3)
{
if (firstMostCommon > secondMostCommon)
firstWin = true;
else if (firstMostCommon < secondMostCommon)
secondWin = true;
}
else
firstWin = true;
}
else if (count[1][secondMostCommon] == 3)
secondWin = true;
else if (count[0][firstMostCommon] == 2 && count[0][firstSecondCommon] == 2)
{
if (count[1][secondMostCommon] == 2 && count[1][secondSecondCommon] == 2)
{
if (firstMostCommon > topTwoMostCommon[1][0] || (firstMostCommon == secondMostCommon && firstSecondCommon > secondSecondCommon))
firstWin = true;
else if (firstMostCommon < topTwoMostCommon[1][0] || (firstMostCommon == secondMostCommon && firstSecondCommon < secondSecondCommon))
secondWin = true;
else
firstWin = BetterThanOther(count[0], count[1], secondWin);
}
else
firstWin = true;
}
else if (count[1][secondMostCommon] == 2 && count[1][secondSecondCommon] == 2)
secondWin = true;
else if (count[0][firstMostCommon] == 2)
{
if (count[1][secondMostCommon] == 2)
{
if (firstMostCommon > topTwoMostCommon[1][0])
firstWin = true;
else if (firstMostCommon < topTwoMostCommon[1][0])
secondWin = true;
else
firstWin = BetterThanOther(count[0], count[1], secondWin);
}
else
firstWin = true;
}
else if (count[1][secondMostCommon] == 2)
secondWin = true;
else
firstWin = BetterThanOther(count[0], count[1], secondWin);
if (firstWin)
printf("Black wins.\n");
else if (secondWin)
printf("White wins.\n");
else
printf("Tie.\n");
}
}