-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresult.c
140 lines (132 loc) · 3.26 KB
/
result.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
/*
Copyright (C) 2019- TheTrustedComputer
*/
#include "result.h"
void Result_print(Result *current, Result *best) {
#if defined(_WIN32) || defined(_WIN64) // For compilation under Windows
#endif
switch (current->wdl) {
default:
case UNKNOWN_CHAR:
case '\0':
printf(NONE_TEXT);
break;
case DRAW_CHAR:
if (best && best->wdl == current->wdl) {
printf("\e[1;33m%c\e[0m ", DRAW_CHAR);
}
else {
printf("\e[0;33m%c\e[0m ", DRAW_CHAR);
}
break;
case WIN_CHAR:
if (current->dtc) {
if (best && best->dtc == current->dtc && best->wdl == current->wdl) {
printf("\e[1;32m%c%d\e[0m ", current->wdl, current->dtc);
}
else {
printf("\e[0;32m%c%d\e[0m ", current->wdl, current->dtc);
}
}
else {
if (best && best->dtc == current->dtc && best->wdl == current->wdl) {
printf("\e[1;32m%s\e[0m ", WIN_TEXT);
}
else {
printf("\e[0;32m%s\e[0m ", WIN_TEXT);
}
}
break;
case LOSS_CHAR:
if (current->dtc) {
if (best && best->dtc == current->dtc && best->wdl == current->wdl) {
printf("\e[1;31m%c%d\e[0m ", current->wdl, current->dtc);
}
else {
printf("\e[0;31m%c%d\e[0m ", current->wdl, current->dtc);
}
}
else {
if (best && best->dtc == current->dtc && best->wdl == current->wdl) {
printf("\e[1;31m%s\e[0m ", LOSS_TEXT);
}
else {
printf("\e[0;31m%s\e[0m ", LOSS_TEXT);
}
}
}
}
void Result_increment(Result *result) {
switch (result->wdl) {
case WIN_CHAR:
result->wdl = LOSS_CHAR;
++result->dtc;
break;
case LOSS_CHAR:
result->wdl = WIN_CHAR;
++result->dtc;
}
}
Result Result_getBestResult(Result *results, unsigned resultSize) {
unsigned i, score;
Result best = { LOSS_CHAR, 0 };
for (i = score = 0; i < resultSize; ++i) {
switch (score) {
case 0:
switch (results[i].wdl) {
case LOSS_CHAR:
if (best.dtc < results[i].dtc) {
best.dtc = results[i].dtc;
}
break;
case DRAW_CHAR:
best = results[i];
score = 1;
break;
case WIN_CHAR:
best = results[i];
score = 2;
}
break;
case 1:
switch (results[i].wdl) {
case WIN_CHAR:
best = results[i];
score = 2;
}
break;
case 2:
if (results[i].wdl == WIN_CHAR && best.dtc > results[i].dtc) {
best.dtc = results[i].dtc;
}
}
}
return best;
}
unsigned Result_getBestMove(Result *results, unsigned resultSize) {
unsigned i, d, *bestMoves = GAME_VARIANT == POPOUT_VARIANT || GAME_VARIANT == POPTEN_VARIANT ? malloc(sizeof(int) * COLUMNS_X2) : malloc(sizeof(int) * COLUMNS), best;
init_genrand64(time(NULL));
Result bestResult = Result_getBestResult(results, resultSize);
for (i = d = 0; i < resultSize; ++i) {
if (bestResult.wdl == results[i].wdl && bestResult.dtc == results[i].dtc) {
bestMoves[d++] = i;
}
}
best = d ? bestMoves[genrand64_int64() % d] : INT32_MAX;
free(bestMoves);
return best;
}
void Result_normal_reverse(Result *results) {
for (unsigned i = 0; i < COLUMNS_D2; ++i) {
Result reverseTemp = results[i];
results[i] = results[COLUMNS - 1 - i];
results[COLUMNS - 1 - i] = reverseTemp;
}
}
void Result_popout_reverse(Result *results) {
for (unsigned i = COLUMNS; i < COLUMNS + COLUMNS_D2; ++i) {
Result reverseTemp = results[i];
results[i] = results[COLUMNS_X2 - 1 - (i - COLUMNS)];
results[COLUMNS_X2 - 1 - (i - COLUMNS)] = reverseTemp;
}
}