Merge branch 'master' into symmetry
[pachi/peepo.git] / board.c
blobd98a73a66d68d90cf2c19a630c31363de393ab2f
1 #include <alloca.h>
2 #include <assert.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
7 #include "board.h"
8 #include "debug.h"
9 #include "random.h"
11 int board_group_capture(struct board *board, group_t group);
13 bool random_pass = false;
16 #if 0
17 #define profiling_noinline __attribute__((noinline))
18 #else
19 #define profiling_noinline
20 #endif
22 #define gi_granularity 4
23 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
26 static void
27 board_setup(struct board *b)
29 memset(b, 0, sizeof(*b));
31 struct move m = { pass, S_NONE };
32 b->last_move = b->ko = m;
35 struct board *
36 board_init(void)
38 struct board *b = malloc(sizeof(struct board));
39 board_setup(b);
40 return b;
43 struct board *
44 board_copy(struct board *b2, struct board *b1)
46 memcpy(b2, b1, sizeof(struct board));
48 int bsize = board_size2(b2) * sizeof(*b2->b);
49 int gsize = board_size2(b2) * sizeof(*b2->g);
50 int fsize = board_size2(b2) * sizeof(*b2->f);
51 int nsize = board_size2(b2) * sizeof(*b2->n);
52 int psize = board_size2(b2) * sizeof(*b2->p);
53 int hsize = board_size2(b2) * 2 * sizeof(*b2->h);
54 int gisize = board_size2(b2) * sizeof(*b2->gi);
55 #ifdef WANT_BOARD_C
56 int csize = board_size2(b2) * sizeof(*b2->c);
57 #else
58 int csize = 0;
59 #endif
60 void *x = malloc(bsize + gsize + fsize + psize + nsize + hsize + gisize + csize);
61 memcpy(x, b1->b, bsize + gsize + fsize + psize + nsize + hsize + gisize + csize);
62 b2->b = x; x += bsize;
63 b2->g = x; x += gsize;
64 b2->f = x; x += fsize;
65 b2->p = x; x += psize;
66 b2->n = x; x += nsize;
67 b2->h = x; x += hsize;
68 b2->gi = x; x += gisize;
69 #ifdef WANT_BOARD_C
70 b2->c = x; x += csize;
71 #endif
73 return b2;
76 void
77 board_done_noalloc(struct board *board)
79 if (board->b) free(board->b);
82 void
83 board_done(struct board *board)
85 board_done_noalloc(board);
86 free(board);
89 void
90 board_resize(struct board *board, int size)
92 #ifdef BOARD_SIZE
93 assert(board_size(board) == size + 2);
94 #else
95 board_size(board) = size + 2 /* S_OFFBOARD margin */;
96 board_size2(board) = board_size(board) * board_size(board);
97 #endif
98 if (board->b)
99 free(board->b);
101 int bsize = board_size2(board) * sizeof(*board->b);
102 int gsize = board_size2(board) * sizeof(*board->g);
103 int fsize = board_size2(board) * sizeof(*board->f);
104 int nsize = board_size2(board) * sizeof(*board->n);
105 int psize = board_size2(board) * sizeof(*board->p);
106 int hsize = board_size2(board) * 2 * sizeof(*board->h);
107 int gisize = board_size2(board) * sizeof(*board->gi);
108 #ifdef WANT_BOARD_C
109 int csize = board_size2(board) * sizeof(*board->c);
110 #else
111 int csize = 0;
112 #endif
113 void *x = malloc(bsize + gsize + fsize + psize + nsize + hsize + gisize + csize);
114 memset(x, 0, bsize + gsize + fsize + psize + nsize + hsize + gisize + csize);
115 board->b = x; x += bsize;
116 board->g = x; x += gsize;
117 board->f = x; x += fsize;
118 board->p = x; x += psize;
119 board->n = x; x += nsize;
120 board->h = x; x += hsize;
121 board->gi = x; x += gisize;
122 #ifdef WANT_BOARD_C
123 board->c = x; x += csize;
124 #endif
127 void
128 board_clear(struct board *board)
130 int size = board_size(board);
132 board_done_noalloc(board);
133 board_setup(board);
134 board_resize(board, size - 2 /* S_OFFBOARD margin */);
136 /* Setup initial symmetry */
137 board->symmetry.d = -1;
138 board->symmetry.x1 = board->symmetry.y1 = board->size / 2;
139 board->symmetry.x2 = board->symmetry.y2 = board->size - 1;
140 board->symmetry.type = SYM_FULL;
141 board->symmetry.free = true;
143 /* Draw the offboard margin */
144 int top_row = board_size2(board) - board_size(board);
145 int i;
146 for (i = 0; i < board_size(board); i++)
147 board->b[i] = board->b[top_row + i] = S_OFFBOARD;
148 for (i = 0; i <= top_row; i += board_size(board))
149 board->b[i] = board->b[board_size(board) - 1 + i] = S_OFFBOARD;
151 foreach_point(board) {
152 coord_t coord = c;
153 if (board_at(board, coord) == S_OFFBOARD)
154 continue;
155 foreach_neighbor(board, c, {
156 inc_neighbor_count_at(board, coord, board_at(board, c));
157 } );
158 } foreach_point_end;
160 /* First, pass is always a free position. */
161 board->f[board->flen++] = coord_raw(pass);
162 /* All positions are free! Except the margin. */
163 for (i = board_size(board); i < (board_size(board) - 1) * board_size(board); i++)
164 if (i % board_size(board) != 0 && i % board_size(board) != board_size(board) - 1)
165 board->f[board->flen++] = i;
167 /* Initialize zobrist hashtable. */
168 foreach_point(board) {
169 int max = (sizeof(hash_t) << history_hash_bits);
170 /* fast_random() is 16-bit only */
171 board->h[coord_raw(c) * 2] = ((hash_t) fast_random(max))
172 | ((hash_t) fast_random(max) << 16)
173 | ((hash_t) fast_random(max) << 32)
174 | ((hash_t) fast_random(max) << 48);
175 if (!board->h[coord_raw(c) * 2])
176 /* Would be kinda "oops". */
177 board->h[coord_raw(c) * 2] = 1;
178 /* And once again for white */
179 board->h[coord_raw(c) * 2 + 1] = ((hash_t) fast_random(max))
180 | ((hash_t) fast_random(max) << 16)
181 | ((hash_t) fast_random(max) << 32)
182 | ((hash_t) fast_random(max) << 48);
183 if (!board->h[coord_raw(c) * 2 + 1])
184 board->h[coord_raw(c) * 2 + 1] = 1;
185 } foreach_point_end;
189 void
190 board_print(struct board *board, FILE *f)
192 fprintf(f, "Move: % 3d Komi: %2.1f Captures B: %d W: %d\n ",
193 board->moves, board->komi,
194 board->captures[S_BLACK], board->captures[S_WHITE]);
195 int x, y;
196 char asdf[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
197 for (x = 1; x < board_size(board) - 1; x++)
198 fprintf(f, "%c ", asdf[x - 1]);
199 fprintf(f, "\n +-");
200 for (x = 1; x < board_size(board) - 1; x++)
201 fprintf(f, "--");
202 fprintf(f, "+\n");
203 for (y = board_size(board) - 2; y >= 1; y--) {
204 fprintf(f, "%2d | ", y);
205 for (x = 1; x < board_size(board) - 1; x++) {
206 if (coord_x(board->last_move.coord, board) == x && coord_y(board->last_move.coord, board) == y)
207 fprintf(f, "%c)", stone2char(board_atxy(board, x, y)));
208 else
209 fprintf(f, "%c ", stone2char(board_atxy(board, x, y)));
211 if (DEBUGL(6)) {
212 fprintf(f, "| ");
213 for (x = 1; x < board_size(board) - 1; x++) {
214 fprintf(f, "%d ", group_base(group_atxy(board, x, y)));
217 fprintf(f, "|\n");
219 fprintf(f, " +-");
220 for (x = 1; x < board_size(board) - 1; x++)
221 fprintf(f, "--");
222 fprintf(f, "+\n\n");
226 /* Update board hash with given coordinate. */
227 static void profiling_noinline
228 board_hash_update(struct board *board, coord_t coord, enum stone color)
230 board->hash ^= hash_at(board, coord, color);
231 if (DEBUGL(8))
232 fprintf(stderr, "board_hash_update(%d,%d,%d) ^ %llx -> %llx\n", color, coord_x(coord, board), coord_y(coord, board), hash_at(board, coord, color), board->hash);
235 /* Commit current board hash to history. */
236 static void profiling_noinline
237 board_hash_commit(struct board *board)
239 if (DEBUGL(8))
240 fprintf(stderr, "board_hash_commit %llx\n", board->hash);
241 if (likely(board->history_hash[board->hash & history_hash_mask]) == 0) {
242 board->history_hash[board->hash & history_hash_mask] = board->hash;
243 } else {
244 hash_t i = board->hash;
245 while (board->history_hash[i & history_hash_mask]) {
246 if (board->history_hash[i & history_hash_mask] == board->hash) {
247 if (DEBUGL(5))
248 fprintf(stderr, "SUPERKO VIOLATION noted at %d,%d\n",
249 coord_x(board->last_move.coord, board), coord_y(board->last_move.coord, board));
250 board->superko_violation = true;
251 return;
253 i = history_hash_next(i);
255 board->history_hash[i & history_hash_mask] = board->hash;
260 static void
261 board_symmetry_update(struct board *b, coord_t c)
263 if (likely(b->symmetry.type == SYM_NONE)) {
264 /* Fully degenerated already. We do not support detection
265 * of restoring of symmetry, assuming that this is too rare
266 * a case to handle. */
267 return;
270 int x = coord_x(c, b), y = coord_y(c, b), t = board_size(b) / 2;
271 int dx = board_size(b) - x; /* for SYM_DOWN */
272 if (DEBUGL(6)) {
273 fprintf(stderr, "SYMMETRY [%d,%d,%d,%d|%d=%d+%d] update for %d,%d\n",
274 b->symmetry.x1, b->symmetry.y1, b->symmetry.x2, b->symmetry.y2,
275 b->symmetry.d, b->symmetry.type, b->symmetry.free, x, y);
278 switch (b->symmetry.type) {
279 case SYM_FULL:
280 if (x == t && y == t) {
281 /* Tengen keeps full symmetry. */
282 return;
284 /* New symmetry now? */
285 if (x == y) {
286 b->symmetry.type = SYM_DIAG_UP;
287 b->symmetry.x1 = b->symmetry.y1 = 1;
288 b->symmetry.x2 = b->symmetry.y2 = board_size(b) - 1;
289 b->symmetry.d = 1;
290 } else if (dx == y) {
291 b->symmetry.type = SYM_DIAG_DOWN;
292 b->symmetry.x1 = b->symmetry.y1 = 1;
293 b->symmetry.x2 = b->symmetry.y2 = board_size(b) - 1;
294 b->symmetry.d = 1;
295 } else if (x == t) {
296 b->symmetry.type = SYM_HORIZ;
297 b->symmetry.y1 = 1;
298 b->symmetry.y2 = board_size(b) - 1;
299 b->symmetry.d = 0;
300 } else if (y == t) {
301 b->symmetry.type = SYM_VERT;
302 b->symmetry.x1 = 1;
303 b->symmetry.x2 = board_size(b) - 1;
304 b->symmetry.d = 0;
305 } else {
306 break_symmetry:
307 b->symmetry.type = SYM_NONE;
308 b->symmetry.x1 = b->symmetry.y1 = 1;
309 b->symmetry.x2 = b->symmetry.y2 = board_size(b) - 1;
310 b->symmetry.d = 0;
312 break;
313 case SYM_DIAG_UP:
314 if (x == y)
315 return;
316 if (b->symmetry.d > 0 ? x < y : x > y) {
317 /* Good side of symmetry. Just froze it. */
318 b->symmetry.free = false;
319 } else if (b->symmetry.free) {
320 /* Flip symmetry. */
321 b->symmetry.d = - b->symmetry.d;
322 b->symmetry.free = false;
323 } else {
324 /* Break symmetry. */
325 goto break_symmetry;
327 break;
328 case SYM_DIAG_DOWN:
329 if (dx == y)
330 return;
331 if (b->symmetry.d > 0 ? dx < y : dx > y) {
332 b->symmetry.free = false;
333 } else if (b->symmetry.free) {
334 b->symmetry.d = - b->symmetry.d;
335 b->symmetry.free = false;
336 } else {
337 goto break_symmetry;
339 break;
340 case SYM_HORIZ:
341 if (x == t)
342 return;
343 if (b->symmetry.x1 <= x && x <= b->symmetry.x2) {
344 b->symmetry.free = false;
345 } else if (b->symmetry.free) {
346 b->symmetry.x1 = t;
347 b->symmetry.x2 = board_size(b) - 1;
348 b->symmetry.free = false;
349 } else {
350 goto break_symmetry;
352 break;
353 case SYM_VERT:
354 if (b->symmetry.y1 <= y && y <= b->symmetry.y2) {
355 b->symmetry.free = false;
356 } else if (b->symmetry.free) {
357 b->symmetry.y1 = t;
358 b->symmetry.y2 = board_size(b) - 1;
359 b->symmetry.free = false;
360 } else {
361 goto break_symmetry;
363 break;
364 case SYM_NONE:
365 assert(0);
366 break;
369 if (DEBUGL(6)) {
370 fprintf(stderr, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d+%d]\n",
371 b->symmetry.x1, b->symmetry.y1, b->symmetry.x2, b->symmetry.y2,
372 b->symmetry.d, b->symmetry.type, b->symmetry.free);
374 /* Whew. */
378 void
379 board_handicap_stone(struct board *board, int x, int y, FILE *f)
381 struct move m;
382 m.color = S_BLACK;
383 coord_xy(m.coord, x, y, board);
385 board_play(board, &m);
387 char *str = coord2str(m.coord, board);
388 if (DEBUGL(1))
389 fprintf(stderr, "choosing handicap %s (%d,%d)\n", str, x, y);
390 fprintf(f, "%s ", str);
391 free(str);
394 void
395 board_handicap(struct board *board, int stones, FILE *f)
397 int margin = 3 + (board_size(board) >= 13);
398 int min = margin;
399 int mid = board_size(board) / 2;
400 int max = board_size(board) - 1 - margin;
401 const int places[][2] = {
402 { min, min }, { max, max }, { max, min }, { min, max },
403 { min, mid }, { max, mid },
404 { mid, min }, { mid, max },
405 { mid, mid },
408 board->handicap = stones;
410 if (stones == 5 || stones == 7) {
411 board_handicap_stone(board, mid, mid, f);
412 stones--;
415 int i;
416 for (i = 0; i < stones; i++)
417 board_handicap_stone(board, places[i][0], places[i][1], f);
421 static void __attribute__((noinline))
422 check_libs_consistency(struct board *board, group_t g)
424 #ifdef DEBUG
425 if (!g) return;
426 struct group *gi = &board_group_info(board, g);
427 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
428 if (gi->lib[i] && board_at(board, gi->lib[i]) != S_NONE) {
429 fprintf(stderr, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi->lib[i], board), g, coord2sstr(group_base(g), board));
430 assert(0);
432 #endif
435 static void
436 board_capturable_add(struct board *board, group_t group)
438 #ifdef WANT_BOARD_C
439 //fprintf(stderr, "add of group %d (%d)\n", group_base(group), board->clen);
440 assert(group);
441 assert(board->clen < board_size2(board));
442 board->c[board->clen++] = group;
443 #endif
445 static void
446 board_capturable_rm(struct board *board, group_t group)
448 #ifdef WANT_BOARD_C
449 //fprintf(stderr, "rm of group %d\n", group_base(group));
450 for (int i = 0; i < board->clen; i++) {
451 if (unlikely(board->c[i] == group)) {
452 board->c[i] = board->c[--board->clen];
453 return;
456 fprintf(stderr, "rm of bad group %d\n", group_base(group));
457 assert(0);
458 #endif
461 static void
462 board_group_addlib(struct board *board, group_t group, coord_t coord)
464 if (DEBUGL(7)) {
465 fprintf(stderr, "Group %d[%s] %d: Adding liberty %s\n",
466 group_base(group), coord2sstr(group_base(group), board),
467 board_group_info(board, group).libs, coord2sstr(coord, board));
470 check_libs_consistency(board, group);
472 struct group *gi = &board_group_info(board, group);
473 if (gi->libs < GROUP_KEEP_LIBS) {
474 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
475 #if 0
476 /* Seems extra branch just slows it down */
477 if (!gi->lib[i])
478 break;
479 #endif
480 if (unlikely(gi->lib[i] == coord))
481 return;
483 if (gi->libs == 0)
484 board_capturable_add(board, group);
485 else if (gi->libs == 1)
486 board_capturable_rm(board, group);
487 gi->lib[gi->libs++] = coord;
490 check_libs_consistency(board, group);
493 static void
494 board_group_find_extra_libs(struct board *board, group_t group, struct group *gi, coord_t avoid)
496 /* Add extra liberty from the board to our liberty list. */
497 enum stone watermark[board_size2(board)];
498 memcpy(watermark, board->b, sizeof(watermark));
500 for (int i = 0; i < GROUP_KEEP_LIBS - 1; i++)
501 watermark[coord_raw(gi->lib[i])] = S_OFFBOARD;
502 watermark[coord_raw(avoid)] = S_OFFBOARD;
504 foreach_in_group(board, group) {
505 coord_t coord2 = c;
506 foreach_neighbor(board, coord2, {
507 if (likely(watermark[coord_raw(c)] != S_NONE))
508 continue;
509 watermark[coord_raw(c)] = S_OFFBOARD;
510 gi->lib[gi->libs++] = c;
511 if (unlikely(gi->libs >= GROUP_KEEP_LIBS))
512 return;
513 } );
514 } foreach_in_group_end;
517 static void
518 board_group_rmlib(struct board *board, group_t group, coord_t coord)
520 if (DEBUGL(7)) {
521 fprintf(stderr, "Group %d[%s] %d: Removing liberty %s\n",
522 group_base(group), coord2sstr(group_base(group), board),
523 board_group_info(board, group).libs, coord2sstr(coord, board));
526 struct group *gi = &board_group_info(board, group);
527 for (int i = 0; i < GROUP_KEEP_LIBS; i++) {
528 #if 0
529 /* Seems extra branch just slows it down */
530 if (!gi->lib[i])
531 break;
532 #endif
533 if (likely(gi->lib[i] != coord))
534 continue;
536 gi->lib[i] = gi->lib[--gi->libs];
537 gi->lib[gi->libs] = 0;
539 check_libs_consistency(board, group);
541 /* Postpone refilling lib[] until we need to. */
542 assert(GROUP_REFILL_LIBS > 1);
543 if (gi->libs > GROUP_REFILL_LIBS)
544 return;
545 if (gi->libs == GROUP_REFILL_LIBS)
546 board_group_find_extra_libs(board, group, gi, coord);
548 if (gi->libs == 1)
549 board_capturable_add(board, group);
550 else if (gi->libs == 0)
551 board_capturable_rm(board, group);
552 return;
555 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
556 * can call this multiple times per coord. */
557 check_libs_consistency(board, group);
558 return;
562 /* This is a low-level routine that doesn't maintain consistency
563 * of all the board data structures. Use board_group_capture() from
564 * your code. */
565 static void
566 board_remove_stone(struct board *board, coord_t c)
568 enum stone color = board_at(board, c);
569 board_at(board, c) = S_NONE;
570 group_at(board, c) = 0;
571 board_hash_update(board, c, color);
573 /* Increase liberties of surrounding groups */
574 coord_t coord = c;
575 foreach_neighbor(board, coord, {
576 dec_neighbor_count_at(board, c, color);
577 group_t g = group_at(board, c);
578 if (g)
579 board_group_addlib(board, g, coord);
582 if (DEBUGL(6))
583 fprintf(stderr, "pushing free move [%d]: %d,%d\n", board->flen, coord_x(c, board), coord_y(c, board));
584 board->f[board->flen++] = coord_raw(c);
588 static void profiling_noinline
589 add_to_group(struct board *board, group_t group, coord_t prevstone, coord_t coord)
591 foreach_neighbor(board, coord, {
592 if (board_at(board, c) == S_NONE)
593 board_group_addlib(board, group, c);
596 group_at(board, coord) = group;
597 groupnext_at(board, coord) = groupnext_at(board, prevstone);
598 groupnext_at(board, prevstone) = coord_raw(coord);
600 if (DEBUGL(8))
601 fprintf(stderr, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
602 coord_x(prevstone, board), coord_y(prevstone, board),
603 coord_x(coord, board), coord_y(coord, board),
604 groupnext_at(board, coord) % board_size(board), groupnext_at(board, coord) / board_size(board),
605 group_base(group));
608 static void profiling_noinline
609 merge_groups(struct board *board, group_t group_to, group_t group_from)
611 if (DEBUGL(7))
612 fprintf(stderr, "board_play_raw: merging groups %d -> %d\n",
613 group_base(group_from), group_base(group_to));
615 coord_t last_in_group;
616 foreach_in_group(board, group_from) {
617 last_in_group = c;
618 group_at(board, c) = group_to;
619 } foreach_in_group_end;
620 groupnext_at(board, last_in_group) = groupnext_at(board, group_base(group_to));
621 groupnext_at(board, group_base(group_to)) = group_base(group_from);
623 struct group *gi_from = &board_group_info(board, group_from);
624 struct group *gi_to = &board_group_info(board, group_to);
625 if (gi_to->libs < GROUP_KEEP_LIBS) {
626 for (int i = 0; i < gi_from->libs; i++) {
627 for (int j = 0; j < gi_to->libs; j++)
628 if (gi_to->lib[j] == gi_from->lib[i])
629 goto next_from_lib;
630 if (gi_to->libs == 0)
631 board_capturable_add(board, group_to);
632 else if (gi_to->libs == 1)
633 board_capturable_rm(board, group_to);
634 gi_to->lib[gi_to->libs++] = gi_from->lib[i];
635 if (gi_to->libs >= GROUP_KEEP_LIBS)
636 break;
637 next_from_lib:;
641 if (gi_from->libs == 1)
642 board_capturable_rm(board, group_from);
643 memset(gi_from, 0, sizeof(struct group));
645 if (DEBUGL(7))
646 fprintf(stderr, "board_play_raw: merged group: %d\n",
647 group_base(group_to));
650 static group_t profiling_noinline
651 new_group(struct board *board, coord_t coord)
653 group_t group = coord_raw(coord);
654 struct group *gi = &board_group_info(board, group);
655 foreach_neighbor(board, coord, {
656 if (board_at(board, c) == S_NONE)
657 /* board_group_addlib is ridiculously expensive for us */
658 #if GROUP_KEEP_LIBS < 4
659 if (gi->libs < GROUP_KEEP_LIBS)
660 #endif
661 gi->lib[gi->libs++] = c;
663 if (gi->libs == 1)
664 board_capturable_add(board, group);
665 check_libs_consistency(board, group);
667 group_at(board, coord) = group;
668 groupnext_at(board, coord) = 0;
670 if (DEBUGL(8))
671 fprintf(stderr, "new_group: added %d,%d to group %d\n",
672 coord_x(coord, board), coord_y(coord, board),
673 group_base(group));
675 return group;
678 static inline group_t
679 play_one_neighbor(struct board *board,
680 coord_t coord, enum stone color, enum stone other_color,
681 coord_t c, group_t group)
683 enum stone ncolor = board_at(board, c);
684 group_t ngroup = group_at(board, c);
686 inc_neighbor_count_at(board, c, color);
688 if (!ngroup)
689 return group;
691 board_group_rmlib(board, ngroup, coord);
692 if (DEBUGL(7))
693 fprintf(stderr, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
694 group_base(ngroup), ncolor, color, other_color);
696 if (ncolor == color && ngroup != group) {
697 if (!group) {
698 group = ngroup;
699 add_to_group(board, group, c, coord);
700 } else {
701 merge_groups(board, group, ngroup);
703 } else if (ncolor == other_color) {
704 if (DEBUGL(8)) {
705 struct group *gi = &board_group_info(board, ngroup);
706 fprintf(stderr, "testing captured group %d[%s]: ", group_base(ngroup), coord2sstr(group_base(ngroup), board));
707 for (int i = 0; i < GROUP_KEEP_LIBS; i++)
708 fprintf(stderr, "%s ", coord2sstr(gi->lib[i], board));
709 fprintf(stderr, "\n");
711 if (unlikely(board_group_captured(board, ngroup)))
712 board_group_capture(board, ngroup);
714 return group;
717 /* We played on a place with at least one liberty. We will become a member of
718 * some group for sure. */
719 static group_t profiling_noinline
720 board_play_outside(struct board *board, struct move *m, int f)
722 coord_t coord = m->coord;
723 enum stone color = m->color;
724 enum stone other_color = stone_other(color);
725 group_t group = 0;
727 board->f[f] = board->f[--board->flen];
728 if (DEBUGL(6))
729 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
731 foreach_neighbor(board, coord, {
732 group = play_one_neighbor(board, coord, color, other_color, c, group);
735 if (unlikely(!group))
736 group = new_group(board, coord);
738 board_at(board, coord) = color;
739 board->last_move = *m;
740 board->moves++;
741 board_hash_update(board, coord, color);
742 board_symmetry_update(board, coord);
743 struct move ko = { pass, S_NONE };
744 board->ko = ko;
746 return group;
749 /* We played in an eye-like shape. Either we capture at least one of the eye
750 * sides in the process of playing, or return -1. */
751 static int profiling_noinline
752 board_play_in_eye(struct board *board, struct move *m, int f)
754 coord_t coord = m->coord;
755 enum stone color = m->color;
756 /* Check ko: Capture at a position of ko capture one move ago */
757 if (unlikely(color == board->ko.color && coord_eq(coord, board->ko.coord))) {
758 if (DEBUGL(5))
759 fprintf(stderr, "board_check: ko at %d,%d color %d\n", coord_x(coord, board), coord_y(coord, board), color);
760 return -1;
761 } else if (DEBUGL(6)) {
762 fprintf(stderr, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
763 color, coord_x(coord, board), coord_y(coord, board),
764 board->ko.color, coord_x(board->ko.coord, board), coord_y(board->ko.coord, board));
767 struct move ko = { pass, S_NONE };
769 int captured_groups = 0;
771 foreach_neighbor(board, coord, {
772 group_t g = group_at(board, c);
773 if (DEBUGL(7))
774 fprintf(stderr, "board_check: group %d has %d libs\n",
775 g, board_group_info(board, g).libs);
776 captured_groups += (board_group_info(board, g).libs == 1);
779 if (likely(captured_groups == 0)) {
780 if (DEBUGL(5)) {
781 if (DEBUGL(6))
782 board_print(board, stderr);
783 fprintf(stderr, "board_check: one-stone suicide\n");
786 return -1;
789 board->f[f] = board->f[--board->flen];
790 if (DEBUGL(6))
791 fprintf(stderr, "popping free move [%d->%d]: %d\n", board->flen, f, board->f[f]);
793 foreach_neighbor(board, coord, {
794 inc_neighbor_count_at(board, c, color);
796 group_t group = group_at(board, c);
797 if (!group)
798 continue;
800 board_group_rmlib(board, group, coord);
801 if (DEBUGL(7))
802 fprintf(stderr, "board_play_raw: reducing libs for group %d\n",
803 group_base(group));
805 if (board_group_captured(board, group)) {
806 if (board_group_capture(board, group) == 1) {
807 /* If we captured multiple groups at once,
808 * we can't be fighting ko so we don't need
809 * to check for that. */
810 ko.color = stone_other(color);
811 ko.coord = c;
812 if (DEBUGL(5))
813 fprintf(stderr, "guarding ko at %d,%d,%d\n", ko.color, coord_x(ko.coord, board), coord_y(ko.coord, board));
818 board_at(board, coord) = color;
820 board->last_move = *m;
821 board->moves++;
822 board_hash_update(board, coord, color);
823 board_hash_commit(board);
824 board_symmetry_update(board, coord);
825 board->ko = ko;
827 return !!new_group(board, coord);
830 static int __attribute__((flatten))
831 board_play_f(struct board *board, struct move *m, int f)
833 if (DEBUGL(7)) {
834 fprintf(stderr, "board_play(): ---- Playing %d,%d\n", coord_x(m->coord, board), coord_y(m->coord, board));
836 if (likely(!board_is_eyelike(board, &m->coord, stone_other(m->color)))) {
837 /* NOT playing in an eye. Thus this move has to succeed. (This
838 * is thanks to New Zealand rules. Otherwise, multi-stone
839 * suicide might fail.) */
840 group_t group = board_play_outside(board, m, f);
841 if (unlikely(board_group_captured(board, group))) {
842 board_group_capture(board, group);
844 board_hash_commit(board);
845 return 0;
846 } else {
847 return board_play_in_eye(board, m, f);
852 board_play(struct board *board, struct move *m)
854 if (unlikely(is_pass(m->coord) || is_resign(m->coord))) {
855 board->last_move = *m;
856 return 0;
859 int f;
860 for (f = 0; f < board->flen; f++)
861 if (board->f[f] == coord_raw(m->coord))
862 return board_play_f(board, m, f);
864 if (DEBUGL(7))
865 fprintf(stderr, "board_check: stone exists\n");
866 return -1;
870 static inline bool
871 board_try_random_move(struct board *b, enum stone color, coord_t *coord, int f)
873 coord_raw(*coord) = b->f[f];
874 if (unlikely(is_pass(*coord)))
875 return random_pass;
876 struct move m = { *coord, color };
877 if (DEBUGL(6))
878 fprintf(stderr, "trying random move %d: %d,%d\n", f, coord_x(*coord, b), coord_y(*coord, b));
879 return (likely(!board_is_one_point_eye(b, coord, color)) /* bad idea to play into one, usually */
880 && likely(board_play_f(b, &m, f) >= 0));
883 void
884 board_play_random(struct board *b, enum stone color, coord_t *coord)
886 int base = fast_random(b->flen);
887 coord_pos(*coord, base, b);
888 if (likely(board_try_random_move(b, color, coord, base)))
889 return;
891 int f;
892 for (f = base + 1; f < b->flen; f++)
893 if (board_try_random_move(b, color, coord, f))
894 return;
895 for (f = 0; f < base; f++)
896 if (board_try_random_move(b, color, coord, f))
897 return;
899 *coord = pass;
903 bool
904 board_is_eyelike(struct board *board, coord_t *coord, enum stone eye_color)
906 return (neighbor_count_at(board, *coord, eye_color) + neighbor_count_at(board, *coord, S_OFFBOARD)) == 4;
909 bool
910 board_is_one_point_eye(struct board *board, coord_t *coord, enum stone eye_color)
912 enum stone color_diag_libs[S_MAX] = {0, 0, 0, 0};
914 if (likely(neighbor_count_at(board, *coord, eye_color) + neighbor_count_at(board, *coord, S_OFFBOARD) < 4)) {
915 return false;
918 /* XXX: We attempt false eye detection but we will yield false
919 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
921 foreach_diag_neighbor(board, *coord) {
922 color_diag_libs[(enum stone) board_at(board, c)]++;
923 } foreach_diag_neighbor_end;
924 color_diag_libs[stone_other(eye_color)] += !!color_diag_libs[S_OFFBOARD];
925 return likely(color_diag_libs[stone_other(eye_color)] < 2);
928 enum stone
929 board_get_one_point_eye(struct board *board, coord_t *coord)
931 if (board_is_one_point_eye(board, coord, S_WHITE))
932 return S_WHITE;
933 else if (board_is_one_point_eye(board, coord, S_BLACK))
934 return S_BLACK;
935 else
936 return S_NONE;
940 int profiling_noinline
941 board_group_capture(struct board *board, group_t group)
943 int stones = 0;
945 foreach_in_group(board, group) {
946 board->captures[stone_other(board_at(board, c))]++;
947 board_remove_stone(board, c);
948 stones++;
949 } foreach_in_group_end;
951 if (board_group_info(board, group).libs == 1)
952 board_capturable_rm(board, group);
953 memset(&board_group_info(board, group), 0, sizeof(struct group));
955 return stones;
958 bool
959 board_group_in_atari(struct board *board, group_t group, coord_t *lastlib)
961 if (board_group_info(board, group).libs != 1)
962 return false;
963 *lastlib = board_group_info(board, group).lib[0];
964 return true;
967 bool
968 board_group_can_atari(struct board *board, group_t group, coord_t lastlib[2])
970 if (board_group_info(board, group).libs != 2)
971 return false;
972 lastlib[0] = board_group_info(board, group).lib[0];
973 lastlib[1] = board_group_info(board, group).lib[1];
974 return true;
978 static enum stone
979 board_tromp_taylor_owner(struct board *board, coord_t c)
981 int x = coord_x(c, board), y = coord_y(c, board);
982 enum stone color = S_NONE;
983 #define TEST_REACH(xx, yy) \
985 enum stone c2 = board_atxy(board, xx, yy); \
986 if (c2 != S_NONE) { \
987 if (color != S_NONE && color != c2) \
988 return S_NONE; \
989 color = c2; \
990 break; \
993 for (int i = x; i > 0; i--)
994 TEST_REACH(i, y);
995 for (int i = x; i < board_size(board) - 1; i++)
996 TEST_REACH(i, y);
997 for (int i = y; i > 0; i--)
998 TEST_REACH(x, i);
999 for (int i = y; i < board_size(board) - 1; i++)
1000 TEST_REACH(x, i);
1001 return color;
1004 /* Tromp-Taylor Counting */
1005 float
1006 board_official_score(struct board *board)
1009 /* A point P, not colored C, is said to reach C, if there is a path of
1010 * (vertically or horizontally) adjacent points of P's color from P to
1011 * a point of color C.
1013 * A player's score is the number of points of her color, plus the
1014 * number of empty points that reach only her color. */
1016 int scores[S_MAX];
1017 memset(scores, 0, sizeof(scores));
1019 foreach_point(board) {
1020 enum stone color = board_at(board, c);
1021 if (color == S_NONE)
1022 color = board_tromp_taylor_owner(board, c);
1023 scores[color]++;
1024 } foreach_point_end;
1026 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];
1029 float
1030 board_fast_score(struct board *board)
1032 int scores[S_MAX];
1033 memset(scores, 0, sizeof(scores));
1035 foreach_point(board) {
1036 enum stone color = board_at(board, c);
1037 if (color == S_NONE)
1038 color = board_get_one_point_eye(board, &c);
1039 scores[color]++;
1040 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1041 } foreach_point_end;
1043 return board->komi + board->handicap + scores[S_WHITE] - scores[S_BLACK];
1047 bool
1048 valid_escape_route(struct board *b, enum stone color, coord_t to)
1050 /* Assess if we actually gain any liberties by this escape route.
1051 * Note that this is not 100% as we cannot check whether we are
1052 * connecting out or just to ourselves. */
1053 /* Also, we prohibit 1-1 here:
1054 * X X X X |
1055 * O X O O |
1056 * O O X O |
1057 * .(O)X . |
1058 * --------+
1060 int friends = neighbor_count_at(b, to, color);
1061 int borders = neighbor_count_at(b, to, S_OFFBOARD);
1062 int libs = immediate_liberty_count(b, to);
1063 return (friends > 1 && friends + borders < 4) || (libs > 1);
1067 bool
1068 board_stone_radar(struct board *b, coord_t coord, int distance)
1070 int bounds[4] = {
1071 coord_x(coord, b) - distance,
1072 coord_y(coord, b) - distance,
1073 coord_x(coord, b) + distance,
1074 coord_y(coord, b) + distance
1076 for (int i = 0; i < 4; i++)
1077 if (bounds[i] < 1)
1078 bounds[i] = 1;
1079 else if (bounds[i] > board_size(b) - 2)
1080 bounds[i] = board_size(b) - 2;
1081 for (int x = bounds[0]; x <= bounds[2]; x++)
1082 for (int y = bounds[1]; y <= bounds[3]; y++)
1083 if (board_atxy(b, x, y) != S_NONE) {
1084 //fprintf(stderr, "radar %d,%d,%d: %d,%d (%d)\n", coord_x(coord, b), coord_y(coord, b), distance, x, y, board_atxy(b, x, y));
1085 return true;
1087 return false;