-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
326 lines (277 loc) · 7.16 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <string>
#include <vector>
#include <array>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cassert>
using std::string;
using std::vector;
enum class Rank { None, HighCard, OnePair, TwoPairs, Threes, Straight, Flush, FullHouse, Fours, StraightFlush, RoyalFlush };
enum class Suit { None, Clubs, Diamonds, Hearts, Spades };
std::vector<string> rankText = { "None", "HighCard", "OnePair", "TwoPairs", "Threes", "Straight", "Flush", "FullHouse", "Fours", "StraightFlush", "RoyalFlush" };
struct Card
{
std::array<char, 2> code;
int value() const
{
switch (code[0])
{
case '2':
return 2;
case '3':
return 3;
case '4':
return 4;
case '5':
return 5;
case '6':
return 6;
case '7':
return 7;
case '8':
return 8;
case '9':
return 9;
case 'T':
return 10;
case 'J':
return 11;
case 'Q':
return 12;
case 'K':
return 13;
case 'A':
return 14;
default:
return 0;
}
}
Suit suit() const
{
switch (code[1])
{
case 'C':
return Suit::Clubs;
case 'D':
return Suit::Diamonds;
case 'H':
return Suit::Hearts;
case 'S':
return Suit::Spades;
default:
return Suit::None;
}
}
};
struct Hand
{
std::array<Card, 5> cards;
Rank rank = Rank::None;
vector<int> scoringValues; // the indexes into `cards` that contribute to the rank in decreasing value
vector<int> remainingValues; // the indexes in decreasing value that are remaining, i.e. the kickers.
void orderByValue()
{
std::sort(cards.begin(), cards.end(), std::greater<Card>());
}
// Does the hard work of determing the rank of a hand, the scoring cards of the rank, and the cards not contributing to the rank
void evaluate()
{
bool flush =
cards[0].suit() == cards[1].suit() &&
cards[1].suit() == cards[2].suit() &&
cards[2].suit() == cards[3].suit() &&
cards[3].suit() == cards[4].suit();
bool straight =
cards[0].value() == cards[1].value() + 1 &&
cards[1].value() == cards[2].value() + 1 &&
cards[2].value() == cards[3].value() + 1 &&
cards[3].value() == cards[4].value() + 1;
bool royal = straight &&
cards[0].value() == 14;
if (royal && flush)
{
rank = Rank::RoyalFlush;
return;
}
else if (straight && flush)
{
rank = Rank::StraightFlush;
return;
}
Rank best = flush ? Rank::Flush : (straight ? Rank::Straight : Rank::HighCard);
scoringValues = { cards[0].value(), cards[1].value(), cards[2].value(), cards[3].value(), cards[4].value() };
// for every permutation keep track of highest rank
vector<size_t> perms(cards.size());
std::iota(perms.begin(), perms.end(), 0);
do
{
auto value0 = cards[perms[0]].value();
auto value1 = cards[perms[1]].value();
auto value2 = cards[perms[2]].value();
auto value3 = cards[perms[3]].value();
auto value4 = cards[perms[4]].value();
bool threes =
value0 == value1 &&
value1 == value2;
bool fours =
threes &&
value2 == value3;
bool fullhouse =
threes &&
value3 == value4;
bool onepair =
value0 == value1;
bool twopair =
onepair &&
value2 == value3;
if (fours && Rank::Fours > best)
{
best = Rank::Fours;
scoringValues = { value0 };
remainingValues = { value4 };
}
else if (fullhouse && Rank::FullHouse > best)
{
best = Rank::FullHouse;
scoringValues = { value0, value3 };
}
else if (threes && Rank::Threes > best)
{
best = Rank::Threes;
scoringValues = { value0 };
remainingValues = { value3, value4 };
}
else if (twopair && Rank::TwoPairs > best)
{
best = Rank::TwoPairs;
scoringValues = { value0, value2 };
remainingValues = { value4 };
}
else if (onepair && Rank::OnePair > best)
{
best = Rank::OnePair;
scoringValues = { value0 };
remainingValues = { value2, value3, value4 };
}
}
while (std::next_permutation(perms.begin(), perms.end()));
// set the final rank of the hand to be the best we've encounterd thus far
rank = best;
// sort the rank values in decreasing value, excepting FullHouse which needs the Threes compared before the Pair
if (rank != Rank::FullHouse)
std::sort(scoringValues.begin(), scoringValues.end(), std::greater<int>());
// sort the kickers in decreasing value
std::sort(remainingValues.begin(), remainingValues.end(), std::greater<int>());
}
};
// A generic function to compare the first differing items of one range with the item of the same index of another range.
template <typename Itr, typename P>
bool innerCompare(const Itr &first1, const Itr &last1, const Itr &first2, const Itr &last2, P predicate, bool &ok)
{
assert(std::distance(first1, last1) == std::distance(first2, last2));
if (std::distance(first1, last1) == std::distance(first2, last2))
{
Itr itr1, itr2;
std::tie(itr1, itr2) = std::mismatch(first1, last1, first2);
if (itr1 != last1 && itr2 != last2)
{
ok = true;
return predicate(*itr1, *itr2);
}
else
{
ok = false;
return false;
}
}
ok = false;
return false;
}
// Comparison operators
bool operator< (const Card &lhs, const Card &rhs)
{
return lhs.value() < rhs.value();
}
bool operator> (const Card &lhs, const Card &rhs)
{
return lhs.value() > rhs.value();
}
bool operator== (const Card &lhs, const Card &rhs)
{
return lhs.value() == rhs.value();
}
bool operator> (const Hand &lhs, const Hand &rhs)
{
if (lhs.rank > rhs.rank)
{
return true;
}
else if (lhs.rank == rhs.rank)
{
bool ok;
bool gt = innerCompare(lhs.scoringValues.cbegin(), lhs.scoringValues.cend(), rhs.scoringValues.cbegin(), rhs.scoringValues.cend(), std::greater<int>(), ok);
if (ok)
return gt;
else
return innerCompare(lhs.cards.cbegin(), lhs.cards.cend(), rhs.cards.cbegin(), rhs.cards.cend(), std::greater<Card>(), ok);
}
return false;
}
// Input stream operators
std::istream &operator>>(std::istream &in, Card &card)
{
in >> card.code[0] >> card.code[1];
return in;
}
std::istream &operator>>(std::istream &in, Hand &hand)
{
hand.rank = Rank::None;
hand.scoringValues.clear();
hand.remainingValues.clear();
if (in >> hand.cards[0] >> hand.cards[1] >> hand.cards[2] >> hand.cards[3] >> hand.cards[4])
{
hand.orderByValue();
hand.evaluate();
}
return in;
}
// Output stream operators
std::ostream &operator<<(std::ostream &out, const Card &card)
{
out << card.code[0] << card.code[1];
return out;
}
std::ostream &operator<<(std::ostream &out, const Hand &hand)
{
out << hand.cards[0] << " "
<< hand.cards[1] << " "
<< hand.cards[2] << " "
<< hand.cards[3] << " "
<< hand.cards[4] << " "
<< std::setw(15) << std::left << rankText.at(static_cast<size_t>(hand.rank));
return out;
}
// All that `main` now needs to do is iteratively construct two hands from the file stream and compare them with operator>.
int main(int argc, char *argv[])
{
if (argc > 1)
{
std::ifstream fin(argv[1]);
Hand hand1, hand2;
while (fin >> hand1 >> hand2)
{
//std::cout << hand1 << " | " << hand2 << "\n";
if (hand1 > hand2)
std::cout << "left\n";
else if (hand2 > hand1)
std::cout << "right\n";
else
std::cout << "none\n";
}
}
return 0;
}