Board: Introduce simple group_stone_count() utility
[pachi/t.git] / board.c
blob5b25d93003ffa8f19a0d29a2a3627d5fc5d2a6ea
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 //#define DEBUG
9 #include "board.h"
10 #include "debug.h"
11 #include "fbook.h"
12 #include "mq.h"
13 #include "random.h"
15 #ifdef BOARD_PAT3
16 #include "pattern3.h"
17 #endif
18 #ifdef BOARD_TRAITS
19 static void board_trait_recompute(struct board *board, coord_t coord);
20 #include "tactics/selfatari.h"
21 #endif
24 #if 0
25 #define profiling_noinline __attribute__((noinline))
26 #else
27 #define profiling_noinline
28 #endif
30 #define gi_granularity 4
31 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
34 static void
35 board_setup(struct board *b)
37 char *fbookfile = b->fbookfile;
39 memset(b, 0, sizeof(*b));
41 b->fbookfile = fbookfile;
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(char *fbookfile)
50 struct board *b = malloc2(sizeof(struct board));
51 board_setup(b);
53 b->fbookfile = fbookfile;
55 // Default setup
56 b->size = 9 + 2;
57 board_clear(b);
59 return b;
62 static size_t
63 board_alloc(struct board *board)
65 /* We do not allocate the board structure itself but we allocate
66 * all the arrays with board contents. */
68 int bsize = board_size2(board) * sizeof(*board->b);
69 int gsize = board_size2(board) * sizeof(*board->g);
70 int fsize = board_size2(board) * sizeof(*board->f);
71 int nsize = board_size2(board) * sizeof(*board->n);
72 int psize = board_size2(board) * sizeof(*board->p);
73 int hsize = board_size2(board) * 2 * sizeof(*board->h);
74 int gisize = board_size2(board) * sizeof(*board->gi);
75 #ifdef WANT_BOARD_C
76 int csize = board_size2(board) * sizeof(*board->c);
77 #else
78 int csize = 0;
79 #endif
80 #ifdef BOARD_PAT3
81 int p3size = board_size2(board) * sizeof(*board->pat3);
82 #else
83 int p3size = 0;
84 #endif
85 #ifdef BOARD_TRAITS
86 int tsize = board_size2(board) * sizeof(*board->t);
87 int tqsize = board_size2(board) * sizeof(*board->t);
88 #else
89 int tsize = 0;
90 int tqsize = 0;
91 #endif
92 int cdsize = board_size2(board) * sizeof(*board->coord);
94 size_t size = bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + p3size + tsize + tqsize + cdsize;
95 void *x = malloc2(size);
97 /* board->b must come first */
98 board->b = x; x += bsize;
99 board->g = x; x += gsize;
100 board->f = x; x += fsize;
101 board->p = x; x += psize;
102 board->n = x; x += nsize;
103 board->h = x; x += hsize;
104 board->gi = x; x += gisize;
105 #ifdef WANT_BOARD_C
106 board->c = x; x += csize;
107 #endif
108 #ifdef BOARD_PAT3
109 board->pat3 = x; x += p3size;
110 #endif
111 #ifdef BOARD_TRAITS
112 board->t = x; x += tsize;
113 board->tq = x; x += tqsize;
114 #endif
115 board->coord = x; x += cdsize;
117 return size;
120 struct board *
121 board_copy(struct board *b2, struct board *b1)
123 memcpy(b2, b1, sizeof(struct board));
125 size_t size = board_alloc(b2);
126 memcpy(b2->b, b1->b, size);
128 // XXX: Special semantics.
129 b2->fbook = NULL;
131 return b2;
134 void
135 board_done_noalloc(struct board *board)
137 if (board->b) free(board->b);
138 if (board->fbook) fbook_done(board->fbook);
141 void
142 board_done(struct board *board)
144 board_done_noalloc(board);
145 free(board);
148 void
149 board_resize(struct board *board, int size)
151 #ifdef BOARD_SIZE
152 assert(board_size(board) == size + 2);
153 #endif
154 assert(size <= BOARD_MAX_SIZE);
155 board->size = size + 2 /* S_OFFBOARD margin */;
156 board->size2 = board_size(board) * board_size(board);
158 board->bits2 = 1;
159 while ((1 << board->bits2) < board->size2) board->bits2++;
161 if (board->b)
162 free(board->b);
164 size_t asize = board_alloc(board);
165 memset(board->b, 0, asize);
168 void
169 board_clear(struct board *board)
171 int size = board_size(board);
172 floating_t komi = board->komi;
174 board_done_noalloc(board);
175 board_setup(board);
176 board_resize(board, size - 2 /* S_OFFBOARD margin */);
178 board->komi = komi;
180 /* Setup neighborhood iterators */
181 board->nei8[0] = -size - 1; // (-1,-1)
182 board->nei8[1] = 1;
183 board->nei8[2] = 1;
184 board->nei8[3] = size - 2; // (-1,0)
185 board->nei8[4] = 2;
186 board->nei8[5] = size - 2; // (-1,1)
187 board->nei8[6] = 1;
188 board->nei8[7] = 1;
189 board->dnei[0] = -size - 1;
190 board->dnei[1] = 2;
191 board->dnei[2] = size*2 - 2;
192 board->dnei[3] = 2;
194 /* Setup initial symmetry */
195 board->symmetry.d = 1;
196 board->symmetry.x1 = board->symmetry.y1 = board_size(board) / 2;
197 board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1;
198 board->symmetry.type = SYM_FULL;
200 /* Set up coordinate cache */
201 foreach_point(board) {
202 board->coord[c][0] = c % board_size(board);
203 board->coord[c][1] = c / board_size(board);
204 } foreach_point_end;
206 /* Draw the offboard margin */
207 int top_row = board_size2(board) - board_size(board);
208 int i;
209 for (i = 0; i < board_size(board); i++)
210 board->b[i] = board->b[top_row + i] = S_OFFBOARD;
211 for (i = 0; i <= top_row; i += board_size(board))
212 board->b[i] = board->b[board_size(board) - 1 + i] = S_OFFBOARD;
214 foreach_point(board) {
215 coord_t coord = c;
216 if (board_at(board, coord) == S_OFFBOARD)
217 continue;
218 foreach_neighbor(board, c, {
219 inc_neighbor_count_at(board, coord, board_at(board, c));
220 } );
221 } foreach_point_end;
223 /* All positions are free! Except the margin. */
224 for (i = board_size(board); i < (board_size(board) - 1) * board_size(board); i++)
225 if (i % board_size(board) != 0 && i % board_size(board) != board_size(board) - 1)
226 board->f[board->flen++] = i;
228 /* Initialize zobrist hashtable. */
229 /* We will need these to be stable across Pachi runs for
230 * certain kinds of pattern matching, thus we do not use
231 * fast_random() for this. */
232 hash_t hseed = 0x3121110101112131;
233 foreach_point(board) {
234 board->h[c * 2] = (hseed *= 16807);
235 if (!board->h[c * 2])
236 board->h[c * 2] = 1;
237 /* And once again for white */
238 board->h[c * 2 + 1] = (hseed *= 16807);
239 if (!board->h[c * 2 + 1])
240 board->h[c * 2 + 1] = 1;
241 } foreach_point_end;
243 #ifdef BOARD_PAT3
244 /* Initialize 3x3 pattern codes. */
245 foreach_point(board) {
246 if (board_at(board, c) == S_NONE)
247 board->pat3[c] = pattern3_hash(board, c);
248 } foreach_point_end;
249 #endif
250 #ifdef BOARD_TRAITS
251 /* Initialize traits. */
252 foreach_point(board) {
253 trait_at(board, c, S_BLACK).cap = 0;
254 trait_at(board, c, S_BLACK).cap1 = 0;
255 trait_at(board, c, S_BLACK).safe = true;
256 trait_at(board, c, S_WHITE).cap = 0;
257 trait_at(board, c, S_WHITE).cap1 = 0;
258 trait_at(board, c, S_WHITE).safe = true;
259 } foreach_point_end;
260 #endif
262 if (board->fbookfile) {
263 board->fbook = fbook_init(board->fbookfile, board);
267 static char *
268 board_print_top(struct board *board, char *s, char *end, int c)
270 for (int i = 0; i < c; i++) {
271 char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
272 s += snprintf(s, end - s, " ");
273 for (int x = 1; x < board_size(board) - 1; x++)
274 s += snprintf(s, end - s, "%c ", asdf[x - 1]);
275 s += snprintf(s, end -s, " ");
277 s += snprintf(s, end - s, "\n");
278 for (int i = 0; i < c; i++) {
279 s += snprintf(s, end - s, " +-");
280 for (int x = 1; x < board_size(board) - 1; x++)
281 s += snprintf(s, end - s, "--");
282 s += snprintf(s, end - s, "+");
284 s += snprintf(s, end - s, "\n");
285 return s;
288 static char *
289 board_print_bottom(struct board *board, char *s, char *end, int c)
291 for (int i = 0; i < c; i++) {
292 s += snprintf(s, end - s, " +-");
293 for (int x = 1; x < board_size(board) - 1; x++)
294 s += snprintf(s, end - s, "--");
295 s += snprintf(s, end - s, "+");
297 s += snprintf(s, end - s, "\n");
298 return s;
301 static char *
302 board_print_row(struct board *board, int y, char *s, char *end, board_cprint cprint)
304 s += snprintf(s, end - s, " %2d | ", y);
305 for (int x = 1; x < board_size(board) - 1; x++) {
306 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
307 s += snprintf(s, end - s, "%c)", stone2char(board_atxy(board, x, y)));
308 else
309 s += snprintf(s, end - s, "%c ", stone2char(board_atxy(board, x, y)));
311 s += snprintf(s, end - s, "|");
312 if (cprint) {
313 s += snprintf(s, end - s, " %2d | ", y);
314 for (int x = 1; x < board_size(board) - 1; x++) {
315 s = cprint(board, coord_xy(board, x, y), s, end);
317 s += snprintf(s, end - s, "|");
319 s += snprintf(s, end - s, "\n");
320 return s;
323 void
324 board_print_custom(struct board *board, FILE *f, board_cprint cprint)
326 char buf[10240];
327 char *s = buf;
328 char *end = buf + sizeof(buf);
329 s += snprintf(s, end - s, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
330 board->moves, board->komi, board->handicap,
331 board->captures[S_BLACK], board->captures[S_WHITE]);
332 s = board_print_top(board, s, end, 1 + !!cprint);
333 for (int y = board_size(board) - 2; y >= 1; y--)
334 s = board_print_row(board, y, s, end, cprint);
335 board_print_bottom(board, s, end, 1 + !!cprint);
336 fprintf(f, "%s\n", buf);
339 static char *
340 cprint_group(struct board *board, coord_t c, char *s, char *end)
342 s += snprintf(s, end - s, "%d ", group_base(group_at(board, c)));
343 return s;
346 void
347 board_print(struct board *board, FILE *f)
349 board_print_custom(board, f, DEBUGL(6) ? cprint_group : NULL);
353 #ifdef BOARD_TRAITS
354 static bool
355 board_trait_safe(struct board *board, coord_t coord, enum stone color)
357 if (board->precise_selfatari)
358 return !is_bad_selfatari(board, color, coord);
359 else
360 return board_safe_to_play(board, coord, color);
363 static void
364 board_trait_recompute(struct board *board, coord_t coord)
366 trait_at(board, coord, S_BLACK).safe = board_trait_safe(board, coord, S_BLACK);;
367 trait_at(board, coord, S_WHITE).safe = board_trait_safe(board, coord, S_WHITE);
368 if (DEBUGL(8)) {
369 fprintf(stderr, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
370 coord2sstr(coord, board), stone2str(board_at(board, coord)), immediate_liberty_count(board, coord),
371 trait_at(board, coord, S_BLACK).cap, trait_at(board, coord, S_BLACK).cap1, trait_at(board, coord, S_BLACK).safe,
372 trait_at(board, coord, S_WHITE).cap, trait_at(board, coord, S_WHITE).cap1, trait_at(board, coord, S_WHITE).safe);
375 #endif
377 /* Recompute traits for dirty points that we have previously touched
378 * somehow (libs of their neighbors changed or so). */
379 static void
380 board_traits_recompute(struct board *board)
382 #ifdef BOARD_TRAITS
383 for (int i = 0; i < board->tqlen; i++) {
384 coord_t coord = board->tq[i];
385 trait_at(board, coord, S_BLACK).dirty = false;
386 if (board_at(board, coord) != S_NONE)
387 continue;
388 board_trait_recompute(board, coord);
390 board->tqlen = 0;
391 #endif
394 /* Queue traits of given point for recomputing. */
395 static void
396 board_trait_queue(struct board *board, coord_t coord)
398 #ifdef BOARD_TRAITS
399 if (trait_at(board, coord, S_BLACK).dirty)
400 return;
401 board->tq[board->tqlen++] = coord;
402 trait_at(board, coord, S_BLACK).dirty = true;
403 #endif
407 /* Update board hash with given coordinate. */
408 static void profiling_noinline
409 board_hash_update(struct board *board, coord_t coord, enum stone color)
411 board->hash ^= hash_at(board, coord, color);
412 board->qhash[coord_quadrant(coord, board)] ^= hash_at(board, coord, color);
413 if (DEBUGL(8))
414 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);
416 #if defined(BOARD_PAT3)
417 /* @color is not what we need in case of capture. */
418 static const int ataribits[8] = { -1, 0, -1, 1, 2, -1, 3, -1 };
419 enum stone new_color = board_at(board, coord);
420 bool in_atari = false;
421 if (new_color == S_NONE) {
422 board->pat3[coord] = pattern3_hash(board, coord);
423 } else {
424 in_atari = (board_group_info(board, group_at(board, coord)).libs == 1);
426 foreach_8neighbor(board, coord) {
427 /* Internally, the loop uses fn__i=[0..7]. We can use
428 * it directly to address bits within the bitmap of the
429 * neighbors since the bitmap order is reverse to the
430 * loop order. */
431 if (board_at(board, c) != S_NONE)
432 continue;
433 board->pat3[c] &= ~(3 << (fn__i*2));
434 board->pat3[c] |= new_color << (fn__i*2);
435 if (ataribits[fn__i] >= 0) {
436 board->pat3[c] &= ~(1 << (16 + ataribits[fn__i]));
437 board->pat3[c] |= in_atari << (16 + ataribits[fn__i]);
439 #if defined(BOARD_TRAITS)
440 board_trait_queue(board, c);
441 #endif
442 } foreach_8neighbor_end;
443 #endif
446 /* Commit current board hash to history. */
447 static void profiling_noinline
448 board_hash_commit(struct board *board)
450 if (DEBUGL(8))
451 fprintf(stderr, "board_hash_commit %"PRIhash"\n", board->hash);
452 if (likely(board->history_hash[board->hash & history_hash_mask]) == 0) {
453 board->history_hash[board->hash & history_hash_mask] = board->hash;
454 } else {
455 hash_t i = board->hash;
456 while (board->history_hash[i & history_hash_mask]) {
457 if (board->history_hash[i & history_hash_mask] == board->hash) {
458 if (DEBUGL(5))
459 fprintf(stderr, "SUPERKO VIOLATION noted at %d,%d\n",
460 coord_x(board->last_move.coord, board), coord_y(board->last_move.coord, board));
461 board->superko_violation = true;
462 return;
464 i = history_hash_next(i);
466 board->history_hash[i & history_hash_mask] = board->hash;
471 void
472 board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c)
474 if (likely(symmetry->type == SYM_NONE)) {
475 /* Fully degenerated already. We do not support detection
476 * of restoring of symmetry, assuming that this is too rare
477 * a case to handle. */
478 return;
481 int x = coord_x(c, b), y = coord_y(c, b), t = board_size(b) / 2;
482 int dx = board_size(b) - 1 - x; /* for SYM_DOWN */
483 if (DEBUGL(6)) {
484 fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
485 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
486 symmetry->d, symmetry->type, x, y);
489 switch (symmetry->type) {
490 case SYM_FULL:
491 if (x == t && y == t) {
492 /* Tengen keeps full symmetry. */
493 return;
495 /* New symmetry now? */
496 if (x == y) {
497 symmetry->type = SYM_DIAG_UP;
498 symmetry->x1 = symmetry->y1 = 1;
499 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
500 symmetry->d = 1;
501 } else if (dx == y) {
502 symmetry->type = SYM_DIAG_DOWN;
503 symmetry->x1 = symmetry->y1 = 1;
504 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
505 symmetry->d = 1;
506 } else if (x == t) {
507 symmetry->type = SYM_HORIZ;
508 symmetry->y1 = 1;
509 symmetry->y2 = board_size(b) - 1;
510 symmetry->d = 0;
511 } else if (y == t) {
512 symmetry->type = SYM_VERT;
513 symmetry->x1 = 1;
514 symmetry->x2 = board_size(b) - 1;
515 symmetry->d = 0;
516 } else {
517 break_symmetry:
518 symmetry->type = SYM_NONE;
519 symmetry->x1 = symmetry->y1 = 1;
520 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
521 symmetry->d = 0;
523 break;
524 case SYM_DIAG_UP:
525 if (x == y)
526 return;
527 goto break_symmetry;
528 case SYM_DIAG_DOWN:
529 if (dx == y)
530 return;
531 goto break_symmetry;
532 case SYM_HORIZ:
533 if (x == t)
534 return;
535 goto break_symmetry;
536 case SYM_VERT:
537 if (y == t)
538 return;
539 goto break_symmetry;
540 case SYM_NONE:
541 assert(0);
542 break;
545 if (DEBUGL(6)) {
546 fprintf(stderr, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
547 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
548 symmetry->d, symmetry->type);
550 /* Whew. */
554 void
555 board_handicap_stone(struct board *board, int x, int y, FILE *f)
557 struct move m;
558 m.color = S_BLACK; m.coord = coord_xy(board, x, y);
560 board_play(board, &m);
561 /* Simulate white passing; otherwise, UCT search can get confused since
562 * tree depth parity won't match the color to move. */
563 board->moves++;
565 char *str = coord2str(m.coord, board);
566 if (DEBUGL(1))
567 fprintf(stderr, "choosing handicap %s (%d,%d)\n", str, x, y);
568 if (f) fprintf(f, "%s ", str);
569 free(str);
572 void
573 board_handicap(struct board *board, int stones, FILE *f)
575 int margin = 3 + (board_size(board) >= 13);
576 int min = margin;
577 int mid = board_size(board) / 2;
578 int max = board_size(board) - 1 - margin;
579 const int places[][2] = {
580 { min, min }, { max, max }, { max, min }, { min, max },
581 { min, mid }, { max, mid },
582 { mid, min }, { mid, max },
583 { mid, mid },
586 board->handicap = stones;
588 if (stones == 5 || stones == 7) {
589 board_handicap_stone(board, mid, mid, f);
590 stones--;
593 int i;
594 for (i = 0; i < stones; i++)
595 board_handicap_stone(board, places[i][0], places[i][1], f);
599 static void __attribute__((noinline))
600 check_libs_consistency(struct board *board, group_t g)
602 #ifdef DEBUG
603 if (!g) return;
604 struct group *gi = &board_group_info(board, g);
605 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
606 if (gi->lib[i] && board_at(board, gi->lib[i]) != S_NONE) {
607 fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(group_base(g), board));
608 assert(0);
610 #endif
613 static void
614 check_pat3_consistency(struct board *board, coord_t coord)
616 #ifdef DEBUG
617 foreach_8neighbor(board, coord) {
618 if (board_at(board, c) == S_NONE && pattern3_hash(board, c) != board->pat3[c]) {
619 board_print(board, stderr);
620 fprintf(stderr, "%s(%d)->%s(%d) computed %x != stored %x (%d)\n", coord2sstr(coord, board), coord, coord2sstr(c, board), c, pattern3_hash(board, c), board->pat3[c], fn__i);
621 assert(0);
623 } foreach_8neighbor_end;
624 #endif
627 static void
628 board_capturable_add(struct board *board, group_t group, coord_t lib, bool onestone)
630 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
631 #ifdef BOARD_TRAITS
632 /* Increase capturable count trait of my last lib. */
633 enum stone capturing_color = stone_other(board_at(board, group));
634 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
635 foreach_neighbor(board, lib, {
636 if (DEBUGL(8) && group_at(board, c) == group)
637 fprintf(stderr, "%s[%d] %s cap bump bc of %s(%d) member %s onestone %d\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), onestone);
638 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group);
639 trait_at(board, lib, capturing_color).cap1 += (group_at(board, c) == group && onestone);
641 board_trait_queue(board, lib);
642 #endif
644 #ifdef BOARD_PAT3
645 int fn__i = 0;
646 foreach_neighbor(board, lib, {
647 board->pat3[lib] |= (group_at(board, c) == group) << (16 + 3 - fn__i);
648 fn__i++;
650 #endif
652 #ifdef WANT_BOARD_C
653 /* Update the list of capturable groups. */
654 assert(group);
655 assert(board->clen < board_size2(board));
656 board->c[board->clen++] = group;
657 #endif
659 static void
660 board_capturable_rm(struct board *board, group_t group, coord_t lib, bool onestone)
662 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
663 #ifdef BOARD_TRAITS
664 /* Decrease capturable count trait of my previously-last lib. */
665 enum stone capturing_color = stone_other(board_at(board, group));
666 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
667 foreach_neighbor(board, lib, {
668 if (DEBUGL(8) && group_at(board, c) == group)
669 fprintf(stderr, "%s[%d] cap dump bc of %s(%d) member %s onestone %d\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap, coord2sstr(group, board), board_group_info(board, group).libs, coord2sstr(c, board), onestone);
670 trait_at(board, lib, capturing_color).cap -= (group_at(board, c) == group);
671 trait_at(board, lib, capturing_color).cap1 -= (group_at(board, c) == group && onestone);
673 board_trait_queue(board, lib);
674 #endif
676 #ifdef BOARD_PAT3
677 int fn__i = 0;
678 foreach_neighbor(board, lib, {
679 board->pat3[lib] &= ~((group_at(board, c) == group) << (16 + 3 - fn__i));
680 fn__i++;
682 #endif
684 #ifdef WANT_BOARD_C
685 /* Update the list of capturable groups. */
686 for (int i = 0; i < board->clen; i++) {
687 if (unlikely(board->c[i] == group)) {
688 board->c[i] = board->c[--board->clen];
689 return;
692 fprintf(stderr, "rm of bad group %d\n", group_base(group));
693 assert(0);
694 #endif
697 static void
698 board_atariable_add(struct board *board, group_t group, coord_t lib1, coord_t lib2)
700 #ifdef BOARD_TRAITS
701 board_trait_queue(board, lib1);
702 board_trait_queue(board, lib2);
703 #endif
705 static void
706 board_atariable_rm(struct board *board, group_t group, coord_t lib1, coord_t lib2)
708 #ifdef BOARD_TRAITS
709 board_trait_queue(board, lib1);
710 board_trait_queue(board, lib2);
711 #endif
714 static void
715 board_group_addlib(struct board *board, group_t group, coord_t coord)
717 if (DEBUGL(7)) {
718 fprintf(stderr, "Group %d[%s] %d: Adding liberty %s\n",
719 group_base(group), coord2sstr(group_base(group), board),
720 board_group_info(board, group).libs, coord2sstr(coord, board));
723 check_libs_consistency(board, group);
725 struct group *gi = &board_group_info(board, group);
726 bool onestone = group_is_onestone(board, group);
727 if (gi->libs < GROUP_KEEP_LIBS) {
728 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
729 #if 0
730 /* Seems extra branch just slows it down */
731 if (!gi->lib[i])
732 break;
733 #endif
734 if (unlikely(gi->lib[i] == coord))
735 return;
737 if (gi->libs == 0) {
738 board_capturable_add(board, group, coord, onestone);
739 } else if (gi->libs == 1) {
740 board_capturable_rm(board, group, gi->lib[0], onestone);
741 board_atariable_add(board, group, gi->lib[0], coord);
742 } else if (gi->libs == 2) {
743 board_atariable_rm(board, group, gi->lib[0], gi->lib[1]);
745 gi->lib[gi->libs++] = coord;
748 check_libs_consistency(board, group);
751 static void
752 board_group_find_extra_libs(struct board *board, group_t group, struct group *gi, coord_t avoid)
754 /* Add extra liberty from the board to our liberty list. */
755 unsigned char watermark[board_size2(board) / 8];
756 memset(watermark, 0, sizeof(watermark));
757 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
758 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
760 for (int i = 0; i < GROUP_KEEP_LIBS - 1; i++)
761 watermark_set(gi->lib[i]);
762 watermark_set(avoid);
764 foreach_in_group(board, group) {
765 coord_t coord2 = c;
766 foreach_neighbor(board, coord2, {
767 if (board_at(board, c) + watermark_get(c) != S_NONE)
768 continue;
769 watermark_set(c);
770 gi->lib[gi->libs++] = c;
771 if (unlikely(gi->libs >= GROUP_KEEP_LIBS))
772 return;
773 } );
774 } foreach_in_group_end;
775 #undef watermark_get
776 #undef watermark_set
779 static void
780 board_group_rmlib(struct board *board, group_t group, coord_t coord)
782 if (DEBUGL(7)) {
783 fprintf(stderr, "Group %d[%s] %d: Removing liberty %s\n",
784 group_base(group), coord2sstr(group_base(group), board),
785 board_group_info(board, group).libs, coord2sstr(coord, board));
788 struct group *gi = &board_group_info(board, group);
789 bool onestone = group_is_onestone(board, group);
790 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
791 #if 0
792 /* Seems extra branch just slows it down */
793 if (!gi->lib[i])
794 break;
795 #endif
796 if (likely(gi->lib[i] != coord))
797 continue;
799 coord_t lib = gi->lib[i] = gi->lib[--gi->libs];
800 gi->lib[gi->libs] = 0;
802 check_libs_consistency(board, group);
804 /* Postpone refilling lib[] until we need to. */
805 assert(GROUP_REFILL_LIBS > 1);
806 if (gi->libs > GROUP_REFILL_LIBS)
807 return;
808 if (gi->libs == GROUP_REFILL_LIBS)
809 board_group_find_extra_libs(board, group, gi, coord);
811 if (gi->libs == 2) {
812 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
813 } else if (gi->libs == 1) {
814 board_capturable_add(board, group, gi->lib[0], onestone);
815 board_atariable_rm(board, group, gi->lib[0], lib);
816 } else if (gi->libs == 0)
817 board_capturable_rm(board, group, lib, onestone);
818 return;
821 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
822 * can call this multiple times per coord. */
823 check_libs_consistency(board, group);
824 return;
828 /* This is a low-level routine that doesn't maintain consistency
829 * of all the board data structures. */
830 static void
831 board_remove_stone(struct board *board, group_t group, coord_t c)
833 enum stone color = board_at(board, c);
834 board_at(board, c) = S_NONE;
835 group_at(board, c) = 0;
836 board_hash_update(board, c, color);
837 #ifdef BOARD_TRAITS
838 /* We mark as cannot-capture now. If this is a ko/snapback,
839 * we will get incremented later in board_group_addlib(). */
840 trait_at(board, c, S_BLACK).cap = trait_at(board, c, S_BLACK).cap1 = 0;
841 trait_at(board, c, S_WHITE).cap = trait_at(board, c, S_WHITE).cap1 = 0;
842 board_trait_queue(board, c);
843 #endif
845 /* Increase liberties of surrounding groups */
846 coord_t coord = c;
847 foreach_neighbor(board, coord, {
848 dec_neighbor_count_at(board, c, color);
849 board_trait_queue(board, c);
850 group_t g = group_at(board, c);
851 if (g && g != group)
852 board_group_addlib(board, g, coord);
855 #ifdef BOARD_PAT3
856 /* board_hash_update() might have seen the freed up point as able
857 * to capture another group in atari that only after the loop
858 * above gained enough liberties. Reset pat3 again. */
859 board->pat3[c] = pattern3_hash(board, c);
860 #endif
862 if (DEBUGL(6))
863 fprintf(stderr, "pushing free move [%d]: %d,%d\n", board->flen, coord_x(c, board), coord_y(c, board));
864 board->f[board->flen++] = c;
867 static int profiling_noinline
868 board_group_capture(struct board *board, group_t group)
870 int stones = 0;
872 foreach_in_group(board, group) {
873 board->captures[stone_other(board_at(board, c))]++;
874 board_remove_stone(board, group, c);
875 stones++;
876 } foreach_in_group_end;
878 struct group *gi = &board_group_info(board, group);
879 assert(gi->libs == 0);
880 memset(gi, 0, sizeof(*gi));
882 return stones;
886 static void profiling_noinline
887 add_to_group(struct board *board, group_t group, coord_t prevstone, coord_t coord)
889 #ifdef BOARD_TRAITS
890 struct group *gi = &board_group_info(board, group);
891 bool onestone = group_is_onestone(board, group);
893 if (gi->libs == 1) {
894 /* Our group is temporarily in atari; make sure the capturable
895 * counts also correspond to the newly added stone before we
896 * start adding liberties again so bump-dump ops match. */
897 enum stone capturing_color = stone_other(board_at(board, group));
898 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
900 coord_t lib = board_group_info(board, group).lib[0];
901 if (coord_is_adjecent(lib, coord, board)) {
902 if (DEBUGL(8))
903 fprintf(stderr, "add_to_group %s: %s[%d] bump\n", coord2sstr(group, board), coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
904 trait_at(board, lib, capturing_color).cap++;
905 /* This is never a 1-stone group, obviously. */
906 board_trait_queue(board, lib);
909 if (onestone) {
910 /* We are not 1-stone group anymore, update the cap1
911 * counter specifically. */
912 foreach_neighbor(board, group, {
913 if (board_at(board, c) != S_NONE) continue;
914 trait_at(board, c, capturing_color).cap1--;
915 board_trait_queue(board, c);
919 #endif
921 group_at(board, coord) = group;
922 groupnext_at(board, coord) = groupnext_at(board, prevstone);
923 groupnext_at(board, prevstone) = coord;
925 foreach_neighbor(board, coord, {
926 if (board_at(board, c) == S_NONE)
927 board_group_addlib(board, group, c);
930 if (DEBUGL(8))
931 fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
932 coord_x(prevstone, board), coord_y(prevstone, board),
933 coord_x(coord, board), coord_y(coord, board),
934 groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board),
935 group_base(group));
938 static void profiling_noinline
939 merge_groups(struct board *board, group_t group_to, group_t group_from)
941 if (DEBUGL(7))
942 fprintf(stderr, "board_play_raw: merging groups %d -> %d\n",
943 group_base(group_from), group_base(group_to));
944 struct group *gi_from = &board_group_info(board, group_from);
945 struct group *gi_to = &board_group_info(board, group_to);
946 bool onestone_from = group_is_onestone(board, group_from);
947 bool onestone_to = group_is_onestone(board, group_to);
949 /* We do this early before the group info is rewritten. */
950 if (gi_from->libs == 2)
951 board_atariable_rm(board, group_from, gi_from->lib[0], gi_from->lib[1]);
952 else if (gi_from->libs == 1)
953 board_capturable_rm(board, group_from, gi_from->lib[0], onestone_from);
955 if (DEBUGL(7))
956 fprintf(stderr,"---- (froml %d, tol %d)\n", gi_from->libs, gi_to->libs);
958 if (gi_to->libs < GROUP_KEEP_LIBS) {
959 for (int i = 0; i < gi_from->libs; i++) {
960 for (int j = 0; j < gi_to->libs; j++)
961 if (gi_to->lib[j] == gi_from->lib[i])
962 goto next_from_lib;
963 if (gi_to->libs == 0) {
964 board_capturable_add(board, group_to, gi_from->lib[i], onestone_to);
965 } else if (gi_to->libs == 1) {
966 board_capturable_rm(board, group_to, gi_to->lib[0], onestone_to);
967 board_atariable_add(board, group_to, gi_to->lib[0], gi_from->lib[i]);
968 } else if (gi_to->libs == 2) {
969 board_atariable_rm(board, group_to, gi_to->lib[0], gi_to->lib[1]);
971 gi_to->lib[gi_to->libs++] = gi_from->lib[i];
972 if (gi_to->libs >= GROUP_KEEP_LIBS)
973 break;
974 next_from_lib:;
978 if (gi_to->libs == 1) {
979 coord_t lib = board_group_info(board, group_to).lib[0];
980 #ifdef BOARD_TRAITS
981 enum stone capturing_color = stone_other(board_at(board, group_to));
982 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
984 /* Our group is currently in atari; make sure we properly
985 * count in even the neighbors from the other group in the
986 * capturable counter. */
987 foreach_neighbor(board, lib, {
988 if (DEBUGL(8) && group_at(board, c) == group_from)
989 fprintf(stderr, "%s[%d] cap bump\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
990 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group_from);
991 /* This is never a 1-stone group, obviously. */
993 board_trait_queue(board, lib);
995 if (onestone_to) {
996 /* We are not 1-stone group anymore, update the cap1
997 * counter specifically. */
998 foreach_neighbor(board, group_to, {
999 if (board_at(board, c) != S_NONE) continue;
1000 trait_at(board, c, capturing_color).cap1--;
1001 board_trait_queue(board, c);
1004 #endif
1005 #ifdef BOARD_PAT3
1006 if (gi_from->libs == 1) {
1007 /* We removed group_from from capturable groups,
1008 * therefore switching the atari flag off.
1009 * We need to set it again since group_to is also
1010 * capturable. */
1011 int fn__i = 0;
1012 foreach_neighbor(board, lib, {
1013 board->pat3[lib] |= (group_at(board, c) == group_from) << (16 + 3 - fn__i);
1014 fn__i++;
1017 #endif
1020 coord_t last_in_group;
1021 foreach_in_group(board, group_from) {
1022 last_in_group = c;
1023 group_at(board, c) = group_to;
1024 } foreach_in_group_end;
1025 groupnext_at(board, last_in_group) = groupnext_at(board, group_base(group_to));
1026 groupnext_at(board, group_base(group_to)) = group_base(group_from);
1027 memset(gi_from, 0, sizeof(struct group));
1029 if (DEBUGL(7))
1030 fprintf(stderr, "board_play_raw: merged group: %d\n",
1031 group_base(group_to));
1034 static group_t profiling_noinline
1035 new_group(struct board *board, coord_t coord)
1037 group_t group = coord;
1038 struct group *gi = &board_group_info(board, group);
1039 foreach_neighbor(board, coord, {
1040 if (board_at(board, c) == S_NONE)
1041 /* board_group_addlib is ridiculously expensive for us */
1042 #if GROUP_KEEP_LIBS < 4
1043 if (gi->libs < GROUP_KEEP_LIBS)
1044 #endif
1045 gi->lib[gi->libs++] = c;
1048 group_at(board, coord) = group;
1049 groupnext_at(board, coord) = 0;
1051 if (gi->libs == 2)
1052 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
1053 else if (gi->libs == 1)
1054 board_capturable_add(board, group, gi->lib[0], true);
1055 check_libs_consistency(board, group);
1057 if (DEBUGL(8))
1058 fprintf(stderr, "new_group: added %d,%d to group %d\n",
1059 coord_x(coord, board), coord_y(coord, board),
1060 group_base(group));
1062 return group;
1065 static inline group_t
1066 play_one_neighbor(struct board *board,
1067 coord_t coord, enum stone color, enum stone other_color,
1068 coord_t c, group_t group)
1070 enum stone ncolor = board_at(board, c);
1071 group_t ngroup = group_at(board, c);
1073 inc_neighbor_count_at(board, c, color);
1074 /* We can be S_NONE, in that case we need to update the safety
1075 * trait since we might be left with only one liberty. */
1076 board_trait_queue(board, c);
1078 if (!ngroup)
1079 return group;
1081 board_group_rmlib(board, ngroup, coord);
1082 if (DEBUGL(7))
1083 fprintf(stderr, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1084 group_base(ngroup), ncolor, color, other_color);
1086 if (ncolor == color && ngroup != group) {
1087 if (!group) {
1088 group = ngroup;
1089 add_to_group(board, group, c, coord);
1090 } else {
1091 merge_groups(board, group, ngroup);
1093 } else if (ncolor == other_color) {
1094 if (DEBUGL(8)) {
1095 struct group *gi = &board_group_info(board, ngroup);
1096 fprintf(stderr, "testing captured group %d[%s]: ", group_base(ngroup), coord2sstr(group_base(ngroup), board));
1097 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
1098 fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board));
1099 fprintf(stderr, "\n");
1101 if (unlikely(board_group_captured(board, ngroup)))
1102 board_group_capture(board, ngroup);
1104 return group;
1107 /* We played on a place with at least one liberty. We will become a member of
1108 * some group for sure. */
1109 static group_t profiling_noinline
1110 board_play_outside(struct board *board, struct move *m, int f)
1112 coord_t coord = m->coord;
1113 enum stone color = m->color;
1114 enum stone other_color = stone_other(color);
1115 group_t group = 0;
1117 board->f[f] = board->f[--board->flen];
1118 if (DEBUGL(6))
1119 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1121 #if defined(BOARD_TRAITS) && defined(DEBUG)
1122 /* Sanity check that cap matches reality. */
1124 int a = 0, b = 0;
1125 foreach_neighbor(board, coord, {
1126 group_t g = group_at(board, c);
1127 a += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1);
1128 b += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1) && group_is_onestone(board, g);
1130 assert(a == trait_at(board, coord, color).cap);
1131 assert(b == trait_at(board, coord, color).cap1);
1132 assert(board_trait_safe(board, coord, color) == trait_at(board, coord, color).safe);
1134 #endif
1135 foreach_neighbor(board, coord, {
1136 group = play_one_neighbor(board, coord, color, other_color, c, group);
1139 board_at(board, coord) = color;
1140 if (unlikely(!group))
1141 group = new_group(board, coord);
1143 board->last_move2 = board->last_move;
1144 board->last_move = *m;
1145 board->moves++;
1146 board_hash_update(board, coord, color);
1147 board_symmetry_update(board, &board->symmetry, coord);
1148 struct move ko = { pass, S_NONE };
1149 board->ko = ko;
1151 check_pat3_consistency(board, coord);
1153 return group;
1156 /* We played in an eye-like shape. Either we capture at least one of the eye
1157 * sides in the process of playing, or return -1. */
1158 static int profiling_noinline
1159 board_play_in_eye(struct board *board, struct move *m, int f)
1161 coord_t coord = m->coord;
1162 enum stone color = m->color;
1163 /* Check ko: Capture at a position of ko capture one move ago */
1164 if (unlikely(color == board->ko.color && coord == board->ko.coord)) {
1165 if (DEBUGL(5))
1166 fprintf(stderr, "board_check: ko at %d,%d color %d\n", coord_x(coord, board), coord_y(coord, board), color);
1167 return -1;
1168 } else if (DEBUGL(6)) {
1169 fprintf(stderr, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1170 color, coord_x(coord, board), coord_y(coord, board),
1171 board->ko.color, coord_x(board->ko.coord, board), coord_y(board->ko.coord, board));
1174 struct move ko = { pass, S_NONE };
1176 int captured_groups = 0;
1178 foreach_neighbor(board, coord, {
1179 group_t g = group_at(board, c);
1180 if (DEBUGL(7))
1181 fprintf(stderr, "board_check: group %d has %d libs\n",
1182 g, board_group_info(board, g).libs);
1183 captured_groups += (board_group_info(board, g).libs == 1);
1186 if (likely(captured_groups == 0)) {
1187 if (DEBUGL(5)) {
1188 if (DEBUGL(6))
1189 board_print(board, stderr);
1190 fprintf(stderr, "board_check: one-stone suicide\n");
1193 return -1;
1195 #ifdef BOARD_TRAITS
1196 /* We _will_ for sure capture something. */
1197 assert(trait_at(board, coord, color).cap > 0);
1198 assert(trait_at(board, coord, color).safe == board_trait_safe(board, coord, color));
1199 #endif
1201 board->f[f] = board->f[--board->flen];
1202 if (DEBUGL(6))
1203 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1205 foreach_neighbor(board, coord, {
1206 inc_neighbor_count_at(board, c, color);
1207 /* Originally, this could not have changed any trait
1208 * since no neighbors were S_NONE, however by now some
1209 * of them might be removed from the board. */
1210 board_trait_queue(board, c);
1212 group_t group = group_at(board, c);
1213 if (!group)
1214 continue;
1216 board_group_rmlib(board, group, coord);
1217 if (DEBUGL(7))
1218 fprintf(stderr, "board_play_raw: reducing libs for group %d\n",
1219 group_base(group));
1221 if (board_group_captured(board, group)) {
1222 if (board_group_capture(board, group) == 1) {
1223 /* If we captured multiple groups at once,
1224 * we can't be fighting ko so we don't need
1225 * to check for that. */
1226 ko.color = stone_other(color);
1227 ko.coord = c;
1228 board->last_ko = ko;
1229 board->last_ko_age = board->moves;
1230 if (DEBUGL(5))
1231 fprintf(stderr, "guarding ko at %d,%s\n", ko.color, coord2sstr(ko.coord, board));
1236 board_at(board, coord) = color;
1237 group_t group = new_group(board, coord);
1239 board->last_move2 = board->last_move;
1240 board->last_move = *m;
1241 board->moves++;
1242 board_hash_update(board, coord, color);
1243 board_hash_commit(board);
1244 board_traits_recompute(board);
1245 board_symmetry_update(board, &board->symmetry, coord);
1246 board->ko = ko;
1248 check_pat3_consistency(board, coord);
1250 return !!group;
1253 static int __attribute__((flatten))
1254 board_play_f(struct board *board, struct move *m, int f)
1256 if (DEBUGL(7)) {
1257 fprintf(stderr, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m->coord, board), coord_x(m->coord, board), coord_y(m->coord, board));
1259 if (likely(!board_is_eyelike(board, m->coord, stone_other(m->color)))) {
1260 /* NOT playing in an eye. Thus this move has to succeed. (This
1261 * is thanks to New Zealand rules. Otherwise, multi-stone
1262 * suicide might fail.) */
1263 group_t group = board_play_outside(board, m, f);
1264 if (unlikely(board_group_captured(board, group))) {
1265 board_group_capture(board, group);
1267 board_hash_commit(board);
1268 board_traits_recompute(board);
1269 return 0;
1270 } else {
1271 return board_play_in_eye(board, m, f);
1276 board_play(struct board *board, struct move *m)
1278 if (unlikely(is_pass(m->coord) || is_resign(m->coord))) {
1279 struct move nomove = { pass, S_NONE };
1280 board->ko = nomove;
1281 board->last_move2 = board->last_move;
1282 board->last_move = *m;
1283 return 0;
1286 int f;
1287 for (f = 0; f < board->flen; f++)
1288 if (board->f[f] == m->coord)
1289 return board_play_f(board, m, f);
1291 if (DEBUGL(7))
1292 fprintf(stderr, "board_check: stone exists\n");
1293 return -1;
1297 static inline bool
1298 board_try_random_move(struct board *b, enum stone color, coord_t *coord, int f, ppr_permit permit, void *permit_data)
1300 *coord = b->f[f];
1301 struct move m = { *coord, color };
1302 if (DEBUGL(6))
1303 fprintf(stderr, "trying random move %d: %d,%d %s %d\n", f, coord_x(*coord, b), coord_y(*coord, b), coord2sstr(*coord, b), board_is_valid_move(b, &m));
1304 if (unlikely(board_is_one_point_eye(b, *coord, color)) /* bad idea to play into one, usually */
1305 || !board_is_valid_move(b, &m)
1306 || (permit && !permit(permit_data, b, &m)))
1307 return false;
1308 if (m.coord == *coord) {
1309 return likely(board_play_f(b, &m, f) >= 0);
1310 } else {
1311 *coord = m.coord; // permit modified the coordinate
1312 return likely(board_play(b, &m) >= 0);
1316 void
1317 board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data)
1319 if (unlikely(b->flen == 0))
1320 goto pass;
1322 int base = fast_random(b->flen), f;
1323 for (f = base; f < b->flen; f++)
1324 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1325 return;
1326 for (f = 0; f < base; f++)
1327 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1328 return;
1330 pass:
1331 *coord = pass;
1332 struct move m = { pass, color };
1333 board_play(b, &m);
1337 bool
1338 board_is_false_eyelike(struct board *board, coord_t coord, enum stone eye_color)
1340 enum stone color_diag_libs[S_MAX] = {0, 0, 0, 0};
1342 /* XXX: We attempt false eye detection but we will yield false
1343 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1345 foreach_diag_neighbor(board, coord) {
1346 color_diag_libs[(enum stone) board_at(board, c)]++;
1347 } foreach_diag_neighbor_end;
1348 /* For false eye, we need two enemy stones diagonally in the
1349 * middle of the board, or just one enemy stone at the edge
1350 * or in the corner. */
1351 color_diag_libs[stone_other(eye_color)] += !!color_diag_libs[S_OFFBOARD];
1352 return color_diag_libs[stone_other(eye_color)] >= 2;
1355 bool
1356 board_is_one_point_eye(struct board *board, coord_t coord, enum stone eye_color)
1358 return board_is_eyelike(board, coord, eye_color)
1359 && !board_is_false_eyelike(board, coord, eye_color);
1362 enum stone
1363 board_get_one_point_eye(struct board *board, coord_t coord)
1365 if (board_is_one_point_eye(board, coord, S_WHITE))
1366 return S_WHITE;
1367 else if (board_is_one_point_eye(board, coord, S_BLACK))
1368 return S_BLACK;
1369 else
1370 return S_NONE;
1374 floating_t
1375 board_fast_score(struct board *board)
1377 int scores[S_MAX];
1378 memset(scores, 0, sizeof(scores));
1380 foreach_point(board) {
1381 enum stone color = board_at(board, c);
1382 if (color == S_NONE)
1383 color = board_get_one_point_eye(board, c);
1384 scores[color]++;
1385 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1386 } foreach_point_end;
1388 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];
1391 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1393 /* One flood-fill iteration; returns true if next iteration
1394 * is required. */
1395 static bool
1396 board_tromp_taylor_iter(struct board *board, int *ownermap)
1398 bool needs_update = false;
1399 foreach_free_point(board) {
1400 /* Ignore occupied and already-dame positions. */
1401 assert(board_at(board, c) == S_NONE);
1402 if (ownermap[c] == 3)
1403 continue;
1404 /* Count neighbors. */
1405 int nei[4] = {0};
1406 foreach_neighbor(board, c, {
1407 nei[ownermap[c]]++;
1409 /* If we have neighbors of both colors, or dame,
1410 * we are dame too. */
1411 if ((nei[1] && nei[2]) || nei[3]) {
1412 ownermap[c] = 3;
1413 /* Speed up the propagation. */
1414 foreach_neighbor(board, c, {
1415 if (board_at(board, c) == S_NONE)
1416 ownermap[c] = 3;
1418 needs_update = true;
1419 continue;
1421 /* If we have neighbors of one color, we are owned
1422 * by that color, too. */
1423 if (!ownermap[c] && (nei[1] || nei[2])) {
1424 int newowner = nei[1] ? 1 : 2;
1425 ownermap[c] = newowner;
1426 /* Speed up the propagation. */
1427 foreach_neighbor(board, c, {
1428 if (board_at(board, c) == S_NONE && !ownermap[c])
1429 ownermap[c] = newowner;
1431 needs_update = true;
1432 continue;
1434 } foreach_free_point_end;
1435 return needs_update;
1438 /* Tromp-Taylor Counting */
1439 floating_t
1440 board_official_score(struct board *board, struct move_queue *q)
1443 /* A point P, not colored C, is said to reach C, if there is a path of
1444 * (vertically or horizontally) adjacent points of P's color from P to
1445 * a point of color C.
1447 * A player's score is the number of points of her color, plus the
1448 * number of empty points that reach only her color. */
1450 int ownermap[board_size2(board)];
1451 int s[4] = {0};
1452 const int o[4] = {0, 1, 2, 0};
1453 foreach_point(board) {
1454 ownermap[c] = o[board_at(board, c)];
1455 s[board_at(board, c)]++;
1456 } foreach_point_end;
1458 if (q) {
1459 /* Process dead groups. */
1460 for (unsigned int i = 0; i < q->moves; i++) {
1461 foreach_in_group(board, q->move[i]) {
1462 enum stone color = board_at(board, c);
1463 ownermap[c] = o[stone_other(color)];
1464 s[color]--; s[stone_other(color)]++;
1465 } foreach_in_group_end;
1469 /* We need to special-case empty board. */
1470 if (!s[S_BLACK] && !s[S_WHITE])
1471 return board->komi + board->handicap;
1473 while (board_tromp_taylor_iter(board, ownermap))
1474 /* Flood-fill... */;
1476 int scores[S_MAX];
1477 memset(scores, 0, sizeof(scores));
1479 foreach_point(board) {
1480 assert(board_at(board, c) == S_OFFBOARD || ownermap[c] != 0);
1481 if (ownermap[c] == 3)
1482 continue;
1483 scores[ownermap[c]]++;
1484 } foreach_point_end;
1486 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];