-
Notifications
You must be signed in to change notification settings - Fork 0
/
naive.cpp
261 lines (229 loc) · 7.68 KB
/
naive.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
#include "player.hpp"
string cards_to_hand(string a, string b)
{
string ans = "";
if(a[1] != b[1]) {
return ans + a[0] + b[0] + 'o';
} else {
return ans + a[0] + b[0] + 's';
}
}
int dnumber(string card)
{
int stuff[100];
stuff['T'] = 8;
stuff['J'] = 9;
stuff['Q'] = 10;
stuff['K'] = 11;
stuff['A'] = 12;
return ((card[0] >= '2' && card[0] <= '9') ? card[0] - '2' : stuff[card[0]]);
}
// returns number between 0 and 3
int suit(string card)
{
int stuff[150];
stuff['c'] = 0;
stuff['d'] = 1;
stuff['h'] = 2;
stuff['s'] = 3;
return stuff[card[1]];
}
// returns number between 0 and 51
int rank(string card)
{
return 4 * dnumber(card) + suit(card);
}
/*
Input: 5-card hand
Output: "position" of the hand in all possible 5-card
hands
currently very crude -- outputs number between 0 and 4
NEED TO IMPROVE
*/
int hand_to_number(vector<string> hand)
{
// count frequencies of each suit and number
vector<int> freq_suits(4, 0), freq_nums(13, 0);
for(int i = 0; i < hand.size(); ++i) {
freq_suits[suit(hand[i])]++;
freq_nums[dnumber(hand[i])]++;
}
int flush = -1, straight = -1, four = -1, three = -1;
int two = -1, pairs = 0;
for(int i = 0; i < 4; ++i) {
if(freq_suits[i] == 5) flush = i;
}
for(int i = 0; i < 13; ++i) {
if(freq_nums[i] == 4) four = i;
else if(freq_nums[i] == 3) three = i;
else if(freq_nums[i] == 2) {
two = i;
pairs++;
}
if(i <= 8 && freq_nums[i] == 1 && freq_nums[i+1] == 1 && freq_nums[i+2] == 1 && freq_nums[i+3] == 1 && freq_nums[i+4] == 1)
straight = i+4;
}
if(straight >= 0 || flush >= 0 || four >= 0 || three >= 8) return 0;
if(three >= 0 && two >= 0) return 0;
if(three >= 0) return 1;
if(pairs == 2 && two >= 11) return 1;
if(pairs == 2) return 2;
if(pairs == 1 && two >= 11) return 2;
if(pairs == 1) return 3;
return 4;
}
/*
hand_strength2: determines strength of 2-card hand
hand_strength5: determines strength of 5-card set
hand_strength6: determines strength of 6-card set
hand_strength7: determines strength of 7-card set
vector<string> hand: the input hand/set
e.g., "2d", "3d", "4d" "5d", "6d"
bool add: used only in sets of sizes 5 and 6 to indicate
that the function should try all possible combinations
of one more card
All outputs are integers from 0 to 4 inclusive.
*/
int hand_strength6(vector<string> hand, bool add);
int hand_strength7(vector<string> hand);
int table[13][13] = {
{ 1, 1, 2, 2, 3, 5, 5, 5, 5, 5, 5, 5, 5 },
{ 2, 1, 2, 3, 4, 6, 7, 7, 7, 7, 7, 7, 7 },
{ 3, 4, 1, 3, 4, 5, 7, 9, 9, 9, 9, 9, 9 },
{ 4, 5, 5, 1, 3, 4, 6, 8, 9, 9, 9, 9, 9 },
{ 6, 6, 6, 5, 2, 4, 5, 7, 9, 9, 9, 9, 9 },
{ 8, 8, 8, 7, 7, 3, 4, 5, 8, 9, 9, 9, 9 },
{ 9, 9, 9, 8, 8, 7, 4, 5, 6, 8, 9, 9, 9 },
{ 9, 9, 9, 9, 9, 9, 8, 5, 5, 6, 8, 9, 9 },
{ 9, 9, 9, 9, 9, 9, 9, 8, 6, 7, 7, 9, 9 },
{ 9, 9, 9, 9, 9, 9, 9, 9, 8, 6, 6, 7, 9 },
{ 9, 9, 9, 9, 9, 9, 9, 9, 9, 8, 7, 7, 8 },
{ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7, 8 },
{ 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 7 }
};
int hand_strength2(vector<string> hand)
{
int c1 = 12-dnumber(hand[0]);
int c2 = 12-dnumber(hand[1]);
int same = (suit(hand[0]) == suit(hand[1]));
//cout << "asdfghjkl\n";
if(same) return (min(table[c1][c2], table[c2][c1])+1)/2-1;
else return (max(table[c1][c2], table[c2][c1])+1)/2-1;
}
int hand_strength4(vector<string> hand)
{
return hand_to_number(hand);
}
int hand_strength5(vector<string> hand, bool add)
{
string cards = "23456789TJQK", suits = "cdhs";
if(!add) return hand_to_number(hand);
else {
int min_strength = 4;
for(int i = 0; i < cards.size(); ++i) {
for(int j = 0; j < suits.size(); ++j) {
string new_card = "";
new_card += cards[i];
new_card += suits[j];
hand.push_back(new_card);
int new_strength = min(min_strength, hand_strength6(hand, 0));
// if a strength-0 hand has 1 card missing, we're calling it strength 2
// 1 -> 3 and 2 -> 4, respectively.
// 3 and 4 stay as 4
min_strength = min(min_strength, new_strength + 2);
hand.erase(hand.end() - 1);
}
}
return min_strength;
}
}
int hand_strength6(vector<string> hand, bool add)
{
string cards = "23456789TJQK", suits = "cdhs";
int res = 4;
if(!add) {
for(int i = 0; i < hand.size(); ++i) {
vector<string> hand2 = hand;
hand2.erase(hand2.begin() + i);
res = min(res, hand_strength5(hand2, 0));
}
return res;
} else {
int min_strength = 4;
for(int i = 0; i < cards.size(); ++i) {
for(int j = 0; j < suits.size(); ++j) {
string new_card = "";
new_card += cards[i];
new_card += suits[j];
hand.push_back(new_card);
int new_strength = min(min_strength, hand_strength7(hand));
// if a strength-0 hand has 1 card missing, we're calling it strength 2
// 1 -> 3 and 2 -> 4, respectively.
// 3 and 4 stay as 4
min_strength = min(min_strength, new_strength + 2);
hand.erase(hand.end() - 1);
}
}
return min_strength;
}
}
int hand_strength7(vector<string> hand)
{
int res = 4;
for(int i=0; i<hand.size(); ++i) {
vector<string> hand2 = hand;
hand2.erase(hand2.begin() + i);
res = min(res, hand_strength6(hand2, 0)); // 0 or 1 as parameter???
// 0 as parameter -- don't want to add any cards here
// as all the cards have been shown
}
return res;
}
/*
Determines usefulness of a given hand.
Only applicable for 2-card hands.
string hole: 2 hole cards
string table: current table cards
*/
/*
0 is most useful
2 is least useful
*/
int Bot::usefulness(vector<string> hole, vector<string> table)
{
vector<string> table1 = table, table2 = table, table3 = table;
table1.push_back(hole[0]);
table2.push_back(hole[1]);
table3.push_back(hole[0]);
table3.push_back(hole[1]);
if(table.size() == 0) return 0;
if(table.size() == 3) {
int strength1 = hand_strength4(table1);
int strength2 = hand_strength4(table2);
int strength3 = hand_strength5(table3, 0);
if((strength1 <= 1 || strength2 <= 1) || strength3 <= 1) return 0;
if((strength1 <= 2 || strength2 <= 2) || strength3 <= 2) return 1;
return 2;
} else if(table.size() == 4) {
int strength1 = hand_strength5(table1, 0);
int strength2 = hand_strength5(table2, 0);
int strength3 = hand_strength6(table3, 0);
if((strength1 <= 1 || strength2 <= 1) || strength3 <= 1) return 0;
if((strength1 <= 2 || strength2 <= 2) || strength3 <= 2) return 1;
return 2;
} else if(table.size() == 5) {
int strength1 = hand_strength6(table1, 0);
int strength2 = hand_strength6(table2, 0);
int strength3 = hand_strength7(table3);
if((strength1 <= 1 || strength2 <= 1) || strength3 <= 1) return 0;
if((strength1 <= 2 || strength2 <= 2) || strength3 <= 2) return 1;
return 2;
}
}
int Bot::hand_strength(vector<string> hand)
{
if(hand.size() == 2) return hand_strength2(hand);
if(hand.size() == 5) return max(hand_strength5(hand, 0), hand_strength5(hand, 1));
if(hand.size() == 6) return max(hand_strength6(hand, 0), hand_strength6(hand, 1));
if(hand.size() == 7) return max(hand_strength7(hand), hand_strength7(hand));
}