-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex.c
469 lines (378 loc) · 9.71 KB
/
hex.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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#include <assert.h>
#include <errno.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Judy.h>
#ifndef SIZE
#error "SIZE not defined"
#endif
#define LEN (SIZE + 1)
#define LIN(x, y) ((x) * LEN + (y))
#define X(f) ((f) / LEN)
#define Y(f) ((f) % LEN)
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define L 0
#define R 1
#define N 2
static char const* const show_player[3] = {"L", "R", "."};
typedef struct
{
uint8_t* seen;
int* open;
int begin;
int end;
} *PState_DFS;
static PState_DFS new_state()
{
PState_DFS state = malloc (sizeof (*state));
state->seen = (uint8_t*) malloc (LEN * LEN * sizeof (uint8_t));
state->open = (int*) malloc (LEN * LEN * sizeof (int));
if (!state->seen || !state->open)
{
fprintf (stderr, "could not allocate memory\n");
exit (EXIT_FAILURE);
}
return state;
}
static void release_state (PState_DFS state)
{
free (state->seen);
free (state->open);
free (state);
}
static void push (PState_DFS state, int x, int y)
{
state->open[state->end++] = x;
state->open[state->end++] = y;
state->seen[LIN (x, y)] = 1;
}
static int pop (PState_DFS state)
{
return state->open[state->begin++];
}
static void prepare_state (PState_DFS state, int f)
{
for (int i = 0; i < LEN * LEN; ++i)
{
state->seen[i] = 0;
}
state->begin = 0;
state->end = 0;
push (state, X(f), Y(f));
}
static int not_done (PState_DFS state)
{
return state->begin < state->end;
}
static int not_seen (PState_DFS state, int x, int y)
{
return !state->seen[LIN (x, y)];
}
typedef struct
{
uint8_t player;
uint8_t winner;
uint8_t* taken;
} *PPosition_type;
static PPosition_type new_position()
{
PPosition_type pos = malloc (sizeof (*pos));
pos->player = R;
pos->winner = N;
pos->taken = (uint8_t*) malloc (LEN * LEN * sizeof (uint8_t));
if (!pos->taken)
{
fprintf (stderr, "could not allocate memory\n");
exit (EXIT_FAILURE);
}
for (int i = 0; i < LEN * LEN; ++i)
{
pos->taken[i] = N;
}
return pos;
}
static void release_position (PPosition_type pos)
{
free (pos->taken);
free (pos);
}
static unsigned long _cnt_put = 0;
static unsigned long _cnt_ins = 0;
static unsigned long _cnt_hit = 0;
static void unput (PPosition_type pos, int f)
{
pos->winner = N;
pos->taken[f] = N;
pos->player = 1 - pos->player;
}
static uint8_t winner_from (PPosition_type pos, PState_DFS state, int f)
{
prepare_state (state, f);
int mi = (pos->player == L) ? X(f) : Y(f);
int ma = (pos->player == L) ? X(f) : Y(f);
while (not_done (state))
{
int const px = pop (state);
int const py = pop (state);
#define DO(dx, dy) \
if ( (px + dx) >= 0 && (px + dx) <= SIZE \
&& (py + dy) >= 0 && (py + dy) <= SIZE \
&& pos->taken[LIN ((px + dx), (py + dy))] == pos->player \
) \
{ \
if (not_seen (state, px + dx, py + dy)) \
{ \
mi = MIN (mi, (pos->player == L) ? (px + dx) : (py + dy)); \
ma = MAX (ma, (pos->player == L) ? (px + dx) : (py + dy)); \
\
if (mi == 0 && ma == SIZE) \
{ \
return pos->player; \
} \
\
push (state, px + dx, py + dy); \
} \
} \
DO ( 0, 1);
DO ( 1, 1);
DO ( 1, 0);
DO ( 0,-1);
DO (-1,-1);
DO (-1, 0);
#undef DO
}
return N;
}
static void put (PPosition_type pos, PState_DFS state, int f)
{
if (++_cnt_put % 1000000 == 0)
{
fprintf (stderr, "...put %lu\n", _cnt_put);
};
pos->winner = winner_from (pos, state, f);
pos->taken[f] = pos->player;
pos->player = 1 - pos->player;
}
static void show (PPosition_type pos)
{
printf ("%s%s\n", show_player[pos->player], show_player[pos->winner]);
for (int x = 0; x <= 2 * SIZE; ++x)
{
for (int y = -2 * SIZE; y <= 2 * SIZE; ++y)
{
int const qx = (2 * x + y) / 4;
int const rx = (2 * x + y) % 4;
int const qy = (2 * x - y) / 4;
int const ry = (2 * x - y) % 4;
if ( rx == 0
&& ry == 0
&& qx >= 0 && qx <= SIZE
&& qy >= 0 && qy <= SIZE
)
{
printf ("%s", show_player[pos->taken[LIN (qx, qy)]]);
}
else
{
printf (" ");
}
}
printf ("\n");
}
}
static Word_t encode (PPosition_type pos)
{
Word_t Index = 0;
for (int i = 0; i < LEN * LEN; ++i)
{
Index <<= 2;
Index += pos->taken[i];
}
Word_t Mirror = 0;
for (int i = LEN * LEN - 1; i >= 0; --i)
{
Mirror <<= 2;
Mirror += pos->taken[i];
assert (LEN * LEN - 1 - i == LIN (SIZE - X(i), SIZE - Y(i)));
}
return MIN (Index, Mirror);
}
static PPosition_type decode (Word_t Index, Word_t Value)
{
int count[3] = {0,0,0};
PPosition_type pos = new_position();
for (int i = 0; i < LEN * LEN; ++i)
{
++count[Index & 3];
pos->taken[LEN * LEN - i - 1] = Index & 3;
Index >>= 2;
}
pos->winner = Value;
pos->player = (count[0] > count[1]) ? R : L;
return pos;
}
static void insert (Pvoid_t* PJArray, Word_t Key, Word_t Value)
{
PWord_t PValue;
JLI (PValue, *PJArray, Key);
if (PValue == PJERR)
{
fprintf (stderr, "JHSI-Error: Out of memory...\n");
exit (EXIT_FAILURE);
}
*PValue = Value;
++_cnt_ins;
}
static uint8_t _winning (PPosition_type pos, PState_DFS state, Pvoid_t* PJArray)
{
if (pos->winner != N)
{
return 1;
}
Word_t const Index = encode (pos);
{
PWord_t PValue;
JLG (PValue, *PJArray, Index);
if (PValue)
{
++_cnt_hit;
return *PValue != pos->player;
}
}
for (int f = 0; f < LEN * LEN; ++f)
{
if (pos->taken[f] == N)
{
put (pos, state, f);
const uint8_t w = _winning (pos, state, PJArray);
unput (pos, f);
if (w)
{
insert (PJArray, Index, pos->player);
return 0;
}
}
}
insert (PJArray, Index, 1 - pos->player);
return 1;
}
#define WRITE(d) \
size_t w = fwrite (buf[d], sizeof (Word_t), buf_pos[d], dat[d]); \
\
if (w != buf_pos[d]) \
{ \
fprintf (stderr, "write error: %s\n", strerror (errno)); \
} \
\
buf_pos[d] = 0
static void save_pjarray (Pvoid_t PJArray)
{
FILE* dat[2];
int const buf_size = (1 << 20);
PWord_t buf[2];
int buf_pos[2] = {0,0};
for (uint8_t d = L; d < N; ++d)
{
char fname[100 + 1];
snprintf (fname, 100, "hex.%i.%s.dat", SIZE, show_player[d]);
dat[d] = fopen (fname, "wb+");
if (!dat[d])
{
fprintf ( stderr, "could not open dat file %s: %s\n"
, fname, strerror (errno)
);
exit (EXIT_FAILURE);
}
buf[d] = malloc (buf_size * sizeof (Word_t));
if (!buf[d])
{
fprintf (stderr, "could not allocate memory\n");
exit (EXIT_FAILURE);
}
}
Word_t Index = 0;
PWord_t PValue;
JLF (PValue, PJArray, Index);
while (PValue)
{
buf[*PValue][buf_pos[*PValue]++] = Index;
if (buf_pos[*PValue] == buf_size)
{
WRITE (*PValue);
}
JLN (PValue, PJArray, Index);
}
for (uint8_t d = L; d < N; ++d)
{
WRITE (d);
free (buf[d]);
fclose (dat[d]);
}
}
#undef WRITE
static int load_pjarray (Pvoid_t* PJArray)
{
int const buf_size = (1 << 20);
for (uint8_t d = L; d < N; ++d)
{
char fname[100 + 1];
snprintf (fname, 100, "hex.%i.%s.dat", SIZE, show_player[d]);
FILE* dat = fopen (fname, "r");
if (!dat)
{
fprintf ( stderr, "could not open dat file %s: %s\n"
, fname, strerror (errno)
);
return 2;
}
fprintf (stderr, "loading data from %s...\n", fname);
PWord_t buf = malloc (buf_size * sizeof (Word_t));
if (!buf)
{
fprintf (stderr, "could not allocate memory\n");
return 1;
}
size_t r;
while ((r = fread (buf, sizeof (Word_t), buf_size, dat)) > 0)
{
for (size_t i = 0; i < r; ++i)
{
insert (PJArray, buf[i], d);
}
}
free (buf);
fclose (dat);
}
fprintf (stderr, "retrieved %lu games\n", _cnt_ins);
return 0;
}
int main()
{
Pvoid_t PJArray = (Pvoid_t) NULL;
PPosition_type pos = new_position();
PState_DFS state = new_state();
load_pjarray (&PJArray);
for (int f = 0; f < LEN * LEN; ++f)
{
if (pos->taken[f] == N)
{
put (pos, state, f);
if (_winning (pos, state, &PJArray))
{
show (pos);
}
unput (pos, f);
}
}
save_pjarray (PJArray);
Word_t Rc_word;
JLFA (Rc_word, PJArray);
printf ( "put %lu ins %lu hit %lu Judy-bytes %lu\n"
, _cnt_put, _cnt_ins, _cnt_hit, Rc_word
);
release_position (pos);
release_state (state);
}