Board: Comment out accidentally introduced DEBUG
[pachi/ann.git] / board.c
blobb7ccc8dd0929d4172c4c0f9f752d3139c7b73bdb
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 "mq.h"
12 #include "random.h"
14 #ifdef BOARD_SPATHASH
15 #include "patternsp.h"
16 #endif
17 #ifdef BOARD_PAT3
18 #include "pattern3.h"
19 #endif
20 #ifdef BOARD_TRAITS
21 static void board_trait_recompute(struct board *board, coord_t coord);
22 #include "tactics.h"
23 #endif
24 #ifdef BOARD_GAMMA
25 #include "pattern.h"
26 #endif
29 #if 0
30 #define profiling_noinline __attribute__((noinline))
31 #else
32 #define profiling_noinline
33 #endif
35 #define gi_granularity 4
36 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
39 static void
40 board_setup(struct board *b)
42 memset(b, 0, sizeof(*b));
44 struct move m = { pass, S_NONE };
45 b->last_move = b->last_move2 = b->last_ko = b->ko = m;
48 struct board *
49 board_init(void)
51 struct board *b = malloc2(sizeof(struct board));
52 board_setup(b);
54 // Default setup
55 b->size = 9 + 2;
56 board_clear(b);
58 return b;
61 static size_t
62 board_alloc(struct board *board)
64 /* We do not allocate the board structure itself but we allocate
65 * all the arrays with board contents. */
67 int bsize = board_size2(board) * sizeof(*board->b);
68 int gsize = board_size2(board) * sizeof(*board->g);
69 int fsize = board_size2(board) * sizeof(*board->f);
70 int nsize = board_size2(board) * sizeof(*board->n);
71 int psize = board_size2(board) * sizeof(*board->p);
72 int hsize = board_size2(board) * 2 * sizeof(*board->h);
73 int gisize = board_size2(board) * sizeof(*board->gi);
74 #ifdef WANT_BOARD_C
75 int csize = board_size2(board) * sizeof(*board->c);
76 #else
77 int csize = 0;
78 #endif
79 #ifdef BOARD_SPATHASH
80 int ssize = board_size2(board) * sizeof(*board->spathash);
81 #else
82 int ssize = 0;
83 #endif
84 #ifdef BOARD_PAT3
85 int p3size = board_size2(board) * sizeof(*board->pat3);
86 #else
87 int p3size = 0;
88 #endif
89 #ifdef BOARD_TRAITS
90 int tsize = board_size2(board) * sizeof(*board->t);
91 int tqsize = board_size2(board) * sizeof(*board->t);
92 #else
93 int tsize = 0;
94 int tqsize = 0;
95 #endif
96 #ifdef BOARD_GAMMA
97 int pbsize = board_size2(board) * sizeof(*board->prob[0].items);
98 int rowpbsize = board_size(board) * sizeof(*board->prob[0].rowtotals);
99 #else
100 int pbsize = 0;
101 int rowpbsize = 0;
102 #endif
103 int cdsize = board_size2(board) * sizeof(*board->coord);
105 size_t size = bsize + gsize + fsize + psize + nsize + hsize + gisize + csize + ssize + p3size + tsize + tqsize + (pbsize + rowpbsize) * 2 + cdsize;
106 void *x = malloc2(size);
108 /* board->b must come first */
109 board->b = x; x += bsize;
110 board->g = x; x += gsize;
111 board->f = x; x += fsize;
112 board->p = x; x += psize;
113 board->n = x; x += nsize;
114 board->h = x; x += hsize;
115 board->gi = x; x += gisize;
116 #ifdef WANT_BOARD_C
117 board->c = x; x += csize;
118 #endif
119 #ifdef BOARD_SPATHASH
120 board->spathash = x; x += ssize;
121 #endif
122 #ifdef BOARD_PAT3
123 board->pat3 = x; x += p3size;
124 #endif
125 #ifdef BOARD_TRAITS
126 board->t = x; x += tsize;
127 board->tq = x; x += tqsize;
128 #endif
129 #ifdef BOARD_GAMMA
130 board->prob[0].items = x; x += pbsize;
131 board->prob[1].items = x; x += pbsize;
132 board->prob[0].rowtotals = x; x += rowpbsize;
133 board->prob[1].rowtotals = x; x += rowpbsize;
134 #endif
135 board->coord = x; x += cdsize;
137 return size;
140 struct board *
141 board_copy(struct board *b2, struct board *b1)
143 memcpy(b2, b1, sizeof(struct board));
145 size_t size = board_alloc(b2);
146 memcpy(b2->b, b1->b, size);
148 return b2;
151 void
152 board_done_noalloc(struct board *board)
154 if (board->b) free(board->b);
157 void
158 board_done(struct board *board)
160 board_done_noalloc(board);
161 free(board);
164 void
165 board_resize(struct board *board, int size)
167 #ifdef BOARD_SIZE
168 assert(board_size(board) == size + 2);
169 #endif
170 board->size = size + 2 /* S_OFFBOARD margin */;
171 board->size2 = board_size(board) * board_size(board);
173 board->bits2 = 1;
174 while ((1 << board->bits2) < board->size2) board->bits2++;
176 if (board->b)
177 free(board->b);
179 size_t asize = board_alloc(board);
180 memset(board->b, 0, asize);
183 void
184 board_clear(struct board *board)
186 int size = board_size(board);
187 float komi = board->komi;
189 board_done_noalloc(board);
190 board_setup(board);
191 board_resize(board, size - 2 /* S_OFFBOARD margin */);
193 board->komi = komi;
195 /* Setup neighborhood iterators */
196 board->nei8[0] = -size - 1; // (-1,-1)
197 board->nei8[1] = 1;
198 board->nei8[2] = 1;
199 board->nei8[3] = size - 2; // (-1,0)
200 board->nei8[4] = 2;
201 board->nei8[5] = size - 2; // (-1,1)
202 board->nei8[6] = 1;
203 board->nei8[7] = 1;
204 board->dnei[0] = -size - 1;
205 board->dnei[1] = 2;
206 board->dnei[2] = size*2 - 2;
207 board->dnei[3] = 2;
209 /* Setup initial symmetry */
210 board->symmetry.d = 1;
211 board->symmetry.x1 = board->symmetry.y1 = board_size(board) / 2;
212 board->symmetry.x2 = board->symmetry.y2 = board_size(board) - 1;
213 board->symmetry.type = SYM_FULL;
215 /* Set up coordinate cache */
216 foreach_point(board) {
217 board->coord[c][0] = c % board_size(board);
218 board->coord[c][1] = c / board_size(board);
219 } foreach_point_end;
221 /* Draw the offboard margin */
222 int top_row = board_size2(board) - board_size(board);
223 int i;
224 for (i = 0; i < board_size(board); i++)
225 board->b[i] = board->b[top_row + i] = S_OFFBOARD;
226 for (i = 0; i <= top_row; i += board_size(board))
227 board->b[i] = board->b[board_size(board) - 1 + i] = S_OFFBOARD;
229 foreach_point(board) {
230 coord_t coord = c;
231 if (board_at(board, coord) == S_OFFBOARD)
232 continue;
233 foreach_neighbor(board, c, {
234 inc_neighbor_count_at(board, coord, board_at(board, c));
235 } );
236 } foreach_point_end;
238 /* All positions are free! Except the margin. */
239 for (i = board_size(board); i < (board_size(board) - 1) * board_size(board); i++)
240 if (i % board_size(board) != 0 && i % board_size(board) != board_size(board) - 1)
241 board->f[board->flen++] = i;
243 /* Initialize zobrist hashtable. */
244 foreach_point(board) {
245 int max = (sizeof(hash_t) << history_hash_bits);
246 /* fast_random() is 16-bit only */
247 board->h[c * 2] = ((hash_t) fast_random(max))
248 | ((hash_t) fast_random(max) << 16)
249 | ((hash_t) fast_random(max) << 32)
250 | ((hash_t) fast_random(max) << 48);
251 if (!board->h[c * 2])
252 /* Would be kinda "oops". */
253 board->h[c * 2] = 1;
254 /* And once again for white */
255 board->h[c * 2 + 1] = ((hash_t) fast_random(max))
256 | ((hash_t) fast_random(max) << 16)
257 | ((hash_t) fast_random(max) << 32)
258 | ((hash_t) fast_random(max) << 48);
259 if (!board->h[c * 2 + 1])
260 board->h[c * 2 + 1] = 1;
261 } foreach_point_end;
263 #ifdef BOARD_SPATHASH
264 /* Initialize spatial hashes. */
265 foreach_point(board) {
266 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
267 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
268 ptcoords_at(x, y, c, board, j);
269 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
270 pthashes[0][j][board_at(board, c)];
271 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
272 pthashes[0][j][stone_other(board_at(board, c))];
275 } foreach_point_end;
276 #endif
277 #ifdef BOARD_PAT3
278 /* Initialize 3x3 pattern codes. */
279 foreach_point(board) {
280 if (board_at(board, c) == S_NONE)
281 board->pat3[c] = pattern3_hash(board, c);
282 } foreach_point_end;
283 #endif
284 #ifdef BOARD_TRAITS
285 /* Initialize traits. */
286 foreach_point(board) {
287 trait_at(board, c, S_BLACK).cap = 0;
288 trait_at(board, c, S_BLACK).cap1 = 0;
289 trait_at(board, c, S_BLACK).safe = true;
290 trait_at(board, c, S_WHITE).cap = 0;
291 trait_at(board, c, S_WHITE).cap1 = 0;
292 trait_at(board, c, S_WHITE).safe = true;
293 } foreach_point_end;
294 #endif
295 #ifdef BOARD_GAMMA
296 board->prob[0].b = board->prob[1].b = board;
297 foreach_point(board) {
298 probdist_set(&board->prob[0], c, double_to_fixp((board_at(board, c) == S_NONE) * 1.0f));
299 probdist_set(&board->prob[1], c, double_to_fixp((board_at(board, c) == S_NONE) * 1.0f));
300 } foreach_point_end;
301 #endif
304 static char *
305 board_print_top(struct board *board, char *s, char *end, int c)
307 for (int i = 0; i < c; i++) {
308 char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
309 s += snprintf(s, end - s, " ");
310 for (int x = 1; x < board_size(board) - 1; x++)
311 s += snprintf(s, end - s, "%c ", asdf[x - 1]);
312 s += snprintf(s, end -s, " ");
314 s += snprintf(s, end - s, "\n");
315 for (int i = 0; i < c; i++) {
316 s += snprintf(s, end - s, " +-");
317 for (int x = 1; x < board_size(board) - 1; x++)
318 s += snprintf(s, end - s, "--");
319 s += snprintf(s, end - s, "+");
321 s += snprintf(s, end - s, "\n");
322 return s;
325 static char *
326 board_print_bottom(struct board *board, char *s, char *end, int c)
328 for (int i = 0; i < c; i++) {
329 s += snprintf(s, end - s, " +-");
330 for (int x = 1; x < board_size(board) - 1; x++)
331 s += snprintf(s, end - s, "--");
332 s += snprintf(s, end - s, "+");
334 s += snprintf(s, end - s, "\n");
335 return s;
338 static char *
339 board_print_row(struct board *board, int y, char *s, char *end, board_cprint cprint)
341 s += snprintf(s, end - s, " %2d | ", y);
342 for (int x = 1; x < board_size(board) - 1; x++) {
343 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
344 s += snprintf(s, end - s, "%c)", stone2char(board_atxy(board, x, y)));
345 else
346 s += snprintf(s, end - s, "%c ", stone2char(board_atxy(board, x, y)));
348 s += snprintf(s, end - s, "|");
349 if (cprint) {
350 s += snprintf(s, end - s, " %2d | ", y);
351 for (int x = 1; x < board_size(board) - 1; x++) {
352 s = cprint(board, coord_xy(board, x, y), s, end);
354 s += snprintf(s, end - s, "|");
356 s += snprintf(s, end - s, "\n");
357 return s;
360 void
361 board_print_custom(struct board *board, FILE *f, board_cprint cprint)
363 char buf[10240];
364 char *s = buf;
365 char *end = buf + sizeof(buf);
366 s += snprintf(s, end - s, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
367 board->moves, board->komi, board->handicap,
368 board->captures[S_BLACK], board->captures[S_WHITE]);
369 s = board_print_top(board, s, end, 1 + !!cprint);
370 for (int y = board_size(board) - 2; y >= 1; y--)
371 s = board_print_row(board, y, s, end, cprint);
372 board_print_bottom(board, s, end, 1 + !!cprint);
373 fprintf(f, "%s\n", buf);
376 static char *
377 cprint_group(struct board *board, coord_t c, char *s, char *end)
379 s += snprintf(s, end - s, "%d ", group_base(group_at(board, c)));
380 return s;
383 void
384 board_print(struct board *board, FILE *f)
386 board_print_custom(board, f, DEBUGL(6) ? cprint_group : NULL);
389 void
390 board_gamma_set(struct board *b, struct features_gamma *gamma, bool precise_selfatari)
392 #ifdef BOARD_GAMMA
393 b->gamma = gamma;
394 b->precise_selfatari = precise_selfatari;
395 for (int i = 0; i < b->flen; i++) {
396 board_trait_recompute(b, b->f[i]);
398 #endif
402 /* Update the probability distribution we maintain incrementally. */
403 void
404 board_gamma_update(struct board *board, coord_t coord, enum stone color)
406 #ifdef BOARD_GAMMA
407 if (!board->gamma)
408 return;
410 /* Punch out invalid moves and moves filling our own eyes. */
411 if (board_at(board, coord) != S_NONE
412 || (board_is_eyelike(board, coord, stone_other(color))
413 && !trait_at(board, coord, color).cap)
414 || (board_is_one_point_eye(board, coord, color))) {
415 probdist_set(&board->prob[color - 1], coord, 0);
416 return;
419 hash3_t pat = board->pat3[coord];
420 if (color == S_WHITE) {
421 /* We work with the pattern3s as black-to-play. */
422 pat = pattern3_reverse(pat);
425 /* We just quickly replicate the general pattern matcher stuff
426 * here in the most bare-bone way. */
427 double value = board->gamma->gamma[FEAT_PATTERN3][pat];
428 if (trait_at(board, coord, color).cap) {
429 int i = 0;
430 i |= (trait_at(board, coord, color).cap1 == trait_at(board, coord, color).cap) << PF_CAPTURE_1STONE;
431 i |= (!trait_at(board, coord, stone_other(color)).safe) << PF_CAPTURE_TRAPPED;
432 i |= (trait_at(board, coord, color).cap < neighbor_count_at(board, coord, stone_other(color))) << PF_CAPTURE_CONNECTION;
433 value *= board->gamma->gamma[FEAT_CAPTURE][i];
435 if (trait_at(board, coord, stone_other(color)).cap) {
436 int i = 0;
437 i |= (trait_at(board, coord, stone_other(color)).cap1 == trait_at(board, coord, stone_other(color)).cap) << PF_AESCAPE_1STONE;
438 i |= (!trait_at(board, coord, color).safe) << PF_AESCAPE_TRAPPED;
439 i |= (trait_at(board, coord, stone_other(color)).cap < neighbor_count_at(board, coord, color)) << PF_AESCAPE_CONNECTION;
440 value *= board->gamma->gamma[FEAT_AESCAPE][i];
442 if (!trait_at(board, coord, color).safe)
443 value *= board->gamma->gamma[FEAT_SELFATARI][1 + board->precise_selfatari];
444 probdist_set(&board->prob[color - 1], coord, double_to_fixp(value));
445 #endif
448 #ifdef BOARD_TRAITS
449 static bool
450 board_trait_safe(struct board *board, coord_t coord, enum stone color)
452 if (board->precise_selfatari)
453 return !is_bad_selfatari(board, color, coord);
454 else
455 return board_safe_to_play(board, coord, color);
458 static void
459 board_trait_recompute(struct board *board, coord_t coord)
461 trait_at(board, coord, S_BLACK).safe = board_trait_safe(board, coord, S_BLACK);;
462 trait_at(board, coord, S_WHITE).safe = board_trait_safe(board, coord, S_WHITE);
463 if (DEBUGL(8)) {
464 fprintf(stderr, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
465 coord2sstr(coord, board), stone2str(board_at(board, coord)), immediate_liberty_count(board, coord),
466 trait_at(board, coord, S_BLACK).cap, trait_at(board, coord, S_BLACK).cap1, trait_at(board, coord, S_BLACK).safe,
467 trait_at(board, coord, S_WHITE).cap, trait_at(board, coord, S_WHITE).cap1, trait_at(board, coord, S_WHITE).safe);
469 board_gamma_update(board, coord, S_BLACK);
470 board_gamma_update(board, coord, S_WHITE);
472 #endif
474 /* Recompute traits for dirty points that we have previously touched
475 * somehow (libs of their neighbors changed or so). */
476 static void
477 board_traits_recompute(struct board *board)
479 #ifdef BOARD_TRAITS
480 for (int i = 0; i < board->tqlen; i++) {
481 coord_t coord = board->tq[i];
482 trait_at(board, coord, S_BLACK).dirty = false;
483 if (board_at(board, coord) != S_NONE)
484 continue;
485 board_trait_recompute(board, coord);
487 board->tqlen = 0;
488 #endif
491 /* Queue traits of given point for recomputing. */
492 static void
493 board_trait_queue(struct board *board, coord_t coord)
495 #ifdef BOARD_TRAITS
496 if (trait_at(board, coord, S_BLACK).dirty)
497 return;
498 board->tq[board->tqlen++] = coord;
499 trait_at(board, coord, S_BLACK).dirty = true;
500 #endif
504 /* Update board hash with given coordinate. */
505 static void profiling_noinline
506 board_hash_update(struct board *board, coord_t coord, enum stone color)
508 board->hash ^= hash_at(board, coord, color);
509 if (DEBUGL(8))
510 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);
512 #ifdef BOARD_SPATHASH
513 /* Gridcular metric is reflective, so we update all hashes
514 * of appropriate ditance in OUR circle. */
515 for (int d = 1; d <= BOARD_SPATHASH_MAXD; d++) {
516 for (int j = ptind[d]; j < ptind[d + 1]; j++) {
517 ptcoords_at(x, y, coord, board, j);
518 /* We either changed from S_NONE to color
519 * or vice versa; doesn't matter. */
520 board->spathash[coord_xy(board, x, y)][d - 1][0] ^=
521 pthashes[0][j][color] ^ pthashes[0][j][S_NONE];
522 board->spathash[coord_xy(board, x, y)][d - 1][1] ^=
523 pthashes[0][j][stone_other(color)] ^ pthashes[0][j][S_NONE];
526 #endif
528 #if defined(BOARD_PAT3)
529 /* @color is not what we need in case of capture. */
530 enum stone new_color = board_at(board, coord);
531 if (new_color == S_NONE)
532 board->pat3[coord] = pattern3_hash(board, coord);
533 foreach_8neighbor(board, coord) { // internally, the loop uses fn__i=[0..7]
534 if (board_at(board, c) != S_NONE)
535 continue;
536 board->pat3[c] &= ~(3 << (fn__i*2));
537 board->pat3[c] |= new_color << (fn__i*2);
538 #if 0
539 if (board_at(board, c) != S_OFFBOARD && pattern3_hash(board, c) != board->pat3[c]) {
540 board_print(board, stderr);
541 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);
542 assert(0);
544 #endif
545 board_trait_queue(board, c);
546 } foreach_8neighbor_end;
547 #endif
550 /* Commit current board hash to history. */
551 static void profiling_noinline
552 board_hash_commit(struct board *board)
554 if (DEBUGL(8))
555 fprintf(stderr, "board_hash_commit %"PRIhash"\n", board->hash);
556 if (likely(board->history_hash[board->hash & history_hash_mask]) == 0) {
557 board->history_hash[board->hash & history_hash_mask] = board->hash;
558 } else {
559 hash_t i = board->hash;
560 while (board->history_hash[i & history_hash_mask]) {
561 if (board->history_hash[i & history_hash_mask] == board->hash) {
562 if (DEBUGL(5))
563 fprintf(stderr, "SUPERKO VIOLATION noted at %d,%d\n",
564 coord_x(board->last_move.coord, board), coord_y(board->last_move.coord, board));
565 board->superko_violation = true;
566 return;
568 i = history_hash_next(i);
570 board->history_hash[i & history_hash_mask] = board->hash;
575 void
576 board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c)
578 if (likely(symmetry->type == SYM_NONE)) {
579 /* Fully degenerated already. We do not support detection
580 * of restoring of symmetry, assuming that this is too rare
581 * a case to handle. */
582 return;
585 int x = coord_x(c, b), y = coord_y(c, b), t = board_size(b) / 2;
586 int dx = board_size(b) - 1 - x; /* for SYM_DOWN */
587 if (DEBUGL(6)) {
588 fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
589 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
590 symmetry->d, symmetry->type, x, y);
593 switch (symmetry->type) {
594 case SYM_FULL:
595 if (x == t && y == t) {
596 /* Tengen keeps full symmetry. */
597 return;
599 /* New symmetry now? */
600 if (x == y) {
601 symmetry->type = SYM_DIAG_UP;
602 symmetry->x1 = symmetry->y1 = 1;
603 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
604 symmetry->d = 1;
605 } else if (dx == y) {
606 symmetry->type = SYM_DIAG_DOWN;
607 symmetry->x1 = symmetry->y1 = 1;
608 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
609 symmetry->d = 1;
610 } else if (x == t) {
611 symmetry->type = SYM_HORIZ;
612 symmetry->y1 = 1;
613 symmetry->y2 = board_size(b) - 1;
614 symmetry->d = 0;
615 } else if (y == t) {
616 symmetry->type = SYM_VERT;
617 symmetry->x1 = 1;
618 symmetry->x2 = board_size(b) - 1;
619 symmetry->d = 0;
620 } else {
621 break_symmetry:
622 symmetry->type = SYM_NONE;
623 symmetry->x1 = symmetry->y1 = 1;
624 symmetry->x2 = symmetry->y2 = board_size(b) - 1;
625 symmetry->d = 0;
627 break;
628 case SYM_DIAG_UP:
629 if (x == y)
630 return;
631 goto break_symmetry;
632 case SYM_DIAG_DOWN:
633 if (dx == y)
634 return;
635 goto break_symmetry;
636 case SYM_HORIZ:
637 if (x == t)
638 return;
639 goto break_symmetry;
640 case SYM_VERT:
641 if (y == t)
642 return;
643 goto break_symmetry;
644 case SYM_NONE:
645 assert(0);
646 break;
649 if (DEBUGL(6)) {
650 fprintf(stderr, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
651 symmetry->x1, symmetry->y1, symmetry->x2, symmetry->y2,
652 symmetry->d, symmetry->type);
654 /* Whew. */
658 void
659 board_handicap_stone(struct board *board, int x, int y, FILE *f)
661 struct move m;
662 m.color = S_BLACK; m.coord = coord_xy(board, x, y);
664 board_play(board, &m);
665 /* Simulate white passing; otherwise, UCT search can get confused since
666 * tree depth parity won't match the color to move. */
667 board->moves++;
669 char *str = coord2str(m.coord, board);
670 if (DEBUGL(1))
671 fprintf(stderr, "choosing handicap %s (%d,%d)\n", str, x, y);
672 if (f) fprintf(f, "%s ", str);
673 free(str);
676 void
677 board_handicap(struct board *board, int stones, FILE *f)
679 int margin = 3 + (board_size(board) >= 13);
680 int min = margin;
681 int mid = board_size(board) / 2;
682 int max = board_size(board) - 1 - margin;
683 const int places[][2] = {
684 { min, min }, { max, max }, { max, min }, { min, max },
685 { min, mid }, { max, mid },
686 { mid, min }, { mid, max },
687 { mid, mid },
690 board->handicap = stones;
692 if (stones == 5 || stones == 7) {
693 board_handicap_stone(board, mid, mid, f);
694 stones--;
697 int i;
698 for (i = 0; i < stones; i++)
699 board_handicap_stone(board, places[i][0], places[i][1], f);
703 static void __attribute__((noinline))
704 check_libs_consistency(struct board *board, group_t g)
706 #ifdef DEBUG
707 if (!g) return;
708 struct group *gi = &board_group_info(board, g);
709 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
710 if (gi->lib[i] && board_at(board, gi->lib[i]) != S_NONE) {
711 fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(group_base(g), board));
712 assert(0);
714 #endif
717 static void
718 board_capturable_add(struct board *board, group_t group, coord_t lib, bool onestone)
720 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
721 #ifdef BOARD_TRAITS
722 /* Increase capturable count trait of my last lib. */
723 enum stone capturing_color = stone_other(board_at(board, group));
724 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
725 foreach_neighbor(board, lib, {
726 if (DEBUGL(8) && group_at(board, c) == group)
727 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);
728 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group);
729 trait_at(board, lib, capturing_color).cap1 += (group_at(board, c) == group && onestone);
731 board_trait_queue(board, lib);
732 #endif
734 #ifdef WANT_BOARD_C
735 /* Update the list of capturable groups. */
736 assert(group);
737 assert(board->clen < board_size2(board));
738 board->c[board->clen++] = group;
739 #endif
741 static void
742 board_capturable_rm(struct board *board, group_t group, coord_t lib, bool onestone)
744 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
745 #ifdef BOARD_TRAITS
746 /* Decrease capturable count trait of my previously-last lib. */
747 enum stone capturing_color = stone_other(board_at(board, group));
748 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
749 foreach_neighbor(board, lib, {
750 if (DEBUGL(8) && group_at(board, c) == group)
751 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);
752 trait_at(board, lib, capturing_color).cap -= (group_at(board, c) == group);
753 trait_at(board, lib, capturing_color).cap1 -= (group_at(board, c) == group && onestone);
755 board_trait_queue(board, lib);
756 #endif
758 #ifdef WANT_BOARD_C
759 /* Update the list of capturable groups. */
760 for (int i = 0; i < board->clen; i++) {
761 if (unlikely(board->c[i] == group)) {
762 board->c[i] = board->c[--board->clen];
763 return;
766 fprintf(stderr, "rm of bad group %d\n", group_base(group));
767 assert(0);
768 #endif
771 static void
772 board_atariable_add(struct board *board, group_t group, coord_t lib1, coord_t lib2)
774 #ifdef BOARD_TRAITS
775 board_trait_queue(board, lib1);
776 board_trait_queue(board, lib2);
777 #endif
779 static void
780 board_atariable_rm(struct board *board, group_t group, coord_t lib1, coord_t lib2)
782 #ifdef BOARD_TRAITS
783 board_trait_queue(board, lib1);
784 board_trait_queue(board, lib2);
785 #endif
788 static void
789 board_group_addlib(struct board *board, group_t group, coord_t coord)
791 if (DEBUGL(7)) {
792 fprintf(stderr, "Group %d[%s] %d: Adding liberty %s\n",
793 group_base(group), coord2sstr(group_base(group), board),
794 board_group_info(board, group).libs, coord2sstr(coord, board));
797 check_libs_consistency(board, group);
799 struct group *gi = &board_group_info(board, group);
800 bool onestone = group_is_onestone(board, group);
801 if (gi->libs < GROUP_KEEP_LIBS) {
802 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
803 #if 0
804 /* Seems extra branch just slows it down */
805 if (!gi->lib[i])
806 break;
807 #endif
808 if (unlikely(gi->lib[i] == coord))
809 return;
811 if (gi->libs == 0) {
812 board_capturable_add(board, group, coord, onestone);
813 } else if (gi->libs == 1) {
814 board_capturable_rm(board, group, gi->lib[0], onestone);
815 board_atariable_add(board, group, gi->lib[0], coord);
816 } else if (gi->libs == 2) {
817 board_atariable_rm(board, group, gi->lib[0], gi->lib[1]);
819 gi->lib[gi->libs++] = coord;
822 check_libs_consistency(board, group);
825 static void
826 board_group_find_extra_libs(struct board *board, group_t group, struct group *gi, coord_t avoid)
828 /* Add extra liberty from the board to our liberty list. */
829 unsigned char watermark[board_size2(board) / 8];
830 memset(watermark, 0, sizeof(watermark));
831 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
832 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
834 for (int i = 0; i < GROUP_KEEP_LIBS - 1; i++)
835 watermark_set(gi->lib[i]);
836 watermark_set(avoid);
838 foreach_in_group(board, group) {
839 coord_t coord2 = c;
840 foreach_neighbor(board, coord2, {
841 if (board_at(board, c) + watermark_get(c) != S_NONE)
842 continue;
843 watermark_set(c);
844 gi->lib[gi->libs++] = c;
845 if (unlikely(gi->libs >= GROUP_KEEP_LIBS))
846 return;
847 } );
848 } foreach_in_group_end;
849 #undef watermark_get
850 #undef watermark_set
853 static void
854 board_group_rmlib(struct board *board, group_t group, coord_t coord)
856 if (DEBUGL(7)) {
857 fprintf(stderr, "Group %d[%s] %d: Removing liberty %s\n",
858 group_base(group), coord2sstr(group_base(group), board),
859 board_group_info(board, group).libs, coord2sstr(coord, board));
862 struct group *gi = &board_group_info(board, group);
863 bool onestone = group_is_onestone(board, group);
864 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
865 #if 0
866 /* Seems extra branch just slows it down */
867 if (!gi->lib[i])
868 break;
869 #endif
870 if (likely(gi->lib[i] != coord))
871 continue;
873 coord_t lib = gi->lib[i] = gi->lib[--gi->libs];
874 gi->lib[gi->libs] = 0;
876 check_libs_consistency(board, group);
878 /* Postpone refilling lib[] until we need to. */
879 assert(GROUP_REFILL_LIBS > 1);
880 if (gi->libs > GROUP_REFILL_LIBS)
881 return;
882 if (gi->libs == GROUP_REFILL_LIBS)
883 board_group_find_extra_libs(board, group, gi, coord);
885 if (gi->libs == 2) {
886 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
887 } else if (gi->libs == 1) {
888 board_capturable_add(board, group, gi->lib[0], onestone);
889 board_atariable_rm(board, group, gi->lib[0], lib);
890 } else if (gi->libs == 0)
891 board_capturable_rm(board, group, lib, onestone);
892 return;
895 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
896 * can call this multiple times per coord. */
897 check_libs_consistency(board, group);
898 return;
902 /* This is a low-level routine that doesn't maintain consistency
903 * of all the board data structures. */
904 static void
905 board_remove_stone(struct board *board, group_t group, coord_t c)
907 enum stone color = board_at(board, c);
908 board_at(board, c) = S_NONE;
909 group_at(board, c) = 0;
910 board_hash_update(board, c, color);
911 #ifdef BOARD_TRAITS
912 /* We mark as cannot-capture now. If this is a ko/snapback,
913 * we will get incremented later in board_group_addlib(). */
914 trait_at(board, c, S_BLACK).cap = trait_at(board, c, S_BLACK).cap1 = 0;
915 trait_at(board, c, S_WHITE).cap = trait_at(board, c, S_WHITE).cap1 = 0;
916 board_trait_queue(board, c);
917 #endif
919 /* Increase liberties of surrounding groups */
920 coord_t coord = c;
921 foreach_neighbor(board, coord, {
922 dec_neighbor_count_at(board, c, color);
923 board_trait_queue(board, c);
924 group_t g = group_at(board, c);
925 if (g && g != group)
926 board_group_addlib(board, g, coord);
929 if (DEBUGL(6))
930 fprintf(stderr, "pushing free move [%d]: %d,%d\n", board->flen, coord_x(c, board), coord_y(c, board));
931 board->f[board->flen++] = c;
934 static int profiling_noinline
935 board_group_capture(struct board *board, group_t group)
937 int stones = 0;
939 foreach_in_group(board, group) {
940 board->captures[stone_other(board_at(board, c))]++;
941 board_remove_stone(board, group, c);
942 stones++;
943 } foreach_in_group_end;
945 struct group *gi = &board_group_info(board, group);
946 assert(gi->libs == 0);
947 memset(gi, 0, sizeof(*gi));
949 return stones;
953 static void profiling_noinline
954 add_to_group(struct board *board, group_t group, coord_t prevstone, coord_t coord)
956 #ifdef BOARD_TRAITS
957 struct group *gi = &board_group_info(board, group);
958 bool onestone = group_is_onestone(board, group);
960 if (gi->libs == 1) {
961 /* Our group is temporarily in atari; make sure the capturable
962 * counts also correspond to the newly added stone before we
963 * start adding liberties again so bump-dump ops match. */
964 enum stone capturing_color = stone_other(board_at(board, group));
965 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
967 coord_t lib = board_group_info(board, group).lib[0];
968 if (coord_is_adjecent(lib, coord, board)) {
969 if (DEBUGL(8))
970 fprintf(stderr, "add_to_group %s: %s[%d] bump\n", coord2sstr(group, board), coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
971 trait_at(board, lib, capturing_color).cap++;
972 /* This is never a 1-stone group, obviously. */
973 board_trait_queue(board, lib);
976 if (onestone) {
977 /* We are not 1-stone group anymore, update the cap1
978 * counter specifically. */
979 foreach_neighbor(board, group, {
980 if (board_at(board, c) != S_NONE) continue;
981 trait_at(board, c, capturing_color).cap1--;
982 board_trait_queue(board, c);
986 #endif
988 group_at(board, coord) = group;
989 groupnext_at(board, coord) = groupnext_at(board, prevstone);
990 groupnext_at(board, prevstone) = coord;
992 foreach_neighbor(board, coord, {
993 if (board_at(board, c) == S_NONE)
994 board_group_addlib(board, group, c);
997 if (DEBUGL(8))
998 fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
999 coord_x(prevstone, board), coord_y(prevstone, board),
1000 coord_x(coord, board), coord_y(coord, board),
1001 groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board),
1002 group_base(group));
1005 static void profiling_noinline
1006 merge_groups(struct board *board, group_t group_to, group_t group_from)
1008 if (DEBUGL(7))
1009 fprintf(stderr, "board_play_raw: merging groups %d -> %d\n",
1010 group_base(group_from), group_base(group_to));
1011 struct group *gi_from = &board_group_info(board, group_from);
1012 struct group *gi_to = &board_group_info(board, group_to);
1013 bool onestone_from = group_is_onestone(board, group_from);
1014 bool onestone_to = group_is_onestone(board, group_to);
1016 /* We do this early before the group info is rewritten. */
1017 if (gi_from->libs == 2)
1018 board_atariable_rm(board, group_from, gi_from->lib[0], gi_from->lib[1]);
1019 else if (gi_from->libs == 1)
1020 board_capturable_rm(board, group_from, gi_from->lib[0], onestone_from);
1022 if (DEBUGL(7))
1023 fprintf(stderr,"---- (froml %d, tol %d)\n", gi_from->libs, gi_to->libs);
1025 if (gi_to->libs < GROUP_KEEP_LIBS) {
1026 for (int i = 0; i < gi_from->libs; i++) {
1027 for (int j = 0; j < gi_to->libs; j++)
1028 if (gi_to->lib[j] == gi_from->lib[i])
1029 goto next_from_lib;
1030 if (gi_to->libs == 0) {
1031 board_capturable_add(board, group_to, gi_from->lib[i], onestone_to);
1032 } else if (gi_to->libs == 1) {
1033 board_capturable_rm(board, group_to, gi_to->lib[0], onestone_to);
1034 board_atariable_add(board, group_to, gi_to->lib[0], gi_from->lib[i]);
1035 } else if (gi_to->libs == 2) {
1036 board_atariable_rm(board, group_to, gi_to->lib[0], gi_to->lib[1]);
1038 gi_to->lib[gi_to->libs++] = gi_from->lib[i];
1039 if (gi_to->libs >= GROUP_KEEP_LIBS)
1040 break;
1041 next_from_lib:;
1045 #ifdef BOARD_TRAITS
1046 if (gi_to->libs == 1) {
1047 enum stone capturing_color = stone_other(board_at(board, group_to));
1048 assert(capturing_color == S_BLACK || capturing_color == S_WHITE);
1050 /* Our group is currently in atari; make sure we properly
1051 * count in even the neighbors from the other group in the
1052 * capturable counter. */
1053 coord_t lib = board_group_info(board, group_to).lib[0];
1054 foreach_neighbor(board, lib, {
1055 if (DEBUGL(8) && group_at(board, c) == group_from)
1056 fprintf(stderr, "%s[%d] cap bump\n", coord2sstr(lib, board), trait_at(board, lib, capturing_color).cap);
1057 trait_at(board, lib, capturing_color).cap += (group_at(board, c) == group_from);
1058 /* This is never a 1-stone group, obviously. */
1060 board_trait_queue(board, lib);
1062 if (onestone_to) {
1063 /* We are not 1-stone group anymore, update the cap1
1064 * counter specifically. */
1065 foreach_neighbor(board, group_to, {
1066 if (board_at(board, c) != S_NONE) continue;
1067 trait_at(board, c, capturing_color).cap1--;
1068 board_trait_queue(board, c);
1072 #endif
1074 coord_t last_in_group;
1075 foreach_in_group(board, group_from) {
1076 last_in_group = c;
1077 group_at(board, c) = group_to;
1078 } foreach_in_group_end;
1079 groupnext_at(board, last_in_group) = groupnext_at(board, group_base(group_to));
1080 groupnext_at(board, group_base(group_to)) = group_base(group_from);
1081 memset(gi_from, 0, sizeof(struct group));
1083 if (DEBUGL(7))
1084 fprintf(stderr, "board_play_raw: merged group: %d\n",
1085 group_base(group_to));
1088 static group_t profiling_noinline
1089 new_group(struct board *board, coord_t coord)
1091 group_t group = coord;
1092 struct group *gi = &board_group_info(board, group);
1093 foreach_neighbor(board, coord, {
1094 if (board_at(board, c) == S_NONE)
1095 /* board_group_addlib is ridiculously expensive for us */
1096 #if GROUP_KEEP_LIBS < 4
1097 if (gi->libs < GROUP_KEEP_LIBS)
1098 #endif
1099 gi->lib[gi->libs++] = c;
1102 group_at(board, coord) = group;
1103 groupnext_at(board, coord) = 0;
1105 if (gi->libs == 2)
1106 board_atariable_add(board, group, gi->lib[0], gi->lib[1]);
1107 else if (gi->libs == 1)
1108 board_capturable_add(board, group, gi->lib[0], true);
1109 check_libs_consistency(board, group);
1111 if (DEBUGL(8))
1112 fprintf(stderr, "new_group: added %d,%d to group %d\n",
1113 coord_x(coord, board), coord_y(coord, board),
1114 group_base(group));
1116 return group;
1119 static inline group_t
1120 play_one_neighbor(struct board *board,
1121 coord_t coord, enum stone color, enum stone other_color,
1122 coord_t c, group_t group)
1124 enum stone ncolor = board_at(board, c);
1125 group_t ngroup = group_at(board, c);
1127 inc_neighbor_count_at(board, c, color);
1128 /* We can be S_NONE, in that case we need to update the safety
1129 * trait since we might be left with only one liberty. */
1130 board_trait_queue(board, c);
1132 if (!ngroup)
1133 return group;
1135 board_group_rmlib(board, ngroup, coord);
1136 if (DEBUGL(7))
1137 fprintf(stderr, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1138 group_base(ngroup), ncolor, color, other_color);
1140 if (ncolor == color && ngroup != group) {
1141 if (!group) {
1142 group = ngroup;
1143 add_to_group(board, group, c, coord);
1144 } else {
1145 merge_groups(board, group, ngroup);
1147 } else if (ncolor == other_color) {
1148 if (DEBUGL(8)) {
1149 struct group *gi = &board_group_info(board, ngroup);
1150 fprintf(stderr, "testing captured group %d[%s]: ", group_base(ngroup), coord2sstr(group_base(ngroup), board));
1151 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
1152 fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board));
1153 fprintf(stderr, "\n");
1155 if (unlikely(board_group_captured(board, ngroup)))
1156 board_group_capture(board, ngroup);
1158 return group;
1161 /* We played on a place with at least one liberty. We will become a member of
1162 * some group for sure. */
1163 static group_t profiling_noinline
1164 board_play_outside(struct board *board, struct move *m, int f)
1166 coord_t coord = m->coord;
1167 enum stone color = m->color;
1168 enum stone other_color = stone_other(color);
1169 group_t group = 0;
1171 board->f[f] = board->f[--board->flen];
1172 if (DEBUGL(6))
1173 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1175 #if defined(BOARD_TRAITS) && defined(DEBUG)
1176 /* Sanity check that cap matches reality. */
1178 int a = 0, b = 0;
1179 foreach_neighbor(board, coord, {
1180 group_t g = group_at(board, c);
1181 a += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1);
1182 b += g && (board_at(board, c) == other_color && board_group_info(board, g).libs == 1) && group_is_onestone(board, g);
1184 assert(a == trait_at(board, coord, color).cap);
1185 assert(b == trait_at(board, coord, color).cap1);
1186 assert(board_trait_safe(board, coord, color) == trait_at(board, coord, color).safe);
1188 #endif
1189 foreach_neighbor(board, coord, {
1190 group = play_one_neighbor(board, coord, color, other_color, c, group);
1193 board_at(board, coord) = color;
1194 if (unlikely(!group))
1195 group = new_group(board, coord);
1196 board_gamma_update(board, coord, S_BLACK);
1197 board_gamma_update(board, coord, S_WHITE);
1199 board->last_move2 = board->last_move;
1200 board->last_move = *m;
1201 board->moves++;
1202 board_hash_update(board, coord, color);
1203 board_symmetry_update(board, &board->symmetry, coord);
1204 struct move ko = { pass, S_NONE };
1205 board->ko = ko;
1207 return group;
1210 /* We played in an eye-like shape. Either we capture at least one of the eye
1211 * sides in the process of playing, or return -1. */
1212 static int profiling_noinline
1213 board_play_in_eye(struct board *board, struct move *m, int f)
1215 coord_t coord = m->coord;
1216 enum stone color = m->color;
1217 /* Check ko: Capture at a position of ko capture one move ago */
1218 if (unlikely(color == board->ko.color && coord == board->ko.coord)) {
1219 if (DEBUGL(5))
1220 fprintf(stderr, "board_check: ko at %d,%d color %d\n", coord_x(coord, board), coord_y(coord, board), color);
1221 return -1;
1222 } else if (DEBUGL(6)) {
1223 fprintf(stderr, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1224 color, coord_x(coord, board), coord_y(coord, board),
1225 board->ko.color, coord_x(board->ko.coord, board), coord_y(board->ko.coord, board));
1228 struct move ko = { pass, S_NONE };
1230 int captured_groups = 0;
1232 foreach_neighbor(board, coord, {
1233 group_t g = group_at(board, c);
1234 if (DEBUGL(7))
1235 fprintf(stderr, "board_check: group %d has %d libs\n",
1236 g, board_group_info(board, g).libs);
1237 captured_groups += (board_group_info(board, g).libs == 1);
1240 if (likely(captured_groups == 0)) {
1241 if (DEBUGL(5)) {
1242 if (DEBUGL(6))
1243 board_print(board, stderr);
1244 fprintf(stderr, "board_check: one-stone suicide\n");
1247 return -1;
1249 #ifdef BOARD_TRAITS
1250 /* We _will_ for sure capture something. */
1251 assert(trait_at(board, coord, color).cap > 0);
1252 assert(trait_at(board, coord, color).safe == board_trait_safe(board, coord, color));
1253 #endif
1255 board->f[f] = board->f[--board->flen];
1256 if (DEBUGL(6))
1257 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
1259 foreach_neighbor(board, coord, {
1260 inc_neighbor_count_at(board, c, color);
1261 /* Originally, this could not have changed any trait
1262 * since no neighbors were S_NONE, however by now some
1263 * of them might be removed from the board. */
1264 board_trait_queue(board, c);
1266 group_t group = group_at(board, c);
1267 if (!group)
1268 continue;
1270 board_group_rmlib(board, group, coord);
1271 if (DEBUGL(7))
1272 fprintf(stderr, "board_play_raw: reducing libs for group %d\n",
1273 group_base(group));
1275 if (board_group_captured(board, group)) {
1276 if (board_group_capture(board, group) == 1) {
1277 /* If we captured multiple groups at once,
1278 * we can't be fighting ko so we don't need
1279 * to check for that. */
1280 ko.color = stone_other(color);
1281 ko.coord = c;
1282 board->last_ko = ko;
1283 board->last_ko_age = board->moves;
1284 if (DEBUGL(5))
1285 fprintf(stderr, "guarding ko at %d,%s\n", ko.color, coord2sstr(ko.coord, board));
1290 board_at(board, coord) = color;
1291 group_t group = new_group(board, coord);
1292 board_gamma_update(board, coord, S_BLACK);
1293 board_gamma_update(board, coord, S_WHITE);
1295 board->last_move2 = board->last_move;
1296 board->last_move = *m;
1297 board->moves++;
1298 board_hash_update(board, coord, color);
1299 board_hash_commit(board);
1300 board_traits_recompute(board);
1301 board_symmetry_update(board, &board->symmetry, coord);
1302 board->ko = ko;
1304 return !!group;
1307 static int __attribute__((flatten))
1308 board_play_f(struct board *board, struct move *m, int f)
1310 if (DEBUGL(7)) {
1311 fprintf(stderr, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m->coord, board), coord_x(m->coord, board), coord_y(m->coord, board));
1313 if (likely(!board_is_eyelike(board, m->coord, stone_other(m->color)))) {
1314 /* NOT playing in an eye. Thus this move has to succeed. (This
1315 * is thanks to New Zealand rules. Otherwise, multi-stone
1316 * suicide might fail.) */
1317 group_t group = board_play_outside(board, m, f);
1318 if (unlikely(board_group_captured(board, group))) {
1319 board_group_capture(board, group);
1321 board_hash_commit(board);
1322 board_traits_recompute(board);
1323 return 0;
1324 } else {
1325 return board_play_in_eye(board, m, f);
1330 board_play(struct board *board, struct move *m)
1332 if (unlikely(is_pass(m->coord) || is_resign(m->coord))) {
1333 struct move nomove = { pass, S_NONE };
1334 board->ko = nomove;
1335 board->last_move2 = board->last_move;
1336 board->last_move = *m;
1337 return 0;
1340 int f;
1341 for (f = 0; f < board->flen; f++)
1342 if (board->f[f] == m->coord)
1343 return board_play_f(board, m, f);
1345 if (DEBUGL(7))
1346 fprintf(stderr, "board_check: stone exists\n");
1347 return -1;
1351 static inline bool
1352 board_try_random_move(struct board *b, enum stone color, coord_t *coord, int f, ppr_permit permit, void *permit_data)
1354 *coord = b->f[f];
1355 struct move m = { *coord, color };
1356 if (DEBUGL(6))
1357 fprintf(stderr, "trying random move %d: %d,%d\n", f, coord_x(*coord, b), coord_y(*coord, b));
1358 if (unlikely(board_is_one_point_eye(b, *coord, color)) /* bad idea to play into one, usually */
1359 || !board_is_valid_move(b, &m)
1360 || (permit && !permit(permit_data, b, &m)))
1361 return false;
1362 *coord = m.coord; // permit might modify it
1363 return likely(board_play_f(b, &m, f) >= 0);
1366 void
1367 board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data)
1369 if (unlikely(b->flen == 0))
1370 goto pass;
1372 int base = fast_random(b->flen), f;
1373 for (f = base; f < b->flen; f++)
1374 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1375 return;
1376 for (f = 0; f < base; f++)
1377 if (board_try_random_move(b, color, coord, f, permit, permit_data))
1378 return;
1380 pass:
1381 *coord = pass;
1382 struct move m = { pass, color };
1383 board_play(b, &m);
1387 bool
1388 board_is_false_eyelike(struct board *board, coord_t coord, enum stone eye_color)
1390 enum stone color_diag_libs[S_MAX] = {0, 0, 0, 0};
1392 /* XXX: We attempt false eye detection but we will yield false
1393 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1395 foreach_diag_neighbor(board, coord) {
1396 color_diag_libs[(enum stone) board_at(board, c)]++;
1397 } foreach_diag_neighbor_end;
1398 /* For false eye, we need two enemy stones diagonally in the
1399 * middle of the board, or just one enemy stone at the edge
1400 * or in the corner. */
1401 color_diag_libs[stone_other(eye_color)] += !!color_diag_libs[S_OFFBOARD];
1402 return color_diag_libs[stone_other(eye_color)] >= 2;
1405 bool
1406 board_is_one_point_eye(struct board *board, coord_t coord, enum stone eye_color)
1408 return board_is_eyelike(board, coord, eye_color)
1409 && !board_is_false_eyelike(board, coord, eye_color);
1412 enum stone
1413 board_get_one_point_eye(struct board *board, coord_t coord)
1415 if (board_is_one_point_eye(board, coord, S_WHITE))
1416 return S_WHITE;
1417 else if (board_is_one_point_eye(board, coord, S_BLACK))
1418 return S_BLACK;
1419 else
1420 return S_NONE;
1424 float
1425 board_fast_score(struct board *board)
1427 int scores[S_MAX];
1428 memset(scores, 0, sizeof(scores));
1430 foreach_point(board) {
1431 enum stone color = board_at(board, c);
1432 if (color == S_NONE)
1433 color = board_get_one_point_eye(board, c);
1434 scores[color]++;
1435 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1436 } foreach_point_end;
1438 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];
1441 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1443 /* One flood-fill iteration; returns true if next iteration
1444 * is required. */
1445 static bool
1446 board_tromp_taylor_iter(struct board *board, int *ownermap)
1448 bool needs_update = false;
1449 foreach_free_point(board) {
1450 /* Ignore occupied and already-dame positions. */
1451 assert(board_at(board, c) == S_NONE);
1452 if (ownermap[c] == 3)
1453 continue;
1454 /* Count neighbors. */
1455 int nei[4] = {0};
1456 foreach_neighbor(board, c, {
1457 nei[ownermap[c]]++;
1459 /* If we have neighbors of both colors, or dame,
1460 * we are dame too. */
1461 if ((nei[1] && nei[2]) || nei[3]) {
1462 ownermap[c] = 3;
1463 /* Speed up the propagation. */
1464 foreach_neighbor(board, c, {
1465 if (board_at(board, c) == S_NONE)
1466 ownermap[c] = 3;
1468 needs_update = true;
1469 continue;
1471 /* If we have neighbors of one color, we are owned
1472 * by that color, too. */
1473 if (!ownermap[c] && (nei[1] || nei[2])) {
1474 int newowner = nei[1] ? 1 : 2;
1475 ownermap[c] = newowner;
1476 /* Speed up the propagation. */
1477 foreach_neighbor(board, c, {
1478 if (board_at(board, c) == S_NONE && !ownermap[c])
1479 ownermap[c] = newowner;
1481 needs_update = true;
1482 continue;
1484 } foreach_free_point_end;
1485 return needs_update;
1488 /* Tromp-Taylor Counting */
1489 float
1490 board_official_score(struct board *board, struct move_queue *q)
1493 /* A point P, not colored C, is said to reach C, if there is a path of
1494 * (vertically or horizontally) adjacent points of P's color from P to
1495 * a point of color C.
1497 * A player's score is the number of points of her color, plus the
1498 * number of empty points that reach only her color. */
1500 int ownermap[board_size2(board)];
1501 int s[4] = {0};
1502 const int o[4] = {0, 1, 2, 0};
1503 foreach_point(board) {
1504 ownermap[c] = o[board_at(board, c)];
1505 s[board_at(board, c)]++;
1506 } foreach_point_end;
1508 if (q) {
1509 /* Process dead groups. */
1510 for (unsigned int i = 0; i < q->moves; i++) {
1511 foreach_in_group(board, q->move[i]) {
1512 enum stone color = board_at(board, c);
1513 ownermap[c] = o[stone_other(color)];
1514 s[color]--; s[stone_other(color)]++;
1515 } foreach_in_group_end;
1519 /* We need to special-case empty board. */
1520 if (!s[S_BLACK] && !s[S_WHITE])
1521 return board->komi + board->handicap;
1523 while (board_tromp_taylor_iter(board, ownermap))
1524 /* Flood-fill... */;
1526 int scores[S_MAX];
1527 memset(scores, 0, sizeof(scores));
1529 foreach_point(board) {
1530 assert(board_at(board, c) == S_OFFBOARD || ownermap[c] != 0);
1531 if (ownermap[c] == 3)
1532 continue;
1533 scores[ownermap[c]]++;
1534 } foreach_point_end;
1536 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];