board_group_capture(): Remove useless atariable/capturable calls
[pachi.git] / board.c
blob80bbc1033502e8a04b7eba1281da4fad179af28a
1 #include <alloca.h>
2 #include <assert.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
8 #include "board.h"
9 #include "debug.h"
10 #include "mq.h"
11 #include "random.h"
13 #ifdef BOARD_SPATHASH
14 #include "patternsp.h"
15 #endif
16 #ifdef BOARD_PAT3
17 #include "pattern3.h"
18 #endif
19 #ifdef BOARD_TRAITS
20 static void board_trait_recompute(struct board *board, coord_t coord);
21 #include "tactics.h"
22 #endif
23 #ifdef BOARD_GAMMA
24 #include "pattern.h"
25 #endif
28 #if 0
29 #define profiling_noinline __attribute__((noinline))
30 #else
31 #define profiling_noinline
32 #endif
34 #define gi_granularity 4
35 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
38 static void
39 board_setup(struct board *b)
41 memset(b, 0, sizeof(*b));
43 struct move m = { pass, S_NONE };
44 b->last_move = b->last_move2 = b->last_ko = b->ko = m;
47 struct board *
48 board_init(void)
50 struct board *b = malloc2(sizeof(struct board));
51 board_setup(b);
53 // Default setup
54 b->size = 9 + 2;
55 board_clear(b);
57 return b;
60 static size_t
61 board_alloc(struct board *board)
63 /* We do not allocate the board structure itself but we allocate
64 * all the arrays with board contents. */
66 int bsize = board_size2(board) * sizeof(*board->b);
67 int gsize = board_size2(board) * sizeof(*board->g);
68 int fsize = board_size2(board) * sizeof(*board->f);
69 int nsize = board_size2(board) * sizeof(*board->n);
70 int psize = board_size2(board) * sizeof(*board->p);
71 int hsize = board_size2(board) * 2 * sizeof(*board->h);
72 int gisize = board_size2(board) * sizeof(*board->gi);
73 #ifdef WANT_BOARD_C
74 int csize = board_size2(board) * sizeof(*board->c);
75 #else
76 int csize = 0;
77 #endif
78 #ifdef BOARD_SPATHASH
79 int ssize = board_size2(board) * sizeof(*board->spathash);
80 #else
81 int ssize = 0;
82 #endif
83 #ifdef BOARD_PAT3
84 int p3size = board_size2(board) * sizeof(*board->pat3);
85 #else
86 int p3size = 0;
87 #endif
88 #ifdef BOARD_TRAITS
89 int tsize = board_size2(board) * sizeof(*board->t);
90 int tqsize = board_size2(board) * sizeof(*board->t);
91 #else
92 int tsize = 0;
93 int tqsize = 0;
94 #endif
95 #ifdef BOARD_GAMMA
96 int pbsize = board_size2(board) * sizeof(*board->prob[0].items);
97 int rowpbsize = board_size(board) * sizeof(*board->prob[0].rowtotals);
98 #else
99 int pbsize = 0;
100 int rowpbsize = 0;
101 #endif
102 int cdsize = board_size2(board) * sizeof(*board->coord);
104 size_t size = bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize;
105 void *x = malloc2(size);
107 /* board->b must come first */
108 board->b = x; x += bsize;
109 board->g = x; x += gsize;
110 board->f = x; x += fsize;
111 board->p = x; x += psize;
112 board->n = x; x += nsize;
113 board->h = x; x += hsize;
114 board->gi = x; x += gisize;
115 #ifdef WANT_BOARD_C
116 board->c = x; x += csize;
117 #endif
118 #ifdef BOARD_SPATHASH
119 board->spathash = x; x += ssize;
120 #endif
121 #ifdef BOARD_PAT3
122 board->pat3 = x; x += p3size;
123 #endif
124 #ifdef BOARD_TRAITS
125 board->t = x; x += tsize;
126 board->tq = x; x += tqsize;
127 #endif
128 #ifdef BOARD_GAMMA
129 board->prob[0].items = x; x += pbsize;
130 board->prob[1].items = x; x += pbsize;
131 board->prob[0].rowtotals = x; x += rowpbsize;
132 board->prob[1].rowtotals = x; x += rowpbsize;
133 #endif
134 board->coord = x; x += cdsize;
136 return size;
139 struct board *
140 board_copy(struct board *b2, struct board *b1)
142 memcpy(b2, b1, sizeof(struct board));
144 size_t size = board_alloc(b2);
145 memcpy(b2->b, b1->b, size);
147 return b2;
150 void
151 board_done_noalloc(struct board *board)
153 if (board->b) free(board->b);
156 void
157 board_done(struct board *board)
159 board_done_noalloc(board);
160 free(board);
163 void
164 board_resize(struct board *board, int size)
166 #ifdef BOARD_SIZE
167 assert(board_size(board) == size + 2);
168 #endif
169 board->size = size + 2 /* S_OFFBOARD margin */;
170 board->size2 = board_size(board) * board_size(board);
172 board->bits2 = 1;
173 while ((1 << board->bits2) < board->size2) board->bits2++;
175 if (board->b)
176 free(board->b);
178 size_t asize = board_alloc(board);
179 memset(board->b, 0, asize);
182 void
183 board_clear(struct board *board)
185 int size = board_size(board);
186 float komi = board->komi;
188 board_done_noalloc(board);
189 board_setup(board);
190 board_resize(board, size - 2 /* S_OFFBOARD margin */);
192 board->komi = komi;
194 /* Setup neighborhood iterators */
195 board->nei8[0] = -size - 1; // (-1,-1)
196 board->nei8[1] = 1;
197 board->nei8[2] = 1;
198 board->nei8[3] = size - 2; // (-1,0)
199 board->nei8[4] = 2;
200 board->nei8[5] = size - 2; // (-1,1)
201 board->nei8[6] = 1;
202 board->nei8[7] = 1;
203 board->dnei[0] = -size - 1;
204 board->dnei[1] = 2;
205 board->dnei[2] = size*2 - 2;
206 board->dnei[3] = 2;
208 /* Setup initial symmetry */
209 board->symmetry.d = 1;
210 board->symmetry.x1 = board->symmetry.y1 = board_size(board) / 2;
211 board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1;
212 board->symmetry.type = SYM_FULL;
214 /* Set up coordinate cache */
215 foreach_point(board) {
216 board->coord[c][0] = c % board_size(board);
217 board->coord[c][1] = c / board_size(board);
218 } foreach_point_end;
220 /* Draw the offboard margin */
221 int top_row = board_size2(board) - board_size(board);
222 int i;
223 for (i = 0; i < board_size(board); i++)
224 board->b[i] = board->b[top_row + i] = S_OFFBOARD;
225 for (i = 0; i <= top_row; i += board_size(board))
226 board->b[i] = board->b[board_size(board) - 1 + i] = S_OFFBOARD;
228 foreach_point(board) {
229 coord_t coord = c;
230 if (board_at(board, coord) == S_OFFBOARD)
231 continue;
232 foreach_neighbor(board, c, {
233 inc_neighbor_count_at(board, coord, board_at(board, c));
234 } );
235 } foreach_point_end;
237 /* All positions are free! Except the margin. */
238 for (i = board_size(board); i < (board_size(board) - 1) * board_size(board); i++)
239 if (i % board_size(board) != 0 && i % board_size(board) != board_size(board) - 1)
240 board->f[board->flen++] = i;
242 /* Initialize zobrist hashtable. */
243 foreach_point(board) {
244 int max = (sizeof(hash_t) << history_hash_bits);
245 /* fast_random() is 16-bit only */
246 board->h[c * 2] = ((hash_t) fast_random(max))
247 | ((hash_t) fast_random(max) << 16)
248 | ((hash_t) fast_random(max) << 32)
249 | ((hash_t) fast_random(max) << 48);
250 if (!board->h[c * 2])
251 /* Would be kinda "oops". */
252 board->h[c * 2] = 1;
253 /* And once again for white */
254 board->h[c * 2 + 1] = ((hash_t) fast_random(max))
255 | ((hash_t) fast_random(max) << 16)
256 | ((hash_t) fast_random(max) << 32)
257 | ((hash_t) fast_random(max) << 48);
258 if (!board->h[c * 2 + 1])
259 board->h[c * 2 + 1] = 1;
260 } foreach_point_end;
262 #ifdef BOARD_SPATHASH
263 /* Initialize spatial hashes. */
264 foreach_point(board) {
265 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
266 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
267 ptcoords_at(x, y, c, board, j);
268 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
269 pthashes[0][j][board_at(board, c)];
270 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
271 pthashes[0][j][stone_other(board_at(board, c))];
274 } foreach_point_end;
275 #endif
276 #ifdef BOARD_PAT3
277 /* Initialize 3x3 pattern codes. */
278 foreach_point(board) {
279 if (board_at(board, c) == S_NONE)
280 board->pat3[c] = pattern3_hash(board, c);
281 } foreach_point_end;
282 #endif
283 #ifdef BOARD_TRAITS
284 /* Initialize traits. */
285 foreach_point(board) {
286 trait_at(board, c, S_BLACK).cap = 0;
287 trait_at(board, c, S_BLACK).safe = true;
288 trait_at(board, c, S_WHITE).cap = 0;
289 trait_at(board, c, S_WHITE).safe = true;
290 } foreach_point_end;
291 #endif
292 #ifdef BOARD_GAMMA
293 board->prob[0].b = board->prob[1].b = board;
294 foreach_point(board) {
295 probdist_set(&board->prob[0], c, double_to_fixp((board_at(board, c) == S_NONE) * 1.0f));
296 probdist_set(&board->prob[1], c, double_to_fixp((board_at(board, c) == S_NONE) * 1.0f));
297 } foreach_point_end;
298 #endif
301 static char *
302 board_print_top(struct board *board, char *s, char *end, int c)
304 for (int i = 0; i < c; i++) {
305 char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
306 s += snprintf(s, end - s, " ");
307 for (int x = 1; x < board_size(board) - 1; x++)
308 s += snprintf(s, end - s, "%c ", asdf[x - 1]);
309 s += snprintf(s, end -s, " ");
311 s += snprintf(s, end - s, "\n");
312 for (int i = 0; i < c; i++) {
313 s += snprintf(s, end - s, " +-");
314 for (int x = 1; x < board_size(board) - 1; x++)
315 s += snprintf(s, end - s, "--");
316 s += snprintf(s, end - s, "+");
318 s += snprintf(s, end - s, "\n");
319 return s;
322 static char *
323 board_print_bottom(struct board *board, char *s, char *end, int c)
325 for (int i = 0; i < c; i++) {
326 s += snprintf(s, end - s, " +-");
327 for (int x = 1; x < board_size(board) - 1; x++)
328 s += snprintf(s, end - s, "--");
329 s += snprintf(s, end - s, "+");
331 s += snprintf(s, end - s, "\n");
332 return s;
335 static char *
336 board_print_row(struct board *board, int y, char *s, char *end, board_cprint cprint)
338 s += snprintf(s, end - s, " %2d | ", y);
339 for (int x = 1; x < board_size(board) - 1; x++) {
340 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
341 s += snprintf(s, end - s, "%c)", stone2char(board_atxy(board, x, y)));
342 else
343 s += snprintf(s, end - s, "%c ", stone2char(board_atxy(board, x, y)));
345 s += snprintf(s, end - s, "|");
346 if (cprint) {
347 s += snprintf(s, end - s, " %2d | ", y);
348 for (int x = 1; x < board_size(board) - 1; x++) {
349 s = cprint(board, coord_xy(board, x, y), s, end);
351 s += snprintf(s, end - s, "|");
353 s += snprintf(s, end - s, "\n");
354 return s;
357 void
358 board_print_custom(struct board *board, FILE *f, board_cprint cprint)
360 char buf[10240];
361 char *s = buf;
362 char *end = buf + sizeof(buf);
363 s += snprintf(s, end - s, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
364 board->moves, board->komi, board->handicap,
365 board->captures[S_BLACK], board->captures[S_WHITE]);
366 s = board_print_top(board, s, end, 1 + !!cprint);
367 for (int y = board_size(board) - 2; y >= 1; y--)
368 s = board_print_row(board, y, s, end, cprint);
369 board_print_bottom(board, s, end, 1 + !!cprint);
370 fprintf(f, "%s\n", buf);
373 static char *
374 cprint_group(struct board *board, coord_t c, char *s, char *end)
376 s += snprintf(s, end - s, "%d ", group_base(group_at(board, c)));
377 return s;
380 void
381 board_print(struct board *board, FILE *f)
383 board_print_custom(board, f, DEBUGL(6) ? cprint_group : NULL);
386 void
387 board_gamma_set(struct board *b, struct features_gamma *gamma, bool precise_selfatari)
389 #ifdef BOARD_GAMMA
390 b->gamma = gamma;
391 b->precise_selfatari = precise_selfatari;
392 for (int i = 0; i < b->flen; i++) {
393 board_trait_recompute(b, b->f[i]);
395 #endif
399 /* Update the probability distribution we maintain incrementally. */
400 void
401 board_gamma_update(struct board *board, coord_t coord, enum stone color)
403 #ifdef BOARD_GAMMA
404 if (!board->gamma)
405 return;
407 /* Punch out invalid moves and moves filling our own eyes. */
408 if (board_at(board, coord) != S_NONE
409 || (board_is_eyelike(board, coord, stone_other(color))
410 && !trait_at(board, coord, color).cap)
411 || (board_is_one_point_eye(board, coord, color))) {
412 probdist_set(&board->prob[color - 1], coord, 0);
413 return;
416 hash3_t pat = board->pat3[coord];
417 if (color == S_WHITE) {
418 /* We work with the pattern3s as black-to-play. */
419 pat = pattern3_reverse(pat);
422 /* We just quickly replicate the general pattern matcher stuff
423 * here in the most bare-bone way. */
424 double value = board->gamma->gamma[FEAT_PATTERN3][pat];
425 if (trait_at(board, coord, color).cap)
426 value *= board->gamma->gamma[FEAT_CAPTURE][0];
427 if (trait_at(board, coord, stone_other(color)).cap
428 && trait_at(board, coord, color).safe)
429 value *= board->gamma->gamma[FEAT_AESCAPE][0];
430 if (!trait_at(board, coord, color).safe)
431 value *= board->gamma->gamma[FEAT_SELFATARI][1 + board->precise_selfatari];
432 probdist_set(&board->prob[color - 1], coord, double_to_fixp(value));
433 #endif
436 #ifdef BOARD_TRAITS
437 static bool
438 board_trait_safe(struct board *board, coord_t coord, enum stone color)
440 if (board->precise_selfatari)
441 return !is_bad_selfatari(board, color, coord);
442 else
443 return board_safe_to_play(board, coord, color);
446 static void
447 board_trait_recompute(struct board *board, coord_t coord)
449 trait_at(board, coord, S_BLACK).safe = board_trait_safe(board, coord, S_BLACK);;
450 trait_at(board, coord, S_WHITE).safe = board_trait_safe(board, coord, S_WHITE);
451 if (DEBUGL(8)) {
452 fprintf(stderr, "traits[%s:%s lib=%d] (black cap=%d safe=%d) (white cap=%d safe=%d)\n",
453 coord2sstr(coord, board), stone2str(board_at(board, coord)), immediate_liberty_count(board, coord),
454 trait_at(board, coord, S_BLACK).cap, trait_at(board, coord, S_BLACK).safe,
455 trait_at(board, coord, S_WHITE).cap, trait_at(board, coord, S_WHITE).safe);
457 board_gamma_update(board, coord, S_BLACK);
458 board_gamma_update(board, coord, S_WHITE);
460 #endif
462 /* Recompute traits for dirty points that we have previously touched
463 * somehow (libs of their neighbors changed or so). */
464 static void
465 board_traits_recompute(struct board *board)
467 #ifdef BOARD_TRAITS
468 for (int i = 0; i < board->tqlen; i++) {
469 coord_t coord = board->tq[i];
470 trait_at(board, coord, S_BLACK).dirty = false;
471 if (board_at(board, coord) != S_NONE)
472 continue;
473 board_trait_recompute(board, coord);
475 board->tqlen = 0;
476 #endif
479 /* Queue traits of given point for recomputing. */
480 static void
481 board_trait_queue(struct board *board, coord_t coord)
483 #ifdef BOARD_TRAITS
484 if (trait_at(board, coord, S_BLACK).dirty)
485 return;
486 board->tq[board->tqlen++] = coord;
487 trait_at(board, coord, S_BLACK).dirty = true;
488 #endif
492 /* Update board hash with given coordinate. */
493 static void profiling_noinline
494 board_hash_update(struct board *board, coord_t coord, enum stone color)
496 board->hash ^= hash_at(board, coord, color);
497 if (DEBUGL(8))
498 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);
500 #ifdef BOARD_SPATHASH
501 /* Gridcular metric is reflective, so we update all hashes
502 * of appropriate ditance in OUR circle. */
503 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
504 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
505 ptcoords_at(x, y, coord, board, j);
506 /* We either changed from S_NONE to color
507 * or vice versa; doesn't matter. */
508 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
509 pthashes[0][j][color] ^ pthashes[0][j][S_NONE];
510 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
511 pthashes[0][j][stone_other(color)] ^ pthashes[0][j][S_NONE];
514 #endif
516 #if defined(BOARD_PAT3)
517 /* @color is not what we need in case of capture. */
518 enum stone new_color = board_at(board, coord);
519 if (new_color == S_NONE)
520 board->pat3[coord] = pattern3_hash(board, coord);
521 foreach_8neighbor(board, coord) { // internally, the loop uses fn__i=[0..7]
522 if (board_at(board, c) != S_NONE)
523 continue;
524 board->pat3[c] &= ~(3 << (fn__i*2));
525 board->pat3[c] |= new_color << (fn__i*2);
526 #if 0
527 if (board_at(board, c) != S_OFFBOARD && pattern3_hash(board, c) != board->pat3[c]) {
528 board_print(board, stderr);
529 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);
530 assert(0);
532 #endif
533 board_trait_queue(board, c);
534 } foreach_8neighbor_end;
535 #endif
538 /* Commit current board hash to history. */
539 static void profiling_noinline
540 board_hash_commit(struct board *board)
542 if (DEBUGL(8))
543 fprintf(stderr, "board_hash_commit %"PRIhash"\n", board->hash);
544 if (likely(board->history_hash[board->hash & history_hash_mask]) == 0) {
545 board->history_hash[board->hash & history_hash_mask] = board->hash;
546 } else {
547 hash_t i = board->hash;
548 while (board->history_hash[i & history_hash_mask]) {
549 if (board->history_hash[i & history_hash_mask] == board->hash) {
550 if (DEBUGL(5))
551 fprintf(stderr, "SUPERKO VIOLATION noted at %d,%d\n",
552 coord_x(board->last_move.coord, board), coord_y(board->last_move.coord, board));
553 board->superko_violation = true;
554 return;
556 i = history_hash_next(i);
558 board->history_hash[i & history_hash_mask] = board->hash;
563 void
564 board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c)
566 if (likely(symmetry->type == SYM_NONE)) {
567 /* Fully degenerated already. We do not support detection
568 * of restoring of symmetry, assuming that this is too rare
569 * a case to handle. */
570 return;
573 int x = coord_x(c, b), y = coord_y(c, b), t = board_size(b) / 2;
574 int dx = board_size(b) - 1 - x; /* for SYM_DOWN */
575 if (DEBUGL(6)) {
576 fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
577 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
578 symmetry->d, symmetry->type, x, y);
581 switch (symmetry->type) {
582 case SYM_FULL:
583 if (x == t && y == t) {
584 /* Tengen keeps full symmetry. */
585 return;
587 /* New symmetry now? */
588 if (x == y) {
589 symmetry->type = SYM_DIAG_UP;
590 symmetry->x1 = symmetry->y1 = 1;
591 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
592 symmetry->d = 1;
593 } else if (dx == y) {
594 symmetry->type = SYM_DIAG_DOWN;
595 symmetry->x1 = symmetry->y1 = 1;
596 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
597 symmetry->d = 1;
598 } else if (x == t) {
599 symmetry->type = SYM_HORIZ;
600 symmetry->y1 = 1;
601 symmetry->y2 = board_size(b) - 1;
602 symmetry->d = 0;
603 } else if (y == t) {
604 symmetry->type = SYM_VERT;
605 symmetry->x1 = 1;
606 symmetry->x2 = board_size(b) - 1;
607 symmetry->d = 0;
608 } else {
609 break_symmetry:
610 symmetry->type = SYM_NONE;
611 symmetry->x1 = symmetry->y1 = 1;
612 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
613 symmetry->d = 0;
615 break;
616 case SYM_DIAG_UP:
617 if (x == y)
618 return;
619 goto break_symmetry;
620 case SYM_DIAG_DOWN:
621 if (dx == y)
622 return;
623 goto break_symmetry;
624 case SYM_HORIZ:
625 if (x == t)
626 return;
627 goto break_symmetry;
628 case SYM_VERT:
629 if (y == t)
630 return;
631 goto break_symmetry;
632 case SYM_NONE:
633 assert(0);
634 break;
637 if (DEBUGL(6)) {
638 fprintf(stderr, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
639 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
640 symmetry->d, symmetry->type);
642 /* Whew. */
646 void
647 board_handicap_stone(struct board *board, int x, int y, FILE *f)
649 struct move m;
650 m.color = S_BLACK; m.coord = coord_xy(board, x, y);
652 board_play(board, &m);
653 /* Simulate white passing; otherwise, UCT search can get confused since
654 * tree depth parity won't match the color to move. */
655 board->moves++;
657 char *str = coord2str(m.coord, board);
658 if (DEBUGL(1))
659 fprintf(stderr, "choosing handicap %s (%d,%d)\n", str, x, y);
660 if (f) fprintf(f, "%s ", str);
661 free(str);
664 void
665 board_handicap(struct board *board, int stones, FILE *f)
667 int margin = 3 + (board_size(board) >= 13);
668 int min = margin;
669 int mid = board_size(board) / 2;
670 int max = board_size(board) - 1 - margin;
671 const int places[][2] = {
672 { min, min }, { max, max }, { max, min }, { min, max },
673 { min, mid }, { max, mid },
674 { mid, min }, { mid, max },
675 { mid, mid },
678 board->handicap = stones;
680 if (stones == 5 || stones == 7) {
681 board_handicap_stone(board, mid, mid, f);
682 stones--;
685 int i;
686 for (i = 0; i < stones; i++)
687 board_handicap_stone(board, places[i][0], places[i][1], f);
691 static void __attribute__((noinline))
692 check_libs_consistency(struct board *board, group_t g)
694 #ifdef DEBUG
695 if (!g) return;
696 struct group *gi = &board_group_info(board, g);
697 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
698 if (gi->lib[i] && board_at(board, gi->lib[i]) != S_NONE) {
699 fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(group_base(g), board));
700 assert(0);
702 #endif
705 static void
706 board_capturable_add(struct board *board, group_t group, coord_t lib)
708 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
709 #ifdef BOARD_TRAITS
710 /* Increase capturable count trait of my last lib. */
711 enum stone capturing_color = stone_other(board_at(board, group));
712 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
713 foreach_neighbor(board, lib, {
714 if (DEBUGL(8) && group_at(board, c) == group)
715 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));
716 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group);
718 board_trait_queue(board, lib);
719 #endif
721 #ifdef WANT_BOARD_C
722 /* Update the list of capturable groups. */
723 assert(group);
724 assert(board->clen < board_size2(board));
725 board->c[board->clen++] = group;
726 #endif
728 static void
729 board_capturable_rm(struct board *board, group_t group, coord_t lib)
731 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
732 #ifdef BOARD_TRAITS
733 /* Decrease capturable count trait of my previously-last lib. */
734 enum stone capturing_color = stone_other(board_at(board, group));
735 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
736 foreach_neighbor(board, lib, {
737 if (DEBUGL(8) && group_at(board, c) == group)
738 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));
739 trait_at(board, lib, capturing_color).cap -= (group_at(board, c) == group);
741 board_trait_queue(board, lib);
742 #endif
744 #ifdef WANT_BOARD_C
745 /* Update the list of capturable groups. */
746 for (int i = 0; i < board->clen; i++) {
747 if (unlikely(board->c[i] == group)) {
748 board->c[i] = board->c[--board->clen];
749 return;
752 fprintf(stderr, "rm of bad group %d\n", group_base(group));
753 assert(0);
754 #endif
757 static void
758 board_atariable_add(struct board *board, group_t group, coord_t lib1, coord_t lib2)
760 #ifdef BOARD_TRAITS
761 board_trait_queue(board, lib1);
762 board_trait_queue(board, lib2);
763 #endif
765 static void
766 board_atariable_rm(struct board *board, group_t group, coord_t lib1, coord_t lib2)
768 #ifdef BOARD_TRAITS
769 board_trait_queue(board, lib1);
770 board_trait_queue(board, lib2);
771 #endif
774 static void
775 board_group_addlib(struct board *board, group_t group, coord_t coord)
777 if (DEBUGL(7)) {
778 fprintf(stderr, "Group %d[%s] %d: Adding liberty %s\n",
779 group_base(group), coord2sstr(group_base(group), board),
780 board_group_info(board, group).libs, coord2sstr(coord, board));
783 check_libs_consistency(board, group);
785 struct group *gi = &board_group_info(board, group);
786 if (gi->libs < GROUP_KEEP_LIBS) {
787 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
788 #if 0
789 /* Seems extra branch just slows it down */
790 if (!gi->lib[i])
791 break;
792 #endif
793 if (unlikely(gi->lib[i] == coord))
794 return;
796 if (gi->libs == 0) {
797 board_capturable_add(board, group, coord);
798 } else if (gi->libs == 1) {
799 board_capturable_rm(board, group, gi->lib[0]);
800 board_atariable_add(board, group, gi->lib[0], coord);
801 } else if (gi->libs == 2) {
802 board_atariable_rm(board, group, gi->lib[0], gi->lib[1]);
804 gi->lib[gi->libs++] = coord;
807 check_libs_consistency(board, group);
810 static void
811 board_group_find_extra_libs(struct board *board, group_t group, struct group *gi, coord_t avoid)
813 /* Add extra liberty from the board to our liberty list. */
814 unsigned char watermark[board_size2(board) / 8];
815 memset(watermark, 0, sizeof(watermark));
816 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
817 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
819 for (int i = 0; i < GROUP_KEEP_LIBS - 1; i++)
820 watermark_set(gi->lib[i]);
821 watermark_set(avoid);
823 foreach_in_group(board, group) {
824 coord_t coord2 = c;
825 foreach_neighbor(board, coord2, {
826 if (board_at(board, c) + watermark_get(c) != S_NONE)
827 continue;
828 watermark_set(c);
829 gi->lib[gi->libs++] = c;
830 if (unlikely(gi->libs >= GROUP_KEEP_LIBS))
831 return;
832 } );
833 } foreach_in_group_end;
834 #undef watermark_get
835 #undef watermark_set
838 static void
839 board_group_rmlib(struct board *board, group_t group, coord_t coord)
841 if (DEBUGL(7)) {
842 fprintf(stderr, "Group %d[%s] %d: Removing liberty %s\n",
843 group_base(group), coord2sstr(group_base(group), board),
844 board_group_info(board, group).libs, coord2sstr(coord, board));
847 struct group *gi = &board_group_info(board, group);
848 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
849 #if 0
850 /* Seems extra branch just slows it down */
851 if (!gi->lib[i])
852 break;
853 #endif
854 if (likely(gi->lib[i] != coord))
855 continue;
857 coord_t lib = gi->lib[i] = gi->lib[--gi->libs];
858 gi->lib[gi->libs] = 0;
860 check_libs_consistency(board, group);
862 /* Postpone refilling lib[] until we need to. */
863 assert(GROUP_REFILL_LIBS > 1);
864 if (gi->libs > GROUP_REFILL_LIBS)
865 return;
866 if (gi->libs == GROUP_REFILL_LIBS)
867 board_group_find_extra_libs(board, group, gi, coord);
869 if (gi->libs == 2) {
870 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
871 } else if (gi->libs == 1) {
872 board_capturable_add(board, group, gi->lib[0]);
873 board_atariable_rm(board, group, gi->lib[0], lib);
874 } else if (gi->libs == 0)
875 board_capturable_rm(board, group, lib);
876 return;
879 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
880 * can call this multiple times per coord. */
881 check_libs_consistency(board, group);
882 return;
886 /* This is a low-level routine that doesn't maintain consistency
887 * of all the board data structures. */
888 static void
889 board_remove_stone(struct board *board, group_t group, coord_t c)
891 enum stone color = board_at(board, c);
892 board_at(board, c) = S_NONE;
893 group_at(board, c) = 0;
894 board_hash_update(board, c, color);
895 #ifdef BOARD_TRAITS
896 /* We mark as cannot-capture now. If this is a ko/snapback,
897 * we will get incremented later in board_group_addlib(). */
898 trait_at(board, c, S_BLACK).cap = 0;
899 trait_at(board, c, S_WHITE).cap = 0;
900 board_trait_queue(board, c);
901 #endif
903 /* Increase liberties of surrounding groups */
904 coord_t coord = c;
905 foreach_neighbor(board, coord, {
906 dec_neighbor_count_at(board, c, color);
907 board_trait_queue(board, c);
908 group_t g = group_at(board, c);
909 if (g && g != group)
910 board_group_addlib(board, g, coord);
913 if (DEBUGL(6))
914 fprintf(stderr, "pushing free move [%d]: %d,%d\n", board->flen, coord_x(c, board), coord_y(c, board));
915 board->f[board->flen++] = c;
918 static int profiling_noinline
919 board_group_capture(struct board *board, group_t group)
921 int stones = 0;
923 foreach_in_group(board, group) {
924 board->captures[stone_other(board_at(board, c))]++;
925 board_remove_stone(board, group, c);
926 stones++;
927 } foreach_in_group_end;
929 struct group *gi = &board_group_info(board, group);
930 assert(gi->libs == 0);
931 memset(gi, 0, sizeof(*gi));
933 return stones;
937 static void profiling_noinline
938 add_to_group(struct board *board, group_t group, coord_t prevstone, coord_t coord)
940 group_at(board, coord) = group;
941 groupnext_at(board, coord) = groupnext_at(board, prevstone);
942 groupnext_at(board, prevstone) = coord;
944 #ifdef BOARD_TRAITS
945 if (board_group_info(board, group).libs == 1) {
946 /* Our group is temporarily in atari; make sure the capturable
947 * counts also correspond to the newly added stone before we
948 * start adding liberties again so bump-dump ops match. */
949 enum stone capturing_color = stone_other(board_at(board, group));
950 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
951 coord_t lib = board_group_info(board, group).lib[0];
952 if (coord_is_adjecent(lib, coord, board)) {
953 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);
954 trait_at(board, lib, capturing_color).cap++;
955 board_trait_queue(board, lib);
958 #endif
960 foreach_neighbor(board, coord, {
961 if (board_at(board, c) == S_NONE)
962 board_group_addlib(board, group, c);
965 if (DEBUGL(8))
966 fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
967 coord_x(prevstone, board), coord_y(prevstone, board),
968 coord_x(coord, board), coord_y(coord, board),
969 groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board),
970 group_base(group));
973 static void profiling_noinline
974 merge_groups(struct board *board, group_t group_to, group_t group_from)
976 if (DEBUGL(7))
977 fprintf(stderr, "board_play_raw: merging groups %d -> %d\n",
978 group_base(group_from), group_base(group_to));
979 struct group *gi_from = &board_group_info(board, group_from);
980 struct group *gi_to = &board_group_info(board, group_to);
982 /* We do this early before the group info is rewritten. */
983 if (gi_from->libs == 2)
984 board_atariable_rm(board, group_from, gi_from->lib[0], gi_from->lib[1]);
985 else if (gi_from->libs == 1)
986 board_capturable_rm(board, group_from, gi_from->lib[0]);
988 if (DEBUGL(7))
989 fprintf(stderr,"---- (froml %d, tol %d)\n", gi_from->libs, gi_to->libs);
991 if (gi_to->libs < GROUP_KEEP_LIBS) {
992 for (int i = 0; i < gi_from->libs; i++) {
993 for (int j = 0; j < gi_to->libs; j++)
994 if (gi_to->lib[j] == gi_from->lib[i])
995 goto next_from_lib;
996 if (gi_to->libs == 0) {
997 board_capturable_add(board, group_to, gi_from->lib[i]);
998 } else if (gi_to->libs == 1) {
999 board_capturable_rm(board, group_to, gi_to->lib[0]);
1000 board_atariable_add(board, group_to, gi_to->lib[0], gi_from->lib[i]);
1001 } else if (gi_to->libs == 2) {
1002 board_atariable_rm(board, group_to, gi_to->lib[0], gi_to->lib[1]);
1004 gi_to->lib[gi_to->libs++] = gi_from->lib[i];
1005 if (gi_to->libs >= GROUP_KEEP_LIBS)
1006 break;
1007 next_from_lib:;
1011 #ifdef BOARD_TRAITS
1012 if (board_group_info(board, group_to).libs == 1) {
1013 /* Our group is currently in atari; make sure we properly
1014 * count in even the neighbors from the other group in the
1015 * capturable counter. */
1016 enum stone capturing_color = stone_other(board_at(board, group_to));
1017 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
1018 coord_t lib = board_group_info(board, group_to).lib[0];
1019 foreach_neighbor(board, lib, {
1020 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);
1021 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group_from);
1023 board_trait_queue(board, lib);
1025 #endif
1027 coord_t last_in_group;
1028 foreach_in_group(board, group_from) {
1029 last_in_group = c;
1030 group_at(board, c) = group_to;
1031 } foreach_in_group_end;
1032 groupnext_at(board, last_in_group) = groupnext_at(board, group_base(group_to));
1033 groupnext_at(board, group_base(group_to)) = group_base(group_from);
1034 memset(gi_from, 0, sizeof(struct group));
1036 if (DEBUGL(7))
1037 fprintf(stderr, "board_play_raw: merged group: %d\n",
1038 group_base(group_to));
1041 static group_t profiling_noinline
1042 new_group(struct board *board, coord_t coord)
1044 group_t group = coord;
1045 struct group *gi = &board_group_info(board, group);
1046 foreach_neighbor(board, coord, {
1047 if (board_at(board, c) == S_NONE)
1048 /* board_group_addlib is ridiculously expensive for us */
1049 #if GROUP_KEEP_LIBS < 4
1050 if (gi->libs < GROUP_KEEP_LIBS)
1051 #endif
1052 gi->lib[gi->libs++] = c;
1055 group_at(board, coord) = group;
1056 groupnext_at(board, coord) = 0;
1058 if (gi->libs == 2)
1059 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
1060 else if (gi->libs == 1)
1061 board_capturable_add(board, group, gi->lib[0]);
1062 check_libs_consistency(board, group);
1064 if (DEBUGL(8))
1065 fprintf(stderr, "new_group: added %d,%d to group %d\n",
1066 coord_x(coord, board), coord_y(coord, board),
1067 group_base(group));
1069 return group;
1072 static inline group_t
1073 play_one_neighbor(struct board *board,
1074 coord_t coord, enum stone color, enum stone other_color,
1075 coord_t c, group_t group)
1077 enum stone ncolor = board_at(board, c);
1078 group_t ngroup = group_at(board, c);
1080 inc_neighbor_count_at(board, c, color);
1081 /* We can be S_NONE, in that case we need to update the safety
1082 * trait since we might be left with only one liberty. */
1083 board_trait_queue(board, c);
1085 if (!ngroup)
1086 return group;
1088 board_group_rmlib(board, ngroup, coord);
1089 if (DEBUGL(7))
1090 fprintf(stderr, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1091 group_base(ngroup), ncolor, color, other_color);
1093 if (ncolor == color && ngroup != group) {
1094 if (!group) {
1095 group = ngroup;
1096 add_to_group(board, group, c, coord);
1097 } else {
1098 merge_groups(board, group, ngroup);
1100 } else if (ncolor == other_color) {
1101 if (DEBUGL(8)) {
1102 struct group *gi = &board_group_info(board, ngroup);
1103 fprintf(stderr, "testing captured group %d[%s]: ", group_base(ngroup), coord2sstr(group_base(ngroup), board));
1104 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
1105 fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board));
1106 fprintf(stderr, "\n");
1108 if (unlikely(board_group_captured(board, ngroup)))
1109 board_group_capture(board, ngroup);
1111 return group;
1114 /* We played on a place with at least one liberty. We will become a member of
1115 * some group for sure. */
1116 static group_t profiling_noinline
1117 board_play_outside(struct board *board, struct move *m, int f)
1119 coord_t coord = m->coord;
1120 enum stone color = m->color;
1121 enum stone other_color = stone_other(color);
1122 group_t group = 0;
1124 board->f[f] = board->f[--board->flen];
1125 if (DEBUGL(6))
1126 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1128 #if defined(BOARD_TRAITS) && defined(DEBUG)
1129 /* Sanity check that cap matches reality. */
1131 int a = 0;
1132 foreach_neighbor(board, coord, {
1133 group_t g = group_at(board, c);
1134 a += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1);
1136 assert(a == trait_at(board, coord, color).cap);
1137 assert(board_trait_safe(board, coord, color) == trait_at(board, coord, color).safe);
1139 #endif
1140 foreach_neighbor(board, coord, {
1141 group = play_one_neighbor(board, coord, color, other_color, c, group);
1144 board_at(board, coord) = color;
1145 if (unlikely(!group))
1146 group = new_group(board, coord);
1147 board_gamma_update(board, coord, S_BLACK);
1148 board_gamma_update(board, coord, S_WHITE);
1150 board->last_move2 = board->last_move;
1151 board->last_move = *m;
1152 board->moves++;
1153 board_hash_update(board, coord, color);
1154 board_symmetry_update(board, &board->symmetry, coord);
1155 struct move ko = { pass, S_NONE };
1156 board->ko = ko;
1158 return group;
1161 /* We played in an eye-like shape. Either we capture at least one of the eye
1162 * sides in the process of playing, or return -1. */
1163 static int profiling_noinline
1164 board_play_in_eye(struct board *board, struct move *m, int f)
1166 coord_t coord = m->coord;
1167 enum stone color = m->color;
1168 /* Check ko: Capture at a position of ko capture one move ago */
1169 if (unlikely(color == board->ko.color && coord == board->ko.coord)) {
1170 if (DEBUGL(5))
1171 fprintf(stderr, "board_check: ko at %d,%d color %d\n", coord_x(coord, board), coord_y(coord, board), color);
1172 return -1;
1173 } else if (DEBUGL(6)) {
1174 fprintf(stderr, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1175 color, coord_x(coord, board), coord_y(coord, board),
1176 board->ko.color, coord_x(board->ko.coord, board), coord_y(board->ko.coord, board));
1179 struct move ko = { pass, S_NONE };
1181 int captured_groups = 0;
1183 foreach_neighbor(board, coord, {
1184 group_t g = group_at(board, c);
1185 if (DEBUGL(7))
1186 fprintf(stderr, "board_check: group %d has %d libs\n",
1187 g, board_group_info(board, g).libs);
1188 captured_groups += (board_group_info(board, g).libs == 1);
1191 if (likely(captured_groups == 0)) {
1192 if (DEBUGL(5)) {
1193 if (DEBUGL(6))
1194 board_print(board, stderr);
1195 fprintf(stderr, "board_check: one-stone suicide\n");
1198 return -1;
1200 #ifdef BOARD_TRAITS
1201 /* We _will_ for sure capture something. */
1202 assert(trait_at(board, coord, color).cap > 0);
1203 assert(trait_at(board, coord, color).safe == board_trait_safe(board, coord, color));
1204 #endif
1206 board->f[f] = board->f[--board->flen];
1207 if (DEBUGL(6))
1208 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1210 foreach_neighbor(board, coord, {
1211 inc_neighbor_count_at(board, c, color);
1212 /* Originally, this could not have changed any trait
1213 * since no neighbors were S_NONE, however by now some
1214 * of them might be removed from the board. */
1215 board_trait_queue(board, c);
1217 group_t group = group_at(board, c);
1218 if (!group)
1219 continue;
1221 board_group_rmlib(board, group, coord);
1222 if (DEBUGL(7))
1223 fprintf(stderr, "board_play_raw: reducing libs for group %d\n",
1224 group_base(group));
1226 if (board_group_captured(board, group)) {
1227 if (board_group_capture(board, group) == 1) {
1228 /* If we captured multiple groups at once,
1229 * we can't be fighting ko so we don't need
1230 * to check for that. */
1231 ko.color = stone_other(color);
1232 ko.coord = c;
1233 board->last_ko = ko;
1234 board->last_ko_age = board->moves;
1235 if (DEBUGL(5))
1236 fprintf(stderr, "guarding ko at %d,%s\n", ko.color, coord2sstr(ko.coord, board));
1241 board_at(board, coord) = color;
1242 group_t group = new_group(board, coord);
1243 board_gamma_update(board, coord, S_BLACK);
1244 board_gamma_update(board, coord, S_WHITE);
1246 board->last_move2 = board->last_move;
1247 board->last_move = *m;
1248 board->moves++;
1249 board_hash_update(board, coord, color);
1250 board_hash_commit(board);
1251 board_traits_recompute(board);
1252 board_symmetry_update(board, &board->symmetry, coord);
1253 board->ko = ko;
1255 return !!group;
1258 static int __attribute__((flatten))
1259 board_play_f(struct board *board, struct move *m, int f)
1261 if (DEBUGL(7)) {
1262 fprintf(stderr, "board_play(): ---- Playing %d,%d\n", coord_x(m->coord, board), coord_y(m->coord, board));
1264 if (likely(!board_is_eyelike(board, m->coord, stone_other(m->color)))) {
1265 /* NOT playing in an eye. Thus this move has to succeed. (This
1266 * is thanks to New Zealand rules. Otherwise, multi-stone
1267 * suicide might fail.) */
1268 group_t group = board_play_outside(board, m, f);
1269 if (unlikely(board_group_captured(board, group))) {
1270 board_group_capture(board, group);
1272 board_hash_commit(board);
1273 board_traits_recompute(board);
1274 return 0;
1275 } else {
1276 return board_play_in_eye(board, m, f);
1281 board_play(struct board *board, struct move *m)
1283 if (unlikely(is_pass(m->coord) || is_resign(m->coord))) {
1284 struct move nomove = { pass, S_NONE };
1285 board->ko = nomove;
1286 board->last_move2 = board->last_move;
1287 board->last_move = *m;
1288 return 0;
1291 int f;
1292 for (f = 0; f < board->flen; f++)
1293 if (board->f[f] == m->coord)
1294 return board_play_f(board, m, f);
1296 if (DEBUGL(7))
1297 fprintf(stderr, "board_check: stone exists\n");
1298 return -1;
1302 static inline bool
1303 board_try_random_move(struct board *b, enum stone color, coord_t *coord, int f, ppr_permit permit, void *permit_data)
1305 *coord = b->f[f];
1306 struct move m = { *coord, color };
1307 if (DEBUGL(6))
1308 fprintf(stderr, "trying random move %d: %d,%d\n", f, coord_x(*coord, b), coord_y(*coord, b));
1309 if (unlikely(board_is_one_point_eye(b, *coord, color)) /* bad idea to play into one, usually */
1310 || !board_is_valid_move(b, &m)
1311 || (permit && !permit(permit_data, b, &m)))
1312 return false;
1313 *coord = m.coord; // permit might modify it
1314 return likely(board_play_f(b, &m, f) >= 0);
1317 void
1318 board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data)
1320 if (unlikely(b->flen == 0))
1321 goto pass;
1323 int base = fast_random(b->flen), f;
1324 for (f = base; f < b->flen; f++)
1325 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1326 return;
1327 for (f = 0; f < base; f++)
1328 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1329 return;
1331 pass:
1332 *coord = pass;
1333 struct move m = { pass, color };
1334 board_play(b, &m);
1338 bool
1339 board_is_false_eyelike(struct board *board, coord_t coord, enum stone eye_color)
1341 enum stone color_diag_libs[S_MAX] = {0, 0, 0, 0};
1343 /* XXX: We attempt false eye detection but we will yield false
1344 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1346 foreach_diag_neighbor(board, coord) {
1347 color_diag_libs[(enum stone) board_at(board, c)]++;
1348 } foreach_diag_neighbor_end;
1349 /* For false eye, we need two enemy stones diagonally in the
1350 * middle of the board, or just one enemy stone at the edge
1351 * or in the corner. */
1352 color_diag_libs[stone_other(eye_color)] += !!color_diag_libs[S_OFFBOARD];
1353 return color_diag_libs[stone_other(eye_color)] >= 2;
1356 bool
1357 board_is_one_point_eye(struct board *board, coord_t coord, enum stone eye_color)
1359 return board_is_eyelike(board, coord, eye_color)
1360 && !board_is_false_eyelike(board, coord, eye_color);
1363 enum stone
1364 board_get_one_point_eye(struct board *board, coord_t coord)
1366 if (board_is_one_point_eye(board, coord, S_WHITE))
1367 return S_WHITE;
1368 else if (board_is_one_point_eye(board, coord, S_BLACK))
1369 return S_BLACK;
1370 else
1371 return S_NONE;
1375 float
1376 board_fast_score(struct board *board)
1378 int scores[S_MAX];
1379 memset(scores, 0, sizeof(scores));
1381 foreach_point(board) {
1382 enum stone color = board_at(board, c);
1383 if (color == S_NONE)
1384 color = board_get_one_point_eye(board, c);
1385 scores[color]++;
1386 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1387 } foreach_point_end;
1389 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];
1392 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1394 /* One flood-fill iteration; returns true if next iteration
1395 * is required. */
1396 static bool
1397 board_tromp_taylor_iter(struct board *board, int *ownermap)
1399 bool needs_update = false;
1400 foreach_free_point(board) {
1401 /* Ignore occupied and already-dame positions. */
1402 assert(board_at(board, c) == S_NONE);
1403 if (ownermap[c] == 3)
1404 continue;
1405 /* Count neighbors. */
1406 int nei[4] = {0};
1407 foreach_neighbor(board, c, {
1408 nei[ownermap[c]]++;
1410 /* If we have neighbors of both colors, or dame,
1411 * we are dame too. */
1412 if ((nei[1] && nei[2]) || nei[3]) {
1413 ownermap[c] = 3;
1414 /* Speed up the propagation. */
1415 foreach_neighbor(board, c, {
1416 if (board_at(board, c) == S_NONE)
1417 ownermap[c] = 3;
1419 needs_update = true;
1420 continue;
1422 /* If we have neighbors of one color, we are owned
1423 * by that color, too. */
1424 if (!ownermap[c] && (nei[1] || nei[2])) {
1425 int newowner = nei[1] ? 1 : 2;
1426 ownermap[c] = newowner;
1427 /* Speed up the propagation. */
1428 foreach_neighbor(board, c, {
1429 if (board_at(board, c) == S_NONE && !ownermap[c])
1430 ownermap[c] = newowner;
1432 needs_update = true;
1433 continue;
1435 } foreach_free_point_end;
1436 return needs_update;
1439 /* Tromp-Taylor Counting */
1440 float
1441 board_official_score(struct board *board, struct move_queue *q)
1444 /* A point P, not colored C, is said to reach C, if there is a path of
1445 * (vertically or horizontally) adjacent points of P's color from P to
1446 * a point of color C.
1448 * A player's score is the number of points of her color, plus the
1449 * number of empty points that reach only her color. */
1451 int ownermap[board_size2(board)];
1452 int s[4] = {0};
1453 const int o[4] = {0, 1, 2, 0};
1454 foreach_point(board) {
1455 ownermap[c] = o[board_at(board, c)];
1456 s[board_at(board, c)]++;
1457 } foreach_point_end;
1459 if (q) {
1460 /* Process dead groups. */
1461 for (unsigned int i = 0; i < q->moves; i++) {
1462 foreach_in_group(board, q->move[i]) {
1463 enum stone color = board_at(board, c);
1464 ownermap[c] = o[stone_other(color)];
1465 s[color]--; s[stone_other(color)]++;
1466 } foreach_in_group_end;
1470 /* We need to special-case empty board. */
1471 if (!s[S_BLACK] && !s[S_WHITE])
1472 return board->komi + board->handicap;
1474 while (board_tromp_taylor_iter(board, ownermap))
1475 /* Flood-fill... */;
1477 int scores[S_MAX];
1478 memset(scores, 0, sizeof(scores));
1480 foreach_point(board) {
1481 assert(board_at(board, c) == S_OFFBOARD || ownermap[c] != 0);
1482 if (ownermap[c] == 3)
1483 continue;
1484 scores[ownermap[c]]++;
1485 } foreach_point_end;
1487 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];