Distributed engine: allow waiting for just one new reply.
[pachi/pachi-r6144.git] / board.c
blobe1b83a7374e272e030e5f709ede3ae33fa15c9cd
1 #include <alloca.h>
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include "board.h"
8 #include "debug.h"
9 #include "mq.h"
10 #include "random.h"
12 #ifdef BOARD_SPATHASH
13 #include "patternsp.h"
14 #endif
15 #ifdef BOARD_PAT3
16 #include "pattern3.h"
17 #endif
18 #ifdef BOARD_TRAITS
19 static void board_trait_recompute(struct board *board, coord_t coord);
20 #include "tactics.h"
21 #endif
22 #ifdef BOARD_GAMMA
23 #include "pattern.h"
24 #endif
27 #if 0
28 #define profiling_noinline __attribute__((noinline))
29 #else
30 #define profiling_noinline
31 #endif
33 #define gi_granularity 4
34 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
37 static void
38 board_setup(struct board *b)
40 memset(b, 0, sizeof(*b));
42 struct move m = { pass, S_NONE };
43 b->last_move = b->last_move2 = b->last_ko = b->ko = m;
46 struct board *
47 board_init(void)
49 struct board *b = malloc2(sizeof(struct board));
50 board_setup(b);
52 // Default setup
53 b->size = 9 + 2;
54 board_clear(b);
56 return b;
59 struct board *
60 board_copy(struct board *b2, struct board *b1)
62 memcpy(b2, b1, sizeof(struct board));
64 int bsize = board_size2(b2) * sizeof(*b2->b);
65 int gsize = board_size2(b2) * sizeof(*b2->g);
66 int fsize = board_size2(b2) * sizeof(*b2->f);
67 int nsize = board_size2(b2) * sizeof(*b2->n);
68 int psize = board_size2(b2) * sizeof(*b2->p);
69 int hsize = board_size2(b2) * 2 * sizeof(*b2->h);
70 int gisize = board_size2(b2) * sizeof(*b2->gi);
71 #ifdef WANT_BOARD_C
72 int csize = board_size2(b2) * sizeof(*b2->c);
73 #else
74 int csize = 0;
75 #endif
76 #ifdef BOARD_SPATHASH
77 int ssize = board_size2(b2) * sizeof(*b2->spathash);
78 #else
79 int ssize = 0;
80 #endif
81 #ifdef BOARD_PAT3
82 int p3size = board_size2(b2) * sizeof(*b2->pat3);
83 #else
84 int p3size = 0;
85 #endif
86 #ifdef BOARD_TRAITS
87 int tsize = board_size2(b2) * sizeof(*b2->t);
88 int tqsize = board_size2(b2) * sizeof(*b2->t);
89 #else
90 int tsize = 0;
91 int tqsize = 0;
92 #endif
93 #ifdef BOARD_GAMMA
94 int pbsize = board_size2(b2) * sizeof(*b2->prob[0].items);
95 #else
96 int pbsize = 0;
97 #endif
98 int cdsize = board_size2(b2) * sizeof(*b2->coord);
99 void *x = malloc2(bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + pbsize * 2 + cdsize);
100 memcpy(x, b1->b, bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + pbsize * 2 + cdsize);
101 b2->b = x; x += bsize;
102 b2->g = x; x += gsize;
103 b2->f = x; x += fsize;
104 b2->p = x; x += psize;
105 b2->n = x; x += nsize;
106 b2->h = x; x += hsize;
107 b2->gi = x; x += gisize;
108 #ifdef WANT_BOARD_C
109 b2->c = x; x += csize;
110 #endif
111 #ifdef BOARD_SPATHASH
112 b2->spathash = x; x += ssize;
113 #endif
114 #ifdef BOARD_PAT3
115 b2->pat3 = x; x += p3size;
116 #endif
117 #ifdef BOARD_TRAITS
118 b2->t = x; x += tsize;
119 b2->tq = x; x += tqsize;
120 #endif
121 #ifdef BOARD_GAMMA
122 b2->prob[0].items = x; x += pbsize;
123 b2->prob[1].items = x; x += pbsize;
124 #endif
125 b2->coord = x; x += cdsize;
127 return b2;
130 void
131 board_done_noalloc(struct board *board)
133 if (board->b) free(board->b);
136 void
137 board_done(struct board *board)
139 board_done_noalloc(board);
140 free(board);
143 void
144 board_resize(struct board *board, int size)
146 #ifdef BOARD_SIZE
147 assert(board_size(board) == size + 2);
148 #endif
149 board->size = size + 2 /* S_OFFBOARD margin */;
150 board->size2 = board_size(board) * board_size(board);
151 if (board->b)
152 free(board->b);
154 int bsize = board_size2(board) * sizeof(*board->b);
155 int gsize = board_size2(board) * sizeof(*board->g);
156 int fsize = board_size2(board) * sizeof(*board->f);
157 int nsize = board_size2(board) * sizeof(*board->n);
158 int psize = board_size2(board) * sizeof(*board->p);
159 int hsize = board_size2(board) * 2 * sizeof(*board->h);
160 int gisize = board_size2(board) * sizeof(*board->gi);
161 #ifdef WANT_BOARD_C
162 int csize = board_size2(board) * sizeof(*board->c);
163 #else
164 int csize = 0;
165 #endif
166 #ifdef BOARD_SPATHASH
167 int ssize = board_size2(board) * sizeof(*board->spathash);
168 #else
169 int ssize = 0;
170 #endif
171 #ifdef BOARD_PAT3
172 int p3size = board_size2(board) * sizeof(*board->pat3);
173 #else
174 int p3size = 0;
175 #endif
176 #ifdef BOARD_TRAITS
177 int tsize = board_size2(board) * sizeof(*board->t);
178 int tqsize = board_size2(board) * sizeof(*board->t);
179 #else
180 int tsize = 0;
181 int tqsize = 0;
182 #endif
183 #ifdef BOARD_GAMMA
184 int pbsize = board_size2(board) * sizeof(*board->prob[0].items);
185 #else
186 int pbsize = 0;
187 #endif
188 int cdsize = board_size2(board) * sizeof(*board->coord);
189 void *x = malloc2(bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + pbsize * 2 + cdsize);
190 memset(x, 0, bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + pbsize * 2 + cdsize);
191 board->b = x; x += bsize;
192 board->g = x; x += gsize;
193 board->f = x; x += fsize;
194 board->p = x; x += psize;
195 board->n = x; x += nsize;
196 board->h = x; x += hsize;
197 board->gi = x; x += gisize;
198 #ifdef WANT_BOARD_C
199 board->c = x; x += csize;
200 #endif
201 #ifdef BOARD_SPATHASH
202 board->spathash = x; x += ssize;
203 #endif
204 #ifdef BOARD_PAT3
205 board->pat3 = x; x += p3size;
206 #endif
207 #ifdef BOARD_TRAITS
208 board->t = x; x += tsize;
209 board->tq = x; x += tqsize;
210 #endif
211 #ifdef BOARD_GAMMA
212 board->prob[0].items = x; x += pbsize;
213 board->prob[1].items = x; x += pbsize;
214 #endif
215 board->coord = x; x += cdsize;
218 void
219 board_clear(struct board *board)
221 int size = board_size(board);
222 float komi = board->komi;
224 board_done_noalloc(board);
225 board_setup(board);
226 board_resize(board, size - 2 /* S_OFFBOARD margin */);
228 board->komi = komi;
230 /* Setup neighborhood iterators */
231 board->nei8[0] = -size - 1; // (-1,-1)
232 board->nei8[1] = 1;
233 board->nei8[2] = 1;
234 board->nei8[3] = size - 2; // (-1,0)
235 board->nei8[4] = 2;
236 board->nei8[5] = size - 2; // (-1,1)
237 board->nei8[6] = 1;
238 board->nei8[7] = 1;
239 board->dnei[0] = -size - 1;
240 board->dnei[1] = 2;
241 board->dnei[2] = size*2 - 2;
242 board->dnei[3] = 2;
244 /* Setup initial symmetry */
245 board->symmetry.d = 1;
246 board->symmetry.x1 = board->symmetry.y1 = board_size(board) / 2;
247 board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1;
248 board->symmetry.type = SYM_FULL;
250 /* Set up coordinate cache */
251 foreach_point(board) {
252 board->coord[c][0] = c % board_size(board);
253 board->coord[c][1] = c / board_size(board);
254 } foreach_point_end;
256 /* Draw the offboard margin */
257 int top_row = board_size2(board) - board_size(board);
258 int i;
259 for (i = 0; i < board_size(board); i++)
260 board->b[i] = board->b[top_row + i] = S_OFFBOARD;
261 for (i = 0; i <= top_row; i += board_size(board))
262 board->b[i] = board->b[board_size(board) - 1 + i] = S_OFFBOARD;
264 foreach_point(board) {
265 coord_t coord = c;
266 if (board_at(board, coord) == S_OFFBOARD)
267 continue;
268 foreach_neighbor(board, c, {
269 inc_neighbor_count_at(board, coord, board_at(board, c));
270 } );
271 } foreach_point_end;
273 /* All positions are free! Except the margin. */
274 for (i = board_size(board); i < (board_size(board) - 1) * board_size(board); i++)
275 if (i % board_size(board) != 0 && i % board_size(board) != board_size(board) - 1)
276 board->f[board->flen++] = i;
278 /* Initialize zobrist hashtable. */
279 foreach_point(board) {
280 int max = (sizeof(hash_t) << history_hash_bits);
281 /* fast_random() is 16-bit only */
282 board->h[c * 2] = ((hash_t) fast_random(max))
283 | ((hash_t) fast_random(max) << 16)
284 | ((hash_t) fast_random(max) << 32)
285 | ((hash_t) fast_random(max) << 48);
286 if (!board->h[c * 2])
287 /* Would be kinda "oops". */
288 board->h[c * 2] = 1;
289 /* And once again for white */
290 board->h[c * 2 + 1] = ((hash_t) fast_random(max))
291 | ((hash_t) fast_random(max) << 16)
292 | ((hash_t) fast_random(max) << 32)
293 | ((hash_t) fast_random(max) << 48);
294 if (!board->h[c * 2 + 1])
295 board->h[c * 2 + 1] = 1;
296 } foreach_point_end;
298 #ifdef BOARD_SPATHASH
299 /* Initialize spatial hashes. */
300 foreach_point(board) {
301 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
302 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
303 ptcoords_at(x, y, c, board, j);
304 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
305 pthashes[0][j][board_at(board, c)];
306 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
307 pthashes[0][j][stone_other(board_at(board, c))];
310 } foreach_point_end;
311 #endif
312 #ifdef BOARD_PAT3
313 /* Initialize 3x3 pattern codes. */
314 foreach_point(board) {
315 if (board_at(board, c) == S_NONE)
316 board->pat3[c] = pattern3_hash(board, c);
317 } foreach_point_end;
318 #endif
319 #ifdef BOARD_TRAITS
320 /* Initialize traits. */
321 foreach_point(board) {
322 trait_at(board, c, S_BLACK).cap = 0;
323 trait_at(board, c, S_BLACK).safe = true;
324 trait_at(board, c, S_WHITE).cap = 0;
325 trait_at(board, c, S_WHITE).safe = true;
326 } foreach_point_end;
327 #endif
328 #ifdef BOARD_GAMMA
329 board->prob[0].n = board->prob[1].n = board_size2(board);
330 foreach_point(board) {
331 probdist_set(&board->prob[0], c, (board_at(board, c) == S_NONE) * 1.0f);
332 probdist_set(&board->prob[1], c, (board_at(board, c) == S_NONE) * 1.0f);
333 } foreach_point_end;
334 #endif
337 static char *
338 board_print_top(struct board *board, char *s, char *end, int c)
340 for (int i = 0; i < c; i++) {
341 char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
342 s += snprintf(s, end - s, " ");
343 for (int x = 1; x < board_size(board) - 1; x++)
344 s += snprintf(s, end - s, "%c ", asdf[x - 1]);
345 s += snprintf(s, end -s, " ");
347 s += snprintf(s, end - s, "\n");
348 for (int i = 0; i < c; i++) {
349 s += snprintf(s, end - s, " +-");
350 for (int x = 1; x < board_size(board) - 1; x++)
351 s += snprintf(s, end - s, "--");
352 s += snprintf(s, end - s, "+");
354 s += snprintf(s, end - s, "\n");
355 return s;
358 static char *
359 board_print_bottom(struct board *board, char *s, char *end, int c)
361 for (int i = 0; i < c; i++) {
362 s += snprintf(s, end - s, " +-");
363 for (int x = 1; x < board_size(board) - 1; x++)
364 s += snprintf(s, end - s, "--");
365 s += snprintf(s, end - s, "+");
367 s += snprintf(s, end - s, "\n");
368 return s;
371 static char *
372 board_print_row(struct board *board, int y, char *s, char *end, board_cprint cprint)
374 s += snprintf(s, end - s, " %2d | ", y);
375 for (int x = 1; x < board_size(board) - 1; x++) {
376 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
377 s += snprintf(s, end - s, "%c)", stone2char(board_atxy(board, x, y)));
378 else
379 s += snprintf(s, end - s, "%c ", stone2char(board_atxy(board, x, y)));
381 s += snprintf(s, end - s, "|");
382 if (cprint) {
383 s += snprintf(s, end - s, " %2d | ", y);
384 for (int x = 1; x < board_size(board) - 1; x++) {
385 s = cprint(board, coord_xy(board, x, y), s, end);
387 s += snprintf(s, end - s, "|");
389 s += snprintf(s, end - s, "\n");
390 return s;
393 void
394 board_print_custom(struct board *board, FILE *f, board_cprint cprint)
396 char buf[10240];
397 char *s = buf;
398 char *end = buf + sizeof(buf);
399 s += snprintf(s, end - s, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
400 board->moves, board->komi, board->handicap,
401 board->captures[S_BLACK], board->captures[S_WHITE]);
402 s = board_print_top(board, s, end, 1 + !!cprint);
403 for (int y = board_size(board) - 2; y >= 1; y--)
404 s = board_print_row(board, y, s, end, cprint);
405 board_print_bottom(board, s, end, 1 + !!cprint);
406 fprintf(f, "%s\n", buf);
409 static char *
410 cprint_group(struct board *board, coord_t c, char *s, char *end)
412 s += snprintf(s, end - s, "%d ", group_base(group_at(board, c)));
413 return s;
416 void
417 board_print(struct board *board, FILE *f)
419 board_print_custom(board, f, DEBUGL(6) ? cprint_group : NULL);
422 void
423 board_gamma_set(struct board *b, struct features_gamma *gamma, bool precise_selfatari)
425 #ifdef BOARD_GAMMA
426 b->gamma = gamma;
427 b->precise_selfatari = precise_selfatari;
428 for (int i = 0; i < b->flen; i++) {
429 board_trait_recompute(b, b->f[i]);
431 #endif
435 /* Update the probability distribution we maintain incrementally. */
436 void
437 board_gamma_update(struct board *board, coord_t coord, enum stone color)
439 #ifdef BOARD_GAMMA
440 if (!board->gamma)
441 return;
443 /* Punch out invalid moves and moves filling our own eyes. */
444 if (board_at(board, coord) != S_NONE
445 || (board_is_eyelike(board, coord, stone_other(color))
446 && !trait_at(board, coord, color).cap)
447 || (board_is_one_point_eye(board, coord, color))) {
448 probdist_set(&board->prob[color - 1], coord, 0);
449 return;
452 int pat = board->pat3[coord];
453 if (color == S_WHITE) {
454 /* We work with the pattern3s as black-to-play. */
455 pat = pattern3_reverse(pat);
458 /* We just quickly replicate the general pattern matcher stuff
459 * here in the most bare-bone way. */
460 double value = board->gamma->gamma[FEAT_PATTERN3][pat];
461 if (trait_at(board, coord, color).cap)
462 value *= board->gamma->gamma[FEAT_CAPTURE][0];
463 if (trait_at(board, coord, stone_other(color)).cap
464 && trait_at(board, coord, color).safe)
465 value *= board->gamma->gamma[FEAT_AESCAPE][0];
466 if (!trait_at(board, coord, color).safe)
467 value *= board->gamma->gamma[FEAT_SELFATARI][1 + board->precise_selfatari];
468 probdist_set(&board->prob[color - 1], coord, value);
469 #endif
472 #ifdef BOARD_TRAITS
473 static bool
474 board_trait_safe(struct board *board, coord_t coord, enum stone color)
476 /* sic! */
477 if (board->precise_selfatari)
478 return is_bad_selfatari(board, color, coord);
479 else
480 return board_safe_to_play(board, coord, color);
483 static void
484 board_trait_recompute(struct board *board, coord_t coord)
486 trait_at(board, coord, S_BLACK).safe = board_trait_safe(board, coord, S_BLACK);;
487 trait_at(board, coord, S_WHITE).safe = board_trait_safe(board, coord, S_WHITE);
488 if (DEBUGL(8)) {
489 fprintf(stderr, "traits[%s:%s lib=%d] (black cap=%d safe=%d) (white cap=%d safe=%d)\n",
490 coord2sstr(coord, board), stone2str(board_at(board, coord)), immediate_liberty_count(board, coord),
491 trait_at(board, coord, S_BLACK).cap, trait_at(board, coord, S_BLACK).safe,
492 trait_at(board, coord, S_WHITE).cap, trait_at(board, coord, S_WHITE).safe);
494 board_gamma_update(board, coord, S_BLACK);
495 board_gamma_update(board, coord, S_WHITE);
497 #endif
499 /* Recompute traits for dirty points that we have previously touched
500 * somehow (libs of their neighbors changed or so). */
501 static void
502 board_traits_recompute(struct board *board)
504 #ifdef BOARD_TRAITS
505 for (int i = 0; i < board->tqlen; i++) {
506 coord_t coord = board->tq[i];
507 if (!trait_at(board, coord, S_BLACK).dirty) continue;
508 if (board_at(board, coord) != S_NONE) continue;
509 board_trait_recompute(board, coord);
510 trait_at(board, coord, S_BLACK).dirty = false;
512 board->tqlen = 0;
513 #endif
516 /* Queue traits of given point for recomputing. */
517 static void
518 board_trait_queue(struct board *board, coord_t coord)
520 #ifdef BOARD_TRAITS
521 board->tq[board->tqlen++] = coord;
522 trait_at(board, coord, S_BLACK).dirty = true;
523 #endif
527 /* Update board hash with given coordinate. */
528 static void profiling_noinline
529 board_hash_update(struct board *board, coord_t coord, enum stone color)
531 board->hash ^= hash_at(board, coord, color);
532 if (DEBUGL(8))
533 fprintf(stderr, "board_hash_update(%d,%d,%d) ^ %"PRIhash" -> %"PRIhash"\n", color, coord_x(coord, board), coord_y(coord, board), hash_at(board, coord, color), board->hash);
535 #ifdef BOARD_SPATHASH
536 /* Gridcular metric is reflective, so we update all hashes
537 * of appropriate ditance in OUR circle. */
538 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
539 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
540 ptcoords_at(x, y, coord, board, j);
541 /* We either changed from S_NONE to color
542 * or vice versa; doesn't matter. */
543 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
544 pthashes[0][j][color] ^ pthashes[0][j][S_NONE];
545 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
546 pthashes[0][j][stone_other(color)] ^ pthashes[0][j][S_NONE];
549 #endif
551 #if defined(BOARD_PAT3)
552 /* @color is not what we need in case of capture. */
553 enum stone new_color = board_at(board, coord);
554 if (new_color == S_NONE)
555 board->pat3[coord] = pattern3_hash(board, coord);
556 foreach_8neighbor(board, coord) { // internally, the loop uses fn__i=[0..7]
557 if (board_at(board, c) != S_NONE)
558 continue;
559 board->pat3[c] &= ~(3 << (fn__i*2));
560 board->pat3[c] |= new_color << (fn__i*2);
561 #if 0
562 if (board_at(board, c) != S_OFFBOARD && pattern3_hash(board, c) != board->pat3[c]) {
563 board_print(board, stderr);
564 fprintf(stderr, "%s->%s %x != %x (%d-%d:%d)\n", coord2sstr(coord, board), coord2sstr(c, board), pattern3_hash(board, c), board->pat3[c], coord, c, fn__i);
565 assert(0);
567 #endif
568 board_gamma_update(board, c, S_BLACK);
569 board_gamma_update(board, c, S_WHITE);
570 } foreach_8neighbor_end;
571 #endif
574 /* Commit current board hash to history. */
575 static void profiling_noinline
576 board_hash_commit(struct board *board)
578 if (DEBUGL(8))
579 fprintf(stderr, "board_hash_commit %"PRIhash"\n", board->hash);
580 if (likely(board->history_hash[board->hash & history_hash_mask]) == 0) {
581 board->history_hash[board->hash & history_hash_mask] = board->hash;
582 } else {
583 hash_t i = board->hash;
584 while (board->history_hash[i & history_hash_mask]) {
585 if (board->history_hash[i & history_hash_mask] == board->hash) {
586 if (DEBUGL(5))
587 fprintf(stderr, "SUPERKO VIOLATION noted at %d,%d\n",
588 coord_x(board->last_move.coord, board), coord_y(board->last_move.coord, board));
589 board->superko_violation = true;
590 return;
592 i = history_hash_next(i);
594 board->history_hash[i & history_hash_mask] = board->hash;
599 void
600 board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c)
602 if (likely(symmetry->type == SYM_NONE)) {
603 /* Fully degenerated already. We do not support detection
604 * of restoring of symmetry, assuming that this is too rare
605 * a case to handle. */
606 return;
609 int x = coord_x(c, b), y = coord_y(c, b), t = board_size(b) / 2;
610 int dx = board_size(b) - 1 - x; /* for SYM_DOWN */
611 if (DEBUGL(6)) {
612 fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
613 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
614 symmetry->d, symmetry->type, x, y);
617 switch (symmetry->type) {
618 case SYM_FULL:
619 if (x == t && y == t) {
620 /* Tengen keeps full symmetry. */
621 return;
623 /* New symmetry now? */
624 if (x == y) {
625 symmetry->type = SYM_DIAG_UP;
626 symmetry->x1 = symmetry->y1 = 1;
627 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
628 symmetry->d = 1;
629 } else if (dx == y) {
630 symmetry->type = SYM_DIAG_DOWN;
631 symmetry->x1 = symmetry->y1 = 1;
632 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
633 symmetry->d = 1;
634 } else if (x == t) {
635 symmetry->type = SYM_HORIZ;
636 symmetry->y1 = 1;
637 symmetry->y2 = board_size(b) - 1;
638 symmetry->d = 0;
639 } else if (y == t) {
640 symmetry->type = SYM_VERT;
641 symmetry->x1 = 1;
642 symmetry->x2 = board_size(b) - 1;
643 symmetry->d = 0;
644 } else {
645 break_symmetry:
646 symmetry->type = SYM_NONE;
647 symmetry->x1 = symmetry->y1 = 1;
648 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
649 symmetry->d = 0;
651 break;
652 case SYM_DIAG_UP:
653 if (x == y)
654 return;
655 goto break_symmetry;
656 case SYM_DIAG_DOWN:
657 if (dx == y)
658 return;
659 goto break_symmetry;
660 case SYM_HORIZ:
661 if (x == t)
662 return;
663 goto break_symmetry;
664 case SYM_VERT:
665 if (y == t)
666 return;
667 goto break_symmetry;
668 case SYM_NONE:
669 assert(0);
670 break;
673 if (DEBUGL(6)) {
674 fprintf(stderr, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
675 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
676 symmetry->d, symmetry->type);
678 /* Whew. */
682 void
683 board_handicap_stone(struct board *board, int x, int y, FILE *f)
685 struct move m;
686 m.color = S_BLACK; m.coord = coord_xy(board, x, y);
688 board_play(board, &m);
689 /* Simulate white passing; otherwise, UCT search can get confused since
690 * tree depth parity won't match the color to move. */
691 board->moves++;
693 char *str = coord2str(m.coord, board);
694 if (DEBUGL(1))
695 fprintf(stderr, "choosing handicap %s (%d,%d)\n", str, x, y);
696 if (f) fprintf(f, "%s ", str);
697 free(str);
700 void
701 board_handicap(struct board *board, int stones, FILE *f)
703 int margin = 3 + (board_size(board) >= 13);
704 int min = margin;
705 int mid = board_size(board) / 2;
706 int max = board_size(board) - 1 - margin;
707 const int places[][2] = {
708 { min, min }, { max, max }, { max, min }, { min, max },
709 { min, mid }, { max, mid },
710 { mid, min }, { mid, max },
711 { mid, mid },
714 board->handicap = stones;
716 if (stones == 5 || stones == 7) {
717 board_handicap_stone(board, mid, mid, f);
718 stones--;
721 int i;
722 for (i = 0; i < stones; i++)
723 board_handicap_stone(board, places[i][0], places[i][1], f);
727 static void __attribute__((noinline))
728 check_libs_consistency(struct board *board, group_t g)
730 #ifdef DEBUG
731 if (!g) return;
732 struct group *gi = &board_group_info(board, g);
733 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
734 if (gi->lib[i] && board_at(board, gi->lib[i]) != S_NONE) {
735 fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(group_base(g), board));
736 assert(0);
738 #endif
741 static void
742 board_capturable_add(struct board *board, group_t group, coord_t lib)
744 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
745 #ifdef BOARD_TRAITS
746 /* Increase capturable count trait of my last lib. */
747 enum stone capturing_color = stone_other(board_at(board, group));
748 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
749 foreach_neighbor(board, lib, {
750 if (DEBUGL(8) && group_at(board, c) == group)
751 fprintf(stderr, "%s[%d] %s cap bump bc of %s(%d) member %s\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap, stone2str(capturing_color), coord2sstr(group, board), board_group_info(board, group).libs, coord2sstr(c, board));
752 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group);
754 board_trait_queue(board, lib);
755 #endif
757 #ifdef WANT_BOARD_C
758 /* Update the list of capturable groups. */
759 assert(group);
760 assert(board->clen < board_size2(board));
761 board->c[board->clen++] = group;
762 #endif
764 static void
765 board_capturable_rm(struct board *board, group_t group, coord_t lib)
767 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
768 #ifdef BOARD_TRAITS
769 /* Decrease capturable count trait of my previously-last lib. */
770 enum stone capturing_color = stone_other(board_at(board, group));
771 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
772 foreach_neighbor(board, lib, {
773 if (DEBUGL(8) && group_at(board, c) == group)
774 fprintf(stderr, "%s[%d] cap dump bc of %s(%d) member %s\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap, coord2sstr(group, board), board_group_info(board, group).libs, coord2sstr(c, board));
775 trait_at(board, lib, capturing_color).cap -= (group_at(board, c) == group);
777 board_trait_queue(board, lib);
778 #endif
780 #ifdef WANT_BOARD_C
781 /* Update the list of capturable groups. */
782 for (int i = 0; i < board->clen; i++) {
783 if (unlikely(board->c[i] == group)) {
784 board->c[i] = board->c[--board->clen];
785 return;
788 fprintf(stderr, "rm of bad group %d\n", group_base(group));
789 assert(0);
790 #endif
793 static void
794 board_atariable_add(struct board *board, group_t group, coord_t lib1, coord_t lib2)
796 #ifdef BOARD_TRAITS
797 board_trait_queue(board, lib1);
798 board_trait_queue(board, lib2);
799 #endif
801 static void
802 board_atariable_rm(struct board *board, group_t group, coord_t lib1, coord_t lib2)
804 #ifdef BOARD_TRAITS
805 board_trait_queue(board, lib1);
806 board_trait_queue(board, lib2);
807 #endif
810 static void
811 board_group_addlib(struct board *board, group_t group, coord_t coord)
813 if (DEBUGL(7)) {
814 fprintf(stderr, "Group %d[%s] %d: Adding liberty %s\n",
815 group_base(group), coord2sstr(group_base(group), board),
816 board_group_info(board, group).libs, coord2sstr(coord, board));
819 check_libs_consistency(board, group);
821 struct group *gi = &board_group_info(board, group);
822 if (gi->libs < GROUP_KEEP_LIBS) {
823 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
824 #if 0
825 /* Seems extra branch just slows it down */
826 if (!gi->lib[i])
827 break;
828 #endif
829 if (unlikely(gi->lib[i] == coord))
830 return;
832 if (gi->libs == 0) {
833 board_capturable_add(board, group, coord);
834 } else if (gi->libs == 1) {
835 board_capturable_rm(board, group, gi->lib[0]);
836 board_atariable_add(board, group, gi->lib[0], coord);
837 } else if (gi->libs == 2) {
838 board_atariable_rm(board, group, gi->lib[0], gi->lib[1]);
840 gi->lib[gi->libs++] = coord;
843 check_libs_consistency(board, group);
846 static void
847 board_group_find_extra_libs(struct board *board, group_t group, struct group *gi, coord_t avoid)
849 /* Add extra liberty from the board to our liberty list. */
850 unsigned char watermark[board_size2(board) / 8];
851 memset(watermark, 0, sizeof(watermark));
852 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
853 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
855 for (int i = 0; i < GROUP_KEEP_LIBS - 1; i++)
856 watermark_set(gi->lib[i]);
857 watermark_set(avoid);
859 foreach_in_group(board, group) {
860 coord_t coord2 = c;
861 foreach_neighbor(board, coord2, {
862 if (board_at(board, c) + watermark_get(c) != S_NONE)
863 continue;
864 watermark_set(c);
865 gi->lib[gi->libs++] = c;
866 if (unlikely(gi->libs >= GROUP_KEEP_LIBS))
867 return;
868 } );
869 } foreach_in_group_end;
870 #undef watermark_get
871 #undef watermark_set
874 static void
875 board_group_rmlib(struct board *board, group_t group, coord_t coord)
877 if (DEBUGL(7)) {
878 fprintf(stderr, "Group %d[%s] %d: Removing liberty %s\n",
879 group_base(group), coord2sstr(group_base(group), board),
880 board_group_info(board, group).libs, coord2sstr(coord, board));
883 struct group *gi = &board_group_info(board, group);
884 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
885 #if 0
886 /* Seems extra branch just slows it down */
887 if (!gi->lib[i])
888 break;
889 #endif
890 if (likely(gi->lib[i] != coord))
891 continue;
893 coord_t lib = gi->lib[i] = gi->lib[--gi->libs];
894 gi->lib[gi->libs] = 0;
896 check_libs_consistency(board, group);
898 /* Postpone refilling lib[] until we need to. */
899 assert(GROUP_REFILL_LIBS > 1);
900 if (gi->libs > GROUP_REFILL_LIBS)
901 return;
902 if (gi->libs == GROUP_REFILL_LIBS)
903 board_group_find_extra_libs(board, group, gi, coord);
905 if (gi->libs == 2) {
906 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
907 } else if (gi->libs == 1) {
908 board_capturable_add(board, group, gi->lib[0]);
909 board_atariable_rm(board, group, gi->lib[0], lib);
910 } else if (gi->libs == 0)
911 board_capturable_rm(board, group, lib);
912 return;
915 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
916 * can call this multiple times per coord. */
917 check_libs_consistency(board, group);
918 return;
922 /* This is a low-level routine that doesn't maintain consistency
923 * of all the board data structures. */
924 static void
925 board_remove_stone(struct board *board, group_t group, coord_t c)
927 enum stone color = board_at(board, c);
928 board_at(board, c) = S_NONE;
929 group_at(board, c) = 0;
930 board_hash_update(board, c, color);
931 #ifdef BOARD_TRAITS
932 /* We mark as cannot-capture now. If this is a ko/snapback,
933 * we will get incremented later in board_group_addlib(). */
934 trait_at(board, c, S_BLACK).cap = 0;
935 trait_at(board, c, S_WHITE).cap = 0;
936 board_trait_queue(board, c);
937 #endif
939 /* Increase liberties of surrounding groups */
940 coord_t coord = c;
941 foreach_neighbor(board, coord, {
942 dec_neighbor_count_at(board, c, color);
943 board_trait_queue(board, c);
944 group_t g = group_at(board, c);
945 if (g && g != group)
946 board_group_addlib(board, g, coord);
949 if (DEBUGL(6))
950 fprintf(stderr, "pushing free move [%d]: %d,%d\n", board->flen, coord_x(c, board), coord_y(c, board));
951 board->f[board->flen++] = c;
954 static int profiling_noinline
955 board_group_capture(struct board *board, group_t group)
957 int stones = 0;
959 foreach_in_group(board, group) {
960 board->captures[stone_other(board_at(board, c))]++;
961 board_remove_stone(board, group, c);
962 stones++;
963 } foreach_in_group_end;
965 struct group *gi = &board_group_info(board, group);
966 if (gi->libs == 2)
967 board_atariable_rm(board, group, gi->lib[0], gi->lib[1]);
968 else if (gi->libs == 1)
969 board_capturable_rm(board, group, gi->lib[0]);
970 memset(gi, 0, sizeof(*gi));
972 return stones;
976 static void profiling_noinline
977 add_to_group(struct board *board, group_t group, coord_t prevstone, coord_t coord)
979 group_at(board, coord) = group;
980 groupnext_at(board, coord) = groupnext_at(board, prevstone);
981 groupnext_at(board, prevstone) = coord;
983 #ifdef BOARD_TRAITS
984 if (board_group_info(board, group).libs == 1) {
985 /* Our group is temporarily in atari; make sure the capturable
986 * counts also correspond to the newly added stone before we
987 * start adding liberties again so bump-dump ops match. */
988 enum stone capturing_color = stone_other(board_at(board, group));
989 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
990 coord_t lib = board_group_info(board, group).lib[0];
991 if (coord_is_adjecent(lib, coord, board)) {
992 if (DEBUGL(8)) fprintf(stderr, "add_to_group %s: %s[%d] bump\n", coord2sstr(group, board), coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
993 trait_at(board, lib, capturing_color).cap++;
994 board_trait_queue(board, lib);
997 #endif
999 foreach_neighbor(board, coord, {
1000 if (board_at(board, c) == S_NONE)
1001 board_group_addlib(board, group, c);
1004 if (DEBUGL(8))
1005 fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1006 coord_x(prevstone, board), coord_y(prevstone, board),
1007 coord_x(coord, board), coord_y(coord, board),
1008 groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board),
1009 group_base(group));
1012 static void profiling_noinline
1013 merge_groups(struct board *board, group_t group_to, group_t group_from)
1015 if (DEBUGL(7))
1016 fprintf(stderr, "board_play_raw: merging groups %d -> %d\n",
1017 group_base(group_from), group_base(group_to));
1018 struct group *gi_from = &board_group_info(board, group_from);
1019 struct group *gi_to = &board_group_info(board, group_to);
1021 /* We do this early before the group info is rewritten. */
1022 if (gi_from->libs == 2)
1023 board_atariable_rm(board, group_from, gi_from->lib[0], gi_from->lib[1]);
1024 else if (gi_from->libs == 1)
1025 board_capturable_rm(board, group_from, gi_from->lib[0]);
1027 if (DEBUGL(7))
1028 fprintf(stderr,"---- (froml %d, tol %d)\n", gi_from->libs, gi_to->libs);
1030 if (gi_to->libs < GROUP_KEEP_LIBS) {
1031 for (int i = 0; i < gi_from->libs; i++) {
1032 for (int j = 0; j < gi_to->libs; j++)
1033 if (gi_to->lib[j] == gi_from->lib[i])
1034 goto next_from_lib;
1035 if (gi_to->libs == 0) {
1036 board_capturable_add(board, group_to, gi_from->lib[i]);
1037 } else if (gi_to->libs == 1) {
1038 board_capturable_rm(board, group_to, gi_to->lib[0]);
1039 board_atariable_add(board, group_to, gi_to->lib[0], gi_from->lib[i]);
1040 } else if (gi_to->libs == 2) {
1041 board_atariable_rm(board, group_to, gi_to->lib[0], gi_to->lib[1]);
1043 gi_to->lib[gi_to->libs++] = gi_from->lib[i];
1044 if (gi_to->libs >= GROUP_KEEP_LIBS)
1045 break;
1046 next_from_lib:;
1050 #ifdef BOARD_TRAITS
1051 if (board_group_info(board, group_to).libs == 1) {
1052 /* Our group is currently in atari; make sure we properly
1053 * count in even the neighbors from the other group in the
1054 * capturable counter. */
1055 enum stone capturing_color = stone_other(board_at(board, group_to));
1056 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
1057 coord_t lib = board_group_info(board, group_to).lib[0];
1058 foreach_neighbor(board, lib, {
1059 if (DEBUGL(8) && group_at(board, c) == group_from) fprintf(stderr, "%s[%d] cap bump\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
1060 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group_from);
1062 board_trait_queue(board, lib);
1064 #endif
1066 coord_t last_in_group;
1067 foreach_in_group(board, group_from) {
1068 last_in_group = c;
1069 group_at(board, c) = group_to;
1070 } foreach_in_group_end;
1071 groupnext_at(board, last_in_group) = groupnext_at(board, group_base(group_to));
1072 groupnext_at(board, group_base(group_to)) = group_base(group_from);
1073 memset(gi_from, 0, sizeof(struct group));
1075 if (DEBUGL(7))
1076 fprintf(stderr, "board_play_raw: merged group: %d\n",
1077 group_base(group_to));
1080 static group_t profiling_noinline
1081 new_group(struct board *board, coord_t coord)
1083 group_t group = coord;
1084 struct group *gi = &board_group_info(board, group);
1085 foreach_neighbor(board, coord, {
1086 if (board_at(board, c) == S_NONE)
1087 /* board_group_addlib is ridiculously expensive for us */
1088 #if GROUP_KEEP_LIBS < 4
1089 if (gi->libs < GROUP_KEEP_LIBS)
1090 #endif
1091 gi->lib[gi->libs++] = c;
1094 group_at(board, coord) = group;
1095 groupnext_at(board, coord) = 0;
1097 if (gi->libs == 2)
1098 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
1099 else if (gi->libs == 1)
1100 board_capturable_add(board, group, gi->lib[0]);
1101 check_libs_consistency(board, group);
1103 if (DEBUGL(8))
1104 fprintf(stderr, "new_group: added %d,%d to group %d\n",
1105 coord_x(coord, board), coord_y(coord, board),
1106 group_base(group));
1108 return group;
1111 static inline group_t
1112 play_one_neighbor(struct board *board,
1113 coord_t coord, enum stone color, enum stone other_color,
1114 coord_t c, group_t group)
1116 enum stone ncolor = board_at(board, c);
1117 group_t ngroup = group_at(board, c);
1119 inc_neighbor_count_at(board, c, color);
1120 /* We can be S_NONE, in that case we need to update the safety
1121 * trait since we might be left with only one liberty. */
1122 board_trait_queue(board, c);
1124 if (!ngroup)
1125 return group;
1127 board_group_rmlib(board, ngroup, coord);
1128 if (DEBUGL(7))
1129 fprintf(stderr, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1130 group_base(ngroup), ncolor, color, other_color);
1132 if (ncolor == color && ngroup != group) {
1133 if (!group) {
1134 group = ngroup;
1135 add_to_group(board, group, c, coord);
1136 } else {
1137 merge_groups(board, group, ngroup);
1139 } else if (ncolor == other_color) {
1140 if (DEBUGL(8)) {
1141 struct group *gi = &board_group_info(board, ngroup);
1142 fprintf(stderr, "testing captured group %d[%s]: ", group_base(ngroup), coord2sstr(group_base(ngroup), board));
1143 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
1144 fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board));
1145 fprintf(stderr, "\n");
1147 if (unlikely(board_group_captured(board, ngroup)))
1148 board_group_capture(board, ngroup);
1150 return group;
1153 /* We played on a place with at least one liberty. We will become a member of
1154 * some group for sure. */
1155 static group_t profiling_noinline
1156 board_play_outside(struct board *board, struct move *m, int f)
1158 coord_t coord = m->coord;
1159 enum stone color = m->color;
1160 enum stone other_color = stone_other(color);
1161 group_t group = 0;
1163 board->f[f] = board->f[--board->flen];
1164 if (DEBUGL(6))
1165 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1167 #if defined(BOARD_TRAITS) && defined(DEBUG)
1168 /* Sanity check that cap matches reality. */
1170 int a = 0;
1171 foreach_neighbor(board, coord, {
1172 group_t g = group_at(board, c);
1173 a += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1);
1175 assert(a == trait_at(board, coord, color).cap);
1176 assert(board_trait_safe(board, coord, color) == trait_at(board, coord, color).safe);
1178 #endif
1179 foreach_neighbor(board, coord, {
1180 group = play_one_neighbor(board, coord, color, other_color, c, group);
1183 board_at(board, coord) = color;
1184 if (unlikely(!group))
1185 group = new_group(board, coord);
1186 board_gamma_update(board, coord, S_BLACK);
1187 board_gamma_update(board, coord, S_WHITE);
1189 board->last_move2 = board->last_move;
1190 board->last_move = *m;
1191 board->moves++;
1192 board_hash_update(board, coord, color);
1193 board_symmetry_update(board, &board->symmetry, coord);
1194 struct move ko = { pass, S_NONE };
1195 board->ko = ko;
1197 return group;
1200 /* We played in an eye-like shape. Either we capture at least one of the eye
1201 * sides in the process of playing, or return -1. */
1202 static int profiling_noinline
1203 board_play_in_eye(struct board *board, struct move *m, int f)
1205 coord_t coord = m->coord;
1206 enum stone color = m->color;
1207 /* Check ko: Capture at a position of ko capture one move ago */
1208 if (unlikely(color == board->ko.color && coord == board->ko.coord)) {
1209 if (DEBUGL(5))
1210 fprintf(stderr, "board_check: ko at %d,%d color %d\n", coord_x(coord, board), coord_y(coord, board), color);
1211 return -1;
1212 } else if (DEBUGL(6)) {
1213 fprintf(stderr, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1214 color, coord_x(coord, board), coord_y(coord, board),
1215 board->ko.color, coord_x(board->ko.coord, board), coord_y(board->ko.coord, board));
1218 struct move ko = { pass, S_NONE };
1220 int captured_groups = 0;
1222 foreach_neighbor(board, coord, {
1223 group_t g = group_at(board, c);
1224 if (DEBUGL(7))
1225 fprintf(stderr, "board_check: group %d has %d libs\n",
1226 g, board_group_info(board, g).libs);
1227 captured_groups += (board_group_info(board, g).libs == 1);
1230 if (likely(captured_groups == 0)) {
1231 if (DEBUGL(5)) {
1232 if (DEBUGL(6))
1233 board_print(board, stderr);
1234 fprintf(stderr, "board_check: one-stone suicide\n");
1237 return -1;
1239 #ifdef BOARD_TRAITS
1240 /* We _will_ for sure capture something. */
1241 assert(trait_at(board, coord, color).cap > 0);
1242 assert(trait_at(board, coord, color).safe == board_trait_safe(board, coord, color));
1243 #endif
1245 board->f[f] = board->f[--board->flen];
1246 if (DEBUGL(6))
1247 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1249 foreach_neighbor(board, coord, {
1250 inc_neighbor_count_at(board, c, color);
1251 /* Originally, this could not have changed any trait
1252 * since no neighbors were S_NONE, however by now some
1253 * of them might be removed from the board. */
1254 board_trait_queue(board, c);
1256 group_t group = group_at(board, c);
1257 if (!group)
1258 continue;
1260 board_group_rmlib(board, group, coord);
1261 if (DEBUGL(7))
1262 fprintf(stderr, "board_play_raw: reducing libs for group %d\n",
1263 group_base(group));
1265 if (board_group_captured(board, group)) {
1266 if (board_group_capture(board, group) == 1) {
1267 /* If we captured multiple groups at once,
1268 * we can't be fighting ko so we don't need
1269 * to check for that. */
1270 ko.color = stone_other(color);
1271 ko.coord = c;
1272 board->last_ko = ko;
1273 board->last_ko_age = board->moves;
1274 if (DEBUGL(5))
1275 fprintf(stderr, "guarding ko at %d,%s\n", ko.color, coord2sstr(ko.coord, board));
1280 board_at(board, coord) = color;
1281 group_t group = new_group(board, coord);
1282 board_gamma_update(board, coord, S_BLACK);
1283 board_gamma_update(board, coord, S_WHITE);
1285 board->last_move2 = board->last_move;
1286 board->last_move = *m;
1287 board->moves++;
1288 board_hash_update(board, coord, color);
1289 board_hash_commit(board);
1290 board_traits_recompute(board);
1291 board_symmetry_update(board, &board->symmetry, coord);
1292 board->ko = ko;
1294 return !!group;
1297 static int __attribute__((flatten))
1298 board_play_f(struct board *board, struct move *m, int f)
1300 if (DEBUGL(7)) {
1301 fprintf(stderr, "board_play(): ---- Playing %d,%d\n", coord_x(m->coord, board), coord_y(m->coord, board));
1303 if (likely(!board_is_eyelike(board, m->coord, stone_other(m->color)))) {
1304 /* NOT playing in an eye. Thus this move has to succeed. (This
1305 * is thanks to New Zealand rules. Otherwise, multi-stone
1306 * suicide might fail.) */
1307 group_t group = board_play_outside(board, m, f);
1308 if (unlikely(board_group_captured(board, group))) {
1309 board_group_capture(board, group);
1311 board_hash_commit(board);
1312 board_traits_recompute(board);
1313 return 0;
1314 } else {
1315 return board_play_in_eye(board, m, f);
1320 board_play(struct board *board, struct move *m)
1322 if (unlikely(is_pass(m->coord) || is_resign(m->coord))) {
1323 struct move nomove = { pass, S_NONE };
1324 board->ko = nomove;
1325 board->last_move2 = board->last_move;
1326 board->last_move = *m;
1327 return 0;
1330 int f;
1331 for (f = 0; f < board->flen; f++)
1332 if (board->f[f] == m->coord)
1333 return board_play_f(board, m, f);
1335 if (DEBUGL(7))
1336 fprintf(stderr, "board_check: stone exists\n");
1337 return -1;
1341 static inline bool
1342 board_try_random_move(struct board *b, enum stone color, coord_t *coord, int f, ppr_permit permit, void *permit_data)
1344 *coord = b->f[f];
1345 struct move m = { *coord, color };
1346 if (DEBUGL(6))
1347 fprintf(stderr, "trying random move %d: %d,%d\n", f, coord_x(*coord, b), coord_y(*coord, b));
1348 return (likely(!board_is_one_point_eye(b, *coord, color)) /* bad idea to play into one, usually */
1349 && board_is_valid_move(b, &m)
1350 && (!permit || permit(permit_data, b, &m))
1351 && likely(board_play_f(b, &m, f) >= 0));
1354 void
1355 board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data)
1357 if (unlikely(b->flen == 0))
1358 goto pass;
1360 int base = fast_random(b->flen), f;
1361 for (f = base; f < b->flen; f++)
1362 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1363 return;
1364 for (f = 0; f < base; f++)
1365 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1366 return;
1368 pass:
1369 *coord = pass;
1370 struct move m = { pass, color };
1371 board_play(b, &m);
1375 bool
1376 board_is_false_eyelike(struct board *board, coord_t coord, enum stone eye_color)
1378 enum stone color_diag_libs[S_MAX] = {0, 0, 0, 0};
1380 /* XXX: We attempt false eye detection but we will yield false
1381 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1383 foreach_diag_neighbor(board, coord) {
1384 color_diag_libs[(enum stone) board_at(board, c)]++;
1385 } foreach_diag_neighbor_end;
1386 /* For false eye, we need two enemy stones diagonally in the
1387 * middle of the board, or just one enemy stone at the edge
1388 * or in the corner. */
1389 color_diag_libs[stone_other(eye_color)] += !!color_diag_libs[S_OFFBOARD];
1390 return color_diag_libs[stone_other(eye_color)] >= 2;
1393 bool
1394 board_is_one_point_eye(struct board *board, coord_t coord, enum stone eye_color)
1396 return board_is_eyelike(board, coord, eye_color)
1397 && !board_is_false_eyelike(board, coord, eye_color);
1400 enum stone
1401 board_get_one_point_eye(struct board *board, coord_t coord)
1403 if (board_is_one_point_eye(board, coord, S_WHITE))
1404 return S_WHITE;
1405 else if (board_is_one_point_eye(board, coord, S_BLACK))
1406 return S_BLACK;
1407 else
1408 return S_NONE;
1412 float
1413 board_fast_score(struct board *board)
1415 int scores[S_MAX];
1416 memset(scores, 0, sizeof(scores));
1418 foreach_point(board) {
1419 enum stone color = board_at(board, c);
1420 if (color == S_NONE)
1421 color = board_get_one_point_eye(board, c);
1422 scores[color]++;
1423 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1424 } foreach_point_end;
1426 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];
1429 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1431 /* One flood-fill iteration; returns true if next iteration
1432 * is required. */
1433 static bool
1434 board_tromp_taylor_iter(struct board *board, int *ownermap)
1436 bool needs_update = false;
1437 foreach_point(board) {
1438 /* Ignore occupied and already-dame positions. */
1439 if (board_at(board, c) != S_NONE || ownermap[c] == 3)
1440 continue;
1441 /* Count neighbors. */
1442 int nei[4] = {0};
1443 foreach_neighbor(board, c, {
1444 nei[ownermap[c]]++;
1446 /* If we have neighbors of both colors, or dame,
1447 * we are dame too. */
1448 if ((nei[1] && nei[2]) || nei[3]) {
1449 ownermap[c] = 3;
1450 /* Speed up the propagation. */
1451 foreach_neighbor(board, c, {
1452 if (board_at(board, c) == S_NONE)
1453 ownermap[c] = 3;
1455 needs_update = true;
1456 continue;
1458 /* If we have neighbors of one color, we are owned
1459 * by that color, too. */
1460 if (!ownermap[c] && (nei[1] || nei[2])) {
1461 int newowner = nei[1] ? 1 : 2;
1462 ownermap[c] = newowner;
1463 /* Speed up the propagation. */
1464 foreach_neighbor(board, c, {
1465 if (board_at(board, c) == S_NONE && !ownermap[c])
1466 ownermap[c] = newowner;
1468 needs_update = true;
1469 continue;
1471 } foreach_point_end;
1472 return needs_update;
1475 /* Tromp-Taylor Counting */
1476 float
1477 board_official_score(struct board *board, struct move_queue *q)
1480 /* A point P, not colored C, is said to reach C, if there is a path of
1481 * (vertically or horizontally) adjacent points of P's color from P to
1482 * a point of color C.
1484 * A player's score is the number of points of her color, plus the
1485 * number of empty points that reach only her color. */
1487 int ownermap[board_size2(board)];
1488 int s[4] = {0};
1489 const int o[4] = {0, 1, 2, 0};
1490 foreach_point(board) {
1491 ownermap[c] = o[board_at(board, c)];
1492 s[board_at(board, c)]++;
1493 } foreach_point_end;
1495 if (q) {
1496 /* Process dead groups. */
1497 for (unsigned int i = 0; i < q->moves; i++) {
1498 foreach_in_group(board, q->move[i]) {
1499 enum stone color = board_at(board, c);
1500 ownermap[c] = o[stone_other(color)];
1501 s[color]--; s[stone_other(color)]++;
1502 } foreach_in_group_end;
1506 /* We need to special-case empty board. */
1507 if (!s[S_BLACK] && !s[S_WHITE])
1508 return board->komi + board->handicap;
1510 while (board_tromp_taylor_iter(board, ownermap))
1511 /* Flood-fill... */;
1513 int scores[S_MAX];
1514 memset(scores, 0, sizeof(scores));
1516 foreach_point(board) {
1517 assert(board_at(board, c) == S_OFFBOARD || ownermap[c] != 0);
1518 if (ownermap[c] == 3)
1519 continue;
1520 scores[ownermap[c]]++;
1521 } foreach_point_end;
1523 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];