-
Notifications
You must be signed in to change notification settings - Fork 0
/
handevaluator.c
355 lines (328 loc) · 9.02 KB
/
handevaluator.c
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
345
346
347
348
349
350
351
352
353
#include "handevaluator.h"
#include <sys/stat.h>
#include <stdio.h>
#include "stdlib.h"
#include <string.h>
handeval_dag_node *dag;
handeval_eq_class *classes[7462];
void error(char* msg) {
fprintf(stderr, "ERROR: %s\n\n", msg);
exit(-1);
}
void errorline(char* msg, unsigned int line) {
fprintf(stderr, "ERROR in line %u: %s\n\n", line, msg);
exit(-1);
}
void load_equivalenceclasses(char* fn) {
char buf[1024];
static const char *DELIM = "\t";
unsigned int i=1;//,j;
FILE* f = fopen(fn, "r");
while (fgets(buf, 1024, f)) { // read a line
// 1 AKQJT 8 Royal Flush 100.00 0.0032
classes[i] = (handeval_eq_class*) malloc(sizeof(handeval_eq_class));
classes[i]->id = i;
strtok(buf, DELIM);
classes[i]->cards = strdup(strtok(0, DELIM));
classes[i]->type = atoi(strtok(0, DELIM));
classes[i]->desc = strdup(strtok(0,DELIM));
classes[i]->domination = atof(strtok(0,DELIM));
classes[i]->likelihood = atof(strtok(0,DELIM));
i++;
}
fclose(f);
}
void load_dag(char* fn) {
static char DELIM[2] = {'\t', '\n'};
char buf[1024];
unsigned int i, id, v, j;
int ecf, ecn;
handeval_dag_node *n;
dag = (handeval_dag_node*) calloc(76154, sizeof(handeval_dag_node));
FILE* f = fopen(fn, "r");
i=0;
while (fgets(buf, 1024, f)) { // read a line
if ((id = atoi(strtok(buf, DELIM))) != i) {
errorline("Nonmatching node ID!", i+1);
}
n = &(dag[i]); // get the target node
n->id = id; // set the id
for (j=0; j < 13; j++) { // iterate the target cards
v = atoi(strtok(0, DELIM));
if (v > 0) { // if valid link - invalid links remain set to 0 (by calloc)
n->succs[j] = &(dag[v]); // set card target
}
}
ecf = atoi(strtok(0, DELIM)); // get and set the equivalence classes
ecn = atoi(strtok(0, DELIM));
if (ecf == 0 || ecn == 0 || ecf > 7500 || ecn > 7500) {
errorline("Invalid equivalence class ID", i+1);
}
n->equiv_class_flush = ecf;
n->equiv_class_normal = ecn;
strtok(0, DELIM); // skip the human-readable part
n->maxn = atoi(strtok(0, DELIM));
n->maxf = atoi(strtok(0, DELIM));
n->minn = atoi(strtok(0, DELIM));
n->minf = atoi(strtok(0, DELIM));
i++;
}
fclose(f);
}
handeval_eq_class* calculate_equivalence_class(char* hand) {
// first, walk the dag for non-flush-hands, and count the colors
unsigned int i, colarr = 0, r, c, flush; // color array stored in a 32-bit value. I hope that there is some way to do bitfiddeling for flush detection
char* carr = (char*) &colarr;
handeval_dag_node* n = 0;
for (i=0; i < 7; i++) {
//rank: divide by 4
r = hand[i] >> 2;
//color:
c = hand[i] & 3;
if (!i) {
n = &(dag[r]); // the first layer - get the r-th node of the dag
} else {
n = n->succs[r]; // later layers - follow the links (we assume the hand is valid and crash upon errors here)
}
carr[c]++; // count the color
}
// next, check for a flush
flush = 4;
for (i=0; i < 4; i++) {
if (carr[i] >= 5) {
flush = i;
}
}
if (flush == 4) { // no flush
return classes[n->equiv_class_normal];
} else {
// finally, if there is a flush, walk the dag again, and use only the cards of the flush-color
n = 0;
for (i=0; i < 7; i++) {
r = hand[i] >> 2;
c = hand[i] & 3;
if (c == flush) { // flush-coloured card?
if (!n) {
n = &(dag[r]); // the first layer - get the r-th node of the dag
} else {
n = n->succs[r]; // later layers - follow the links (we assume the hand is valid and crash upon errors here)
}
}
}
return classes[n->equiv_class_flush];
}
}
handeval_eq_class* calculate_equivalence_class_ncards(char* hand, unsigned int nCards) {
// first, walk the dag for non-flush-hands, and count the colors
unsigned int i, colarr = 0, r, c, flush; // color array stored in a 32-bit value. I hope that there is some way to do bitfiddeling for flush detection
char* carr = (char*) &colarr;
handeval_dag_node* n = 0;
for (i=0; i < nCards; i++) {
//rank: divide by 4
r = hand[i] >> 2;
//color:
c = hand[i] & 3;
if (!i) {
n = &(dag[r]); // the first layer - get the r-th node of the dag
} else {
n = n->succs[r]; // later layers - follow the links (we assume the hand is valid and crash upon errors here)
}
carr[c]++; // count the color
}
// next, check for a flush
flush = 4;
for (i=0; i < 4; i++) {
if (carr[i] >= 5) {
flush = i;
}
}
if (flush == 4) { // no flush
//return the worst possible hand for this set of cards, which is the same as the best
//hand for 7 cards (when nCards == 7)
return classes[n->maxn];
} else {
// finally, if there is a flush, walk the dag again, and use only the cards of the flush-color
n = 0;
for (i=0; i < nCards; i++) {
r = hand[i] >> 2;
c = hand[i] & 3;
if (c == flush) { // flush-coloured card?
if (!n) {
n = &(dag[r]); // the first layer - get the r-th node of the dag
} else {
n = n->succs[r]; // later layers - follow the links (we assume the hand is valid and crash upon errors here)
}
}
}
return classes[n->maxf];
}
}
//char* hand_to_str(char* hand, unsigned int cards) {
// unsigned int i;
// char* res = malloc(cards*3);
// for (i=0; i < cards; i++) {
// res[i*3+0] = CARD[hand[i] >> 2];
// res[i*3+1] = COLOR[hand[i] & 3];
// res[i*3+2] = ' ';
// }
// res[cards*3-1] = 0;
// return res;
//}
char* hand_to_str(char* hand, unsigned int cards, char* threeCharsPerCard) {
unsigned int i;
char* res = threeCharsPerCard;
for (i=0; i < cards; i++) {
res[i*3+0] = CARD[hand[i] >> 2];
res[i*3+1] = COLOR[hand[i] & 3];
res[i*3+2] = ' ';
}
res[cards*3-1] = 0;
return res;
}
/** Convert a string to a hand, for debugging purposes. VERY UNSAFE! */
char* str_to_cards(char* str, unsigned int number) {
unsigned int i, pos = 0, j;
char val, r, c;
char* res = (char*) malloc(number);
for (i=0; i < number; i++) {
r = str[pos++];
c = str[pos++];
pos++;
val = 0;
for (j=0; j < 13; j++) {
if (r == CARD[j]) {
val = j << 2;
}
}
for (j=0; j < 4; j++) {
if (c == COLOR[j]) {
val += j;
}
}
res[i] = val;
}
return res;
}
void handeval_init(char* currDir) {
//*********************** TODO: fix this!!! for some reason requires full path
//load_equivalenceclasses("/Users/mark/work/sniper/eqcllist");
//load_dag("/Users/mark/work/sniper/carddag");
char buf[1024];
strcpy(buf, currDir);
strcat(buf, "eqcllist");
load_equivalenceclasses(buf);
strcpy(buf, currDir);
strcat(buf, "carddag");
load_dag(buf);
}
void partial_evaluate(unsigned int cardcnt, char* cards, partial_evaluation* pe) {
unsigned int i, r, c;
assert_local(cardcnt); // there must be more than one card
pe->evaluated = cardcnt;
for (i=0; i < 4; i++) {
pe->flushnodes[i] = 0;
pe->colors[i] = 0;
}
for (i=0; i < cardcnt; i++) {
r = cards[i] >> 2;
c = cards[i] & 3;
// follow the dag for non-flush nodes
if (!i) {
pe->noflushnode = &(dag[r]);
} else {
pe->noflushnode = pe->noflushnode->succs[r];
}
// follow the dag for flush color candidates nodes
pe->colors[c]++;
if (pe->flushnodes[c] == 0) {
pe->flushnodes[c] = &(dag[r]);
} else {
pe->flushnodes[c] = pe->flushnodes[c]->succs[r];
}
}
}
handeval_eq_class* resume_evaluation(char* cards, const partial_evaluation* pe) {
unsigned int i, r, c;
handeval_dag_node *n = pe->noflushnode;
unsigned int remain = 7 - pe->evaluated;
int cl;
char col[4] = {pe->colors[0], pe->colors[1], pe->colors[2], pe->colors[3]};
for (i=0; i < remain; i++) {
r = cards[i] >> 2;
c = cards[i] & 3;
n = n->succs[r]; // follow the non-flush dag
col[c]++;
}
// check for flushes
int fc = -1;
for (i=0; i < 4; i++) {
if (col[i] >= 5) {
fc = i;
break;
}
}
assert_local(n->equiv_class_normal > 0);
if (fc >= 0) {
n = pe->flushnodes[fc];
for (i=0; i < remain; i++) {
c = cards[i] & 3;
if ((int) c == fc) {
r = cards[i] >> 2;
if (n) {
n = n->succs[r];
} else {
n = &(dag[r]);
}
}
}
assert_local(n->equiv_class_flush > 0);
cl = n->equiv_class_flush;
} else {
cl = n->equiv_class_normal;
}
return classes[cl];
}
handeval_eq_class* calculate_minimal_class(unsigned int cardcnt, char* cards) {
unsigned int i, r, c;
partial_evaluation pe;
assert_local(cardcnt); // there must be more than one card
pe.noflushnode = 0;
for (i=0; i < 4; i++) {
pe.flushnodes[i] = 0;
pe.colors[i] = 0;
}
for (i=0; i < cardcnt; i++) {
r = cards[i] >> 2;
c = cards[i] & 3;
// follow the dag for non-flush nodes
if (!i) {
pe.noflushnode = &(dag[r]);
} else {
pe.noflushnode = pe.noflushnode->succs[r];
}
// follow the dag for flush color candidates nodes
pe.colors[c]++;
if (pe.flushnodes[c] == 0) {
pe.flushnodes[c] = &(dag[r]);
} else {
pe.flushnodes[c] = pe.flushnodes[c]->succs[r];
}
}
int minc = pe.noflushnode->maxn;
if (minc == 0) {
printf("MORE SEVERE BUG!\n\n\n");
fflush(stdout);
}
for (i=0; i < 4; i++) {
if (pe.colors[i] >= 5) { // flush found
if (pe.flushnodes[i]->equiv_class_flush < minc) {
minc = pe.flushnodes[i]->equiv_class_flush;
}
}
}
if (minc <= 0) {
printf("SEVERE BUG!\n\n\n");
fflush(stdout);
}
return classes[minc];
}