Moggy: Add eye-piercing side pattern - commented out
[pachi/pachi-r6144.git] / board.c
blob09becfc8fd9b9595800ba158aad3356203677eeb
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_SPATHASH
16 #include "patternsp.h"
17 #endif
18 #ifdef BOARD_PAT3
19 #include "pattern3.h"
20 #endif
21 #ifdef BOARD_TRAITS
22 static void board_trait_recompute(struct board *board, coord_t coord);
23 #include "tactics/selfatari.h"
24 #endif
25 #ifdef BOARD_GAMMA
26 #include "pattern.h"
27 #endif
30 #if 0
31 #define profiling_noinline __attribute__((noinline))
32 #else
33 #define profiling_noinline
34 #endif
36 #define gi_granularity 4
37 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
40 static void
41 board_setup(struct board *b)
43 char *fbookfile = b->fbookfile;
45 memset(b, 0, sizeof(*b));
47 b->fbookfile = fbookfile;
49 struct move m = { pass, S_NONE };
50 b->last_move = b->last_move2 = b->last_ko = b->ko = m;
53 struct board *
54 board_init(char *fbookfile)
56 struct board *b = malloc2(sizeof(struct board));
57 board_setup(b);
59 b->fbookfile = fbookfile;
61 // Default setup
62 b->size = 9 + 2;
63 board_clear(b);
65 return b;
68 static size_t
69 board_alloc(struct board *board)
71 /* We do not allocate the board structure itself but we allocate
72 * all the arrays with board contents. */
74 int bsize = board_size2(board) * sizeof(*board->b);
75 int gsize = board_size2(board) * sizeof(*board->g);
76 int fsize = board_size2(board) * sizeof(*board->f);
77 int nsize = board_size2(board) * sizeof(*board->n);
78 int psize = board_size2(board) * sizeof(*board->p);
79 int hsize = board_size2(board) * 2 * sizeof(*board->h);
80 int gisize = board_size2(board) * sizeof(*board->gi);
81 #ifdef WANT_BOARD_C
82 int csize = board_size2(board) * sizeof(*board->c);
83 #else
84 int csize = 0;
85 #endif
86 #ifdef BOARD_SPATHASH
87 int ssize = board_size2(board) * sizeof(*board->spathash);
88 #else
89 int ssize = 0;
90 #endif
91 #ifdef BOARD_PAT3
92 int p3size = board_size2(board) * sizeof(*board->pat3);
93 #else
94 int p3size = 0;
95 #endif
96 #ifdef BOARD_TRAITS
97 int tsize = board_size2(board) * sizeof(*board->t);
98 int tqsize = board_size2(board) * sizeof(*board->t);
99 #else
100 int tsize = 0;
101 int tqsize = 0;
102 #endif
103 #ifdef BOARD_GAMMA
104 int pbsize = board_size2(board) * sizeof(*board->prob[0].items);
105 int rowpbsize = board_size(board) * sizeof(*board->prob[0].rowtotals);
106 #else
107 int pbsize = 0;
108 int rowpbsize = 0;
109 #endif
110 int cdsize = board_size2(board) * sizeof(*board->coord);
112 size_t size = bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize;
113 void *x = malloc2(size);
115 /* board->b must come first */
116 board->b = x; x += bsize;
117 board->g = x; x += gsize;
118 board->f = x; x += fsize;
119 board->p = x; x += psize;
120 board->n = x; x += nsize;
121 board->h = x; x += hsize;
122 board->gi = x; x += gisize;
123 #ifdef WANT_BOARD_C
124 board->c = x; x += csize;
125 #endif
126 #ifdef BOARD_SPATHASH
127 board->spathash = x; x += ssize;
128 #endif
129 #ifdef BOARD_PAT3
130 board->pat3 = x; x += p3size;
131 #endif
132 #ifdef BOARD_TRAITS
133 board->t = x; x += tsize;
134 board->tq = x; x += tqsize;
135 #endif
136 #ifdef BOARD_GAMMA
137 board->prob[0].items = x; x += pbsize;
138 board->prob[1].items = x; x += pbsize;
139 board->prob[0].rowtotals = x; x += rowpbsize;
140 board->prob[1].rowtotals = x; x += rowpbsize;
141 #endif
142 board->coord = x; x += cdsize;
144 return size;
147 struct board *
148 board_copy(struct board *b2, struct board *b1)
150 memcpy(b2, b1, sizeof(struct board));
152 size_t size = board_alloc(b2);
153 memcpy(b2->b, b1->b, size);
155 // XXX: Special semantics.
156 b2->fbook = NULL;
158 return b2;
161 void
162 board_done_noalloc(struct board *board)
164 if (board->b) free(board->b);
165 if (board->fbook) fbook_done(board->fbook);
168 void
169 board_done(struct board *board)
171 board_done_noalloc(board);
172 free(board);
175 void
176 board_resize(struct board *board, int size)
178 #ifdef BOARD_SIZE
179 assert(board_size(board) == size + 2);
180 #endif
181 assert(size <= BOARD_MAX_SIZE);
182 board->size = size + 2 /* S_OFFBOARD margin */;
183 board->size2 = board_size(board) * board_size(board);
185 board->bits2 = 1;
186 while ((1 << board->bits2) < board->size2) board->bits2++;
188 if (board->b)
189 free(board->b);
191 size_t asize = board_alloc(board);
192 memset(board->b, 0, asize);
195 void
196 board_clear(struct board *board)
198 int size = board_size(board);
199 floating_t komi = board->komi;
201 board_done_noalloc(board);
202 board_setup(board);
203 board_resize(board, size - 2 /* S_OFFBOARD margin */);
205 board->komi = komi;
207 /* Setup neighborhood iterators */
208 board->nei8[0] = -size - 1; // (-1,-1)
209 board->nei8[1] = 1;
210 board->nei8[2] = 1;
211 board->nei8[3] = size - 2; // (-1,0)
212 board->nei8[4] = 2;
213 board->nei8[5] = size - 2; // (-1,1)
214 board->nei8[6] = 1;
215 board->nei8[7] = 1;
216 board->dnei[0] = -size - 1;
217 board->dnei[1] = 2;
218 board->dnei[2] = size*2 - 2;
219 board->dnei[3] = 2;
221 /* Setup initial symmetry */
222 board->symmetry.d = 1;
223 board->symmetry.x1 = board->symmetry.y1 = board_size(board) / 2;
224 board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1;
225 board->symmetry.type = SYM_FULL;
227 /* Set up coordinate cache */
228 foreach_point(board) {
229 board->coord[c][0] = c % board_size(board);
230 board->coord[c][1] = c / board_size(board);
231 } foreach_point_end;
233 /* Draw the offboard margin */
234 int top_row = board_size2(board) - board_size(board);
235 int i;
236 for (i = 0; i < board_size(board); i++)
237 board->b[i] = board->b[top_row + i] = S_OFFBOARD;
238 for (i = 0; i <= top_row; i += board_size(board))
239 board->b[i] = board->b[board_size(board) - 1 + i] = S_OFFBOARD;
241 foreach_point(board) {
242 coord_t coord = c;
243 if (board_at(board, coord) == S_OFFBOARD)
244 continue;
245 foreach_neighbor(board, c, {
246 inc_neighbor_count_at(board, coord, board_at(board, c));
247 } );
248 } foreach_point_end;
250 /* All positions are free! Except the margin. */
251 for (i = board_size(board); i < (board_size(board) - 1) * board_size(board); i++)
252 if (i % board_size(board) != 0 && i % board_size(board) != board_size(board) - 1)
253 board->f[board->flen++] = i;
255 /* Initialize zobrist hashtable. */
256 /* We will need these to be stable across Pachi runs for
257 * certain kinds of pattern matching, thus we do not use
258 * fast_random() for this. */
259 hash_t hseed = 0x3121110101112131;
260 foreach_point(board) {
261 board->h[c * 2] = (hseed *= 16807);
262 if (!board->h[c * 2])
263 board->h[c * 2] = 1;
264 /* And once again for white */
265 board->h[c * 2 + 1] = (hseed *= 16807);
266 if (!board->h[c * 2 + 1])
267 board->h[c * 2 + 1] = 1;
268 } foreach_point_end;
270 #ifdef BOARD_SPATHASH
271 /* Initialize spatial hashes. */
272 foreach_point(board) {
273 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
274 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
275 ptcoords_at(x, y, c, board, j);
276 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
277 pthashes[0][j][board_at(board, c)];
278 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
279 pthashes[0][j][stone_other(board_at(board, c))];
282 } foreach_point_end;
283 #endif
284 #ifdef BOARD_PAT3
285 /* Initialize 3x3 pattern codes. */
286 foreach_point(board) {
287 if (board_at(board, c) == S_NONE)
288 board->pat3[c] = pattern3_hash(board, c);
289 } foreach_point_end;
290 #endif
291 #ifdef BOARD_TRAITS
292 /* Initialize traits. */
293 foreach_point(board) {
294 trait_at(board, c, S_BLACK).cap = 0;
295 trait_at(board, c, S_BLACK).cap1 = 0;
296 trait_at(board, c, S_BLACK).safe = true;
297 trait_at(board, c, S_WHITE).cap = 0;
298 trait_at(board, c, S_WHITE).cap1 = 0;
299 trait_at(board, c, S_WHITE).safe = true;
300 } foreach_point_end;
301 #endif
302 #ifdef BOARD_GAMMA
303 board->prob[0].b = board->prob[1].b = board;
304 foreach_point(board) {
305 probdist_set(&board->prob[0], c, double_to_fixp((board_at(board, c) == S_NONE) * 1.0f));
306 probdist_set(&board->prob[1], c, double_to_fixp((board_at(board, c) == S_NONE) * 1.0f));
307 } foreach_point_end;
308 #endif
310 if (board->fbookfile) {
311 board->fbook = fbook_init(board->fbookfile, board);
315 static char *
316 board_print_top(struct board *board, char *s, char *end, int c)
318 for (int i = 0; i < c; i++) {
319 char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
320 s += snprintf(s, end - s, " ");
321 for (int x = 1; x < board_size(board) - 1; x++)
322 s += snprintf(s, end - s, "%c ", asdf[x - 1]);
323 s += snprintf(s, end -s, " ");
325 s += snprintf(s, end - s, "\n");
326 for (int i = 0; i < c; i++) {
327 s += snprintf(s, end - s, " +-");
328 for (int x = 1; x < board_size(board) - 1; x++)
329 s += snprintf(s, end - s, "--");
330 s += snprintf(s, end - s, "+");
332 s += snprintf(s, end - s, "\n");
333 return s;
336 static char *
337 board_print_bottom(struct board *board, char *s, char *end, int c)
339 for (int i = 0; i < c; i++) {
340 s += snprintf(s, end - s, " +-");
341 for (int x = 1; x < board_size(board) - 1; x++)
342 s += snprintf(s, end - s, "--");
343 s += snprintf(s, end - s, "+");
345 s += snprintf(s, end - s, "\n");
346 return s;
349 static char *
350 board_print_row(struct board *board, int y, char *s, char *end, board_cprint cprint)
352 s += snprintf(s, end - s, " %2d | ", y);
353 for (int x = 1; x < board_size(board) - 1; x++) {
354 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
355 s += snprintf(s, end - s, "%c)", stone2char(board_atxy(board, x, y)));
356 else
357 s += snprintf(s, end - s, "%c ", stone2char(board_atxy(board, x, y)));
359 s += snprintf(s, end - s, "|");
360 if (cprint) {
361 s += snprintf(s, end - s, " %2d | ", y);
362 for (int x = 1; x < board_size(board) - 1; x++) {
363 s = cprint(board, coord_xy(board, x, y), s, end);
365 s += snprintf(s, end - s, "|");
367 s += snprintf(s, end - s, "\n");
368 return s;
371 void
372 board_print_custom(struct board *board, FILE *f, board_cprint cprint)
374 char buf[10240];
375 char *s = buf;
376 char *end = buf + sizeof(buf);
377 s += snprintf(s, end - s, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
378 board->moves, board->komi, board->handicap,
379 board->captures[S_BLACK], board->captures[S_WHITE]);
380 s = board_print_top(board, s, end, 1 + !!cprint);
381 for (int y = board_size(board) - 2; y >= 1; y--)
382 s = board_print_row(board, y, s, end, cprint);
383 board_print_bottom(board, s, end, 1 + !!cprint);
384 fprintf(f, "%s\n", buf);
387 static char *
388 cprint_group(struct board *board, coord_t c, char *s, char *end)
390 s += snprintf(s, end - s, "%d ", group_base(group_at(board, c)));
391 return s;
394 void
395 board_print(struct board *board, FILE *f)
397 board_print_custom(board, f, DEBUGL(6) ? cprint_group : NULL);
400 void
401 board_gamma_set(struct board *b, struct features_gamma *gamma, bool precise_selfatari)
403 #ifdef BOARD_GAMMA
404 b->gamma = gamma;
405 b->precise_selfatari = precise_selfatari;
406 #ifdef BOARD_TRAITS
407 for (int i = 0; i < b->flen; i++) {
408 board_trait_recompute(b, b->f[i]);
410 #endif
411 #endif
415 /* Update the probability distribution we maintain incrementally. */
416 void
417 board_gamma_update(struct board *board, coord_t coord, enum stone color)
419 #if defined(BOARD_GAMMA) && defined(BOARD_TRAITS)
420 if (!board->gamma)
421 return;
423 /* Punch out invalid moves and moves filling our own eyes. */
424 if (board_at(board, coord) != S_NONE
425 || (board_is_eyelike(board, coord, stone_other(color))
426 && !trait_at(board, coord, color).cap)
427 || (board_is_one_point_eye(board, coord, color))) {
428 probdist_set(&board->prob[color - 1], coord, 0);
429 return;
432 hash3_t pat = board->pat3[coord];
433 if (color == S_WHITE) {
434 /* We work with the pattern3s as black-to-play. */
435 pat = pattern3_reverse(pat);
438 /* We just quickly replicate the general pattern matcher stuff
439 * here in the most bare-bone way. */
440 double value = board->gamma->gamma[FEAT_PATTERN3][pat];
441 if (trait_at(board, coord, color).cap) {
442 int i = 0;
443 i |= (trait_at(board, coord, color).cap1 == trait_at(board, coord, color).cap) << PF_CAPTURE_1STONE;
444 i |= (!trait_at(board, coord, stone_other(color)).safe) << PF_CAPTURE_TRAPPED;
445 i |= (trait_at(board, coord, color).cap < neighbor_count_at(board, coord, stone_other(color))) << PF_CAPTURE_CONNECTION;
446 value *= board->gamma->gamma[FEAT_CAPTURE][i];
448 if (trait_at(board, coord, stone_other(color)).cap) {
449 int i = 0;
450 i |= (trait_at(board, coord, stone_other(color)).cap1 == trait_at(board, coord, stone_other(color)).cap) << PF_AESCAPE_1STONE;
451 i |= (!trait_at(board, coord, color).safe) << PF_AESCAPE_TRAPPED;
452 i |= (trait_at(board, coord, stone_other(color)).cap < neighbor_count_at(board, coord, color)) << PF_AESCAPE_CONNECTION;
453 value *= board->gamma->gamma[FEAT_AESCAPE][i];
455 if (!trait_at(board, coord, color).safe)
456 value *= board->gamma->gamma[FEAT_SELFATARI][1 + board->precise_selfatari];
457 probdist_set(&board->prob[color - 1], coord, double_to_fixp(value));
458 #endif
461 #ifdef BOARD_TRAITS
462 static bool
463 board_trait_safe(struct board *board, coord_t coord, enum stone color)
465 if (board->precise_selfatari)
466 return !is_bad_selfatari(board, color, coord);
467 else
468 return board_safe_to_play(board, coord, color);
471 static void
472 board_trait_recompute(struct board *board, coord_t coord)
474 trait_at(board, coord, S_BLACK).safe = board_trait_safe(board, coord, S_BLACK);;
475 trait_at(board, coord, S_WHITE).safe = board_trait_safe(board, coord, S_WHITE);
476 if (DEBUGL(8)) {
477 fprintf(stderr, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
478 coord2sstr(coord, board), stone2str(board_at(board, coord)), immediate_liberty_count(board, coord),
479 trait_at(board, coord, S_BLACK).cap, trait_at(board, coord, S_BLACK).cap1, trait_at(board, coord, S_BLACK).safe,
480 trait_at(board, coord, S_WHITE).cap, trait_at(board, coord, S_WHITE).cap1, trait_at(board, coord, S_WHITE).safe);
482 board_gamma_update(board, coord, S_BLACK);
483 board_gamma_update(board, coord, S_WHITE);
485 #endif
487 /* Recompute traits for dirty points that we have previously touched
488 * somehow (libs of their neighbors changed or so). */
489 static void
490 board_traits_recompute(struct board *board)
492 #ifdef BOARD_TRAITS
493 for (int i = 0; i < board->tqlen; i++) {
494 coord_t coord = board->tq[i];
495 trait_at(board, coord, S_BLACK).dirty = false;
496 if (board_at(board, coord) != S_NONE)
497 continue;
498 board_trait_recompute(board, coord);
500 board->tqlen = 0;
501 #endif
504 /* Queue traits of given point for recomputing. */
505 static void
506 board_trait_queue(struct board *board, coord_t coord)
508 #ifdef BOARD_TRAITS
509 if (trait_at(board, coord, S_BLACK).dirty)
510 return;
511 board->tq[board->tqlen++] = coord;
512 trait_at(board, coord, S_BLACK).dirty = true;
513 #endif
517 /* Update board hash with given coordinate. */
518 static void profiling_noinline
519 board_hash_update(struct board *board, coord_t coord, enum stone color)
521 board->hash ^= hash_at(board, coord, color);
522 board->qhash[coord_quadrant(coord, board)] ^= hash_at(board, coord, color);
523 if (DEBUGL(8))
524 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);
526 #ifdef BOARD_SPATHASH
527 /* Gridcular metric is reflective, so we update all hashes
528 * of appropriate ditance in OUR circle. */
529 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
530 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
531 ptcoords_at(x, y, coord, board, j);
532 /* We either changed from S_NONE to color
533 * or vice versa; doesn't matter. */
534 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
535 pthashes[0][j][color] ^ pthashes[0][j][S_NONE];
536 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
537 pthashes[0][j][stone_other(color)] ^ pthashes[0][j][S_NONE];
540 #endif
542 #if defined(BOARD_PAT3)
543 /* @color is not what we need in case of capture. */
544 static const int ataribits[8] = { -1, 0, -1, 1, 2, -1, 3, -1 };
545 enum stone new_color = board_at(board, coord);
546 bool in_atari = false;
547 if (new_color == S_NONE) {
548 board->pat3[coord] = pattern3_hash(board, coord);
549 } else {
550 in_atari = (board_group_info(board, group_at(board, coord)).libs == 1);
552 foreach_8neighbor(board, coord) {
553 /* Internally, the loop uses fn__i=[0..7]. We can use
554 * it directly to address bits within the bitmap of the
555 * neighbors since the bitmap order is reverse to the
556 * loop order. */
557 if (board_at(board, c) != S_NONE)
558 continue;
559 board->pat3[c] &= ~(3 << (fn__i*2));
560 board->pat3[c] |= new_color << (fn__i*2);
561 if (ataribits[fn__i] >= 0) {
562 board->pat3[c] &= ~(1 << (16 + ataribits[fn__i]));
563 board->pat3[c] |= in_atari << (16 + ataribits[fn__i]);
565 #if defined(BOARD_TRAITS)
566 board_trait_queue(board, c);
567 #elif defined(BOARD_GAMMA)
568 if (board->gamma) {
569 hash3_t pat = board->pat3[c];
570 if (color == S_WHITE) pat = pattern3_reverse(pat);
571 double value = board->gamma->gamma[FEAT_PATTERN3][pat];
572 probdist_set(&board->prob[color - 1], c, double_to_fixp(value));
574 #endif
575 } foreach_8neighbor_end;
576 #endif
579 /* Commit current board hash to history. */
580 static void profiling_noinline
581 board_hash_commit(struct board *board)
583 if (DEBUGL(8))
584 fprintf(stderr, "board_hash_commit %"PRIhash"\n", board->hash);
585 if (likely(board->history_hash[board->hash & history_hash_mask]) == 0) {
586 board->history_hash[board->hash & history_hash_mask] = board->hash;
587 } else {
588 hash_t i = board->hash;
589 while (board->history_hash[i & history_hash_mask]) {
590 if (board->history_hash[i & history_hash_mask] == board->hash) {
591 if (DEBUGL(5))
592 fprintf(stderr, "SUPERKO VIOLATION noted at %d,%d\n",
593 coord_x(board->last_move.coord, board), coord_y(board->last_move.coord, board));
594 board->superko_violation = true;
595 return;
597 i = history_hash_next(i);
599 board->history_hash[i & history_hash_mask] = board->hash;
604 void
605 board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c)
607 if (likely(symmetry->type == SYM_NONE)) {
608 /* Fully degenerated already. We do not support detection
609 * of restoring of symmetry, assuming that this is too rare
610 * a case to handle. */
611 return;
614 int x = coord_x(c, b), y = coord_y(c, b), t = board_size(b) / 2;
615 int dx = board_size(b) - 1 - x; /* for SYM_DOWN */
616 if (DEBUGL(6)) {
617 fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
618 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
619 symmetry->d, symmetry->type, x, y);
622 switch (symmetry->type) {
623 case SYM_FULL:
624 if (x == t && y == t) {
625 /* Tengen keeps full symmetry. */
626 return;
628 /* New symmetry now? */
629 if (x == y) {
630 symmetry->type = SYM_DIAG_UP;
631 symmetry->x1 = symmetry->y1 = 1;
632 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
633 symmetry->d = 1;
634 } else if (dx == y) {
635 symmetry->type = SYM_DIAG_DOWN;
636 symmetry->x1 = symmetry->y1 = 1;
637 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
638 symmetry->d = 1;
639 } else if (x == t) {
640 symmetry->type = SYM_HORIZ;
641 symmetry->y1 = 1;
642 symmetry->y2 = board_size(b) - 1;
643 symmetry->d = 0;
644 } else if (y == t) {
645 symmetry->type = SYM_VERT;
646 symmetry->x1 = 1;
647 symmetry->x2 = board_size(b) - 1;
648 symmetry->d = 0;
649 } else {
650 break_symmetry:
651 symmetry->type = SYM_NONE;
652 symmetry->x1 = symmetry->y1 = 1;
653 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
654 symmetry->d = 0;
656 break;
657 case SYM_DIAG_UP:
658 if (x == y)
659 return;
660 goto break_symmetry;
661 case SYM_DIAG_DOWN:
662 if (dx == y)
663 return;
664 goto break_symmetry;
665 case SYM_HORIZ:
666 if (x == t)
667 return;
668 goto break_symmetry;
669 case SYM_VERT:
670 if (y == t)
671 return;
672 goto break_symmetry;
673 case SYM_NONE:
674 assert(0);
675 break;
678 if (DEBUGL(6)) {
679 fprintf(stderr, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
680 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
681 symmetry->d, symmetry->type);
683 /* Whew. */
687 void
688 board_handicap_stone(struct board *board, int x, int y, FILE *f)
690 struct move m;
691 m.color = S_BLACK; m.coord = coord_xy(board, x, y);
693 board_play(board, &m);
694 /* Simulate white passing; otherwise, UCT search can get confused since
695 * tree depth parity won't match the color to move. */
696 board->moves++;
698 char *str = coord2str(m.coord, board);
699 if (DEBUGL(1))
700 fprintf(stderr, "choosing handicap %s (%d,%d)\n", str, x, y);
701 if (f) fprintf(f, "%s ", str);
702 free(str);
705 void
706 board_handicap(struct board *board, int stones, FILE *f)
708 int margin = 3 + (board_size(board) >= 13);
709 int min = margin;
710 int mid = board_size(board) / 2;
711 int max = board_size(board) - 1 - margin;
712 const int places[][2] = {
713 { min, min }, { max, max }, { max, min }, { min, max },
714 { min, mid }, { max, mid },
715 { mid, min }, { mid, max },
716 { mid, mid },
719 board->handicap = stones;
721 if (stones == 5 || stones == 7) {
722 board_handicap_stone(board, mid, mid, f);
723 stones--;
726 int i;
727 for (i = 0; i < stones; i++)
728 board_handicap_stone(board, places[i][0], places[i][1], f);
732 static void __attribute__((noinline))
733 check_libs_consistency(struct board *board, group_t g)
735 #ifdef DEBUG
736 if (!g) return;
737 struct group *gi = &board_group_info(board, g);
738 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
739 if (gi->lib[i] && board_at(board, gi->lib[i]) != S_NONE) {
740 fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(group_base(g), board));
741 assert(0);
743 #endif
746 static void
747 check_pat3_consistency(struct board *board, coord_t coord)
749 #ifdef DEBUG
750 foreach_8neighbor(board, coord) {
751 if (board_at(board, c) == S_NONE && pattern3_hash(board, c) != board->pat3[c]) {
752 board_print(board, stderr);
753 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);
754 assert(0);
756 } foreach_8neighbor_end;
757 #endif
760 static void
761 board_capturable_add(struct board *board, group_t group, coord_t lib, bool onestone)
763 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
764 #ifdef BOARD_TRAITS
765 /* Increase capturable count trait of my last lib. */
766 enum stone capturing_color = stone_other(board_at(board, group));
767 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
768 foreach_neighbor(board, lib, {
769 if (DEBUGL(8) && group_at(board, c) == group)
770 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);
771 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group);
772 trait_at(board, lib, capturing_color).cap1 += (group_at(board, c) == group && onestone);
774 board_trait_queue(board, lib);
775 #endif
777 #ifdef BOARD_PAT3
778 int fn__i = 0;
779 foreach_neighbor(board, lib, {
780 board->pat3[lib] |= (group_at(board, c) == group) << (16 + 3 - fn__i);
781 fn__i++;
783 #endif
785 #ifdef WANT_BOARD_C
786 /* Update the list of capturable groups. */
787 assert(group);
788 assert(board->clen < board_size2(board));
789 board->c[board->clen++] = group;
790 #endif
792 static void
793 board_capturable_rm(struct board *board, group_t group, coord_t lib, bool onestone)
795 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
796 #ifdef BOARD_TRAITS
797 /* Decrease capturable count trait of my previously-last lib. */
798 enum stone capturing_color = stone_other(board_at(board, group));
799 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
800 foreach_neighbor(board, lib, {
801 if (DEBUGL(8) && group_at(board, c) == group)
802 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);
803 trait_at(board, lib, capturing_color).cap -= (group_at(board, c) == group);
804 trait_at(board, lib, capturing_color).cap1 -= (group_at(board, c) == group && onestone);
806 board_trait_queue(board, lib);
807 #endif
809 #ifdef BOARD_PAT3
810 int fn__i = 0;
811 foreach_neighbor(board, lib, {
812 board->pat3[lib] &= ~((group_at(board, c) == group) << (16 + 3 - fn__i));
813 fn__i++;
815 #endif
817 #ifdef WANT_BOARD_C
818 /* Update the list of capturable groups. */
819 for (int i = 0; i < board->clen; i++) {
820 if (unlikely(board->c[i] == group)) {
821 board->c[i] = board->c[--board->clen];
822 return;
825 fprintf(stderr, "rm of bad group %d\n", group_base(group));
826 assert(0);
827 #endif
830 static void
831 board_atariable_add(struct board *board, group_t group, coord_t lib1, coord_t lib2)
833 #ifdef BOARD_TRAITS
834 board_trait_queue(board, lib1);
835 board_trait_queue(board, lib2);
836 #endif
838 static void
839 board_atariable_rm(struct board *board, group_t group, coord_t lib1, coord_t lib2)
841 #ifdef BOARD_TRAITS
842 board_trait_queue(board, lib1);
843 board_trait_queue(board, lib2);
844 #endif
847 static void
848 board_group_addlib(struct board *board, group_t group, coord_t coord)
850 if (DEBUGL(7)) {
851 fprintf(stderr, "Group %d[%s] %d: Adding liberty %s\n",
852 group_base(group), coord2sstr(group_base(group), board),
853 board_group_info(board, group).libs, coord2sstr(coord, board));
856 check_libs_consistency(board, group);
858 struct group *gi = &board_group_info(board, group);
859 bool onestone = group_is_onestone(board, group);
860 if (gi->libs < GROUP_KEEP_LIBS) {
861 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
862 #if 0
863 /* Seems extra branch just slows it down */
864 if (!gi->lib[i])
865 break;
866 #endif
867 if (unlikely(gi->lib[i] == coord))
868 return;
870 if (gi->libs == 0) {
871 board_capturable_add(board, group, coord, onestone);
872 } else if (gi->libs == 1) {
873 board_capturable_rm(board, group, gi->lib[0], onestone);
874 board_atariable_add(board, group, gi->lib[0], coord);
875 } else if (gi->libs == 2) {
876 board_atariable_rm(board, group, gi->lib[0], gi->lib[1]);
878 gi->lib[gi->libs++] = coord;
881 check_libs_consistency(board, group);
884 static void
885 board_group_find_extra_libs(struct board *board, group_t group, struct group *gi, coord_t avoid)
887 /* Add extra liberty from the board to our liberty list. */
888 unsigned char watermark[board_size2(board) / 8];
889 memset(watermark, 0, sizeof(watermark));
890 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
891 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
893 for (int i = 0; i < GROUP_KEEP_LIBS - 1; i++)
894 watermark_set(gi->lib[i]);
895 watermark_set(avoid);
897 foreach_in_group(board, group) {
898 coord_t coord2 = c;
899 foreach_neighbor(board, coord2, {
900 if (board_at(board, c) + watermark_get(c) != S_NONE)
901 continue;
902 watermark_set(c);
903 gi->lib[gi->libs++] = c;
904 if (unlikely(gi->libs >= GROUP_KEEP_LIBS))
905 return;
906 } );
907 } foreach_in_group_end;
908 #undef watermark_get
909 #undef watermark_set
912 static void
913 board_group_rmlib(struct board *board, group_t group, coord_t coord)
915 if (DEBUGL(7)) {
916 fprintf(stderr, "Group %d[%s] %d: Removing liberty %s\n",
917 group_base(group), coord2sstr(group_base(group), board),
918 board_group_info(board, group).libs, coord2sstr(coord, board));
921 struct group *gi = &board_group_info(board, group);
922 bool onestone = group_is_onestone(board, group);
923 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
924 #if 0
925 /* Seems extra branch just slows it down */
926 if (!gi->lib[i])
927 break;
928 #endif
929 if (likely(gi->lib[i] != coord))
930 continue;
932 coord_t lib = gi->lib[i] = gi->lib[--gi->libs];
933 gi->lib[gi->libs] = 0;
935 check_libs_consistency(board, group);
937 /* Postpone refilling lib[] until we need to. */
938 assert(GROUP_REFILL_LIBS > 1);
939 if (gi->libs > GROUP_REFILL_LIBS)
940 return;
941 if (gi->libs == GROUP_REFILL_LIBS)
942 board_group_find_extra_libs(board, group, gi, coord);
944 if (gi->libs == 2) {
945 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
946 } else if (gi->libs == 1) {
947 board_capturable_add(board, group, gi->lib[0], onestone);
948 board_atariable_rm(board, group, gi->lib[0], lib);
949 } else if (gi->libs == 0)
950 board_capturable_rm(board, group, lib, onestone);
951 return;
954 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
955 * can call this multiple times per coord. */
956 check_libs_consistency(board, group);
957 return;
961 /* This is a low-level routine that doesn't maintain consistency
962 * of all the board data structures. */
963 static void
964 board_remove_stone(struct board *board, group_t group, coord_t c)
966 enum stone color = board_at(board, c);
967 board_at(board, c) = S_NONE;
968 group_at(board, c) = 0;
969 board_hash_update(board, c, color);
970 #ifdef BOARD_TRAITS
971 /* We mark as cannot-capture now. If this is a ko/snapback,
972 * we will get incremented later in board_group_addlib(). */
973 trait_at(board, c, S_BLACK).cap = trait_at(board, c, S_BLACK).cap1 = 0;
974 trait_at(board, c, S_WHITE).cap = trait_at(board, c, S_WHITE).cap1 = 0;
975 board_trait_queue(board, c);
976 #endif
978 /* Increase liberties of surrounding groups */
979 coord_t coord = c;
980 foreach_neighbor(board, coord, {
981 dec_neighbor_count_at(board, c, color);
982 board_trait_queue(board, c);
983 group_t g = group_at(board, c);
984 if (g && g != group)
985 board_group_addlib(board, g, coord);
988 #ifdef BOARD_PAT3
989 /* board_hash_update() might have seen the freed up point as able
990 * to capture another group in atari that only after the loop
991 * above gained enough liberties. Reset pat3 again. */
992 board->pat3[c] = pattern3_hash(board, c);
993 #endif
995 if (DEBUGL(6))
996 fprintf(stderr, "pushing free move [%d]: %d,%d\n", board->flen, coord_x(c, board), coord_y(c, board));
997 board->f[board->flen++] = c;
1000 static int profiling_noinline
1001 board_group_capture(struct board *board, group_t group)
1003 int stones = 0;
1005 foreach_in_group(board, group) {
1006 board->captures[stone_other(board_at(board, c))]++;
1007 board_remove_stone(board, group, c);
1008 stones++;
1009 } foreach_in_group_end;
1011 struct group *gi = &board_group_info(board, group);
1012 assert(gi->libs == 0);
1013 memset(gi, 0, sizeof(*gi));
1015 return stones;
1019 static void profiling_noinline
1020 add_to_group(struct board *board, group_t group, coord_t prevstone, coord_t coord)
1022 #ifdef BOARD_TRAITS
1023 struct group *gi = &board_group_info(board, group);
1024 bool onestone = group_is_onestone(board, group);
1026 if (gi->libs == 1) {
1027 /* Our group is temporarily in atari; make sure the capturable
1028 * counts also correspond to the newly added stone before we
1029 * start adding liberties again so bump-dump ops match. */
1030 enum stone capturing_color = stone_other(board_at(board, group));
1031 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
1033 coord_t lib = board_group_info(board, group).lib[0];
1034 if (coord_is_adjecent(lib, coord, board)) {
1035 if (DEBUGL(8))
1036 fprintf(stderr, "add_to_group %s: %s[%d] bump\n", coord2sstr(group, board), coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
1037 trait_at(board, lib, capturing_color).cap++;
1038 /* This is never a 1-stone group, obviously. */
1039 board_trait_queue(board, lib);
1042 if (onestone) {
1043 /* We are not 1-stone group anymore, update the cap1
1044 * counter specifically. */
1045 foreach_neighbor(board, group, {
1046 if (board_at(board, c) != S_NONE) continue;
1047 trait_at(board, c, capturing_color).cap1--;
1048 board_trait_queue(board, c);
1052 #endif
1054 group_at(board, coord) = group;
1055 groupnext_at(board, coord) = groupnext_at(board, prevstone);
1056 groupnext_at(board, prevstone) = coord;
1058 foreach_neighbor(board, coord, {
1059 if (board_at(board, c) == S_NONE)
1060 board_group_addlib(board, group, c);
1063 if (DEBUGL(8))
1064 fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1065 coord_x(prevstone, board), coord_y(prevstone, board),
1066 coord_x(coord, board), coord_y(coord, board),
1067 groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board),
1068 group_base(group));
1071 static void profiling_noinline
1072 merge_groups(struct board *board, group_t group_to, group_t group_from)
1074 if (DEBUGL(7))
1075 fprintf(stderr, "board_play_raw: merging groups %d -> %d\n",
1076 group_base(group_from), group_base(group_to));
1077 struct group *gi_from = &board_group_info(board, group_from);
1078 struct group *gi_to = &board_group_info(board, group_to);
1079 bool onestone_from = group_is_onestone(board, group_from);
1080 bool onestone_to = group_is_onestone(board, group_to);
1082 /* We do this early before the group info is rewritten. */
1083 if (gi_from->libs == 2)
1084 board_atariable_rm(board, group_from, gi_from->lib[0], gi_from->lib[1]);
1085 else if (gi_from->libs == 1)
1086 board_capturable_rm(board, group_from, gi_from->lib[0], onestone_from);
1088 if (DEBUGL(7))
1089 fprintf(stderr,"---- (froml %d, tol %d)\n", gi_from->libs, gi_to->libs);
1091 if (gi_to->libs < GROUP_KEEP_LIBS) {
1092 for (int i = 0; i < gi_from->libs; i++) {
1093 for (int j = 0; j < gi_to->libs; j++)
1094 if (gi_to->lib[j] == gi_from->lib[i])
1095 goto next_from_lib;
1096 if (gi_to->libs == 0) {
1097 board_capturable_add(board, group_to, gi_from->lib[i], onestone_to);
1098 } else if (gi_to->libs == 1) {
1099 board_capturable_rm(board, group_to, gi_to->lib[0], onestone_to);
1100 board_atariable_add(board, group_to, gi_to->lib[0], gi_from->lib[i]);
1101 } else if (gi_to->libs == 2) {
1102 board_atariable_rm(board, group_to, gi_to->lib[0], gi_to->lib[1]);
1104 gi_to->lib[gi_to->libs++] = gi_from->lib[i];
1105 if (gi_to->libs >= GROUP_KEEP_LIBS)
1106 break;
1107 next_from_lib:;
1111 if (gi_to->libs == 1) {
1112 coord_t lib = board_group_info(board, group_to).lib[0];
1113 #ifdef BOARD_TRAITS
1114 enum stone capturing_color = stone_other(board_at(board, group_to));
1115 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
1117 /* Our group is currently in atari; make sure we properly
1118 * count in even the neighbors from the other group in the
1119 * capturable counter. */
1120 foreach_neighbor(board, lib, {
1121 if (DEBUGL(8) && group_at(board, c) == group_from)
1122 fprintf(stderr, "%s[%d] cap bump\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
1123 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group_from);
1124 /* This is never a 1-stone group, obviously. */
1126 board_trait_queue(board, lib);
1128 if (onestone_to) {
1129 /* We are not 1-stone group anymore, update the cap1
1130 * counter specifically. */
1131 foreach_neighbor(board, group_to, {
1132 if (board_at(board, c) != S_NONE) continue;
1133 trait_at(board, c, capturing_color).cap1--;
1134 board_trait_queue(board, c);
1137 #endif
1138 #ifdef BOARD_PAT3
1139 if (gi_from->libs == 1) {
1140 /* We removed group_from from capturable groups,
1141 * therefore switching the atari flag off.
1142 * We need to set it again since group_to is also
1143 * capturable. */
1144 int fn__i = 0;
1145 foreach_neighbor(board, lib, {
1146 board->pat3[lib] |= (group_at(board, c) == group_from) << (16 + 3 - fn__i);
1147 fn__i++;
1150 #endif
1153 coord_t last_in_group;
1154 foreach_in_group(board, group_from) {
1155 last_in_group = c;
1156 group_at(board, c) = group_to;
1157 } foreach_in_group_end;
1158 groupnext_at(board, last_in_group) = groupnext_at(board, group_base(group_to));
1159 groupnext_at(board, group_base(group_to)) = group_base(group_from);
1160 memset(gi_from, 0, sizeof(struct group));
1162 if (DEBUGL(7))
1163 fprintf(stderr, "board_play_raw: merged group: %d\n",
1164 group_base(group_to));
1167 static group_t profiling_noinline
1168 new_group(struct board *board, coord_t coord)
1170 group_t group = coord;
1171 struct group *gi = &board_group_info(board, group);
1172 foreach_neighbor(board, coord, {
1173 if (board_at(board, c) == S_NONE)
1174 /* board_group_addlib is ridiculously expensive for us */
1175 #if GROUP_KEEP_LIBS < 4
1176 if (gi->libs < GROUP_KEEP_LIBS)
1177 #endif
1178 gi->lib[gi->libs++] = c;
1181 group_at(board, coord) = group;
1182 groupnext_at(board, coord) = 0;
1184 if (gi->libs == 2)
1185 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
1186 else if (gi->libs == 1)
1187 board_capturable_add(board, group, gi->lib[0], true);
1188 check_libs_consistency(board, group);
1190 if (DEBUGL(8))
1191 fprintf(stderr, "new_group: added %d,%d to group %d\n",
1192 coord_x(coord, board), coord_y(coord, board),
1193 group_base(group));
1195 return group;
1198 static inline group_t
1199 play_one_neighbor(struct board *board,
1200 coord_t coord, enum stone color, enum stone other_color,
1201 coord_t c, group_t group)
1203 enum stone ncolor = board_at(board, c);
1204 group_t ngroup = group_at(board, c);
1206 inc_neighbor_count_at(board, c, color);
1207 /* We can be S_NONE, in that case we need to update the safety
1208 * trait since we might be left with only one liberty. */
1209 board_trait_queue(board, c);
1211 if (!ngroup)
1212 return group;
1214 board_group_rmlib(board, ngroup, coord);
1215 if (DEBUGL(7))
1216 fprintf(stderr, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1217 group_base(ngroup), ncolor, color, other_color);
1219 if (ncolor == color && ngroup != group) {
1220 if (!group) {
1221 group = ngroup;
1222 add_to_group(board, group, c, coord);
1223 } else {
1224 merge_groups(board, group, ngroup);
1226 } else if (ncolor == other_color) {
1227 if (DEBUGL(8)) {
1228 struct group *gi = &board_group_info(board, ngroup);
1229 fprintf(stderr, "testing captured group %d[%s]: ", group_base(ngroup), coord2sstr(group_base(ngroup), board));
1230 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
1231 fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board));
1232 fprintf(stderr, "\n");
1234 if (unlikely(board_group_captured(board, ngroup)))
1235 board_group_capture(board, ngroup);
1237 return group;
1240 /* We played on a place with at least one liberty. We will become a member of
1241 * some group for sure. */
1242 static group_t profiling_noinline
1243 board_play_outside(struct board *board, struct move *m, int f)
1245 coord_t coord = m->coord;
1246 enum stone color = m->color;
1247 enum stone other_color = stone_other(color);
1248 group_t group = 0;
1250 board->f[f] = board->f[--board->flen];
1251 if (DEBUGL(6))
1252 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1254 #if defined(BOARD_TRAITS) && defined(DEBUG)
1255 /* Sanity check that cap matches reality. */
1257 int a = 0, b = 0;
1258 foreach_neighbor(board, coord, {
1259 group_t g = group_at(board, c);
1260 a += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1);
1261 b += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1) && group_is_onestone(board, g);
1263 assert(a == trait_at(board, coord, color).cap);
1264 assert(b == trait_at(board, coord, color).cap1);
1265 assert(board_trait_safe(board, coord, color) == trait_at(board, coord, color).safe);
1267 #endif
1268 foreach_neighbor(board, coord, {
1269 group = play_one_neighbor(board, coord, color, other_color, c, group);
1272 board_at(board, coord) = color;
1273 if (unlikely(!group))
1274 group = new_group(board, coord);
1275 board_gamma_update(board, coord, S_BLACK);
1276 board_gamma_update(board, coord, S_WHITE);
1278 board->last_move2 = board->last_move;
1279 board->last_move = *m;
1280 board->moves++;
1281 board_hash_update(board, coord, color);
1282 board_symmetry_update(board, &board->symmetry, coord);
1283 struct move ko = { pass, S_NONE };
1284 board->ko = ko;
1286 check_pat3_consistency(board, coord);
1288 return group;
1291 /* We played in an eye-like shape. Either we capture at least one of the eye
1292 * sides in the process of playing, or return -1. */
1293 static int profiling_noinline
1294 board_play_in_eye(struct board *board, struct move *m, int f)
1296 coord_t coord = m->coord;
1297 enum stone color = m->color;
1298 /* Check ko: Capture at a position of ko capture one move ago */
1299 if (unlikely(color == board->ko.color && coord == board->ko.coord)) {
1300 if (DEBUGL(5))
1301 fprintf(stderr, "board_check: ko at %d,%d color %d\n", coord_x(coord, board), coord_y(coord, board), color);
1302 return -1;
1303 } else if (DEBUGL(6)) {
1304 fprintf(stderr, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1305 color, coord_x(coord, board), coord_y(coord, board),
1306 board->ko.color, coord_x(board->ko.coord, board), coord_y(board->ko.coord, board));
1309 struct move ko = { pass, S_NONE };
1311 int captured_groups = 0;
1313 foreach_neighbor(board, coord, {
1314 group_t g = group_at(board, c);
1315 if (DEBUGL(7))
1316 fprintf(stderr, "board_check: group %d has %d libs\n",
1317 g, board_group_info(board, g).libs);
1318 captured_groups += (board_group_info(board, g).libs == 1);
1321 if (likely(captured_groups == 0)) {
1322 if (DEBUGL(5)) {
1323 if (DEBUGL(6))
1324 board_print(board, stderr);
1325 fprintf(stderr, "board_check: one-stone suicide\n");
1328 return -1;
1330 #ifdef BOARD_TRAITS
1331 /* We _will_ for sure capture something. */
1332 assert(trait_at(board, coord, color).cap > 0);
1333 assert(trait_at(board, coord, color).safe == board_trait_safe(board, coord, color));
1334 #endif
1336 board->f[f] = board->f[--board->flen];
1337 if (DEBUGL(6))
1338 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1340 foreach_neighbor(board, coord, {
1341 inc_neighbor_count_at(board, c, color);
1342 /* Originally, this could not have changed any trait
1343 * since no neighbors were S_NONE, however by now some
1344 * of them might be removed from the board. */
1345 board_trait_queue(board, c);
1347 group_t group = group_at(board, c);
1348 if (!group)
1349 continue;
1351 board_group_rmlib(board, group, coord);
1352 if (DEBUGL(7))
1353 fprintf(stderr, "board_play_raw: reducing libs for group %d\n",
1354 group_base(group));
1356 if (board_group_captured(board, group)) {
1357 if (board_group_capture(board, group) == 1) {
1358 /* If we captured multiple groups at once,
1359 * we can't be fighting ko so we don't need
1360 * to check for that. */
1361 ko.color = stone_other(color);
1362 ko.coord = c;
1363 board->last_ko = ko;
1364 board->last_ko_age = board->moves;
1365 if (DEBUGL(5))
1366 fprintf(stderr, "guarding ko at %d,%s\n", ko.color, coord2sstr(ko.coord, board));
1371 board_at(board, coord) = color;
1372 group_t group = new_group(board, coord);
1373 board_gamma_update(board, coord, S_BLACK);
1374 board_gamma_update(board, coord, S_WHITE);
1376 board->last_move2 = board->last_move;
1377 board->last_move = *m;
1378 board->moves++;
1379 board_hash_update(board, coord, color);
1380 board_hash_commit(board);
1381 board_traits_recompute(board);
1382 board_symmetry_update(board, &board->symmetry, coord);
1383 board->ko = ko;
1385 check_pat3_consistency(board, coord);
1387 return !!group;
1390 static int __attribute__((flatten))
1391 board_play_f(struct board *board, struct move *m, int f)
1393 if (DEBUGL(7)) {
1394 fprintf(stderr, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m->coord, board), coord_x(m->coord, board), coord_y(m->coord, board));
1396 if (likely(!board_is_eyelike(board, m->coord, stone_other(m->color)))) {
1397 /* NOT playing in an eye. Thus this move has to succeed. (This
1398 * is thanks to New Zealand rules. Otherwise, multi-stone
1399 * suicide might fail.) */
1400 group_t group = board_play_outside(board, m, f);
1401 if (unlikely(board_group_captured(board, group))) {
1402 board_group_capture(board, group);
1404 board_hash_commit(board);
1405 board_traits_recompute(board);
1406 return 0;
1407 } else {
1408 return board_play_in_eye(board, m, f);
1413 board_play(struct board *board, struct move *m)
1415 if (unlikely(is_pass(m->coord) || is_resign(m->coord))) {
1416 struct move nomove = { pass, S_NONE };
1417 board->ko = nomove;
1418 board->last_move2 = board->last_move;
1419 board->last_move = *m;
1420 return 0;
1423 int f;
1424 for (f = 0; f < board->flen; f++)
1425 if (board->f[f] == m->coord)
1426 return board_play_f(board, m, f);
1428 if (DEBUGL(7))
1429 fprintf(stderr, "board_check: stone exists\n");
1430 return -1;
1434 static inline bool
1435 board_try_random_move(struct board *b, enum stone color, coord_t *coord, int f, ppr_permit permit, void *permit_data)
1437 *coord = b->f[f];
1438 struct move m = { *coord, color };
1439 if (DEBUGL(6))
1440 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));
1441 if (unlikely(board_is_one_point_eye(b, *coord, color)) /* bad idea to play into one, usually */
1442 || !board_is_valid_move(b, &m)
1443 || (permit && !permit(permit_data, b, &m)))
1444 return false;
1445 if (m.coord == *coord) {
1446 return likely(board_play_f(b, &m, f) >= 0);
1447 } else {
1448 *coord = m.coord; // permit modified the coordinate
1449 return likely(board_play(b, &m) >= 0);
1453 void
1454 board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data)
1456 if (unlikely(b->flen == 0))
1457 goto pass;
1459 int base = fast_random(b->flen), f;
1460 for (f = base; f < b->flen; f++)
1461 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1462 return;
1463 for (f = 0; f < base; f++)
1464 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1465 return;
1467 pass:
1468 *coord = pass;
1469 struct move m = { pass, color };
1470 board_play(b, &m);
1474 bool
1475 board_is_false_eyelike(struct board *board, coord_t coord, enum stone eye_color)
1477 enum stone color_diag_libs[S_MAX] = {0, 0, 0, 0};
1479 /* XXX: We attempt false eye detection but we will yield false
1480 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1482 foreach_diag_neighbor(board, coord) {
1483 color_diag_libs[(enum stone) board_at(board, c)]++;
1484 } foreach_diag_neighbor_end;
1485 /* For false eye, we need two enemy stones diagonally in the
1486 * middle of the board, or just one enemy stone at the edge
1487 * or in the corner. */
1488 color_diag_libs[stone_other(eye_color)] += !!color_diag_libs[S_OFFBOARD];
1489 return color_diag_libs[stone_other(eye_color)] >= 2;
1492 bool
1493 board_is_one_point_eye(struct board *board, coord_t coord, enum stone eye_color)
1495 return board_is_eyelike(board, coord, eye_color)
1496 && !board_is_false_eyelike(board, coord, eye_color);
1499 enum stone
1500 board_get_one_point_eye(struct board *board, coord_t coord)
1502 if (board_is_one_point_eye(board, coord, S_WHITE))
1503 return S_WHITE;
1504 else if (board_is_one_point_eye(board, coord, S_BLACK))
1505 return S_BLACK;
1506 else
1507 return S_NONE;
1511 floating_t
1512 board_fast_score(struct board *board)
1514 int scores[S_MAX];
1515 memset(scores, 0, sizeof(scores));
1517 foreach_point(board) {
1518 enum stone color = board_at(board, c);
1519 if (color == S_NONE)
1520 color = board_get_one_point_eye(board, c);
1521 scores[color]++;
1522 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1523 } foreach_point_end;
1525 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];
1528 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1530 /* One flood-fill iteration; returns true if next iteration
1531 * is required. */
1532 static bool
1533 board_tromp_taylor_iter(struct board *board, int *ownermap)
1535 bool needs_update = false;
1536 foreach_free_point(board) {
1537 /* Ignore occupied and already-dame positions. */
1538 assert(board_at(board, c) == S_NONE);
1539 if (ownermap[c] == 3)
1540 continue;
1541 /* Count neighbors. */
1542 int nei[4] = {0};
1543 foreach_neighbor(board, c, {
1544 nei[ownermap[c]]++;
1546 /* If we have neighbors of both colors, or dame,
1547 * we are dame too. */
1548 if ((nei[1] && nei[2]) || nei[3]) {
1549 ownermap[c] = 3;
1550 /* Speed up the propagation. */
1551 foreach_neighbor(board, c, {
1552 if (board_at(board, c) == S_NONE)
1553 ownermap[c] = 3;
1555 needs_update = true;
1556 continue;
1558 /* If we have neighbors of one color, we are owned
1559 * by that color, too. */
1560 if (!ownermap[c] && (nei[1] || nei[2])) {
1561 int newowner = nei[1] ? 1 : 2;
1562 ownermap[c] = newowner;
1563 /* Speed up the propagation. */
1564 foreach_neighbor(board, c, {
1565 if (board_at(board, c) == S_NONE && !ownermap[c])
1566 ownermap[c] = newowner;
1568 needs_update = true;
1569 continue;
1571 } foreach_free_point_end;
1572 return needs_update;
1575 /* Tromp-Taylor Counting */
1576 floating_t
1577 board_official_score(struct board *board, struct move_queue *q)
1580 /* A point P, not colored C, is said to reach C, if there is a path of
1581 * (vertically or horizontally) adjacent points of P's color from P to
1582 * a point of color C.
1584 * A player's score is the number of points of her color, plus the
1585 * number of empty points that reach only her color. */
1587 int ownermap[board_size2(board)];
1588 int s[4] = {0};
1589 const int o[4] = {0, 1, 2, 0};
1590 foreach_point(board) {
1591 ownermap[c] = o[board_at(board, c)];
1592 s[board_at(board, c)]++;
1593 } foreach_point_end;
1595 if (q) {
1596 /* Process dead groups. */
1597 for (unsigned int i = 0; i < q->moves; i++) {
1598 foreach_in_group(board, q->move[i]) {
1599 enum stone color = board_at(board, c);
1600 ownermap[c] = o[stone_other(color)];
1601 s[color]--; s[stone_other(color)]++;
1602 } foreach_in_group_end;
1606 /* We need to special-case empty board. */
1607 if (!s[S_BLACK] && !s[S_WHITE])
1608 return board->komi + board->handicap;
1610 while (board_tromp_taylor_iter(board, ownermap))
1611 /* Flood-fill... */;
1613 int scores[S_MAX];
1614 memset(scores, 0, sizeof(scores));
1616 foreach_point(board) {
1617 assert(board_at(board, c) == S_OFFBOARD || ownermap[c] != 0);
1618 if (ownermap[c] == 3)
1619 continue;
1620 scores[ownermap[c]]++;
1621 } foreach_point_end;
1623 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];