board_gamma_update(): Export function
[pachi.git] / board.c
blobf8e621243ccc66447d01ba8f5fdab8fd26b1e927
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 #endif
319 static void
320 board_print_top(struct board *board, FILE *f, int c)
322 for (int i = 0; i < c; i++) {
323 char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
324 fprintf(f, " ");
325 for (int x = 1; x < board_size(board) - 1; x++)
326 fprintf(f, "%c ", asdf[x - 1]);
327 fprintf(f, " ");
329 fprintf(f, "\n");
330 for (int i = 0; i < c; i++) {
331 fprintf(f, " +-");
332 for (int x = 1; x < board_size(board) - 1; x++)
333 fprintf(f, "--");
334 fprintf(f, "+");
336 fprintf(f, "\n");
339 static void
340 board_print_bottom(struct board *board, FILE *f, int c)
342 for (int i = 0; i < c; i++) {
343 fprintf(f, " +-");
344 for (int x = 1; x < board_size(board) - 1; x++)
345 fprintf(f, "--");
346 fprintf(f, "+");
348 fprintf(f, "\n");
351 static void
352 board_print_row(struct board *board, int y, FILE *f, board_cprint cprint)
354 fprintf(f, " %2d | ", y);
355 for (int x = 1; x < board_size(board) - 1; x++) {
356 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
357 fprintf(f, "%c)", stone2char(board_atxy(board, x, y)));
358 else
359 fprintf(f, "%c ", stone2char(board_atxy(board, x, y)));
361 fprintf(f, "|");
362 if (cprint) {
363 fprintf(f, " %2d | ", y);
364 for (int x = 1; x < board_size(board) - 1; x++) {
365 cprint(board, coord_xy(board, x, y), f);
367 fprintf(f, "|");
369 fprintf(f, "\n");
372 void
373 board_print_custom(struct board *board, FILE *f, board_cprint cprint)
375 fprintf(f, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
376 board->moves, board->komi, board->handicap,
377 board->captures[S_BLACK], board->captures[S_WHITE]);
378 board_print_top(board, f, 1 + !!cprint);
379 for (int y = board_size(board) - 2; y >= 1; y--)
380 board_print_row(board, y, f, cprint);
381 board_print_bottom(board, f, 1 + !!cprint);
382 fprintf(f, "\n");
385 static void
386 cprint_group(struct board *board, coord_t c, FILE *f)
388 fprintf(f, "%d ", group_base(group_at(board, c)));
391 void
392 board_print(struct board *board, FILE *f)
394 board_print_custom(board, f, DEBUGL(6) ? cprint_group : NULL);
398 /* Update the probability distribution we maintain incrementally. */
399 void
400 board_gamma_update(struct board *board, coord_t coord, enum stone color)
402 #ifdef BOARD_GAMMA
403 assert(board->gamma);
404 float value = 1.0f;
405 /* We just quickly replicate the general pattern matcher stuff
406 * here in the most bare-bone way. */
407 if (trait_at(board, coord, color).cap)
408 value *= board->gamma->gamma[FEAT_CAPTURE][0];
409 if (trait_at(board, coord, stone_other(color)).cap
410 && trait_at(board, coord, color).safe)
411 value *= board->gamma->gamma[FEAT_AESCAPE][0];
412 if (!trait_at(board, coord, color).safe)
413 value *= board->gamma->gamma[FEAT_SELFATARI][0];
414 probdist_set(&board->prob[color - 1], coord, value);
415 #endif
418 /* Recompute some of the traits for given point from scratch. Note that
419 * some traits are updated incrementally elsewhere. */
420 static void
421 board_trait_recompute(struct board *board, coord_t coord)
423 #ifdef BOARD_TRAITS
424 trait_at(board, coord, S_BLACK).safe = board_safe_to_play(board, coord, S_BLACK);
425 trait_at(board, coord, S_WHITE).safe = board_safe_to_play(board, coord, S_WHITE);
426 if (DEBUGL(8)) {
427 fprintf(stderr, "traits[%s:%s lib=%d] (black cap=%d safe=%d) (white cap=%d safe=%d)\n",
428 coord2sstr(coord, board), stone2str(board_at(board, coord)), immediate_liberty_count(board, coord),
429 trait_at(board, coord, S_BLACK).cap, trait_at(board, coord, S_BLACK).safe,
430 trait_at(board, coord, S_WHITE).cap, trait_at(board, coord, S_WHITE).safe);
432 #endif
433 board_gamma_update(board, coord, S_BLACK);
434 board_gamma_update(board, coord, S_WHITE);
437 /* Update board hash with given coordinate. */
438 static void profiling_noinline
439 board_hash_update(struct board *board, coord_t coord, enum stone color)
441 board->hash ^= hash_at(board, coord, color);
442 if (DEBUGL(8))
443 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);
445 #ifdef BOARD_SPATHASH
446 /* Gridcular metric is reflective, so we update all hashes
447 * of appropriate ditance in OUR circle. */
448 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
449 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
450 ptcoords_at(x, y, coord, board, j);
451 /* We either changed from S_NONE to color
452 * or vice versa; doesn't matter. */
453 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
454 pthashes[0][j][color] ^ pthashes[0][j][S_NONE];
455 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
456 pthashes[0][j][stone_other(color)] ^ pthashes[0][j][S_NONE];
459 #endif
461 #if defined(BOARD_PAT3)
462 /* @color is not what we need in case of capture. */
463 enum stone new_color = board_at(board, coord);
464 if (new_color == S_NONE)
465 board->pat3[coord] = pattern3_hash(board, coord);
466 foreach_8neighbor(board, coord) { // internally, the loop uses fn__i=[0..7]
467 if (board_at(board, c) != S_NONE)
468 continue;
469 board->pat3[c] &= ~(3 << (fn__i*2));
470 board->pat3[c] |= new_color << (fn__i*2);
471 #if 0
472 if (board_at(board, c) != S_OFFBOARD && pattern3_hash(board, c) != board->pat3[c]) {
473 board_print(board, stderr);
474 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);
475 assert(0);
477 #endif
478 board_gamma_update(board, c, S_BLACK);
479 board_gamma_update(board, c, S_WHITE);
480 } foreach_8neighbor_end;
481 #endif
484 /* Commit current board hash to history. */
485 static void profiling_noinline
486 board_hash_commit(struct board *board)
488 if (DEBUGL(8))
489 fprintf(stderr, "board_hash_commit %"PRIhash"\n", board->hash);
490 if (likely(board->history_hash[board->hash & history_hash_mask]) == 0) {
491 board->history_hash[board->hash & history_hash_mask] = board->hash;
492 } else {
493 hash_t i = board->hash;
494 while (board->history_hash[i & history_hash_mask]) {
495 if (board->history_hash[i & history_hash_mask] == board->hash) {
496 if (DEBUGL(5))
497 fprintf(stderr, "SUPERKO VIOLATION noted at %d,%d\n",
498 coord_x(board->last_move.coord, board), coord_y(board->last_move.coord, board));
499 board->superko_violation = true;
500 return;
502 i = history_hash_next(i);
504 board->history_hash[i & history_hash_mask] = board->hash;
509 void
510 board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c)
512 if (likely(symmetry->type == SYM_NONE)) {
513 /* Fully degenerated already. We do not support detection
514 * of restoring of symmetry, assuming that this is too rare
515 * a case to handle. */
516 return;
519 int x = coord_x(c, b), y = coord_y(c, b), t = board_size(b) / 2;
520 int dx = board_size(b) - 1 - x; /* for SYM_DOWN */
521 if (DEBUGL(6)) {
522 fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
523 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
524 symmetry->d, symmetry->type, x, y);
527 switch (symmetry->type) {
528 case SYM_FULL:
529 if (x == t && y == t) {
530 /* Tengen keeps full symmetry. */
531 return;
533 /* New symmetry now? */
534 if (x == y) {
535 symmetry->type = SYM_DIAG_UP;
536 symmetry->x1 = symmetry->y1 = 1;
537 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
538 symmetry->d = 1;
539 } else if (dx == y) {
540 symmetry->type = SYM_DIAG_DOWN;
541 symmetry->x1 = symmetry->y1 = 1;
542 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
543 symmetry->d = 1;
544 } else if (x == t) {
545 symmetry->type = SYM_HORIZ;
546 symmetry->y1 = 1;
547 symmetry->y2 = board_size(b) - 1;
548 symmetry->d = 0;
549 } else if (y == t) {
550 symmetry->type = SYM_VERT;
551 symmetry->x1 = 1;
552 symmetry->x2 = board_size(b) - 1;
553 symmetry->d = 0;
554 } else {
555 break_symmetry:
556 symmetry->type = SYM_NONE;
557 symmetry->x1 = symmetry->y1 = 1;
558 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
559 symmetry->d = 0;
561 break;
562 case SYM_DIAG_UP:
563 if (x == y)
564 return;
565 goto break_symmetry;
566 case SYM_DIAG_DOWN:
567 if (dx == y)
568 return;
569 goto break_symmetry;
570 case SYM_HORIZ:
571 if (x == t)
572 return;
573 goto break_symmetry;
574 case SYM_VERT:
575 if (y == t)
576 return;
577 goto break_symmetry;
578 case SYM_NONE:
579 assert(0);
580 break;
583 if (DEBUGL(6)) {
584 fprintf(stderr, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
585 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
586 symmetry->d, symmetry->type);
588 /* Whew. */
592 void
593 board_handicap_stone(struct board *board, int x, int y, FILE *f)
595 struct move m;
596 m.color = S_BLACK; m.coord = coord_xy(board, x, y);
598 board_play(board, &m);
599 /* Simulate white passing; otherwise, UCT search can get confused since
600 * tree depth parity won't match the color to move. */
601 board->moves++;
603 char *str = coord2str(m.coord, board);
604 if (DEBUGL(1))
605 fprintf(stderr, "choosing handicap %s (%d,%d)\n", str, x, y);
606 fprintf(f, "%s ", str);
607 free(str);
610 void
611 board_handicap(struct board *board, int stones, FILE *f)
613 int margin = 3 + (board_size(board) >= 13);
614 int min = margin;
615 int mid = board_size(board) / 2;
616 int max = board_size(board) - 1 - margin;
617 const int places[][2] = {
618 { min, min }, { max, max }, { max, min }, { min, max },
619 { min, mid }, { max, mid },
620 { mid, min }, { mid, max },
621 { mid, mid },
624 board->handicap = stones;
626 if (stones == 5 || stones == 7) {
627 board_handicap_stone(board, mid, mid, f);
628 stones--;
631 int i;
632 for (i = 0; i < stones; i++)
633 board_handicap_stone(board, places[i][0], places[i][1], f);
637 static void __attribute__((noinline))
638 check_libs_consistency(struct board *board, group_t g)
640 #ifdef DEBUG
641 if (!g) return;
642 struct group *gi = &board_group_info(board, g);
643 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
644 if (gi->lib[i] && board_at(board, gi->lib[i]) != S_NONE) {
645 fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(group_base(g), board));
646 assert(0);
648 #endif
651 static void
652 board_capturable_add(struct board *board, group_t group, coord_t lib)
654 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
655 #ifdef BOARD_TRAITS
656 /* Increase capturable count trait of my last lib. */
657 enum stone capturing_color = stone_other(board_at(board, group));
658 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
659 foreach_neighbor(board, lib, {
660 if (DEBUGL(8) && group_at(board, c) == group)
661 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));
662 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group);
664 board_trait_recompute(board, lib);
665 #endif
667 #ifdef WANT_BOARD_C
668 /* Update the list of capturable groups. */
669 assert(group);
670 assert(board->clen < board_size2(board));
671 board->c[board->clen++] = group;
672 #endif
674 static void
675 board_capturable_rm(struct board *board, group_t group, coord_t lib)
677 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
678 #ifdef BOARD_TRAITS
679 /* Decrease capturable count trait of my previously-last lib. */
680 enum stone capturing_color = stone_other(board_at(board, group));
681 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
682 foreach_neighbor(board, lib, {
683 if (DEBUGL(8) && group_at(board, c) == group)
684 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));
685 trait_at(board, lib, capturing_color).cap -= (group_at(board, c) == group);
687 board_trait_recompute(board, lib);
688 #endif
690 #ifdef WANT_BOARD_C
691 /* Update the list of capturable groups. */
692 for (int i = 0; i < board->clen; i++) {
693 if (unlikely(board->c[i] == group)) {
694 board->c[i] = board->c[--board->clen];
695 return;
698 fprintf(stderr, "rm of bad group %d\n", group_base(group));
699 assert(0);
700 #endif
703 static void
704 board_group_addlib(struct board *board, group_t group, coord_t coord)
706 if (DEBUGL(7)) {
707 fprintf(stderr, "Group %d[%s] %d: Adding liberty %s\n",
708 group_base(group), coord2sstr(group_base(group), board),
709 board_group_info(board, group).libs, coord2sstr(coord, board));
712 check_libs_consistency(board, group);
714 struct group *gi = &board_group_info(board, group);
715 if (gi->libs < GROUP_KEEP_LIBS) {
716 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
717 #if 0
718 /* Seems extra branch just slows it down */
719 if (!gi->lib[i])
720 break;
721 #endif
722 if (unlikely(gi->lib[i] == coord))
723 return;
725 if (gi->libs == 0)
726 board_capturable_add(board, group, coord);
727 else if (gi->libs == 1)
728 board_capturable_rm(board, group, gi->lib[0]);
729 gi->lib[gi->libs++] = coord;
732 check_libs_consistency(board, group);
735 static void
736 board_group_find_extra_libs(struct board *board, group_t group, struct group *gi, coord_t avoid)
738 /* Add extra liberty from the board to our liberty list. */
739 unsigned char watermark[board_size2(board) / 8];
740 memset(watermark, 0, sizeof(watermark));
741 #define watermark_get(c) (watermark[coord_raw(c) >> 3] & (1 << (coord_raw(c) & 7)))
742 #define watermark_set(c) watermark[coord_raw(c) >> 3] |= (1 << (coord_raw(c) & 7))
744 for (int i = 0; i < GROUP_KEEP_LIBS - 1; i++)
745 watermark_set(gi->lib[i]);
746 watermark_set(avoid);
748 foreach_in_group(board, group) {
749 coord_t coord2 = c;
750 foreach_neighbor(board, coord2, {
751 if (board_at(board, c) + watermark_get(c) != S_NONE)
752 continue;
753 watermark_set(c);
754 gi->lib[gi->libs++] = c;
755 if (unlikely(gi->libs >= GROUP_KEEP_LIBS))
756 return;
757 } );
758 } foreach_in_group_end;
759 #undef watermark_get
760 #undef watermark_set
763 static void
764 board_group_rmlib(struct board *board, group_t group, coord_t coord)
766 if (DEBUGL(7)) {
767 fprintf(stderr, "Group %d[%s] %d: Removing liberty %s\n",
768 group_base(group), coord2sstr(group_base(group), board),
769 board_group_info(board, group).libs, coord2sstr(coord, board));
772 struct group *gi = &board_group_info(board, group);
773 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
774 #if 0
775 /* Seems extra branch just slows it down */
776 if (!gi->lib[i])
777 break;
778 #endif
779 if (likely(gi->lib[i] != coord))
780 continue;
782 coord_t lib = gi->lib[i] = gi->lib[--gi->libs];
783 gi->lib[gi->libs] = 0;
785 check_libs_consistency(board, group);
787 /* Postpone refilling lib[] until we need to. */
788 assert(GROUP_REFILL_LIBS > 1);
789 if (gi->libs > GROUP_REFILL_LIBS)
790 return;
791 if (gi->libs == GROUP_REFILL_LIBS)
792 board_group_find_extra_libs(board, group, gi, coord);
794 if (gi->libs == 1)
795 board_capturable_add(board, group, gi->lib[0]);
796 else if (gi->libs == 0)
797 board_capturable_rm(board, group, lib);
798 return;
801 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
802 * can call this multiple times per coord. */
803 check_libs_consistency(board, group);
804 return;
808 /* This is a low-level routine that doesn't maintain consistency
809 * of all the board data structures. */
810 static void
811 board_remove_stone(struct board *board, group_t group, coord_t c)
813 enum stone color = board_at(board, c);
814 board_at(board, c) = S_NONE;
815 group_at(board, c) = 0;
816 board_hash_update(board, c, color);
817 #ifdef BOARD_TRAITS
818 /* We mark as cannot-capture now. If this is a ko/snapback,
819 * we will get incremented later in board_group_addlib(). */
820 trait_at(board, c, S_BLACK).cap = 0;
821 trait_at(board, c, S_WHITE).cap = 0;
822 /* However, we do decide safety statically; we might get
823 * over-paranoid, but in that case the neighbor loop for
824 * stones removed next will repair the flag. */
825 /* We must do this update after the loop when our neighbor count is correct. */
826 board_trait_recompute(board, c);
827 #endif
829 /* Increase liberties of surrounding groups */
830 coord_t coord = c;
831 foreach_neighbor(board, coord, {
832 dec_neighbor_count_at(board, c, color);
833 board_trait_recompute(board, c);
834 group_t g = group_at(board, c);
835 if (g && g != group)
836 board_group_addlib(board, g, coord);
839 if (DEBUGL(6))
840 fprintf(stderr, "pushing free move [%d]: %d,%d\n", board->flen, coord_x(c, board), coord_y(c, board));
841 board->f[board->flen++] = coord_raw(c);
844 static int profiling_noinline
845 board_group_capture(struct board *board, group_t group)
847 int stones = 0;
849 foreach_in_group(board, group) {
850 board->captures[stone_other(board_at(board, c))]++;
851 board_remove_stone(board, group, c);
852 stones++;
853 } foreach_in_group_end;
855 if (board_group_info(board, group).libs == 1)
856 board_capturable_rm(board, group, board_group_info(board, group).lib[0]);
857 memset(&board_group_info(board, group), 0, sizeof(struct group));
859 return stones;
863 static void profiling_noinline
864 add_to_group(struct board *board, group_t group, coord_t prevstone, coord_t coord)
866 group_at(board, coord) = group;
867 groupnext_at(board, coord) = groupnext_at(board, prevstone);
868 groupnext_at(board, prevstone) = coord_raw(coord);
870 #ifdef BOARD_TRAITS
871 if (board_group_info(board, group).libs == 1) {
872 /* Our group is temporarily in atari; make sure the capturable
873 * counts also correspond to the newly added stone before we
874 * start adding liberties again so bump-dump ops match. */
875 enum stone capturing_color = stone_other(board_at(board, group));
876 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
877 coord_t lib = board_group_info(board, group).lib[0];
878 if (coord_is_adjecent(lib, coord, board)) {
879 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);
880 trait_at(board, lib, capturing_color).cap++;
881 board_trait_recompute(board, lib);
884 #endif
886 foreach_neighbor(board, coord, {
887 if (board_at(board, c) == S_NONE)
888 board_group_addlib(board, group, c);
891 if (DEBUGL(8))
892 fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
893 coord_x(prevstone, board), coord_y(prevstone, board),
894 coord_x(coord, board), coord_y(coord, board),
895 groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board),
896 group_base(group));
899 static void profiling_noinline
900 merge_groups(struct board *board, group_t group_to, group_t group_from)
902 if (DEBUGL(7))
903 fprintf(stderr, "board_play_raw: merging groups %d -> %d\n",
904 group_base(group_from), group_base(group_to));
905 struct group *gi_from = &board_group_info(board, group_from);
906 struct group *gi_to = &board_group_info(board, group_to);
908 /* We do this early before the group info is rewritten. */
909 if (gi_from->libs == 1)
910 board_capturable_rm(board, group_from, gi_from->lib[0]);
912 if (DEBUGL(7))
913 fprintf(stderr,"---- (froml %d, tol %d)\n", gi_from->libs, gi_to->libs);
915 if (gi_to->libs < GROUP_KEEP_LIBS) {
916 for (int i = 0; i < gi_from->libs; i++) {
917 for (int j = 0; j < gi_to->libs; j++)
918 if (gi_to->lib[j] == gi_from->lib[i])
919 goto next_from_lib;
920 if (gi_to->libs == 0)
921 board_capturable_add(board, group_to, gi_from->lib[i]);
922 else if (gi_to->libs == 1)
923 board_capturable_rm(board, group_to, gi_to->lib[0]);
924 gi_to->lib[gi_to->libs++] = gi_from->lib[i];
925 if (gi_to->libs >= GROUP_KEEP_LIBS)
926 break;
927 next_from_lib:;
931 #ifdef BOARD_TRAITS
932 if (board_group_info(board, group_to).libs == 1) {
933 /* Our group is currently in atari; make sure we properly
934 * count in even the neighbors from the other group in the
935 * capturable counter. */
936 enum stone capturing_color = stone_other(board_at(board, group_to));
937 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
938 coord_t lib = board_group_info(board, group_to).lib[0];
939 foreach_neighbor(board, lib, {
940 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);
941 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group_from);
943 board_trait_recompute(board, lib);
945 #endif
947 coord_t last_in_group;
948 foreach_in_group(board, group_from) {
949 last_in_group = c;
950 group_at(board, c) = group_to;
951 } foreach_in_group_end;
952 groupnext_at(board, last_in_group) = groupnext_at(board, group_base(group_to));
953 groupnext_at(board, group_base(group_to)) = group_base(group_from);
954 memset(gi_from, 0, sizeof(struct group));
956 if (DEBUGL(7))
957 fprintf(stderr, "board_play_raw: merged group: %d\n",
958 group_base(group_to));
961 static group_t profiling_noinline
962 new_group(struct board *board, coord_t coord)
964 group_t group = coord_raw(coord);
965 struct group *gi = &board_group_info(board, group);
966 foreach_neighbor(board, coord, {
967 if (board_at(board, c) == S_NONE)
968 /* board_group_addlib is ridiculously expensive for us */
969 #if GROUP_KEEP_LIBS < 4
970 if (gi->libs < GROUP_KEEP_LIBS)
971 #endif
972 gi->lib[gi->libs++] = c;
975 group_at(board, coord) = group;
976 groupnext_at(board, coord) = 0;
978 if (gi->libs == 1)
979 board_capturable_add(board, group, gi->lib[0]);
980 check_libs_consistency(board, group);
982 if (DEBUGL(8))
983 fprintf(stderr, "new_group: added %d,%d to group %d\n",
984 coord_x(coord, board), coord_y(coord, board),
985 group_base(group));
987 return group;
990 static inline group_t
991 play_one_neighbor(struct board *board,
992 coord_t coord, enum stone color, enum stone other_color,
993 coord_t c, group_t group)
995 enum stone ncolor = board_at(board, c);
996 group_t ngroup = group_at(board, c);
998 inc_neighbor_count_at(board, c, color);
999 /* We can be S_NONE, in that case we need to update the safety
1000 * trait since we might be left with only one liberty. */
1001 board_trait_recompute(board, c);
1003 if (!ngroup)
1004 return group;
1006 board_group_rmlib(board, ngroup, coord);
1007 if (DEBUGL(7))
1008 fprintf(stderr, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1009 group_base(ngroup), ncolor, color, other_color);
1011 if (ncolor == color && ngroup != group) {
1012 if (!group) {
1013 group = ngroup;
1014 add_to_group(board, group, c, coord);
1015 } else {
1016 merge_groups(board, group, ngroup);
1018 } else if (ncolor == other_color) {
1019 if (DEBUGL(8)) {
1020 struct group *gi = &board_group_info(board, ngroup);
1021 fprintf(stderr, "testing captured group %d[%s]: ", group_base(ngroup), coord2sstr(group_base(ngroup), board));
1022 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
1023 fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board));
1024 fprintf(stderr, "\n");
1026 if (unlikely(board_group_captured(board, ngroup)))
1027 board_group_capture(board, ngroup);
1029 return group;
1032 /* We played on a place with at least one liberty. We will become a member of
1033 * some group for sure. */
1034 static group_t profiling_noinline
1035 board_play_outside(struct board *board, struct move *m, int f)
1037 coord_t coord = m->coord;
1038 enum stone color = m->color;
1039 enum stone other_color = stone_other(color);
1040 group_t group = 0;
1042 board->f[f] = board->f[--board->flen];
1043 if (DEBUGL(6))
1044 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1046 #if defined(BOARD_TRAITS) && !defined(NDEBUG)
1047 /* Sanity check that cap matches reality. */
1049 int a = 0;
1050 foreach_neighbor(board, coord, {
1051 group_t g = group_at(board, c);
1052 a += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1);
1054 assert(a == trait_at(board, coord, color).cap);
1055 assert(board_safe_to_play(board, coord, color) == trait_at(board, coord, color).safe);
1057 #endif
1058 foreach_neighbor(board, coord, {
1059 group = play_one_neighbor(board, coord, color, other_color, c, group);
1062 board_at(board, coord) = color;
1063 if (unlikely(!group))
1064 group = new_group(board, coord);
1066 board->last_move2 = board->last_move;
1067 board->last_move = *m;
1068 board->moves++;
1069 board_hash_update(board, coord, color);
1070 board_symmetry_update(board, &board->symmetry, coord);
1071 struct move ko = { pass, S_NONE };
1072 board->ko = ko;
1074 return group;
1077 /* We played in an eye-like shape. Either we capture at least one of the eye
1078 * sides in the process of playing, or return -1. */
1079 static int profiling_noinline
1080 board_play_in_eye(struct board *board, struct move *m, int f)
1082 coord_t coord = m->coord;
1083 enum stone color = m->color;
1084 /* Check ko: Capture at a position of ko capture one move ago */
1085 if (unlikely(color == board->ko.color && coord_eq(coord, board->ko.coord))) {
1086 if (DEBUGL(5))
1087 fprintf(stderr, "board_check: ko at %d,%d color %d\n", coord_x(coord, board), coord_y(coord, board), color);
1088 return -1;
1089 } else if (DEBUGL(6)) {
1090 fprintf(stderr, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1091 color, coord_x(coord, board), coord_y(coord, board),
1092 board->ko.color, coord_x(board->ko.coord, board), coord_y(board->ko.coord, board));
1095 struct move ko = { pass, S_NONE };
1097 int captured_groups = 0;
1099 foreach_neighbor(board, coord, {
1100 group_t g = group_at(board, c);
1101 if (DEBUGL(7))
1102 fprintf(stderr, "board_check: group %d has %d libs\n",
1103 g, board_group_info(board, g).libs);
1104 captured_groups += (board_group_info(board, g).libs == 1);
1107 if (likely(captured_groups == 0)) {
1108 if (DEBUGL(5)) {
1109 if (DEBUGL(6))
1110 board_print(board, stderr);
1111 fprintf(stderr, "board_check: one-stone suicide\n");
1114 return -1;
1116 #ifdef BOARD_TRAITS
1117 /* We _will_ for sure capture something. */
1118 assert(trait_at(board, coord, color).cap > 0);
1119 assert(trait_at(board, coord, color).safe == board_safe_to_play(board, coord, color));
1120 #endif
1122 board->f[f] = board->f[--board->flen];
1123 if (DEBUGL(6))
1124 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1126 foreach_neighbor(board, coord, {
1127 inc_neighbor_count_at(board, c, color);
1128 /* Originally, this could not have changed any trait
1129 * since no neighbors were S_NONE, however by now some
1130 * of them might be removed from the board. */
1131 board_trait_recompute(board, c);
1133 group_t group = group_at(board, c);
1134 if (!group)
1135 continue;
1137 board_group_rmlib(board, group, coord);
1138 if (DEBUGL(7))
1139 fprintf(stderr, "board_play_raw: reducing libs for group %d\n",
1140 group_base(group));
1142 if (board_group_captured(board, group)) {
1143 if (board_group_capture(board, group) == 1) {
1144 /* If we captured multiple groups at once,
1145 * we can't be fighting ko so we don't need
1146 * to check for that. */
1147 ko.color = stone_other(color);
1148 ko.coord = c;
1149 board->last_ko = ko;
1150 board->last_ko_age = board->moves;
1151 if (DEBUGL(5))
1152 fprintf(stderr, "guarding ko at %d,%s\n", ko.color, coord2sstr(ko.coord, board));
1157 board_at(board, coord) = color;
1158 group_t group = new_group(board, coord);
1160 board->last_move2 = board->last_move;
1161 board->last_move = *m;
1162 board->moves++;
1163 board_hash_update(board, coord, color);
1164 board_hash_commit(board);
1165 board_symmetry_update(board, &board->symmetry, coord);
1166 board->ko = ko;
1168 return !!group;
1171 static int __attribute__((flatten))
1172 board_play_f(struct board *board, struct move *m, int f)
1174 if (DEBUGL(7)) {
1175 fprintf(stderr, "board_play(): ---- Playing %d,%d\n", coord_x(m->coord, board), coord_y(m->coord, board));
1177 if (likely(!board_is_eyelike(board, &m->coord, stone_other(m->color)))) {
1178 /* NOT playing in an eye. Thus this move has to succeed. (This
1179 * is thanks to New Zealand rules. Otherwise, multi-stone
1180 * suicide might fail.) */
1181 group_t group = board_play_outside(board, m, f);
1182 if (unlikely(board_group_captured(board, group))) {
1183 board_group_capture(board, group);
1185 board_hash_commit(board);
1186 return 0;
1187 } else {
1188 return board_play_in_eye(board, m, f);
1193 board_play(struct board *board, struct move *m)
1195 if (unlikely(is_pass(m->coord) || is_resign(m->coord))) {
1196 board->last_move2 = board->last_move;
1197 board->last_move = *m;
1198 return 0;
1201 int f;
1202 for (f = 0; f < board->flen; f++)
1203 if (board->f[f] == coord_raw(m->coord))
1204 return board_play_f(board, m, f);
1206 if (DEBUGL(7))
1207 fprintf(stderr, "board_check: stone exists\n");
1208 return -1;
1212 static inline bool
1213 board_try_random_move(struct board *b, enum stone color, coord_t *coord, int f, ppr_permit permit, void *permit_data)
1215 coord_raw(*coord) = b->f[f];
1216 if (unlikely(is_pass(*coord)))
1217 return random_pass;
1218 struct move m = { *coord, color };
1219 if (DEBUGL(6))
1220 fprintf(stderr, "trying random move %d: %d,%d\n", f, coord_x(*coord, b), coord_y(*coord, b));
1221 return (likely(!board_is_one_point_eye(b, coord, color)) /* bad idea to play into one, usually */
1222 && board_is_valid_move(b, &m)
1223 && (!permit || permit(permit_data, b, &m))
1224 && likely(board_play_f(b, &m, f) >= 0));
1227 void
1228 board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data)
1230 int base = fast_random(b->flen);
1231 coord_pos(*coord, base, b);
1232 if (likely(board_try_random_move(b, color, coord, base, permit, permit_data)))
1233 return;
1235 int f;
1236 for (f = base + 1; f < b->flen; f++)
1237 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1238 return;
1239 for (f = 0; f < base; f++)
1240 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1241 return;
1243 *coord = pass;
1247 bool
1248 board_is_false_eyelike(struct board *board, coord_t *coord, enum stone eye_color)
1250 enum stone color_diag_libs[S_MAX] = {0, 0, 0, 0};
1252 /* XXX: We attempt false eye detection but we will yield false
1253 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1255 foreach_diag_neighbor(board, *coord) {
1256 color_diag_libs[(enum stone) board_at(board, c)]++;
1257 } foreach_diag_neighbor_end;
1258 /* For false eye, we need two enemy stones diagonally in the
1259 * middle of the board, or just one enemy stone at the edge
1260 * or in the corner. */
1261 color_diag_libs[stone_other(eye_color)] += !!color_diag_libs[S_OFFBOARD];
1262 return color_diag_libs[stone_other(eye_color)] >= 2;
1265 bool
1266 board_is_one_point_eye(struct board *board, coord_t *coord, enum stone eye_color)
1268 return board_is_eyelike(board, coord, eye_color)
1269 && !board_is_false_eyelike(board, coord, eye_color);
1272 enum stone
1273 board_get_one_point_eye(struct board *board, coord_t *coord)
1275 if (board_is_one_point_eye(board, coord, S_WHITE))
1276 return S_WHITE;
1277 else if (board_is_one_point_eye(board, coord, S_BLACK))
1278 return S_BLACK;
1279 else
1280 return S_NONE;
1284 float
1285 board_fast_score(struct board *board)
1287 int scores[S_MAX];
1288 memset(scores, 0, sizeof(scores));
1290 foreach_point(board) {
1291 enum stone color = board_at(board, c);
1292 if (color == S_NONE)
1293 color = board_get_one_point_eye(board, &c);
1294 scores[color]++;
1295 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1296 } foreach_point_end;
1298 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];
1301 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1303 /* One flood-fill iteration; returns true if next iteration
1304 * is required. */
1305 static bool
1306 board_tromp_taylor_iter(struct board *board, int *ownermap)
1308 bool needs_update = false;
1309 foreach_point(board) {
1310 /* Ignore occupied and already-dame positions. */
1311 if (board_at(board, c) != S_NONE || ownermap[c] == 3)
1312 continue;
1313 /* Count neighbors. */
1314 int nei[4] = {0};
1315 foreach_neighbor(board, c, {
1316 nei[ownermap[c]]++;
1318 /* If we have neighbors of both colors, or dame,
1319 * we are dame too. */
1320 if ((nei[1] && nei[2]) || nei[3]) {
1321 ownermap[c] = 3;
1322 /* Speed up the propagation. */
1323 foreach_neighbor(board, c, {
1324 if (board_at(board, c) == S_NONE)
1325 ownermap[c] = 3;
1327 needs_update = true;
1328 continue;
1330 /* If we have neighbors of one color, we are owned
1331 * by that color, too. */
1332 if (!ownermap[c] && (nei[1] || nei[2])) {
1333 int newowner = nei[1] ? 1 : 2;
1334 ownermap[c] = newowner;
1335 /* Speed up the propagation. */
1336 foreach_neighbor(board, c, {
1337 if (board_at(board, c) == S_NONE && !ownermap[c])
1338 ownermap[c] = newowner;
1340 needs_update = true;
1341 continue;
1343 } foreach_point_end;
1344 return needs_update;
1347 /* Tromp-Taylor Counting */
1348 float
1349 board_official_score(struct board *board, struct move_queue *q)
1352 /* A point P, not colored C, is said to reach C, if there is a path of
1353 * (vertically or horizontally) adjacent points of P's color from P to
1354 * a point of color C.
1356 * A player's score is the number of points of her color, plus the
1357 * number of empty points that reach only her color. */
1359 int ownermap[board_size2(board)];
1360 int s[4] = {0};
1361 const int o[4] = {0, 1, 2, 0};
1362 foreach_point(board) {
1363 ownermap[c] = o[board_at(board, c)];
1364 s[board_at(board, c)]++;
1365 } foreach_point_end;
1367 if (q) {
1368 /* Process dead groups. */
1369 for (int i = 0; i < q->moves; i++) {
1370 foreach_in_group(board, q->move[i]) {
1371 enum stone color = board_at(board, c);
1372 ownermap[c] = o[stone_other(color)];
1373 s[color]--; s[stone_other(color)]++;
1374 } foreach_in_group_end;
1378 /* We need to special-case empty board. */
1379 if (!s[S_BLACK] && !s[S_WHITE])
1380 return board->komi + board->handicap;
1382 while (board_tromp_taylor_iter(board, ownermap))
1383 /* Flood-fill... */;
1385 int scores[S_MAX];
1386 memset(scores, 0, sizeof(scores));
1388 foreach_point(board) {
1389 assert(board_at(board, c) == S_OFFBOARD || ownermap[c] != 0);
1390 if (ownermap[c] == 3)
1391 continue;
1392 scores[ownermap[c]]++;
1393 } foreach_point_end;
1395 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];