-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAIUtilities.c
More file actions
343 lines (315 loc) · 8.1 KB
/
AIUtilities.c
File metadata and controls
343 lines (315 loc) · 8.1 KB
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
#include "AIUtilities.h"
#include "Action.h"
#include <string.h>
#include <assert.h>
#include "mt19937.h"
PowerMap NewPowerMap()
{
PowerMap re = malloc(sizeof(*re));
memset(re->linePower, 0, sizeof(re->linePower));
int pl = 0;
for (int x = 0; x < LLN; ++x)
{
for (int y = 0; y < LLN; ++y)
{
Point p = GetPoint(x, y);
for (int d = 0; d < DireLen; ++d)
{
Point s = CBINF[p].start[d];
if (re->linePower[s][d] == NULL)
{
re->powers[pl] = 0;
re->linePower[s][d] = &re->powers[pl++];
}
re->linePower[p][d] = re->linePower[s][d];
}
}
}
#ifdef DEBUG
assert(pl == POWERSLEN);
#endif
return re;
}
void FreePowerMap(PowerMap pm)
{
free(pm);
}
void PowerMapFlush(AIData aidata, const ChessBoard cb, const int PatternLen)
{
PowerMap pm = aidata->powerMap;
memset(pm->powers, 0, sizeof(pm->powers));
pm->powerSum = 0;
char computed[POWERSLEN];
memset(computed, 0, sizeof(computed));
for (int x = 0; x < LLN; ++x)
{
for (int y = 0; y < LLN; ++y)
{
Point p = GetPoint(x, y);
for (int d = 0; d < DireLen; ++d)
{
int index = pm->linePower[p][d] - pm->powers;
if (computed[index])
continue;
computed[index] = 1;
Power ls = TrieQuery(&GetChess(cb, CBINF[p].start[d]),
DireSteps[d], CBINF[p].lens[d], aidata->patterns);
*(pm->linePower[p][d]) = ls;
pm->powerSum += ls;
}
}
}
}
void PrintPowerMap(PowerMap pm)
{
for (int d = 0; d < DireLen; ++d)
{
printf("%c:\n", DireNames[d]);
for (int x = 0; x < LLN; ++x)
{
for (int y = 0; y < LLN; ++y)
{
printf("%f ", *(pm->linePower)[GetPoint(y, x)][d]);
}
putchar('\n');
}
}
printf("Sum: %f\n", pm->powerSum);
}
PowerMap ClonePowerMap(const PowerMap pm)
{
PowerMap re = NewPowerMap();
memcpy(re->powers, pm->powers, sizeof(re->powers));
re->powerSum = pm->powerSum;
return re;
}
ChessPot NewChessPot()
{
ChessPot re = malloc(sizeof(*re));
ChessPotTie(re, ChessPotHead, ChessPotTail);
return re;
}
ChessPot CloneChessPot(const ChessPot cp)
{
ChessPot re = malloc(sizeof(*re));
memcpy(re, cp, sizeof(*cp));
return re;
}
void FreeChessPot(ChessPot pot)
{
free(pot);
}
NeighborMap NewNeighborMap()
{
NeighborMap re = malloc(sizeof(*re));
memset(re->map, 0, sizeof(re->map));
re->pot = NewChessPot();
re->historyCount = 0;
return re;
}
NeighborMap CloneNeighborMap(const NeighborMap nbm)
{
NeighborMap re = malloc(sizeof(*re));
memcpy(re, nbm, sizeof(*nbm));
re->pot = CloneChessPot(nbm->pot);
return re;
}
void FreeNeighborMap(NeighborMap nbm)
{
FreeChessPot(nbm->pot);
free(nbm);
}
void NeighborMapAddChess(NeighborMap nbm, Point p)
{
ChessBoardNeighbor cbn = &CBNEI[p];
char *cb = nbm->map;
ChessPot pot = nbm->pot;
Point nxt = pot->nxtnode[ChessPotHead];
nbm->history[nbm->historyCount++] = nxt;
for (int i = 0; i < cbn->len; ++i)
{
Point p = cbn->neighbors[i];
if (cb[p] == 0)
{
ChessPotTie(pot, p, nxt);
nxt = p;
}
cb[p] += 1;
}
ChessPotTie(pot, ChessPotHead, nxt);
}
void NeighborMapUndo(NeighborMap nbm, Point s)
{
char *cb = nbm->map;
ChessPot pot = nbm->pot;
Point p = nbm->history[--nbm->historyCount];
ChessPotTie(nbm->pot, ChessPotHead, p);
ChessBoardNeighbor cbn = &CBNEI[s];
for (int i = 0; i < cbn->len; ++i)
{
Point p = cbn->neighbors[i];
cb[p] -= 1;
}
}
void NeighborMapFlush(NeighborMap nbm, const Stack actionHistory)
{
nbm->historyCount = 0;
ChessPotClean(nbm->pot);
memset(nbm->map, 0, sizeof(nbm->map));
for (int i = 0; i < actionHistory->Count; ++i)
{
NeighborMapAddChess(nbm, ((Action)actionHistory->Items[i])->point);
}
}
void PrintNeighborMap(NeighborMap nbm)
{
printf(" ");
char a = 'a';
for (int i = 0; i < LLN; i++, a++)
printf(" %c ", a);
printf("\n");
for (int i = 0; i < LLN; i++)
{
printf(i < 9 ? " %d " : "%d ", i + 1);
for (int j = 0; j < LLN; j++)
{
int k = GetChessXY(nbm->map, j, i);
printf(k == 0 ? " . " : "%2d ", k);
}
printf("%d", i + 1);
printf("\n");
}
printf(" ");
a = 'a';
for (int i = 0; i < LLN; i++, a++)
printf(" %c ", a);
printf("\nPot: ");
ChessPot pot = nbm->pot;
int count = 0;
for (Point p = pot->nxtnode[ChessPotHead]; p != ChessPotTail; p = pot->nxtnode[p])
{
printf("%d%c, ", PointTo2C(p));
count += 1;
if (count > BLEN)
{
assert(0);
}
}
printf("Count: %d\n", count);
}
void NeighborMaptest()
{
char buff[64];
NeighborMap nbm = NewNeighborMap();
while (1)
{
PrintNeighborMap(nbm);
int x, y;
scanf("%d%d", &x, &y);
if (x < 0)
NeighborMapUndo(nbm, GetPoint(-x, y));
else
NeighborMapAddChess(nbm, GetPoint(x, y));
}
}
#define SETPower(pm, point, d, power) pm->powerSum += power - *(pm->linePower)[point][d], *(pm->linePower)[point][d] = power
void PowerMaptest()
{
char buff[64];
PowerMap pm = NewPowerMap();
while (1)
{
PrintPowerMap(pm);
int x, y;
scanf("%d%d", &x, &y);
for (int d = 0; d < DireLen; ++d)
{
SETPower(pm, GetPoint(x, y), d, d + 1);
}
}
}
uint64 TurnTable[2][BLEN];
ZobristTable NewZobristTable()
{
static char inited = 0;
if (!inited)
{
for (int p = 0; p < 2; ++p)
{
init_genrand64(3213123442ull ^ p);
for (int i = 0; i < BLEN; ++i)
{
TurnTable[p][i] = genrand64_int64();
}
}
inited = 1;
}
ZobristTable re = malloc(sizeof(*re));
re->turnTable[0] = &TurnTable[0][0];
re->turnTable[1] = &TurnTable[1][0];
memset(re->hashTable, 0, sizeof(re->hashTable));
re->count = 0;
re->start = genrand64_int64();
return re;
}
ZobristTable CloneZobristTable(const ZobristTable zb)
{ // not clone hashtable
ZobristTable re = malloc(sizeof(*re));
memset(re->hashTable, 0, sizeof(re->hashTable));
re->count = 0;
re->start = zb->start;
re->turnTable[0] = zb->turnTable[0];
re->turnTable[1] = zb->turnTable[1];
return re;
}
void FreeZobristTable(ZobristTable zt)
{
free(zt);
}
HashNode ZobristTableFind(ZobristTable zt, const uint64 key)
{
HashNode node = zt->hashTable[key & HASHMASK];
while (node != NULL && node->val.key != key)
{
node = node->nxt;
}
return node;
}
void ZobristTableInsert(ZobristTable zt, const uint64 key, const Power power)
{
HashNode node = &zt->nodesPool[zt->count++];
node->val.key = key;
node->val.power = power;
int k = key & HASHMASK;
node->nxt = zt->hashTable[k];
zt->hashTable[k] = node;
#ifdef DEBUG
assert(zt->count <= HASHPOOLLEN);
#endif
}
AIData CloneAIData(const AIData aidata) // Not Clone TRIE, Zobrist
{
AIData re = malloc(sizeof(*re));
re->powerMap = ClonePowerMap(aidata->powerMap);
re->neighborMap = CloneNeighborMap(aidata->neighborMap);
re->patterns = aidata->patterns;
re->playerid = aidata->playerid;
re->needflush = aidata->needflush;
re->zobristTable = CloneZobristTable(aidata->zobristTable);
return re;
}
void PrintAIData(const AIData aidata)
{
printf("PowerMap:\n");
PrintPowerMap(aidata->powerMap);
printf("NeighborMap:\n");
PrintNeighborMap(aidata->neighborMap);
printf("Playerid: %d, needflush: %d\n", aidata->playerid, aidata->needflush);
}
void FreeAIData(AIData aidata)
{
FreePowerMap(aidata->powerMap);
FreeNeighborMap(aidata->neighborMap);
FreeZobristTable(aidata->zobristTable);
free(aidata);
}