UCT: bestr_ratio 0.02 by default, best2_ratio 2.5 by default
[pachi.git] / board.c
blob4dff05f9d91bd4e375087109885aa130f2652411
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_GAMMA
19 #include "pattern.h"
20 #endif
22 bool random_pass = false;
25 #if 0
26 #define profiling_noinline __attribute__((noinline))
27 #else
28 #define profiling_noinline
29 #endif
31 #define gi_granularity 4
32 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
35 static void
36 board_setup(struct board *b)
38 memset(b, 0, sizeof(*b));
40 struct move m = { pass, S_NONE };
41 b->last_move = b->last_move2 = b->last_ko = b->ko = m;
44 struct board *
45 board_init(void)
47 struct board *b = malloc(sizeof(struct board));
48 board_setup(b);
50 // Default setup
51 b->size = 9 + 2;
52 board_clear(b);
54 return b;
57 struct board *
58 board_copy(struct board *b2, struct board *b1)
60 memcpy(b2, b1, sizeof(struct board));
62 int bsize = board_size2(b2) * sizeof(*b2->b);
63 int gsize = board_size2(b2) * sizeof(*b2->g);
64 int fsize = board_size2(b2) * sizeof(*b2->f);
65 int nsize = board_size2(b2) * sizeof(*b2->n);
66 int psize = board_size2(b2) * sizeof(*b2->p);
67 int hsize = board_size2(b2) * 2 * sizeof(*b2->h);
68 int gisize = board_size2(b2) * sizeof(*b2->gi);
69 #ifdef WANT_BOARD_C
70 int csize = board_size2(b2) * sizeof(*b2->c);
71 #else
72 int csize = 0;
73 #endif
74 #ifdef BOARD_SPATHASH
75 int ssize = board_size2(b2) * sizeof(*b2->spathash);
76 #else
77 int ssize = 0;
78 #endif
79 #ifdef BOARD_PAT3
80 int p3size = board_size2(b2) * sizeof(*b2->pat3);
81 #else
82 int p3size = 0;
83 #endif
84 #ifdef BOARD_TRAITS
85 int tsize = board_size2(b2) * sizeof(*b2->t);
86 #else
87 int tsize = 0;
88 #endif
89 #ifdef BOARD_GAMMA
90 int pbsize = board_size2(b2) * sizeof(*b2->prob[0].items);
91 #else
92 int pbsize = 0;
93 #endif
94 void *x = malloc(bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + pbsize * 2);
95 memcpy(x, b1->b, bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + pbsize * 2);
96 b2->b = x; x += bsize;
97 b2->g = x; x += gsize;
98 b2->f = x; x += fsize;
99 b2->p = x; x += psize;
100 b2->n = x; x += nsize;
101 b2->h = x; x += hsize;
102 b2->gi = x; x += gisize;
103 #ifdef WANT_BOARD_C
104 b2->c = x; x += csize;
105 #endif
106 #ifdef BOARD_SPATHASH
107 b2->spathash = x; x += ssize;
108 #endif
109 #ifdef BOARD_PAT3
110 b2->pat3 = x; x += p3size;
111 #endif
112 #ifdef BOARD_TRAITS
113 b2->t = x; x += tsize;
114 #endif
115 #ifdef BOARD_GAMMA
116 b2->prob[0].items = x; x += pbsize;
117 b2->prob[1].items = x; x += pbsize;
118 #endif
120 return b2;
123 void
124 board_done_noalloc(struct board *board)
126 if (board->b) free(board->b);
129 void
130 board_done(struct board *board)
132 board_done_noalloc(board);
133 free(board);
136 void
137 board_resize(struct board *board, int size)
139 #ifdef BOARD_SIZE
140 assert(board_size(board) == size + 2);
141 #else
142 board_size(board) = size + 2 /* S_OFFBOARD margin */;
143 board_size2(board) = board_size(board) * board_size(board);
144 #endif
145 if (board->b)
146 free(board->b);
148 int bsize = board_size2(board) * sizeof(*board->b);
149 int gsize = board_size2(board) * sizeof(*board->g);
150 int fsize = board_size2(board) * sizeof(*board->f);
151 int nsize = board_size2(board) * sizeof(*board->n);
152 int psize = board_size2(board) * sizeof(*board->p);
153 int hsize = board_size2(board) * 2 * sizeof(*board->h);
154 int gisize = board_size2(board) * sizeof(*board->gi);
155 #ifdef WANT_BOARD_C
156 int csize = board_size2(board) * sizeof(*board->c);
157 #else
158 int csize = 0;
159 #endif
160 #ifdef BOARD_SPATHASH
161 int ssize = board_size2(board) * sizeof(*board->spathash);
162 #else
163 int ssize = 0;
164 #endif
165 #ifdef BOARD_PAT3
166 int p3size = board_size2(board) * sizeof(*board->pat3);
167 #else
168 int p3size = 0;
169 #endif
170 #ifdef BOARD_TRAITS
171 int tsize = board_size2(board) * sizeof(*board->t);
172 #else
173 int tsize = 0;
174 #endif
175 #ifdef BOARD_GAMMA
176 int pbsize = board_size2(board) * sizeof(*board->prob[0].items);
177 #else
178 int pbsize = 0;
179 #endif
180 void *x = malloc(bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + pbsize * 2);
181 memset(x, 0, bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + pbsize * 2);
182 board->b = x; x += bsize;
183 board->g = x; x += gsize;
184 board->f = x; x += fsize;
185 board->p = x; x += psize;
186 board->n = x; x += nsize;
187 board->h = x; x += hsize;
188 board->gi = x; x += gisize;
189 #ifdef WANT_BOARD_C
190 board->c = x; x += csize;
191 #endif
192 #ifdef BOARD_SPATHASH
193 board->spathash = x; x += ssize;
194 #endif
195 #ifdef BOARD_PAT3
196 board->pat3 = x; x += p3size;
197 #endif
198 #ifdef BOARD_TRAITS
199 board->t = x; x += tsize;
200 #endif
201 #ifdef BOARD_GAMMA
202 board->prob[0].items = x; x += pbsize;
203 board->prob[1].items = x; x += pbsize;
204 #endif
207 void
208 board_clear(struct board *board)
210 int size = board_size(board);
211 float komi = board->komi;
213 board_done_noalloc(board);
214 board_setup(board);
215 board_resize(board, size - 2 /* S_OFFBOARD margin */);
217 board->komi = komi;
219 /* Setup neighborhood iterators */
220 board->nei8[0] = -size - 1; // (-1,-1)
221 board->nei8[1] = 1;
222 board->nei8[2] = 1;
223 board->nei8[3] = size - 2; // (-1,0)
224 board->nei8[4] = 2;
225 board->nei8[5] = size - 2; // (-1,1)
226 board->nei8[6] = 1;
227 board->nei8[7] = 1;
228 board->dnei[0] = -size - 1;
229 board->dnei[1] = 2;
230 board->dnei[2] = size*2 - 2;
231 board->dnei[3] = 2;
233 /* Setup initial symmetry */
234 board->symmetry.d = 1;
235 board->symmetry.x1 = board->symmetry.y1 = board_size(board) / 2;
236 board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1;
237 board->symmetry.type = SYM_FULL;
239 /* Draw the offboard margin */
240 int top_row = board_size2(board) - board_size(board);
241 int i;
242 for (i = 0; i < board_size(board); i++)
243 board->b[i] = board->b[top_row + i] = S_OFFBOARD;
244 for (i = 0; i <= top_row; i += board_size(board))
245 board->b[i] = board->b[board_size(board) - 1 + i] = S_OFFBOARD;
247 foreach_point(board) {
248 coord_t coord = c;
249 if (board_at(board, coord) == S_OFFBOARD)
250 continue;
251 foreach_neighbor(board, c, {
252 inc_neighbor_count_at(board, coord, board_at(board, c));
253 } );
254 } foreach_point_end;
256 /* First, pass is always a free position. */
257 board->f[board->flen++] = coord_raw(pass);
258 /* All positions are free! Except the margin. */
259 for (i = board_size(board); i < (board_size(board) - 1) * board_size(board); i++)
260 if (i % board_size(board) != 0 && i % board_size(board) != board_size(board) - 1)
261 board->f[board->flen++] = i;
263 /* Initialize zobrist hashtable. */
264 foreach_point(board) {
265 int max = (sizeof(hash_t) << history_hash_bits);
266 /* fast_random() is 16-bit only */
267 board->h[coord_raw(c) * 2] = ((hash_t) fast_random(max))
268 | ((hash_t) fast_random(max) << 16)
269 | ((hash_t) fast_random(max) << 32)
270 | ((hash_t) fast_random(max) << 48);
271 if (!board->h[coord_raw(c) * 2])
272 /* Would be kinda "oops". */
273 board->h[coord_raw(c) * 2] = 1;
274 /* And once again for white */
275 board->h[coord_raw(c) * 2 + 1] = ((hash_t) fast_random(max))
276 | ((hash_t) fast_random(max) << 16)
277 | ((hash_t) fast_random(max) << 32)
278 | ((hash_t) fast_random(max) << 48);
279 if (!board->h[coord_raw(c) * 2 + 1])
280 board->h[coord_raw(c) * 2 + 1] = 1;
281 } foreach_point_end;
283 #ifdef BOARD_SPATHASH
284 /* Initialize spatial hashes. */
285 foreach_point(board) {
286 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
287 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
288 ptcoords_at(x, y, c, board, j);
289 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
290 pthashes[0][j][board_at(board, c)];
291 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
292 pthashes[0][j][stone_other(board_at(board, c))];
295 } foreach_point_end;
296 #endif
297 #ifdef BOARD_PAT3
298 /* Initialize 3x3 pattern codes. */
299 foreach_point(board) {
300 if (board_at(board, c) == S_NONE)
301 board->pat3[c] = pattern3_hash(board, c);
302 } foreach_point_end;
303 #endif
304 #ifdef BOARD_TRAITS
305 /* Initialize traits. */
306 foreach_point(board) {
307 trait_at(board, c, S_BLACK).cap = 0;
308 trait_at(board, c, S_BLACK).safe = true;
309 trait_at(board, c, S_WHITE).cap = 0;
310 trait_at(board, c, S_WHITE).safe = true;
311 } foreach_point_end;
312 #endif
313 #ifdef BOARD_GAMMA
314 board->prob[0].n = board->prob[1].n = board_size2(board);
315 foreach_point(board) {
316 probdist_set(&board->prob[0], c, (board_at(board, c) == S_NONE) * 1.0f);
317 probdist_set(&board->prob[1], c, (board_at(board, c) == S_NONE) * 1.0f);
318 } foreach_point_end;
319 #endif
323 static void
324 board_print_top(struct board *board, FILE *f, int c)
326 for (int i = 0; i < c; i++) {
327 char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
328 fprintf(f, " ");
329 for (int x = 1; x < board_size(board) - 1; x++)
330 fprintf(f, "%c ", asdf[x - 1]);
331 fprintf(f, " ");
333 fprintf(f, "\n");
334 for (int i = 0; i < c; i++) {
335 fprintf(f, " +-");
336 for (int x = 1; x < board_size(board) - 1; x++)
337 fprintf(f, "--");
338 fprintf(f, "+");
340 fprintf(f, "\n");
343 static void
344 board_print_bottom(struct board *board, FILE *f, int c)
346 for (int i = 0; i < c; i++) {
347 fprintf(f, " +-");
348 for (int x = 1; x < board_size(board) - 1; x++)
349 fprintf(f, "--");
350 fprintf(f, "+");
352 fprintf(f, "\n");
355 static void
356 board_print_row(struct board *board, int y, FILE *f, board_cprint cprint)
358 fprintf(f, " %2d | ", y);
359 for (int x = 1; x < board_size(board) - 1; x++) {
360 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
361 fprintf(f, "%c)", stone2char(board_atxy(board, x, y)));
362 else
363 fprintf(f, "%c ", stone2char(board_atxy(board, x, y)));
365 fprintf(f, "|");
366 if (cprint) {
367 fprintf(f, " %2d | ", y);
368 for (int x = 1; x < board_size(board) - 1; x++) {
369 cprint(board, coord_xy(board, x, y), f);
371 fprintf(f, "|");
373 fprintf(f, "\n");
376 void
377 board_print_custom(struct board *board, FILE *f, board_cprint cprint)
379 fprintf(f, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
380 board->moves, board->komi, board->handicap,
381 board->captures[S_BLACK], board->captures[S_WHITE]);
382 board_print_top(board, f, 1 + !!cprint);
383 for (int y = board_size(board) - 2; y >= 1; y--)
384 board_print_row(board, y, f, cprint);
385 board_print_bottom(board, f, 1 + !!cprint);
386 fprintf(f, "\n");
389 static void
390 cprint_group(struct board *board, coord_t c, FILE *f)
392 fprintf(f, "%d ", group_base(group_at(board, c)));
395 void
396 board_print(struct board *board, FILE *f)
398 board_print_custom(board, f, DEBUGL(6) ? cprint_group : NULL);
402 /* Update the probability distribution we maintain incrementally. */
403 void
404 board_gamma_update(struct board *board, coord_t coord, enum stone color)
406 #ifdef BOARD_GAMMA
407 if (!board->gamma)
408 return;
410 /* Punch out invalid moves and moves filling our own eyes. */
411 if (board_at(board, coord) != S_NONE
412 || (board_is_eyelike(board, &coord, stone_other(color))
413 && !trait_at(board, coord, color).cap)
414 || (board_is_one_point_eye(board, &coord, color))) {
415 probdist_set(&board->prob[color - 1], coord, 0);
416 return;
419 /* We just quickly replicate the general pattern matcher stuff
420 * here in the most bare-bone way. */
421 float value = board->gamma->gamma[FEAT_PATTERN3][board->pat3[coord]];
422 if (trait_at(board, coord, color).cap)
423 value *= board->gamma->gamma[FEAT_CAPTURE][0];
424 if (trait_at(board, coord, stone_other(color)).cap
425 && trait_at(board, coord, color).safe)
426 value *= board->gamma->gamma[FEAT_AESCAPE][0];
427 if (!trait_at(board, coord, color).safe)
428 value *= board->gamma->gamma[FEAT_SELFATARI][0];
429 probdist_set(&board->prob[color - 1], coord, value);
430 #endif
433 /* Recompute some of the traits for given point from scratch. Note that
434 * some traits are updated incrementally elsewhere. */
435 static void
436 board_trait_recompute(struct board *board, coord_t coord)
438 #ifdef BOARD_TRAITS
439 trait_at(board, coord, S_BLACK).safe = board_safe_to_play(board, coord, S_BLACK);
440 trait_at(board, coord, S_WHITE).safe = board_safe_to_play(board, coord, S_WHITE);
441 if (DEBUGL(8)) {
442 fprintf(stderr, "traits[%s:%s lib=%d] (black cap=%d safe=%d) (white cap=%d safe=%d)\n",
443 coord2sstr(coord, board), stone2str(board_at(board, coord)), immediate_liberty_count(board, coord),
444 trait_at(board, coord, S_BLACK).cap, trait_at(board, coord, S_BLACK).safe,
445 trait_at(board, coord, S_WHITE).cap, trait_at(board, coord, S_WHITE).safe);
447 #endif
448 board_gamma_update(board, coord, S_BLACK);
449 board_gamma_update(board, coord, S_WHITE);
452 /* Update board hash with given coordinate. */
453 static void profiling_noinline
454 board_hash_update(struct board *board, coord_t coord, enum stone color)
456 board->hash ^= hash_at(board, coord, color);
457 if (DEBUGL(8))
458 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);
460 #ifdef BOARD_SPATHASH
461 /* Gridcular metric is reflective, so we update all hashes
462 * of appropriate ditance in OUR circle. */
463 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
464 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
465 ptcoords_at(x, y, coord, board, j);
466 /* We either changed from S_NONE to color
467 * or vice versa; doesn't matter. */
468 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
469 pthashes[0][j][color] ^ pthashes[0][j][S_NONE];
470 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
471 pthashes[0][j][stone_other(color)] ^ pthashes[0][j][S_NONE];
474 #endif
476 #if defined(BOARD_PAT3)
477 /* @color is not what we need in case of capture. */
478 enum stone new_color = board_at(board, coord);
479 if (new_color == S_NONE)
480 board->pat3[coord] = pattern3_hash(board, coord);
481 foreach_8neighbor(board, coord) { // internally, the loop uses fn__i=[0..7]
482 if (board_at(board, c) != S_NONE)
483 continue;
484 board->pat3[c] &= ~(3 << (fn__i*2));
485 board->pat3[c] |= new_color << (fn__i*2);
486 #if 0
487 if (board_at(board, c) != S_OFFBOARD && pattern3_hash(board, c) != board->pat3[c]) {
488 board_print(board, stderr);
489 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);
490 assert(0);
492 #endif
493 board_gamma_update(board, c, S_BLACK);
494 board_gamma_update(board, c, S_WHITE);
495 } foreach_8neighbor_end;
496 #endif
499 /* Commit current board hash to history. */
500 static void profiling_noinline
501 board_hash_commit(struct board *board)
503 if (DEBUGL(8))
504 fprintf(stderr, "board_hash_commit %"PRIhash"\n", board->hash);
505 if (likely(board->history_hash[board->hash & history_hash_mask]) == 0) {
506 board->history_hash[board->hash & history_hash_mask] = board->hash;
507 } else {
508 hash_t i = board->hash;
509 while (board->history_hash[i & history_hash_mask]) {
510 if (board->history_hash[i & history_hash_mask] == board->hash) {
511 if (DEBUGL(5))
512 fprintf(stderr, "SUPERKO VIOLATION noted at %d,%d\n",
513 coord_x(board->last_move.coord, board), coord_y(board->last_move.coord, board));
514 board->superko_violation = true;
515 return;
517 i = history_hash_next(i);
519 board->history_hash[i & history_hash_mask] = board->hash;
524 void
525 board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c)
527 if (likely(symmetry->type == SYM_NONE)) {
528 /* Fully degenerated already. We do not support detection
529 * of restoring of symmetry, assuming that this is too rare
530 * a case to handle. */
531 return;
534 int x = coord_x(c, b), y = coord_y(c, b), t = board_size(b) / 2;
535 int dx = board_size(b) - 1 - x; /* for SYM_DOWN */
536 if (DEBUGL(6)) {
537 fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
538 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
539 symmetry->d, symmetry->type, x, y);
542 switch (symmetry->type) {
543 case SYM_FULL:
544 if (x == t && y == t) {
545 /* Tengen keeps full symmetry. */
546 return;
548 /* New symmetry now? */
549 if (x == y) {
550 symmetry->type = SYM_DIAG_UP;
551 symmetry->x1 = symmetry->y1 = 1;
552 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
553 symmetry->d = 1;
554 } else if (dx == y) {
555 symmetry->type = SYM_DIAG_DOWN;
556 symmetry->x1 = symmetry->y1 = 1;
557 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
558 symmetry->d = 1;
559 } else if (x == t) {
560 symmetry->type = SYM_HORIZ;
561 symmetry->y1 = 1;
562 symmetry->y2 = board_size(b) - 1;
563 symmetry->d = 0;
564 } else if (y == t) {
565 symmetry->type = SYM_VERT;
566 symmetry->x1 = 1;
567 symmetry->x2 = board_size(b) - 1;
568 symmetry->d = 0;
569 } else {
570 break_symmetry:
571 symmetry->type = SYM_NONE;
572 symmetry->x1 = symmetry->y1 = 1;
573 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
574 symmetry->d = 0;
576 break;
577 case SYM_DIAG_UP:
578 if (x == y)
579 return;
580 goto break_symmetry;
581 case SYM_DIAG_DOWN:
582 if (dx == y)
583 return;
584 goto break_symmetry;
585 case SYM_HORIZ:
586 if (x == t)
587 return;
588 goto break_symmetry;
589 case SYM_VERT:
590 if (y == t)
591 return;
592 goto break_symmetry;
593 case SYM_NONE:
594 assert(0);
595 break;
598 if (DEBUGL(6)) {
599 fprintf(stderr, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
600 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
601 symmetry->d, symmetry->type);
603 /* Whew. */
607 void
608 board_handicap_stone(struct board *board, int x, int y, FILE *f)
610 struct move m;
611 m.color = S_BLACK; m.coord = coord_xy(board, x, y);
613 board_play(board, &m);
614 /* Simulate white passing; otherwise, UCT search can get confused since
615 * tree depth parity won't match the color to move. */
616 board->moves++;
618 char *str = coord2str(m.coord, board);
619 if (DEBUGL(1))
620 fprintf(stderr, "choosing handicap %s (%d,%d)\n", str, x, y);
621 fprintf(f, "%s ", str);
622 free(str);
625 void
626 board_handicap(struct board *board, int stones, FILE *f)
628 int margin = 3 + (board_size(board) >= 13);
629 int min = margin;
630 int mid = board_size(board) / 2;
631 int max = board_size(board) - 1 - margin;
632 const int places[][2] = {
633 { min, min }, { max, max }, { max, min }, { min, max },
634 { min, mid }, { max, mid },
635 { mid, min }, { mid, max },
636 { mid, mid },
639 board->handicap = stones;
641 if (stones == 5 || stones == 7) {
642 board_handicap_stone(board, mid, mid, f);
643 stones--;
646 int i;
647 for (i = 0; i < stones; i++)
648 board_handicap_stone(board, places[i][0], places[i][1], f);
652 static void __attribute__((noinline))
653 check_libs_consistency(struct board *board, group_t g)
655 #ifdef DEBUG
656 if (!g) return;
657 struct group *gi = &board_group_info(board, g);
658 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
659 if (gi->lib[i] && board_at(board, gi->lib[i]) != S_NONE) {
660 fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(group_base(g), board));
661 assert(0);
663 #endif
666 static void
667 board_capturable_add(struct board *board, group_t group, coord_t lib)
669 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
670 #ifdef BOARD_TRAITS
671 /* Increase capturable count trait of my last lib. */
672 enum stone capturing_color = stone_other(board_at(board, group));
673 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
674 foreach_neighbor(board, lib, {
675 if (DEBUGL(8) && group_at(board, c) == group)
676 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));
677 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group);
679 board_trait_recompute(board, lib);
680 #endif
682 #ifdef WANT_BOARD_C
683 /* Update the list of capturable groups. */
684 assert(group);
685 assert(board->clen < board_size2(board));
686 board->c[board->clen++] = group;
687 #endif
689 static void
690 board_capturable_rm(struct board *board, group_t group, coord_t lib)
692 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
693 #ifdef BOARD_TRAITS
694 /* Decrease capturable count trait of my previously-last lib. */
695 enum stone capturing_color = stone_other(board_at(board, group));
696 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
697 foreach_neighbor(board, lib, {
698 if (DEBUGL(8) && group_at(board, c) == group)
699 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));
700 trait_at(board, lib, capturing_color).cap -= (group_at(board, c) == group);
702 board_trait_recompute(board, lib);
703 #endif
705 #ifdef WANT_BOARD_C
706 /* Update the list of capturable groups. */
707 for (int i = 0; i < board->clen; i++) {
708 if (unlikely(board->c[i] == group)) {
709 board->c[i] = board->c[--board->clen];
710 return;
713 fprintf(stderr, "rm of bad group %d\n", group_base(group));
714 assert(0);
715 #endif
718 static void
719 board_group_addlib(struct board *board, group_t group, coord_t coord)
721 if (DEBUGL(7)) {
722 fprintf(stderr, "Group %d[%s] %d: Adding liberty %s\n",
723 group_base(group), coord2sstr(group_base(group), board),
724 board_group_info(board, group).libs, coord2sstr(coord, board));
727 check_libs_consistency(board, group);
729 struct group *gi = &board_group_info(board, group);
730 if (gi->libs < GROUP_KEEP_LIBS) {
731 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
732 #if 0
733 /* Seems extra branch just slows it down */
734 if (!gi->lib[i])
735 break;
736 #endif
737 if (unlikely(gi->lib[i] == coord))
738 return;
740 if (gi->libs == 0)
741 board_capturable_add(board, group, coord);
742 else if (gi->libs == 1)
743 board_capturable_rm(board, group, gi->lib[0]);
744 gi->lib[gi->libs++] = coord;
747 check_libs_consistency(board, group);
750 static void
751 board_group_find_extra_libs(struct board *board, group_t group, struct group *gi, coord_t avoid)
753 /* Add extra liberty from the board to our liberty list. */
754 unsigned char watermark[board_size2(board) / 8];
755 memset(watermark, 0, sizeof(watermark));
756 #define watermark_get(c) (watermark[coord_raw(c) >> 3] & (1 << (coord_raw(c) & 7)))
757 #define watermark_set(c) watermark[coord_raw(c) >> 3] |= (1 << (coord_raw(c) & 7))
759 for (int i = 0; i < GROUP_KEEP_LIBS - 1; i++)
760 watermark_set(gi->lib[i]);
761 watermark_set(avoid);
763 foreach_in_group(board, group) {
764 coord_t coord2 = c;
765 foreach_neighbor(board, coord2, {
766 if (board_at(board, c) + watermark_get(c) != S_NONE)
767 continue;
768 watermark_set(c);
769 gi->lib[gi->libs++] = c;
770 if (unlikely(gi->libs >= GROUP_KEEP_LIBS))
771 return;
772 } );
773 } foreach_in_group_end;
774 #undef watermark_get
775 #undef watermark_set
778 static void
779 board_group_rmlib(struct board *board, group_t group, coord_t coord)
781 if (DEBUGL(7)) {
782 fprintf(stderr, "Group %d[%s] %d: Removing liberty %s\n",
783 group_base(group), coord2sstr(group_base(group), board),
784 board_group_info(board, group).libs, coord2sstr(coord, board));
787 struct group *gi = &board_group_info(board, group);
788 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
789 #if 0
790 /* Seems extra branch just slows it down */
791 if (!gi->lib[i])
792 break;
793 #endif
794 if (likely(gi->lib[i] != coord))
795 continue;
797 coord_t lib = gi->lib[i] = gi->lib[--gi->libs];
798 gi->lib[gi->libs] = 0;
800 check_libs_consistency(board, group);
802 /* Postpone refilling lib[] until we need to. */
803 assert(GROUP_REFILL_LIBS > 1);
804 if (gi->libs > GROUP_REFILL_LIBS)
805 return;
806 if (gi->libs == GROUP_REFILL_LIBS)
807 board_group_find_extra_libs(board, group, gi, coord);
809 if (gi->libs == 1)
810 board_capturable_add(board, group, gi->lib[0]);
811 else if (gi->libs == 0)
812 board_capturable_rm(board, group, lib);
813 return;
816 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
817 * can call this multiple times per coord. */
818 check_libs_consistency(board, group);
819 return;
823 /* This is a low-level routine that doesn't maintain consistency
824 * of all the board data structures. */
825 static void
826 board_remove_stone(struct board *board, group_t group, coord_t c)
828 enum stone color = board_at(board, c);
829 board_at(board, c) = S_NONE;
830 group_at(board, c) = 0;
831 board_hash_update(board, c, color);
832 #ifdef BOARD_TRAITS
833 /* We mark as cannot-capture now. If this is a ko/snapback,
834 * we will get incremented later in board_group_addlib(). */
835 trait_at(board, c, S_BLACK).cap = 0;
836 trait_at(board, c, S_WHITE).cap = 0;
837 /* However, we do decide safety statically; we might get
838 * over-paranoid, but in that case the neighbor loop for
839 * stones removed next will repair the flag. */
840 /* We must do this update after the loop when our neighbor count is correct. */
841 board_trait_recompute(board, c);
842 #endif
844 /* Increase liberties of surrounding groups */
845 coord_t coord = c;
846 foreach_neighbor(board, coord, {
847 dec_neighbor_count_at(board, c, color);
848 board_trait_recompute(board, c);
849 group_t g = group_at(board, c);
850 if (g && g != group)
851 board_group_addlib(board, g, coord);
854 if (DEBUGL(6))
855 fprintf(stderr, "pushing free move [%d]: %d,%d\n", board->flen, coord_x(c, board), coord_y(c, board));
856 board->f[board->flen++] = coord_raw(c);
859 static int profiling_noinline
860 board_group_capture(struct board *board, group_t group)
862 int stones = 0;
864 foreach_in_group(board, group) {
865 board->captures[stone_other(board_at(board, c))]++;
866 board_remove_stone(board, group, c);
867 stones++;
868 } foreach_in_group_end;
870 if (board_group_info(board, group).libs == 1)
871 board_capturable_rm(board, group, board_group_info(board, group).lib[0]);
872 memset(&board_group_info(board, group), 0, sizeof(struct group));
874 return stones;
878 static void profiling_noinline
879 add_to_group(struct board *board, group_t group, coord_t prevstone, coord_t coord)
881 group_at(board, coord) = group;
882 groupnext_at(board, coord) = groupnext_at(board, prevstone);
883 groupnext_at(board, prevstone) = coord_raw(coord);
885 #ifdef BOARD_TRAITS
886 if (board_group_info(board, group).libs == 1) {
887 /* Our group is temporarily in atari; make sure the capturable
888 * counts also correspond to the newly added stone before we
889 * start adding liberties again so bump-dump ops match. */
890 enum stone capturing_color = stone_other(board_at(board, group));
891 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
892 coord_t lib = board_group_info(board, group).lib[0];
893 if (coord_is_adjecent(lib, coord, board)) {
894 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);
895 trait_at(board, lib, capturing_color).cap++;
896 board_trait_recompute(board, lib);
899 #endif
901 foreach_neighbor(board, coord, {
902 if (board_at(board, c) == S_NONE)
903 board_group_addlib(board, group, c);
906 if (DEBUGL(8))
907 fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
908 coord_x(prevstone, board), coord_y(prevstone, board),
909 coord_x(coord, board), coord_y(coord, board),
910 groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board),
911 group_base(group));
914 static void profiling_noinline
915 merge_groups(struct board *board, group_t group_to, group_t group_from)
917 if (DEBUGL(7))
918 fprintf(stderr, "board_play_raw: merging groups %d -> %d\n",
919 group_base(group_from), group_base(group_to));
920 struct group *gi_from = &board_group_info(board, group_from);
921 struct group *gi_to = &board_group_info(board, group_to);
923 /* We do this early before the group info is rewritten. */
924 if (gi_from->libs == 1)
925 board_capturable_rm(board, group_from, gi_from->lib[0]);
927 if (DEBUGL(7))
928 fprintf(stderr,"---- (froml %d, tol %d)\n", gi_from->libs, gi_to->libs);
930 if (gi_to->libs < GROUP_KEEP_LIBS) {
931 for (int i = 0; i < gi_from->libs; i++) {
932 for (int j = 0; j < gi_to->libs; j++)
933 if (gi_to->lib[j] == gi_from->lib[i])
934 goto next_from_lib;
935 if (gi_to->libs == 0)
936 board_capturable_add(board, group_to, gi_from->lib[i]);
937 else if (gi_to->libs == 1)
938 board_capturable_rm(board, group_to, gi_to->lib[0]);
939 gi_to->lib[gi_to->libs++] = gi_from->lib[i];
940 if (gi_to->libs >= GROUP_KEEP_LIBS)
941 break;
942 next_from_lib:;
946 #ifdef BOARD_TRAITS
947 if (board_group_info(board, group_to).libs == 1) {
948 /* Our group is currently in atari; make sure we properly
949 * count in even the neighbors from the other group in the
950 * capturable counter. */
951 enum stone capturing_color = stone_other(board_at(board, group_to));
952 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
953 coord_t lib = board_group_info(board, group_to).lib[0];
954 foreach_neighbor(board, lib, {
955 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);
956 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group_from);
958 board_trait_recompute(board, lib);
960 #endif
962 coord_t last_in_group;
963 foreach_in_group(board, group_from) {
964 last_in_group = c;
965 group_at(board, c) = group_to;
966 } foreach_in_group_end;
967 groupnext_at(board, last_in_group) = groupnext_at(board, group_base(group_to));
968 groupnext_at(board, group_base(group_to)) = group_base(group_from);
969 memset(gi_from, 0, sizeof(struct group));
971 if (DEBUGL(7))
972 fprintf(stderr, "board_play_raw: merged group: %d\n",
973 group_base(group_to));
976 static group_t profiling_noinline
977 new_group(struct board *board, coord_t coord)
979 group_t group = coord_raw(coord);
980 struct group *gi = &board_group_info(board, group);
981 foreach_neighbor(board, coord, {
982 if (board_at(board, c) == S_NONE)
983 /* board_group_addlib is ridiculously expensive for us */
984 #if GROUP_KEEP_LIBS < 4
985 if (gi->libs < GROUP_KEEP_LIBS)
986 #endif
987 gi->lib[gi->libs++] = c;
990 group_at(board, coord) = group;
991 groupnext_at(board, coord) = 0;
993 if (gi->libs == 1)
994 board_capturable_add(board, group, gi->lib[0]);
995 check_libs_consistency(board, group);
997 if (DEBUGL(8))
998 fprintf(stderr, "new_group: added %d,%d to group %d\n",
999 coord_x(coord, board), coord_y(coord, board),
1000 group_base(group));
1002 return group;
1005 static inline group_t
1006 play_one_neighbor(struct board *board,
1007 coord_t coord, enum stone color, enum stone other_color,
1008 coord_t c, group_t group)
1010 enum stone ncolor = board_at(board, c);
1011 group_t ngroup = group_at(board, c);
1013 inc_neighbor_count_at(board, c, color);
1014 /* We can be S_NONE, in that case we need to update the safety
1015 * trait since we might be left with only one liberty. */
1016 board_trait_recompute(board, c);
1018 if (!ngroup)
1019 return group;
1021 board_group_rmlib(board, ngroup, coord);
1022 if (DEBUGL(7))
1023 fprintf(stderr, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1024 group_base(ngroup), ncolor, color, other_color);
1026 if (ncolor == color && ngroup != group) {
1027 if (!group) {
1028 group = ngroup;
1029 add_to_group(board, group, c, coord);
1030 } else {
1031 merge_groups(board, group, ngroup);
1033 } else if (ncolor == other_color) {
1034 if (DEBUGL(8)) {
1035 struct group *gi = &board_group_info(board, ngroup);
1036 fprintf(stderr, "testing captured group %d[%s]: ", group_base(ngroup), coord2sstr(group_base(ngroup), board));
1037 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
1038 fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board));
1039 fprintf(stderr, "\n");
1041 if (unlikely(board_group_captured(board, ngroup)))
1042 board_group_capture(board, ngroup);
1044 return group;
1047 /* We played on a place with at least one liberty. We will become a member of
1048 * some group for sure. */
1049 static group_t profiling_noinline
1050 board_play_outside(struct board *board, struct move *m, int f)
1052 coord_t coord = m->coord;
1053 enum stone color = m->color;
1054 enum stone other_color = stone_other(color);
1055 group_t group = 0;
1057 board->f[f] = board->f[--board->flen];
1058 if (DEBUGL(6))
1059 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1061 #if defined(BOARD_TRAITS) && !defined(NDEBUG)
1062 /* Sanity check that cap matches reality. */
1064 int a = 0;
1065 foreach_neighbor(board, coord, {
1066 group_t g = group_at(board, c);
1067 a += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1);
1069 assert(a == trait_at(board, coord, color).cap);
1070 assert(board_safe_to_play(board, coord, color) == trait_at(board, coord, color).safe);
1072 #endif
1073 foreach_neighbor(board, coord, {
1074 group = play_one_neighbor(board, coord, color, other_color, c, group);
1077 board_at(board, coord) = color;
1078 if (unlikely(!group))
1079 group = new_group(board, coord);
1080 board_gamma_update(board, coord, S_BLACK);
1081 board_gamma_update(board, coord, S_WHITE);
1083 board->last_move2 = board->last_move;
1084 board->last_move = *m;
1085 board->moves++;
1086 board_hash_update(board, coord, color);
1087 board_symmetry_update(board, &board->symmetry, coord);
1088 struct move ko = { pass, S_NONE };
1089 board->ko = ko;
1091 return group;
1094 /* We played in an eye-like shape. Either we capture at least one of the eye
1095 * sides in the process of playing, or return -1. */
1096 static int profiling_noinline
1097 board_play_in_eye(struct board *board, struct move *m, int f)
1099 coord_t coord = m->coord;
1100 enum stone color = m->color;
1101 /* Check ko: Capture at a position of ko capture one move ago */
1102 if (unlikely(color == board->ko.color && coord_eq(coord, board->ko.coord))) {
1103 if (DEBUGL(5))
1104 fprintf(stderr, "board_check: ko at %d,%d color %d\n", coord_x(coord, board), coord_y(coord, board), color);
1105 return -1;
1106 } else if (DEBUGL(6)) {
1107 fprintf(stderr, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1108 color, coord_x(coord, board), coord_y(coord, board),
1109 board->ko.color, coord_x(board->ko.coord, board), coord_y(board->ko.coord, board));
1112 struct move ko = { pass, S_NONE };
1114 int captured_groups = 0;
1116 foreach_neighbor(board, coord, {
1117 group_t g = group_at(board, c);
1118 if (DEBUGL(7))
1119 fprintf(stderr, "board_check: group %d has %d libs\n",
1120 g, board_group_info(board, g).libs);
1121 captured_groups += (board_group_info(board, g).libs == 1);
1124 if (likely(captured_groups == 0)) {
1125 if (DEBUGL(5)) {
1126 if (DEBUGL(6))
1127 board_print(board, stderr);
1128 fprintf(stderr, "board_check: one-stone suicide\n");
1131 return -1;
1133 #ifdef BOARD_TRAITS
1134 /* We _will_ for sure capture something. */
1135 assert(trait_at(board, coord, color).cap > 0);
1136 assert(trait_at(board, coord, color).safe == board_safe_to_play(board, coord, color));
1137 #endif
1139 board->f[f] = board->f[--board->flen];
1140 if (DEBUGL(6))
1141 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1143 foreach_neighbor(board, coord, {
1144 inc_neighbor_count_at(board, c, color);
1145 /* Originally, this could not have changed any trait
1146 * since no neighbors were S_NONE, however by now some
1147 * of them might be removed from the board. */
1148 board_trait_recompute(board, c);
1150 group_t group = group_at(board, c);
1151 if (!group)
1152 continue;
1154 board_group_rmlib(board, group, coord);
1155 if (DEBUGL(7))
1156 fprintf(stderr, "board_play_raw: reducing libs for group %d\n",
1157 group_base(group));
1159 if (board_group_captured(board, group)) {
1160 if (board_group_capture(board, group) == 1) {
1161 /* If we captured multiple groups at once,
1162 * we can't be fighting ko so we don't need
1163 * to check for that. */
1164 ko.color = stone_other(color);
1165 ko.coord = c;
1166 board->last_ko = ko;
1167 board->last_ko_age = board->moves;
1168 if (DEBUGL(5))
1169 fprintf(stderr, "guarding ko at %d,%s\n", ko.color, coord2sstr(ko.coord, board));
1174 board_at(board, coord) = color;
1175 group_t group = new_group(board, coord);
1176 board_gamma_update(board, coord, S_BLACK);
1177 board_gamma_update(board, coord, S_WHITE);
1179 board->last_move2 = board->last_move;
1180 board->last_move = *m;
1181 board->moves++;
1182 board_hash_update(board, coord, color);
1183 board_hash_commit(board);
1184 board_symmetry_update(board, &board->symmetry, coord);
1185 board->ko = ko;
1187 return !!group;
1190 static int __attribute__((flatten))
1191 board_play_f(struct board *board, struct move *m, int f)
1193 if (DEBUGL(7)) {
1194 fprintf(stderr, "board_play(): ---- Playing %d,%d\n", coord_x(m->coord, board), coord_y(m->coord, board));
1196 if (likely(!board_is_eyelike(board, &m->coord, stone_other(m->color)))) {
1197 /* NOT playing in an eye. Thus this move has to succeed. (This
1198 * is thanks to New Zealand rules. Otherwise, multi-stone
1199 * suicide might fail.) */
1200 group_t group = board_play_outside(board, m, f);
1201 if (unlikely(board_group_captured(board, group))) {
1202 board_group_capture(board, group);
1204 board_hash_commit(board);
1205 return 0;
1206 } else {
1207 return board_play_in_eye(board, m, f);
1212 board_play(struct board *board, struct move *m)
1214 if (unlikely(is_pass(m->coord) || is_resign(m->coord))) {
1215 struct move nomove = { pass, S_NONE };
1216 board->ko = nomove;
1217 board->last_move2 = board->last_move;
1218 board->last_move = *m;
1219 return 0;
1222 int f;
1223 for (f = 0; f < board->flen; f++)
1224 if (board->f[f] == coord_raw(m->coord))
1225 return board_play_f(board, m, f);
1227 if (DEBUGL(7))
1228 fprintf(stderr, "board_check: stone exists\n");
1229 return -1;
1233 static inline bool
1234 board_try_random_move(struct board *b, enum stone color, coord_t *coord, int f, ppr_permit permit, void *permit_data)
1236 coord_raw(*coord) = b->f[f];
1237 if (unlikely(is_pass(*coord)))
1238 return random_pass;
1239 struct move m = { *coord, color };
1240 if (DEBUGL(6))
1241 fprintf(stderr, "trying random move %d: %d,%d\n", f, coord_x(*coord, b), coord_y(*coord, b));
1242 return (likely(!board_is_one_point_eye(b, coord, color)) /* bad idea to play into one, usually */
1243 && board_is_valid_move(b, &m)
1244 && (!permit || permit(permit_data, b, &m))
1245 && likely(board_play_f(b, &m, f) >= 0));
1248 void
1249 board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data)
1251 int base = fast_random(b->flen);
1252 coord_pos(*coord, base, b);
1253 if (likely(board_try_random_move(b, color, coord, base, permit, permit_data)))
1254 return;
1256 int f;
1257 for (f = base + 1; f < b->flen; f++)
1258 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1259 return;
1260 for (f = 0; f < base; f++)
1261 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1262 return;
1264 *coord = pass;
1265 struct move m = { pass, color };
1266 board_play(b, &m);
1270 bool
1271 board_is_false_eyelike(struct board *board, coord_t *coord, enum stone eye_color)
1273 enum stone color_diag_libs[S_MAX] = {0, 0, 0, 0};
1275 /* XXX: We attempt false eye detection but we will yield false
1276 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1278 foreach_diag_neighbor(board, *coord) {
1279 color_diag_libs[(enum stone) board_at(board, c)]++;
1280 } foreach_diag_neighbor_end;
1281 /* For false eye, we need two enemy stones diagonally in the
1282 * middle of the board, or just one enemy stone at the edge
1283 * or in the corner. */
1284 color_diag_libs[stone_other(eye_color)] += !!color_diag_libs[S_OFFBOARD];
1285 return color_diag_libs[stone_other(eye_color)] >= 2;
1288 bool
1289 board_is_one_point_eye(struct board *board, coord_t *coord, enum stone eye_color)
1291 return board_is_eyelike(board, coord, eye_color)
1292 && !board_is_false_eyelike(board, coord, eye_color);
1295 enum stone
1296 board_get_one_point_eye(struct board *board, coord_t *coord)
1298 if (board_is_one_point_eye(board, coord, S_WHITE))
1299 return S_WHITE;
1300 else if (board_is_one_point_eye(board, coord, S_BLACK))
1301 return S_BLACK;
1302 else
1303 return S_NONE;
1307 float
1308 board_fast_score(struct board *board)
1310 int scores[S_MAX];
1311 memset(scores, 0, sizeof(scores));
1313 foreach_point(board) {
1314 enum stone color = board_at(board, c);
1315 if (color == S_NONE)
1316 color = board_get_one_point_eye(board, &c);
1317 scores[color]++;
1318 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1319 } foreach_point_end;
1321 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];
1324 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1326 /* One flood-fill iteration; returns true if next iteration
1327 * is required. */
1328 static bool
1329 board_tromp_taylor_iter(struct board *board, int *ownermap)
1331 bool needs_update = false;
1332 foreach_point(board) {
1333 /* Ignore occupied and already-dame positions. */
1334 if (board_at(board, c) != S_NONE || ownermap[c] == 3)
1335 continue;
1336 /* Count neighbors. */
1337 int nei[4] = {0};
1338 foreach_neighbor(board, c, {
1339 nei[ownermap[c]]++;
1341 /* If we have neighbors of both colors, or dame,
1342 * we are dame too. */
1343 if ((nei[1] && nei[2]) || nei[3]) {
1344 ownermap[c] = 3;
1345 /* Speed up the propagation. */
1346 foreach_neighbor(board, c, {
1347 if (board_at(board, c) == S_NONE)
1348 ownermap[c] = 3;
1350 needs_update = true;
1351 continue;
1353 /* If we have neighbors of one color, we are owned
1354 * by that color, too. */
1355 if (!ownermap[c] && (nei[1] || nei[2])) {
1356 int newowner = nei[1] ? 1 : 2;
1357 ownermap[c] = newowner;
1358 /* Speed up the propagation. */
1359 foreach_neighbor(board, c, {
1360 if (board_at(board, c) == S_NONE && !ownermap[c])
1361 ownermap[c] = newowner;
1363 needs_update = true;
1364 continue;
1366 } foreach_point_end;
1367 return needs_update;
1370 /* Tromp-Taylor Counting */
1371 float
1372 board_official_score(struct board *board, struct move_queue *q)
1375 /* A point P, not colored C, is said to reach C, if there is a path of
1376 * (vertically or horizontally) adjacent points of P's color from P to
1377 * a point of color C.
1379 * A player's score is the number of points of her color, plus the
1380 * number of empty points that reach only her color. */
1382 int ownermap[board_size2(board)];
1383 int s[4] = {0};
1384 const int o[4] = {0, 1, 2, 0};
1385 foreach_point(board) {
1386 ownermap[c] = o[board_at(board, c)];
1387 s[board_at(board, c)]++;
1388 } foreach_point_end;
1390 if (q) {
1391 /* Process dead groups. */
1392 for (int i = 0; i < q->moves; i++) {
1393 foreach_in_group(board, q->move[i]) {
1394 enum stone color = board_at(board, c);
1395 ownermap[c] = o[stone_other(color)];
1396 s[color]--; s[stone_other(color)]++;
1397 } foreach_in_group_end;
1401 /* We need to special-case empty board. */
1402 if (!s[S_BLACK] && !s[S_WHITE])
1403 return board->komi + board->handicap;
1405 while (board_tromp_taylor_iter(board, ownermap))
1406 /* Flood-fill... */;
1408 int scores[S_MAX];
1409 memset(scores, 0, sizeof(scores));
1411 foreach_point(board) {
1412 assert(board_at(board, c) == S_OFFBOARD || ownermap[c] != 0);
1413 if (ownermap[c] == 3)
1414 continue;
1415 scores[ownermap[c]]++;
1416 } foreach_point_end;
1418 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];