Introduce cap1 trait and PF_*_1STONE for single-stone captures/escapes
[pachi.git] / board.c
blob8008cf807ec3be735b9cdacf9361666979d3d3cf
1 #include <alloca.h>
2 #include <assert.h>
3 #include <math.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
8 #include "board.h"
9 #include "debug.h"
10 #include "mq.h"
11 #include "random.h"
13 #ifdef BOARD_SPATHASH
14 #include "patternsp.h"
15 #endif
16 #ifdef BOARD_PAT3
17 #include "pattern3.h"
18 #endif
19 #ifdef BOARD_TRAITS
20 static void board_trait_recompute(struct board *board, coord_t coord);
21 #include "tactics.h"
22 #endif
23 #ifdef BOARD_GAMMA
24 #include "pattern.h"
25 #endif
28 #if 0
29 #define profiling_noinline __attribute__((noinline))
30 #else
31 #define profiling_noinline
32 #endif
34 #define gi_granularity 4
35 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
38 static void
39 board_setup(struct board *b)
41 memset(b, 0, sizeof(*b));
43 struct move m = { pass, S_NONE };
44 b->last_move = b->last_move2 = b->last_ko = b->ko = m;
47 struct board *
48 board_init(void)
50 struct board *b = malloc2(sizeof(struct board));
51 board_setup(b);
53 // Default setup
54 b->size = 9 + 2;
55 board_clear(b);
57 return b;
60 static size_t
61 board_alloc(struct board *board)
63 /* We do not allocate the board structure itself but we allocate
64 * all the arrays with board contents. */
66 int bsize = board_size2(board) * sizeof(*board->b);
67 int gsize = board_size2(board) * sizeof(*board->g);
68 int fsize = board_size2(board) * sizeof(*board->f);
69 int nsize = board_size2(board) * sizeof(*board->n);
70 int psize = board_size2(board) * sizeof(*board->p);
71 int hsize = board_size2(board) * 2 * sizeof(*board->h);
72 int gisize = board_size2(board) * sizeof(*board->gi);
73 #ifdef WANT_BOARD_C
74 int csize = board_size2(board) * sizeof(*board->c);
75 #else
76 int csize = 0;
77 #endif
78 #ifdef BOARD_SPATHASH
79 int ssize = board_size2(board) * sizeof(*board->spathash);
80 #else
81 int ssize = 0;
82 #endif
83 #ifdef BOARD_PAT3
84 int p3size = board_size2(board) * sizeof(*board->pat3);
85 #else
86 int p3size = 0;
87 #endif
88 #ifdef BOARD_TRAITS
89 int tsize = board_size2(board) * sizeof(*board->t);
90 int tqsize = board_size2(board) * sizeof(*board->t);
91 #else
92 int tsize = 0;
93 int tqsize = 0;
94 #endif
95 #ifdef BOARD_GAMMA
96 int pbsize = board_size2(board) * sizeof(*board->prob[0].items);
97 int rowpbsize = board_size(board) * sizeof(*board->prob[0].rowtotals);
98 #else
99 int pbsize = 0;
100 int rowpbsize = 0;
101 #endif
102 int cdsize = board_size2(board) * sizeof(*board->coord);
104 size_t size = bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize;
105 void *x = malloc2(size);
107 /* board->b must come first */
108 board->b = x; x += bsize;
109 board->g = x; x += gsize;
110 board->f = x; x += fsize;
111 board->p = x; x += psize;
112 board->n = x; x += nsize;
113 board->h = x; x += hsize;
114 board->gi = x; x += gisize;
115 #ifdef WANT_BOARD_C
116 board->c = x; x += csize;
117 #endif
118 #ifdef BOARD_SPATHASH
119 board->spathash = x; x += ssize;
120 #endif
121 #ifdef BOARD_PAT3
122 board->pat3 = x; x += p3size;
123 #endif
124 #ifdef BOARD_TRAITS
125 board->t = x; x += tsize;
126 board->tq = x; x += tqsize;
127 #endif
128 #ifdef BOARD_GAMMA
129 board->prob[0].items = x; x += pbsize;
130 board->prob[1].items = x; x += pbsize;
131 board->prob[0].rowtotals = x; x += rowpbsize;
132 board->prob[1].rowtotals = x; x += rowpbsize;
133 #endif
134 board->coord = x; x += cdsize;
136 return size;
139 struct board *
140 board_copy(struct board *b2, struct board *b1)
142 memcpy(b2, b1, sizeof(struct board));
144 size_t size = board_alloc(b2);
145 memcpy(b2->b, b1->b, size);
147 return b2;
150 void
151 board_done_noalloc(struct board *board)
153 if (board->b) free(board->b);
156 void
157 board_done(struct board *board)
159 board_done_noalloc(board);
160 free(board);
163 void
164 board_resize(struct board *board, int size)
166 #ifdef BOARD_SIZE
167 assert(board_size(board) == size + 2);
168 #endif
169 board->size = size + 2 /* S_OFFBOARD margin */;
170 board->size2 = board_size(board) * board_size(board);
172 board->bits2 = 1;
173 while ((1 << board->bits2) < board->size2) board->bits2++;
175 if (board->b)
176 free(board->b);
178 size_t asize = board_alloc(board);
179 memset(board->b, 0, asize);
182 void
183 board_clear(struct board *board)
185 int size = board_size(board);
186 float komi = board->komi;
188 board_done_noalloc(board);
189 board_setup(board);
190 board_resize(board, size - 2 /* S_OFFBOARD margin */);
192 board->komi = komi;
194 /* Setup neighborhood iterators */
195 board->nei8[0] = -size - 1; // (-1,-1)
196 board->nei8[1] = 1;
197 board->nei8[2] = 1;
198 board->nei8[3] = size - 2; // (-1,0)
199 board->nei8[4] = 2;
200 board->nei8[5] = size - 2; // (-1,1)
201 board->nei8[6] = 1;
202 board->nei8[7] = 1;
203 board->dnei[0] = -size - 1;
204 board->dnei[1] = 2;
205 board->dnei[2] = size*2 - 2;
206 board->dnei[3] = 2;
208 /* Setup initial symmetry */
209 board->symmetry.d = 1;
210 board->symmetry.x1 = board->symmetry.y1 = board_size(board) / 2;
211 board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1;
212 board->symmetry.type = SYM_FULL;
214 /* Set up coordinate cache */
215 foreach_point(board) {
216 board->coord[c][0] = c % board_size(board);
217 board->coord[c][1] = c / board_size(board);
218 } foreach_point_end;
220 /* Draw the offboard margin */
221 int top_row = board_size2(board) - board_size(board);
222 int i;
223 for (i = 0; i < board_size(board); i++)
224 board->b[i] = board->b[top_row + i] = S_OFFBOARD;
225 for (i = 0; i <= top_row; i += board_size(board))
226 board->b[i] = board->b[board_size(board) - 1 + i] = S_OFFBOARD;
228 foreach_point(board) {
229 coord_t coord = c;
230 if (board_at(board, coord) == S_OFFBOARD)
231 continue;
232 foreach_neighbor(board, c, {
233 inc_neighbor_count_at(board, coord, board_at(board, c));
234 } );
235 } foreach_point_end;
237 /* All positions are free! Except the margin. */
238 for (i = board_size(board); i < (board_size(board) - 1) * board_size(board); i++)
239 if (i % board_size(board) != 0 && i % board_size(board) != board_size(board) - 1)
240 board->f[board->flen++] = i;
242 /* Initialize zobrist hashtable. */
243 foreach_point(board) {
244 int max = (sizeof(hash_t) << history_hash_bits);
245 /* fast_random() is 16-bit only */
246 board->h[c * 2] = ((hash_t) fast_random(max))
247 | ((hash_t) fast_random(max) << 16)
248 | ((hash_t) fast_random(max) << 32)
249 | ((hash_t) fast_random(max) << 48);
250 if (!board->h[c * 2])
251 /* Would be kinda "oops". */
252 board->h[c * 2] = 1;
253 /* And once again for white */
254 board->h[c * 2 + 1] = ((hash_t) fast_random(max))
255 | ((hash_t) fast_random(max) << 16)
256 | ((hash_t) fast_random(max) << 32)
257 | ((hash_t) fast_random(max) << 48);
258 if (!board->h[c * 2 + 1])
259 board->h[c * 2 + 1] = 1;
260 } foreach_point_end;
262 #ifdef BOARD_SPATHASH
263 /* Initialize spatial hashes. */
264 foreach_point(board) {
265 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
266 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
267 ptcoords_at(x, y, c, board, j);
268 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
269 pthashes[0][j][board_at(board, c)];
270 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
271 pthashes[0][j][stone_other(board_at(board, c))];
274 } foreach_point_end;
275 #endif
276 #ifdef BOARD_PAT3
277 /* Initialize 3x3 pattern codes. */
278 foreach_point(board) {
279 if (board_at(board, c) == S_NONE)
280 board->pat3[c] = pattern3_hash(board, c);
281 } foreach_point_end;
282 #endif
283 #ifdef BOARD_TRAITS
284 /* Initialize traits. */
285 foreach_point(board) {
286 trait_at(board, c, S_BLACK).cap = 0;
287 trait_at(board, c, S_BLACK).cap1 = 0;
288 trait_at(board, c, S_BLACK).safe = true;
289 trait_at(board, c, S_WHITE).cap = 0;
290 trait_at(board, c, S_WHITE).cap1 = 0;
291 trait_at(board, c, S_WHITE).safe = true;
292 } foreach_point_end;
293 #endif
294 #ifdef BOARD_GAMMA
295 board->prob[0].b = board->prob[1].b = board;
296 foreach_point(board) {
297 probdist_set(&board->prob[0], c, double_to_fixp((board_at(board, c) == S_NONE) * 1.0f));
298 probdist_set(&board->prob[1], c, double_to_fixp((board_at(board, c) == S_NONE) * 1.0f));
299 } foreach_point_end;
300 #endif
303 static char *
304 board_print_top(struct board *board, char *s, char *end, int c)
306 for (int i = 0; i < c; i++) {
307 char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
308 s += snprintf(s, end - s, " ");
309 for (int x = 1; x < board_size(board) - 1; x++)
310 s += snprintf(s, end - s, "%c ", asdf[x - 1]);
311 s += snprintf(s, end -s, " ");
313 s += snprintf(s, end - s, "\n");
314 for (int i = 0; i < c; i++) {
315 s += snprintf(s, end - s, " +-");
316 for (int x = 1; x < board_size(board) - 1; x++)
317 s += snprintf(s, end - s, "--");
318 s += snprintf(s, end - s, "+");
320 s += snprintf(s, end - s, "\n");
321 return s;
324 static char *
325 board_print_bottom(struct board *board, char *s, char *end, int c)
327 for (int i = 0; i < c; i++) {
328 s += snprintf(s, end - s, " +-");
329 for (int x = 1; x < board_size(board) - 1; x++)
330 s += snprintf(s, end - s, "--");
331 s += snprintf(s, end - s, "+");
333 s += snprintf(s, end - s, "\n");
334 return s;
337 static char *
338 board_print_row(struct board *board, int y, char *s, char *end, board_cprint cprint)
340 s += snprintf(s, end - s, " %2d | ", y);
341 for (int x = 1; x < board_size(board) - 1; x++) {
342 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
343 s += snprintf(s, end - s, "%c)", stone2char(board_atxy(board, x, y)));
344 else
345 s += snprintf(s, end - s, "%c ", stone2char(board_atxy(board, x, y)));
347 s += snprintf(s, end - s, "|");
348 if (cprint) {
349 s += snprintf(s, end - s, " %2d | ", y);
350 for (int x = 1; x < board_size(board) - 1; x++) {
351 s = cprint(board, coord_xy(board, x, y), s, end);
353 s += snprintf(s, end - s, "|");
355 s += snprintf(s, end - s, "\n");
356 return s;
359 void
360 board_print_custom(struct board *board, FILE *f, board_cprint cprint)
362 char buf[10240];
363 char *s = buf;
364 char *end = buf + sizeof(buf);
365 s += snprintf(s, end - s, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
366 board->moves, board->komi, board->handicap,
367 board->captures[S_BLACK], board->captures[S_WHITE]);
368 s = board_print_top(board, s, end, 1 + !!cprint);
369 for (int y = board_size(board) - 2; y >= 1; y--)
370 s = board_print_row(board, y, s, end, cprint);
371 board_print_bottom(board, s, end, 1 + !!cprint);
372 fprintf(f, "%s\n", buf);
375 static char *
376 cprint_group(struct board *board, coord_t c, char *s, char *end)
378 s += snprintf(s, end - s, "%d ", group_base(group_at(board, c)));
379 return s;
382 void
383 board_print(struct board *board, FILE *f)
385 board_print_custom(board, f, DEBUGL(6) ? cprint_group : NULL);
388 void
389 board_gamma_set(struct board *b, struct features_gamma *gamma, bool precise_selfatari)
391 #ifdef BOARD_GAMMA
392 b->gamma = gamma;
393 b->precise_selfatari = precise_selfatari;
394 for (int i = 0; i < b->flen; i++) {
395 board_trait_recompute(b, b->f[i]);
397 #endif
401 /* Update the probability distribution we maintain incrementally. */
402 void
403 board_gamma_update(struct board *board, coord_t coord, enum stone color)
405 #ifdef BOARD_GAMMA
406 if (!board->gamma)
407 return;
409 /* Punch out invalid moves and moves filling our own eyes. */
410 if (board_at(board, coord) != S_NONE
411 || (board_is_eyelike(board, coord, stone_other(color))
412 && !trait_at(board, coord, color).cap)
413 || (board_is_one_point_eye(board, coord, color))) {
414 probdist_set(&board->prob[color - 1], coord, 0);
415 return;
418 hash3_t pat = board->pat3[coord];
419 if (color == S_WHITE) {
420 /* We work with the pattern3s as black-to-play. */
421 pat = pattern3_reverse(pat);
424 /* We just quickly replicate the general pattern matcher stuff
425 * here in the most bare-bone way. */
426 double value = board->gamma->gamma[FEAT_PATTERN3][pat];
427 if (trait_at(board, coord, color).cap) {
428 int i = 0;
429 i |= (trait_at(board, coord, color).cap1 > 0) << PF_CAPTURE_1STONE;
430 value *= board->gamma->gamma[FEAT_CAPTURE][i];
432 if (trait_at(board, coord, stone_other(color)).cap
433 && trait_at(board, coord, color).safe) {
434 int i = 0;
435 i |= (trait_at(board, coord, stone_other(color)).cap1 > 0) << PF_AESCAPE_1STONE;
436 value *= board->gamma->gamma[FEAT_AESCAPE][i];
438 if (!trait_at(board, coord, color).safe)
439 value *= board->gamma->gamma[FEAT_SELFATARI][1 + board->precise_selfatari];
440 probdist_set(&board->prob[color - 1], coord, double_to_fixp(value));
441 #endif
444 #ifdef BOARD_TRAITS
445 static bool
446 board_trait_safe(struct board *board, coord_t coord, enum stone color)
448 if (board->precise_selfatari)
449 return !is_bad_selfatari(board, color, coord);
450 else
451 return board_safe_to_play(board, coord, color);
454 static void
455 board_trait_recompute(struct board *board, coord_t coord)
457 trait_at(board, coord, S_BLACK).safe = board_trait_safe(board, coord, S_BLACK);;
458 trait_at(board, coord, S_WHITE).safe = board_trait_safe(board, coord, S_WHITE);
459 if (DEBUGL(8)) {
460 fprintf(stderr, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
461 coord2sstr(coord, board), stone2str(board_at(board, coord)), immediate_liberty_count(board, coord),
462 trait_at(board, coord, S_BLACK).cap, trait_at(board, coord, S_BLACK).cap1, trait_at(board, coord, S_BLACK).safe,
463 trait_at(board, coord, S_WHITE).cap, trait_at(board, coord, S_WHITE).cap1, trait_at(board, coord, S_WHITE).safe);
465 board_gamma_update(board, coord, S_BLACK);
466 board_gamma_update(board, coord, S_WHITE);
468 #endif
470 /* Recompute traits for dirty points that we have previously touched
471 * somehow (libs of their neighbors changed or so). */
472 static void
473 board_traits_recompute(struct board *board)
475 #ifdef BOARD_TRAITS
476 for (int i = 0; i < board->tqlen; i++) {
477 coord_t coord = board->tq[i];
478 trait_at(board, coord, S_BLACK).dirty = false;
479 if (board_at(board, coord) != S_NONE)
480 continue;
481 board_trait_recompute(board, coord);
483 board->tqlen = 0;
484 #endif
487 /* Queue traits of given point for recomputing. */
488 static void
489 board_trait_queue(struct board *board, coord_t coord)
491 #ifdef BOARD_TRAITS
492 if (trait_at(board, coord, S_BLACK).dirty)
493 return;
494 board->tq[board->tqlen++] = coord;
495 trait_at(board, coord, S_BLACK).dirty = true;
496 #endif
500 /* Update board hash with given coordinate. */
501 static void profiling_noinline
502 board_hash_update(struct board *board, coord_t coord, enum stone color)
504 board->hash ^= hash_at(board, coord, color);
505 if (DEBUGL(8))
506 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);
508 #ifdef BOARD_SPATHASH
509 /* Gridcular metric is reflective, so we update all hashes
510 * of appropriate ditance in OUR circle. */
511 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
512 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
513 ptcoords_at(x, y, coord, board, j);
514 /* We either changed from S_NONE to color
515 * or vice versa; doesn't matter. */
516 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
517 pthashes[0][j][color] ^ pthashes[0][j][S_NONE];
518 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
519 pthashes[0][j][stone_other(color)] ^ pthashes[0][j][S_NONE];
522 #endif
524 #if defined(BOARD_PAT3)
525 /* @color is not what we need in case of capture. */
526 enum stone new_color = board_at(board, coord);
527 if (new_color == S_NONE)
528 board->pat3[coord] = pattern3_hash(board, coord);
529 foreach_8neighbor(board, coord) { // internally, the loop uses fn__i=[0..7]
530 if (board_at(board, c) != S_NONE)
531 continue;
532 board->pat3[c] &= ~(3 << (fn__i*2));
533 board->pat3[c] |= new_color << (fn__i*2);
534 #if 0
535 if (board_at(board, c) != S_OFFBOARD && pattern3_hash(board, c) != board->pat3[c]) {
536 board_print(board, stderr);
537 fprintf(stderr, "%s->%s %x != %x (%d-%d:%d)\n", coord2sstr(coord, board), coord2sstr(c, board), pattern3_hash(board, c), board->pat3[c], coord, c, fn__i);
538 assert(0);
540 #endif
541 board_trait_queue(board, c);
542 } foreach_8neighbor_end;
543 #endif
546 /* Commit current board hash to history. */
547 static void profiling_noinline
548 board_hash_commit(struct board *board)
550 if (DEBUGL(8))
551 fprintf(stderr, "board_hash_commit %"PRIhash"\n", board->hash);
552 if (likely(board->history_hash[board->hash & history_hash_mask]) == 0) {
553 board->history_hash[board->hash & history_hash_mask] = board->hash;
554 } else {
555 hash_t i = board->hash;
556 while (board->history_hash[i & history_hash_mask]) {
557 if (board->history_hash[i & history_hash_mask] == board->hash) {
558 if (DEBUGL(5))
559 fprintf(stderr, "SUPERKO VIOLATION noted at %d,%d\n",
560 coord_x(board->last_move.coord, board), coord_y(board->last_move.coord, board));
561 board->superko_violation = true;
562 return;
564 i = history_hash_next(i);
566 board->history_hash[i & history_hash_mask] = board->hash;
571 void
572 board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c)
574 if (likely(symmetry->type == SYM_NONE)) {
575 /* Fully degenerated already. We do not support detection
576 * of restoring of symmetry, assuming that this is too rare
577 * a case to handle. */
578 return;
581 int x = coord_x(c, b), y = coord_y(c, b), t = board_size(b) / 2;
582 int dx = board_size(b) - 1 - x; /* for SYM_DOWN */
583 if (DEBUGL(6)) {
584 fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
585 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
586 symmetry->d, symmetry->type, x, y);
589 switch (symmetry->type) {
590 case SYM_FULL:
591 if (x == t && y == t) {
592 /* Tengen keeps full symmetry. */
593 return;
595 /* New symmetry now? */
596 if (x == y) {
597 symmetry->type = SYM_DIAG_UP;
598 symmetry->x1 = symmetry->y1 = 1;
599 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
600 symmetry->d = 1;
601 } else if (dx == y) {
602 symmetry->type = SYM_DIAG_DOWN;
603 symmetry->x1 = symmetry->y1 = 1;
604 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
605 symmetry->d = 1;
606 } else if (x == t) {
607 symmetry->type = SYM_HORIZ;
608 symmetry->y1 = 1;
609 symmetry->y2 = board_size(b) - 1;
610 symmetry->d = 0;
611 } else if (y == t) {
612 symmetry->type = SYM_VERT;
613 symmetry->x1 = 1;
614 symmetry->x2 = board_size(b) - 1;
615 symmetry->d = 0;
616 } else {
617 break_symmetry:
618 symmetry->type = SYM_NONE;
619 symmetry->x1 = symmetry->y1 = 1;
620 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
621 symmetry->d = 0;
623 break;
624 case SYM_DIAG_UP:
625 if (x == y)
626 return;
627 goto break_symmetry;
628 case SYM_DIAG_DOWN:
629 if (dx == y)
630 return;
631 goto break_symmetry;
632 case SYM_HORIZ:
633 if (x == t)
634 return;
635 goto break_symmetry;
636 case SYM_VERT:
637 if (y == t)
638 return;
639 goto break_symmetry;
640 case SYM_NONE:
641 assert(0);
642 break;
645 if (DEBUGL(6)) {
646 fprintf(stderr, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
647 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
648 symmetry->d, symmetry->type);
650 /* Whew. */
654 void
655 board_handicap_stone(struct board *board, int x, int y, FILE *f)
657 struct move m;
658 m.color = S_BLACK; m.coord = coord_xy(board, x, y);
660 board_play(board, &m);
661 /* Simulate white passing; otherwise, UCT search can get confused since
662 * tree depth parity won't match the color to move. */
663 board->moves++;
665 char *str = coord2str(m.coord, board);
666 if (DEBUGL(1))
667 fprintf(stderr, "choosing handicap %s (%d,%d)\n", str, x, y);
668 if (f) fprintf(f, "%s ", str);
669 free(str);
672 void
673 board_handicap(struct board *board, int stones, FILE *f)
675 int margin = 3 + (board_size(board) >= 13);
676 int min = margin;
677 int mid = board_size(board) / 2;
678 int max = board_size(board) - 1 - margin;
679 const int places[][2] = {
680 { min, min }, { max, max }, { max, min }, { min, max },
681 { min, mid }, { max, mid },
682 { mid, min }, { mid, max },
683 { mid, mid },
686 board->handicap = stones;
688 if (stones == 5 || stones == 7) {
689 board_handicap_stone(board, mid, mid, f);
690 stones--;
693 int i;
694 for (i = 0; i < stones; i++)
695 board_handicap_stone(board, places[i][0], places[i][1], f);
699 static void __attribute__((noinline))
700 check_libs_consistency(struct board *board, group_t g)
702 #ifdef DEBUG
703 if (!g) return;
704 struct group *gi = &board_group_info(board, g);
705 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
706 if (gi->lib[i] && board_at(board, gi->lib[i]) != S_NONE) {
707 fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(group_base(g), board));
708 assert(0);
710 #endif
713 static void
714 board_capturable_add(struct board *board, group_t group, coord_t lib, bool onestone)
716 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
717 #ifdef BOARD_TRAITS
718 /* Increase capturable count trait of my last lib. */
719 enum stone capturing_color = stone_other(board_at(board, group));
720 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
721 foreach_neighbor(board, lib, {
722 if (DEBUGL(8) && group_at(board, c) == group)
723 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);
724 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group);
725 trait_at(board, lib, capturing_color).cap1 += (group_at(board, c) == group && onestone);
727 board_trait_queue(board, lib);
728 #endif
730 #ifdef WANT_BOARD_C
731 /* Update the list of capturable groups. */
732 assert(group);
733 assert(board->clen < board_size2(board));
734 board->c[board->clen++] = group;
735 #endif
737 static void
738 board_capturable_rm(struct board *board, group_t group, coord_t lib, bool onestone)
740 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
741 #ifdef BOARD_TRAITS
742 /* Decrease capturable count trait of my previously-last lib. */
743 enum stone capturing_color = stone_other(board_at(board, group));
744 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
745 foreach_neighbor(board, lib, {
746 if (DEBUGL(8) && group_at(board, c) == group)
747 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);
748 trait_at(board, lib, capturing_color).cap -= (group_at(board, c) == group);
749 trait_at(board, lib, capturing_color).cap1 -= (group_at(board, c) == group && onestone);
751 board_trait_queue(board, lib);
752 #endif
754 #ifdef WANT_BOARD_C
755 /* Update the list of capturable groups. */
756 for (int i = 0; i < board->clen; i++) {
757 if (unlikely(board->c[i] == group)) {
758 board->c[i] = board->c[--board->clen];
759 return;
762 fprintf(stderr, "rm of bad group %d\n", group_base(group));
763 assert(0);
764 #endif
767 static void
768 board_atariable_add(struct board *board, group_t group, coord_t lib1, coord_t lib2)
770 #ifdef BOARD_TRAITS
771 board_trait_queue(board, lib1);
772 board_trait_queue(board, lib2);
773 #endif
775 static void
776 board_atariable_rm(struct board *board, group_t group, coord_t lib1, coord_t lib2)
778 #ifdef BOARD_TRAITS
779 board_trait_queue(board, lib1);
780 board_trait_queue(board, lib2);
781 #endif
784 static void
785 board_group_addlib(struct board *board, group_t group, coord_t coord)
787 if (DEBUGL(7)) {
788 fprintf(stderr, "Group %d[%s] %d: Adding liberty %s\n",
789 group_base(group), coord2sstr(group_base(group), board),
790 board_group_info(board, group).libs, coord2sstr(coord, board));
793 check_libs_consistency(board, group);
795 struct group *gi = &board_group_info(board, group);
796 bool onestone = group_is_onestone(board, group);
797 if (gi->libs < GROUP_KEEP_LIBS) {
798 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
799 #if 0
800 /* Seems extra branch just slows it down */
801 if (!gi->lib[i])
802 break;
803 #endif
804 if (unlikely(gi->lib[i] == coord))
805 return;
807 if (gi->libs == 0) {
808 board_capturable_add(board, group, coord, onestone);
809 } else if (gi->libs == 1) {
810 board_capturable_rm(board, group, gi->lib[0], onestone);
811 board_atariable_add(board, group, gi->lib[0], coord);
812 } else if (gi->libs == 2) {
813 board_atariable_rm(board, group, gi->lib[0], gi->lib[1]);
815 gi->lib[gi->libs++] = coord;
818 check_libs_consistency(board, group);
821 static void
822 board_group_find_extra_libs(struct board *board, group_t group, struct group *gi, coord_t avoid)
824 /* Add extra liberty from the board to our liberty list. */
825 unsigned char watermark[board_size2(board) / 8];
826 memset(watermark, 0, sizeof(watermark));
827 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
828 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
830 for (int i = 0; i < GROUP_KEEP_LIBS - 1; i++)
831 watermark_set(gi->lib[i]);
832 watermark_set(avoid);
834 foreach_in_group(board, group) {
835 coord_t coord2 = c;
836 foreach_neighbor(board, coord2, {
837 if (board_at(board, c) + watermark_get(c) != S_NONE)
838 continue;
839 watermark_set(c);
840 gi->lib[gi->libs++] = c;
841 if (unlikely(gi->libs >= GROUP_KEEP_LIBS))
842 return;
843 } );
844 } foreach_in_group_end;
845 #undef watermark_get
846 #undef watermark_set
849 static void
850 board_group_rmlib(struct board *board, group_t group, coord_t coord)
852 if (DEBUGL(7)) {
853 fprintf(stderr, "Group %d[%s] %d: Removing liberty %s\n",
854 group_base(group), coord2sstr(group_base(group), board),
855 board_group_info(board, group).libs, coord2sstr(coord, board));
858 struct group *gi = &board_group_info(board, group);
859 bool onestone = group_is_onestone(board, group);
860 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
861 #if 0
862 /* Seems extra branch just slows it down */
863 if (!gi->lib[i])
864 break;
865 #endif
866 if (likely(gi->lib[i] != coord))
867 continue;
869 coord_t lib = gi->lib[i] = gi->lib[--gi->libs];
870 gi->lib[gi->libs] = 0;
872 check_libs_consistency(board, group);
874 /* Postpone refilling lib[] until we need to. */
875 assert(GROUP_REFILL_LIBS > 1);
876 if (gi->libs > GROUP_REFILL_LIBS)
877 return;
878 if (gi->libs == GROUP_REFILL_LIBS)
879 board_group_find_extra_libs(board, group, gi, coord);
881 if (gi->libs == 2) {
882 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
883 } else if (gi->libs == 1) {
884 board_capturable_add(board, group, gi->lib[0], onestone);
885 board_atariable_rm(board, group, gi->lib[0], lib);
886 } else if (gi->libs == 0)
887 board_capturable_rm(board, group, lib, onestone);
888 return;
891 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
892 * can call this multiple times per coord. */
893 check_libs_consistency(board, group);
894 return;
898 /* This is a low-level routine that doesn't maintain consistency
899 * of all the board data structures. */
900 static void
901 board_remove_stone(struct board *board, group_t group, coord_t c)
903 enum stone color = board_at(board, c);
904 board_at(board, c) = S_NONE;
905 group_at(board, c) = 0;
906 board_hash_update(board, c, color);
907 #ifdef BOARD_TRAITS
908 /* We mark as cannot-capture now. If this is a ko/snapback,
909 * we will get incremented later in board_group_addlib(). */
910 trait_at(board, c, S_BLACK).cap = trait_at(board, c, S_BLACK).cap1 = 0;
911 trait_at(board, c, S_WHITE).cap = trait_at(board, c, S_WHITE).cap1 = 0;
912 board_trait_queue(board, c);
913 #endif
915 /* Increase liberties of surrounding groups */
916 coord_t coord = c;
917 foreach_neighbor(board, coord, {
918 dec_neighbor_count_at(board, c, color);
919 board_trait_queue(board, c);
920 group_t g = group_at(board, c);
921 if (g && g != group)
922 board_group_addlib(board, g, coord);
925 if (DEBUGL(6))
926 fprintf(stderr, "pushing free move [%d]: %d,%d\n", board->flen, coord_x(c, board), coord_y(c, board));
927 board->f[board->flen++] = c;
930 static int profiling_noinline
931 board_group_capture(struct board *board, group_t group)
933 int stones = 0;
935 foreach_in_group(board, group) {
936 board->captures[stone_other(board_at(board, c))]++;
937 board_remove_stone(board, group, c);
938 stones++;
939 } foreach_in_group_end;
941 struct group *gi = &board_group_info(board, group);
942 assert(gi->libs == 0);
943 memset(gi, 0, sizeof(*gi));
945 return stones;
949 static void profiling_noinline
950 add_to_group(struct board *board, group_t group, coord_t prevstone, coord_t coord)
952 #ifdef BOARD_TRAITS
953 struct group *gi = &board_group_info(board, group);
954 bool onestone = group_is_onestone(board, group);
956 if (gi->libs == 1) {
957 /* Our group is temporarily in atari; make sure the capturable
958 * counts also correspond to the newly added stone before we
959 * start adding liberties again so bump-dump ops match. */
960 enum stone capturing_color = stone_other(board_at(board, group));
961 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
963 coord_t lib = board_group_info(board, group).lib[0];
964 if (coord_is_adjecent(lib, coord, board)) {
965 if (DEBUGL(8))
966 fprintf(stderr, "add_to_group %s: %s[%d] bump\n", coord2sstr(group, board), coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
967 trait_at(board, lib, capturing_color).cap++;
968 /* This is never a 1-stone group, obviously. */
969 board_trait_queue(board, lib);
972 if (onestone) {
973 /* We are not 1-stone group anymore, update the cap1
974 * counter specifically. */
975 foreach_neighbor(board, group, {
976 if (board_at(board, c) != S_NONE) continue;
977 trait_at(board, c, capturing_color).cap1--;
978 board_trait_queue(board, c);
982 #endif
984 group_at(board, coord) = group;
985 groupnext_at(board, coord) = groupnext_at(board, prevstone);
986 groupnext_at(board, prevstone) = coord;
988 foreach_neighbor(board, coord, {
989 if (board_at(board, c) == S_NONE)
990 board_group_addlib(board, group, c);
993 if (DEBUGL(8))
994 fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
995 coord_x(prevstone, board), coord_y(prevstone, board),
996 coord_x(coord, board), coord_y(coord, board),
997 groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board),
998 group_base(group));
1001 static void profiling_noinline
1002 merge_groups(struct board *board, group_t group_to, group_t group_from)
1004 if (DEBUGL(7))
1005 fprintf(stderr, "board_play_raw: merging groups %d -> %d\n",
1006 group_base(group_from), group_base(group_to));
1007 struct group *gi_from = &board_group_info(board, group_from);
1008 struct group *gi_to = &board_group_info(board, group_to);
1009 bool onestone_from = group_is_onestone(board, group_from);
1010 bool onestone_to = group_is_onestone(board, group_to);
1012 /* We do this early before the group info is rewritten. */
1013 if (gi_from->libs == 2)
1014 board_atariable_rm(board, group_from, gi_from->lib[0], gi_from->lib[1]);
1015 else if (gi_from->libs == 1)
1016 board_capturable_rm(board, group_from, gi_from->lib[0], onestone_from);
1018 if (DEBUGL(7))
1019 fprintf(stderr,"---- (froml %d, tol %d)\n", gi_from->libs, gi_to->libs);
1021 if (gi_to->libs < GROUP_KEEP_LIBS) {
1022 for (int i = 0; i < gi_from->libs; i++) {
1023 for (int j = 0; j < gi_to->libs; j++)
1024 if (gi_to->lib[j] == gi_from->lib[i])
1025 goto next_from_lib;
1026 if (gi_to->libs == 0) {
1027 board_capturable_add(board, group_to, gi_from->lib[i], onestone_to);
1028 } else if (gi_to->libs == 1) {
1029 board_capturable_rm(board, group_to, gi_to->lib[0], onestone_to);
1030 board_atariable_add(board, group_to, gi_to->lib[0], gi_from->lib[i]);
1031 } else if (gi_to->libs == 2) {
1032 board_atariable_rm(board, group_to, gi_to->lib[0], gi_to->lib[1]);
1034 gi_to->lib[gi_to->libs++] = gi_from->lib[i];
1035 if (gi_to->libs >= GROUP_KEEP_LIBS)
1036 break;
1037 next_from_lib:;
1041 #ifdef BOARD_TRAITS
1042 if (gi_to->libs == 1) {
1043 enum stone capturing_color = stone_other(board_at(board, group_to));
1044 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
1046 /* Our group is currently in atari; make sure we properly
1047 * count in even the neighbors from the other group in the
1048 * capturable counter. */
1049 coord_t lib = board_group_info(board, group_to).lib[0];
1050 foreach_neighbor(board, lib, {
1051 if (DEBUGL(8) && group_at(board, c) == group_from)
1052 fprintf(stderr, "%s[%d] cap bump\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
1053 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group_from);
1054 /* This is never a 1-stone group, obviously. */
1056 board_trait_queue(board, lib);
1058 if (onestone_to) {
1059 /* We are not 1-stone group anymore, update the cap1
1060 * counter specifically. */
1061 foreach_neighbor(board, group_to, {
1062 if (board_at(board, c) != S_NONE) continue;
1063 trait_at(board, c, capturing_color).cap1--;
1064 board_trait_queue(board, c);
1068 #endif
1070 coord_t last_in_group;
1071 foreach_in_group(board, group_from) {
1072 last_in_group = c;
1073 group_at(board, c) = group_to;
1074 } foreach_in_group_end;
1075 groupnext_at(board, last_in_group) = groupnext_at(board, group_base(group_to));
1076 groupnext_at(board, group_base(group_to)) = group_base(group_from);
1077 memset(gi_from, 0, sizeof(struct group));
1079 if (DEBUGL(7))
1080 fprintf(stderr, "board_play_raw: merged group: %d\n",
1081 group_base(group_to));
1084 static group_t profiling_noinline
1085 new_group(struct board *board, coord_t coord)
1087 group_t group = coord;
1088 struct group *gi = &board_group_info(board, group);
1089 foreach_neighbor(board, coord, {
1090 if (board_at(board, c) == S_NONE)
1091 /* board_group_addlib is ridiculously expensive for us */
1092 #if GROUP_KEEP_LIBS < 4
1093 if (gi->libs < GROUP_KEEP_LIBS)
1094 #endif
1095 gi->lib[gi->libs++] = c;
1098 group_at(board, coord) = group;
1099 groupnext_at(board, coord) = 0;
1101 if (gi->libs == 2)
1102 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
1103 else if (gi->libs == 1)
1104 board_capturable_add(board, group, gi->lib[0], true);
1105 check_libs_consistency(board, group);
1107 if (DEBUGL(8))
1108 fprintf(stderr, "new_group: added %d,%d to group %d\n",
1109 coord_x(coord, board), coord_y(coord, board),
1110 group_base(group));
1112 return group;
1115 static inline group_t
1116 play_one_neighbor(struct board *board,
1117 coord_t coord, enum stone color, enum stone other_color,
1118 coord_t c, group_t group)
1120 enum stone ncolor = board_at(board, c);
1121 group_t ngroup = group_at(board, c);
1123 inc_neighbor_count_at(board, c, color);
1124 /* We can be S_NONE, in that case we need to update the safety
1125 * trait since we might be left with only one liberty. */
1126 board_trait_queue(board, c);
1128 if (!ngroup)
1129 return group;
1131 board_group_rmlib(board, ngroup, coord);
1132 if (DEBUGL(7))
1133 fprintf(stderr, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1134 group_base(ngroup), ncolor, color, other_color);
1136 if (ncolor == color && ngroup != group) {
1137 if (!group) {
1138 group = ngroup;
1139 add_to_group(board, group, c, coord);
1140 } else {
1141 merge_groups(board, group, ngroup);
1143 } else if (ncolor == other_color) {
1144 if (DEBUGL(8)) {
1145 struct group *gi = &board_group_info(board, ngroup);
1146 fprintf(stderr, "testing captured group %d[%s]: ", group_base(ngroup), coord2sstr(group_base(ngroup), board));
1147 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
1148 fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board));
1149 fprintf(stderr, "\n");
1151 if (unlikely(board_group_captured(board, ngroup)))
1152 board_group_capture(board, ngroup);
1154 return group;
1157 /* We played on a place with at least one liberty. We will become a member of
1158 * some group for sure. */
1159 static group_t profiling_noinline
1160 board_play_outside(struct board *board, struct move *m, int f)
1162 coord_t coord = m->coord;
1163 enum stone color = m->color;
1164 enum stone other_color = stone_other(color);
1165 group_t group = 0;
1167 board->f[f] = board->f[--board->flen];
1168 if (DEBUGL(6))
1169 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1171 #if defined(BOARD_TRAITS) && defined(DEBUG)
1172 /* Sanity check that cap matches reality. */
1174 int a = 0, b = 0;
1175 foreach_neighbor(board, coord, {
1176 group_t g = group_at(board, c);
1177 a += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1);
1178 b += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1) && group_is_onestone(board, g);
1180 assert(a == trait_at(board, coord, color).cap);
1181 assert(b == trait_at(board, coord, color).cap1);
1182 assert(board_trait_safe(board, coord, color) == trait_at(board, coord, color).safe);
1184 #endif
1185 foreach_neighbor(board, coord, {
1186 group = play_one_neighbor(board, coord, color, other_color, c, group);
1189 board_at(board, coord) = color;
1190 if (unlikely(!group))
1191 group = new_group(board, coord);
1192 board_gamma_update(board, coord, S_BLACK);
1193 board_gamma_update(board, coord, S_WHITE);
1195 board->last_move2 = board->last_move;
1196 board->last_move = *m;
1197 board->moves++;
1198 board_hash_update(board, coord, color);
1199 board_symmetry_update(board, &board->symmetry, coord);
1200 struct move ko = { pass, S_NONE };
1201 board->ko = ko;
1203 return group;
1206 /* We played in an eye-like shape. Either we capture at least one of the eye
1207 * sides in the process of playing, or return -1. */
1208 static int profiling_noinline
1209 board_play_in_eye(struct board *board, struct move *m, int f)
1211 coord_t coord = m->coord;
1212 enum stone color = m->color;
1213 /* Check ko: Capture at a position of ko capture one move ago */
1214 if (unlikely(color == board->ko.color && coord == board->ko.coord)) {
1215 if (DEBUGL(5))
1216 fprintf(stderr, "board_check: ko at %d,%d color %d\n", coord_x(coord, board), coord_y(coord, board), color);
1217 return -1;
1218 } else if (DEBUGL(6)) {
1219 fprintf(stderr, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1220 color, coord_x(coord, board), coord_y(coord, board),
1221 board->ko.color, coord_x(board->ko.coord, board), coord_y(board->ko.coord, board));
1224 struct move ko = { pass, S_NONE };
1226 int captured_groups = 0;
1228 foreach_neighbor(board, coord, {
1229 group_t g = group_at(board, c);
1230 if (DEBUGL(7))
1231 fprintf(stderr, "board_check: group %d has %d libs\n",
1232 g, board_group_info(board, g).libs);
1233 captured_groups += (board_group_info(board, g).libs == 1);
1236 if (likely(captured_groups == 0)) {
1237 if (DEBUGL(5)) {
1238 if (DEBUGL(6))
1239 board_print(board, stderr);
1240 fprintf(stderr, "board_check: one-stone suicide\n");
1243 return -1;
1245 #ifdef BOARD_TRAITS
1246 /* We _will_ for sure capture something. */
1247 assert(trait_at(board, coord, color).cap > 0);
1248 assert(trait_at(board, coord, color).safe == board_trait_safe(board, coord, color));
1249 #endif
1251 board->f[f] = board->f[--board->flen];
1252 if (DEBUGL(6))
1253 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1255 foreach_neighbor(board, coord, {
1256 inc_neighbor_count_at(board, c, color);
1257 /* Originally, this could not have changed any trait
1258 * since no neighbors were S_NONE, however by now some
1259 * of them might be removed from the board. */
1260 board_trait_queue(board, c);
1262 group_t group = group_at(board, c);
1263 if (!group)
1264 continue;
1266 board_group_rmlib(board, group, coord);
1267 if (DEBUGL(7))
1268 fprintf(stderr, "board_play_raw: reducing libs for group %d\n",
1269 group_base(group));
1271 if (board_group_captured(board, group)) {
1272 if (board_group_capture(board, group) == 1) {
1273 /* If we captured multiple groups at once,
1274 * we can't be fighting ko so we don't need
1275 * to check for that. */
1276 ko.color = stone_other(color);
1277 ko.coord = c;
1278 board->last_ko = ko;
1279 board->last_ko_age = board->moves;
1280 if (DEBUGL(5))
1281 fprintf(stderr, "guarding ko at %d,%s\n", ko.color, coord2sstr(ko.coord, board));
1286 board_at(board, coord) = color;
1287 group_t group = new_group(board, coord);
1288 board_gamma_update(board, coord, S_BLACK);
1289 board_gamma_update(board, coord, S_WHITE);
1291 board->last_move2 = board->last_move;
1292 board->last_move = *m;
1293 board->moves++;
1294 board_hash_update(board, coord, color);
1295 board_hash_commit(board);
1296 board_traits_recompute(board);
1297 board_symmetry_update(board, &board->symmetry, coord);
1298 board->ko = ko;
1300 return !!group;
1303 static int __attribute__((flatten))
1304 board_play_f(struct board *board, struct move *m, int f)
1306 if (DEBUGL(7)) {
1307 fprintf(stderr, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m->coord, board), coord_x(m->coord, board), coord_y(m->coord, board));
1309 if (likely(!board_is_eyelike(board, m->coord, stone_other(m->color)))) {
1310 /* NOT playing in an eye. Thus this move has to succeed. (This
1311 * is thanks to New Zealand rules. Otherwise, multi-stone
1312 * suicide might fail.) */
1313 group_t group = board_play_outside(board, m, f);
1314 if (unlikely(board_group_captured(board, group))) {
1315 board_group_capture(board, group);
1317 board_hash_commit(board);
1318 board_traits_recompute(board);
1319 return 0;
1320 } else {
1321 return board_play_in_eye(board, m, f);
1326 board_play(struct board *board, struct move *m)
1328 if (unlikely(is_pass(m->coord) || is_resign(m->coord))) {
1329 struct move nomove = { pass, S_NONE };
1330 board->ko = nomove;
1331 board->last_move2 = board->last_move;
1332 board->last_move = *m;
1333 return 0;
1336 int f;
1337 for (f = 0; f < board->flen; f++)
1338 if (board->f[f] == m->coord)
1339 return board_play_f(board, m, f);
1341 if (DEBUGL(7))
1342 fprintf(stderr, "board_check: stone exists\n");
1343 return -1;
1347 static inline bool
1348 board_try_random_move(struct board *b, enum stone color, coord_t *coord, int f, ppr_permit permit, void *permit_data)
1350 *coord = b->f[f];
1351 struct move m = { *coord, color };
1352 if (DEBUGL(6))
1353 fprintf(stderr, "trying random move %d: %d,%d\n", f, coord_x(*coord, b), coord_y(*coord, b));
1354 if (unlikely(board_is_one_point_eye(b, *coord, color)) /* bad idea to play into one, usually */
1355 || !board_is_valid_move(b, &m)
1356 || (permit && !permit(permit_data, b, &m)))
1357 return false;
1358 *coord = m.coord; // permit might modify it
1359 return likely(board_play_f(b, &m, f) >= 0);
1362 void
1363 board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data)
1365 if (unlikely(b->flen == 0))
1366 goto pass;
1368 int base = fast_random(b->flen), f;
1369 for (f = base; f < b->flen; f++)
1370 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1371 return;
1372 for (f = 0; f < base; f++)
1373 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1374 return;
1376 pass:
1377 *coord = pass;
1378 struct move m = { pass, color };
1379 board_play(b, &m);
1383 bool
1384 board_is_false_eyelike(struct board *board, coord_t coord, enum stone eye_color)
1386 enum stone color_diag_libs[S_MAX] = {0, 0, 0, 0};
1388 /* XXX: We attempt false eye detection but we will yield false
1389 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1391 foreach_diag_neighbor(board, coord) {
1392 color_diag_libs[(enum stone) board_at(board, c)]++;
1393 } foreach_diag_neighbor_end;
1394 /* For false eye, we need two enemy stones diagonally in the
1395 * middle of the board, or just one enemy stone at the edge
1396 * or in the corner. */
1397 color_diag_libs[stone_other(eye_color)] += !!color_diag_libs[S_OFFBOARD];
1398 return color_diag_libs[stone_other(eye_color)] >= 2;
1401 bool
1402 board_is_one_point_eye(struct board *board, coord_t coord, enum stone eye_color)
1404 return board_is_eyelike(board, coord, eye_color)
1405 && !board_is_false_eyelike(board, coord, eye_color);
1408 enum stone
1409 board_get_one_point_eye(struct board *board, coord_t coord)
1411 if (board_is_one_point_eye(board, coord, S_WHITE))
1412 return S_WHITE;
1413 else if (board_is_one_point_eye(board, coord, S_BLACK))
1414 return S_BLACK;
1415 else
1416 return S_NONE;
1420 float
1421 board_fast_score(struct board *board)
1423 int scores[S_MAX];
1424 memset(scores, 0, sizeof(scores));
1426 foreach_point(board) {
1427 enum stone color = board_at(board, c);
1428 if (color == S_NONE)
1429 color = board_get_one_point_eye(board, c);
1430 scores[color]++;
1431 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1432 } foreach_point_end;
1434 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];
1437 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1439 /* One flood-fill iteration; returns true if next iteration
1440 * is required. */
1441 static bool
1442 board_tromp_taylor_iter(struct board *board, int *ownermap)
1444 bool needs_update = false;
1445 foreach_free_point(board) {
1446 /* Ignore occupied and already-dame positions. */
1447 assert(board_at(board, c) == S_NONE);
1448 if (ownermap[c] == 3)
1449 continue;
1450 /* Count neighbors. */
1451 int nei[4] = {0};
1452 foreach_neighbor(board, c, {
1453 nei[ownermap[c]]++;
1455 /* If we have neighbors of both colors, or dame,
1456 * we are dame too. */
1457 if ((nei[1] && nei[2]) || nei[3]) {
1458 ownermap[c] = 3;
1459 /* Speed up the propagation. */
1460 foreach_neighbor(board, c, {
1461 if (board_at(board, c) == S_NONE)
1462 ownermap[c] = 3;
1464 needs_update = true;
1465 continue;
1467 /* If we have neighbors of one color, we are owned
1468 * by that color, too. */
1469 if (!ownermap[c] && (nei[1] || nei[2])) {
1470 int newowner = nei[1] ? 1 : 2;
1471 ownermap[c] = newowner;
1472 /* Speed up the propagation. */
1473 foreach_neighbor(board, c, {
1474 if (board_at(board, c) == S_NONE && !ownermap[c])
1475 ownermap[c] = newowner;
1477 needs_update = true;
1478 continue;
1480 } foreach_free_point_end;
1481 return needs_update;
1484 /* Tromp-Taylor Counting */
1485 float
1486 board_official_score(struct board *board, struct move_queue *q)
1489 /* A point P, not colored C, is said to reach C, if there is a path of
1490 * (vertically or horizontally) adjacent points of P's color from P to
1491 * a point of color C.
1493 * A player's score is the number of points of her color, plus the
1494 * number of empty points that reach only her color. */
1496 int ownermap[board_size2(board)];
1497 int s[4] = {0};
1498 const int o[4] = {0, 1, 2, 0};
1499 foreach_point(board) {
1500 ownermap[c] = o[board_at(board, c)];
1501 s[board_at(board, c)]++;
1502 } foreach_point_end;
1504 if (q) {
1505 /* Process dead groups. */
1506 for (unsigned int i = 0; i < q->moves; i++) {
1507 foreach_in_group(board, q->move[i]) {
1508 enum stone color = board_at(board, c);
1509 ownermap[c] = o[stone_other(color)];
1510 s[color]--; s[stone_other(color)]++;
1511 } foreach_in_group_end;
1515 /* We need to special-case empty board. */
1516 if (!s[S_BLACK] && !s[S_WHITE])
1517 return board->komi + board->handicap;
1519 while (board_tromp_taylor_iter(board, ownermap))
1520 /* Flood-fill... */;
1522 int scores[S_MAX];
1523 memset(scores, 0, sizeof(scores));
1525 foreach_point(board) {
1526 assert(board_at(board, c) == S_OFFBOARD || ownermap[c] != 0);
1527 if (ownermap[c] == 3)
1528 continue;
1529 scores[ownermap[c]]++;
1530 } foreach_point_end;
1532 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];