-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathP131.cpp
201 lines (183 loc) · 4.44 KB
/
P131.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
#include <iostream>
#include <algorithm>
#define SUIT second
#define VAL first
typedef std::pair<int,char> Card;
std::ostream& operator<<(std::ostream& os, const Card &obj) {
os << obj.VAL << obj.SUIT;
return os;
}
int numberOfSetBits(unsigned int i) {
int res = 0;
while(i != 0) {
if((i & 1) == 1)
++res;
i>>=1;
}
return res;
}
void testOnePair(int &highest, Card *hand) {
if(highest >= 1)
return;
for(int i = 0; i < 4; ++i) {
if(hand[i].VAL == hand[i+1].VAL) {
highest = 1;
return;
}
}
}
void testFlush(int &highest, Card *hand) {
if(highest >= 5)
return;
if(hand[0].SUIT == hand[1].SUIT &&
hand[0].SUIT == hand[2].SUIT &&
hand[0].SUIT == hand[3].SUIT &&
hand[0].SUIT == hand[4].SUIT) {
highest = 5;
}
}
void testStraightFlush(int &highest, Card *hand) {
if(highest >= 8)
return;
if(hand[0].SUIT == hand[1].SUIT &&
hand[0].SUIT == hand[2].SUIT &&
hand[0].SUIT == hand[3].SUIT &&
hand[0].SUIT == hand[4].SUIT) {
if(0 == hand[0].VAL &&
9 == hand[1].VAL &&
10 == hand[2].VAL &&
11 == hand[3].VAL &&
12 == hand[4].VAL) {
highest = 8;
}
}
}
void testThreeOfAKind(int &highest, Card *hand) {
if(highest >= 3)
return;
for(int i = 0; i < 3; ++i) {
if(hand[i].VAL == hand[i+1].VAL && hand[i+1].VAL == hand[i+2].VAL) {
highest = 3;
return;
}
}
}
void testFourOfAKind(int &highest, Card *hand) {
if(highest >= 7)
return;
for(int i = 0; i < 2; ++i) {
if(hand[i].VAL == hand[i+1].VAL && hand[i].VAL == hand[i+2].VAL && hand[i].VAL == hand[i+3].VAL) {
highest = 7;
return;
}
}
}
void testFullHouse(int &highest, Card *hand) {
if(highest >= 6)
return;
if(hand[0].VAL == hand[1].VAL && hand[0].VAL == hand[2].VAL && hand[3].VAL == hand[4].VAL) {
highest = 6;
return;
}
if(hand[2].VAL == hand[3].VAL && hand[2].VAL == hand[4].VAL && hand[1].VAL == hand[0].VAL) {
highest = 6;
return;
}
}
void testTwoPairs(int &highest, Card *hand) {
if(highest >= 2)
return;
if(hand[0].VAL == hand[1].VAL && hand[2].VAL == hand[3].VAL) {
highest = 2;
return;
}
if(hand[0].VAL == hand[1].VAL && hand[3].VAL == hand[4].VAL) {
highest = 2;
return;
}
if(hand[1].VAL == hand[2].VAL && hand[3].VAL == hand[4].VAL) {
highest = 2;
return;
}
}
void testStraight(int &highest, Card *hand) {
if(highest >= 4)
return;
int i = 0;
if(((hand[(i+0)%5].VAL+1)%13 == hand[(i+1)%5].VAL) &&
((hand[(i+1)%5].VAL+1)%13 == hand[(i+2)%5].VAL) &&
((hand[(i+2)%5].VAL+1)%13 == hand[(i+3)%5].VAL) &&
((hand[(i+3)%5].VAL+1)%13 == hand[(i+4)%5].VAL)) {
highest = 4;
return;
}
}
int main() {
std::string names[9];
names[0] = "highest-card";
names[1] = "one-pair";
names[2] = "two-pairs";
names[3] = "three-of-a-kind";
names[4] = "straight";
names[5] = "flush";
names[6] = "full-house";
names[7] = "four-of-a-kind";
names[8] = "straight-flush";
// mapping:
int mapping[256];
for(int i = 2; i <= 9; ++i) {
mapping['0'+i] = i-1;
}
mapping['A'] = 0;
mapping['T'] = 9;
mapping['J'] = 10;
mapping['Q'] = 11;
mapping['K'] = 12;
char line[32];
Card cards[10];
Card hand[5];
while(std::cin.getline(line,32)) {
line[14] = '\0';
line[29] = '\0';
std::cout << "Hand: " << line << " Deck: " << &(line[15]) << " Best hand: ";
// Read cards:
for(int i = 0; i < 10; ++i) {
char SUIT = line[3*i+1];
char VAL = line[3*i];
cards[i].SUIT = SUIT;
cards[i].VAL = mapping[(int)VAL];
}
int best = 0;
// test cards:
for(unsigned int i = 0; i <= 0x1F; ++i) {
unsigned int copyI = i;
int stackI = 5;
for(int handI = 0; handI < 5; ++handI) {
if((copyI & 1) == 1) {
hand[handI] = cards[stackI++];
}
else {
hand[handI] = cards[handI];
}
copyI >>= 1;
}
std::sort(hand, &hand[5]);
testStraightFlush(best, hand);
testFourOfAKind(best, hand);
testFullHouse(best, hand);
testFlush(best, hand);
testStraight(best, hand);
testThreeOfAKind(best, hand);
testTwoPairs(best, hand);
testOnePair(best, hand);
/*
std::cerr << "run tests for " << i << ": ";
for(int j = 0; j < 5; ++j)
std::cerr << hand[j] << " ";
std::cerr << std::endl;
std::cerr << names[best] << std::endl;//*/
}
std::cout << names[best] << std::endl;
}
return 0;
}