11 int board_group_capture(struct board
*board
, group_t group
);
13 bool random_pass
= false;
17 #define profiling_noinline __attribute__((noinline))
19 #define profiling_noinline
22 #define gi_granularity 4
23 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
27 board_setup(struct board
*b
)
29 memset(b
, 0, sizeof(*b
));
31 struct move m
= { pass
, S_NONE
};
32 b
->last_move
= b
->last_move2
= b
->last_ko
= b
->ko
= m
;
38 struct board
*b
= malloc(sizeof(struct board
));
49 board_copy(struct board
*b2
, struct board
*b1
)
51 memcpy(b2
, b1
, sizeof(struct board
));
53 int bsize
= board_size2(b2
) * sizeof(*b2
->b
);
54 int gsize
= board_size2(b2
) * sizeof(*b2
->g
);
55 int fsize
= board_size2(b2
) * sizeof(*b2
->f
);
56 int nsize
= board_size2(b2
) * sizeof(*b2
->n
);
57 int psize
= board_size2(b2
) * sizeof(*b2
->p
);
58 int hsize
= board_size2(b2
) * 2 * sizeof(*b2
->h
);
59 int gisize
= board_size2(b2
) * sizeof(*b2
->gi
);
61 int csize
= board_size2(b2
) * sizeof(*b2
->c
);
65 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
);
66 memcpy(x
, b1
->b
, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
);
67 b2
->b
= x
; x
+= bsize
;
68 b2
->g
= x
; x
+= gsize
;
69 b2
->f
= x
; x
+= fsize
;
70 b2
->p
= x
; x
+= psize
;
71 b2
->n
= x
; x
+= nsize
;
72 b2
->h
= x
; x
+= hsize
;
73 b2
->gi
= x
; x
+= gisize
;
75 b2
->c
= x
; x
+= csize
;
82 board_done_noalloc(struct board
*board
)
84 if (board
->b
) free(board
->b
);
88 board_done(struct board
*board
)
90 board_done_noalloc(board
);
95 board_resize(struct board
*board
, int size
)
98 assert(board_size(board
) == size
+ 2);
100 board_size(board
) = size
+ 2 /* S_OFFBOARD margin */;
101 board_size2(board
) = board_size(board
) * board_size(board
);
106 int bsize
= board_size2(board
) * sizeof(*board
->b
);
107 int gsize
= board_size2(board
) * sizeof(*board
->g
);
108 int fsize
= board_size2(board
) * sizeof(*board
->f
);
109 int nsize
= board_size2(board
) * sizeof(*board
->n
);
110 int psize
= board_size2(board
) * sizeof(*board
->p
);
111 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
112 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
114 int csize
= board_size2(board
) * sizeof(*board
->c
);
118 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
);
119 memset(x
, 0, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
);
120 board
->b
= x
; x
+= bsize
;
121 board
->g
= x
; x
+= gsize
;
122 board
->f
= x
; x
+= fsize
;
123 board
->p
= x
; x
+= psize
;
124 board
->n
= x
; x
+= nsize
;
125 board
->h
= x
; x
+= hsize
;
126 board
->gi
= x
; x
+= gisize
;
128 board
->c
= x
; x
+= csize
;
133 board_clear(struct board
*board
)
135 int size
= board_size(board
);
136 float komi
= board
->komi
;
138 board_done_noalloc(board
);
140 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
144 /* Setup initial symmetry */
145 board
->symmetry
.d
= 1;
146 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
147 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
148 board
->symmetry
.type
= SYM_FULL
;
150 /* Draw the offboard margin */
151 int top_row
= board_size2(board
) - board_size(board
);
153 for (i
= 0; i
< board_size(board
); i
++)
154 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
155 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
156 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
158 foreach_point(board
) {
160 if (board_at(board
, coord
) == S_OFFBOARD
)
162 foreach_neighbor(board
, c
, {
163 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
167 /* First, pass is always a free position. */
168 board
->f
[board
->flen
++] = coord_raw(pass
);
169 /* All positions are free! Except the margin. */
170 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
171 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
172 board
->f
[board
->flen
++] = i
;
174 /* Initialize zobrist hashtable. */
175 foreach_point(board
) {
176 int max
= (sizeof(hash_t
) << history_hash_bits
);
177 /* fast_random() is 16-bit only */
178 board
->h
[coord_raw(c
) * 2] = ((hash_t
) fast_random(max
))
179 | ((hash_t
) fast_random(max
) << 16)
180 | ((hash_t
) fast_random(max
) << 32)
181 | ((hash_t
) fast_random(max
) << 48);
182 if (!board
->h
[coord_raw(c
) * 2])
183 /* Would be kinda "oops". */
184 board
->h
[coord_raw(c
) * 2] = 1;
185 /* And once again for white */
186 board
->h
[coord_raw(c
) * 2 + 1] = ((hash_t
) fast_random(max
))
187 | ((hash_t
) fast_random(max
) << 16)
188 | ((hash_t
) fast_random(max
) << 32)
189 | ((hash_t
) fast_random(max
) << 48);
190 if (!board
->h
[coord_raw(c
) * 2 + 1])
191 board
->h
[coord_raw(c
) * 2 + 1] = 1;
197 board_print_top(struct board
*board
, FILE *f
, int c
)
199 for (int i
= 0; i
< c
; i
++) {
200 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
202 for (int x
= 1; x
< board_size(board
) - 1; x
++)
203 fprintf(f
, "%c ", asdf
[x
- 1]);
207 for (int i
= 0; i
< c
; i
++) {
209 for (int x
= 1; x
< board_size(board
) - 1; x
++)
217 board_print_bottom(struct board
*board
, FILE *f
, int c
)
219 for (int i
= 0; i
< c
; i
++) {
221 for (int x
= 1; x
< board_size(board
) - 1; x
++)
228 typedef void (*board_cprint
)(struct board
*b
, coord_t c
, FILE *f
);
231 board_print_row(struct board
*board
, int y
, FILE *f
, board_cprint cprint
)
233 fprintf(f
, " %2d | ", y
);
234 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
235 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
236 fprintf(f
, "%c)", stone2char(board_atxy(board
, x
, y
)));
238 fprintf(f
, "%c ", stone2char(board_atxy(board
, x
, y
)));
242 fprintf(f
, " %2d | ", y
);
243 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
244 cprint(board
, coord_xy(board
, x
, y
), f
);
252 board_print_x(struct board
*board
, FILE *f
, board_cprint cprint
)
254 fprintf(f
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
255 board
->moves
, board
->komi
, board
->handicap
,
256 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
257 board_print_top(board
, f
, 1 + !!cprint
);
258 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
259 board_print_row(board
, y
, f
, cprint
);
260 board_print_bottom(board
, f
, 1 + !!cprint
);
265 cprint_group(struct board
*board
, coord_t c
, FILE *f
)
267 fprintf(f
, "%d ", group_base(group_at(board
, c
)));
271 board_print(struct board
*board
, FILE *f
)
273 board_print_x(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
277 /* Update board hash with given coordinate. */
278 static void profiling_noinline
279 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
281 board
->hash
^= hash_at(board
, coord
, color
);
283 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
);
286 /* Commit current board hash to history. */
287 static void profiling_noinline
288 board_hash_commit(struct board
*board
)
291 fprintf(stderr
, "board_hash_commit %llx\n", board
->hash
);
292 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
293 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
295 hash_t i
= board
->hash
;
296 while (board
->history_hash
[i
& history_hash_mask
]) {
297 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
299 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
300 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
301 board
->superko_violation
= true;
304 i
= history_hash_next(i
);
306 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
312 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
314 if (likely(symmetry
->type
== SYM_NONE
)) {
315 /* Fully degenerated already. We do not support detection
316 * of restoring of symmetry, assuming that this is too rare
317 * a case to handle. */
321 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
322 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
324 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
325 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
326 symmetry
->d
, symmetry
->type
, x
, y
);
329 switch (symmetry
->type
) {
331 if (x
== t
&& y
== t
) {
332 /* Tengen keeps full symmetry. */
335 /* New symmetry now? */
337 symmetry
->type
= SYM_DIAG_UP
;
338 symmetry
->x1
= symmetry
->y1
= 1;
339 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
341 } else if (dx
== y
) {
342 symmetry
->type
= SYM_DIAG_DOWN
;
343 symmetry
->x1
= symmetry
->y1
= 1;
344 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
347 symmetry
->type
= SYM_HORIZ
;
349 symmetry
->y2
= board_size(b
) - 1;
352 symmetry
->type
= SYM_VERT
;
354 symmetry
->x2
= board_size(b
) - 1;
358 symmetry
->type
= SYM_NONE
;
359 symmetry
->x1
= symmetry
->y1
= 1;
360 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
386 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
387 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
388 symmetry
->d
, symmetry
->type
);
395 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
398 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
400 board_play(board
, &m
);
401 /* Simulate white passing; otherwise, UCT search can get confused since
402 * tree depth parity won't match the color to move. */
405 char *str
= coord2str(m
.coord
, board
);
407 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
408 fprintf(f
, "%s ", str
);
413 board_handicap(struct board
*board
, int stones
, FILE *f
)
415 int margin
= 3 + (board_size(board
) >= 13);
417 int mid
= board_size(board
) / 2;
418 int max
= board_size(board
) - 1 - margin
;
419 const int places
[][2] = {
420 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
421 { min
, mid
}, { max
, mid
},
422 { mid
, min
}, { mid
, max
},
426 board
->handicap
= stones
;
428 if (stones
== 5 || stones
== 7) {
429 board_handicap_stone(board
, mid
, mid
, f
);
434 for (i
= 0; i
< stones
; i
++)
435 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
439 static void __attribute__((noinline
))
440 check_libs_consistency(struct board
*board
, group_t g
)
444 struct group
*gi
= &board_group_info(board
, g
);
445 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
446 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
447 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
454 board_capturable_add(struct board
*board
, group_t group
)
457 //fprintf(stderr, "add of group %d (%d)\n", group_base(group), board->clen);
459 assert(board
->clen
< board_size2(board
));
460 board
->c
[board
->clen
++] = group
;
464 board_capturable_rm(struct board
*board
, group_t group
)
467 //fprintf(stderr, "rm of group %d\n", group_base(group));
468 for (int i
= 0; i
< board
->clen
; i
++) {
469 if (unlikely(board
->c
[i
] == group
)) {
470 board
->c
[i
] = board
->c
[--board
->clen
];
474 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
480 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
483 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
484 group_base(group
), coord2sstr(group_base(group
), board
),
485 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
488 check_libs_consistency(board
, group
);
490 struct group
*gi
= &board_group_info(board
, group
);
491 if (gi
->libs
< GROUP_KEEP_LIBS
) {
492 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
494 /* Seems extra branch just slows it down */
498 if (unlikely(gi
->lib
[i
] == coord
))
502 board_capturable_add(board
, group
);
503 else if (gi
->libs
== 1)
504 board_capturable_rm(board
, group
);
505 gi
->lib
[gi
->libs
++] = coord
;
508 check_libs_consistency(board
, group
);
512 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
514 /* Add extra liberty from the board to our liberty list. */
515 unsigned char watermark
[board_size2(board
) / 8];
516 memset(watermark
, 0, sizeof(watermark
));
517 #define watermark_get(c) (watermark[coord_raw(c) >> 3] & (1 << (coord_raw(c) & 7)))
518 #define watermark_set(c) watermark[coord_raw(c) >> 3] |= (1 << (coord_raw(c) & 7))
520 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
521 watermark_set(gi
->lib
[i
]);
522 watermark_set(avoid
);
524 foreach_in_group(board
, group
) {
526 foreach_neighbor(board
, coord2
, {
527 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
530 gi
->lib
[gi
->libs
++] = c
;
531 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
534 } foreach_in_group_end
;
540 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
543 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
544 group_base(group
), coord2sstr(group_base(group
), board
),
545 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
548 struct group
*gi
= &board_group_info(board
, group
);
549 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
551 /* Seems extra branch just slows it down */
555 if (likely(gi
->lib
[i
] != coord
))
558 gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
559 gi
->lib
[gi
->libs
] = 0;
561 check_libs_consistency(board
, group
);
563 /* Postpone refilling lib[] until we need to. */
564 assert(GROUP_REFILL_LIBS
> 1);
565 if (gi
->libs
> GROUP_REFILL_LIBS
)
567 if (gi
->libs
== GROUP_REFILL_LIBS
)
568 board_group_find_extra_libs(board
, group
, gi
, coord
);
571 board_capturable_add(board
, group
);
572 else if (gi
->libs
== 0)
573 board_capturable_rm(board
, group
);
577 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
578 * can call this multiple times per coord. */
579 check_libs_consistency(board
, group
);
584 /* This is a low-level routine that doesn't maintain consistency
585 * of all the board data structures. Use board_group_capture() from
588 board_remove_stone(struct board
*board
, coord_t c
)
590 enum stone color
= board_at(board
, c
);
591 board_at(board
, c
) = S_NONE
;
592 group_at(board
, c
) = 0;
593 board_hash_update(board
, c
, color
);
595 /* Increase liberties of surrounding groups */
597 foreach_neighbor(board
, coord
, {
598 dec_neighbor_count_at(board
, c
, color
);
599 group_t g
= group_at(board
, c
);
601 board_group_addlib(board
, g
, coord
);
605 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
606 board
->f
[board
->flen
++] = coord_raw(c
);
610 static void profiling_noinline
611 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
613 foreach_neighbor(board
, coord
, {
614 if (board_at(board
, c
) == S_NONE
)
615 board_group_addlib(board
, group
, c
);
618 group_at(board
, coord
) = group
;
619 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
620 groupnext_at(board
, prevstone
) = coord_raw(coord
);
623 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
624 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
625 coord_x(coord
, board
), coord_y(coord
, board
),
626 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
630 static void profiling_noinline
631 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
634 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
635 group_base(group_from
), group_base(group_to
));
637 coord_t last_in_group
;
638 foreach_in_group(board
, group_from
) {
640 group_at(board
, c
) = group_to
;
641 } foreach_in_group_end
;
642 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
643 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
645 struct group
*gi_from
= &board_group_info(board
, group_from
);
646 struct group
*gi_to
= &board_group_info(board
, group_to
);
647 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
648 for (int i
= 0; i
< gi_from
->libs
; i
++) {
649 for (int j
= 0; j
< gi_to
->libs
; j
++)
650 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
652 if (gi_to
->libs
== 0)
653 board_capturable_add(board
, group_to
);
654 else if (gi_to
->libs
== 1)
655 board_capturable_rm(board
, group_to
);
656 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
657 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
663 if (gi_from
->libs
== 1)
664 board_capturable_rm(board
, group_from
);
665 memset(gi_from
, 0, sizeof(struct group
));
668 fprintf(stderr
, "board_play_raw: merged group: %d\n",
669 group_base(group_to
));
672 static group_t profiling_noinline
673 new_group(struct board
*board
, coord_t coord
)
675 group_t group
= coord_raw(coord
);
676 struct group
*gi
= &board_group_info(board
, group
);
677 foreach_neighbor(board
, coord
, {
678 if (board_at(board
, c
) == S_NONE
)
679 /* board_group_addlib is ridiculously expensive for us */
680 #if GROUP_KEEP_LIBS < 4
681 if (gi
->libs
< GROUP_KEEP_LIBS
)
683 gi
->lib
[gi
->libs
++] = c
;
686 board_capturable_add(board
, group
);
687 check_libs_consistency(board
, group
);
689 group_at(board
, coord
) = group
;
690 groupnext_at(board
, coord
) = 0;
693 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
694 coord_x(coord
, board
), coord_y(coord
, board
),
700 static inline group_t
701 play_one_neighbor(struct board
*board
,
702 coord_t coord
, enum stone color
, enum stone other_color
,
703 coord_t c
, group_t group
)
705 enum stone ncolor
= board_at(board
, c
);
706 group_t ngroup
= group_at(board
, c
);
708 inc_neighbor_count_at(board
, c
, color
);
713 board_group_rmlib(board
, ngroup
, coord
);
715 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
716 group_base(ngroup
), ncolor
, color
, other_color
);
718 if (ncolor
== color
&& ngroup
!= group
) {
721 add_to_group(board
, group
, c
, coord
);
723 merge_groups(board
, group
, ngroup
);
725 } else if (ncolor
== other_color
) {
727 struct group
*gi
= &board_group_info(board
, ngroup
);
728 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
729 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
730 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
731 fprintf(stderr
, "\n");
733 if (unlikely(board_group_captured(board
, ngroup
)))
734 board_group_capture(board
, ngroup
);
739 /* We played on a place with at least one liberty. We will become a member of
740 * some group for sure. */
741 static group_t profiling_noinline
742 board_play_outside(struct board
*board
, struct move
*m
, int f
)
744 coord_t coord
= m
->coord
;
745 enum stone color
= m
->color
;
746 enum stone other_color
= stone_other(color
);
749 board
->f
[f
] = board
->f
[--board
->flen
];
751 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
753 foreach_neighbor(board
, coord
, {
754 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
757 if (unlikely(!group
))
758 group
= new_group(board
, coord
);
760 board_at(board
, coord
) = color
;
761 board
->last_move2
= board
->last_move
;
762 board
->last_move
= *m
;
764 board_hash_update(board
, coord
, color
);
765 board_symmetry_update(board
, &board
->symmetry
, coord
);
766 struct move ko
= { pass
, S_NONE
};
772 /* We played in an eye-like shape. Either we capture at least one of the eye
773 * sides in the process of playing, or return -1. */
774 static int profiling_noinline
775 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
777 coord_t coord
= m
->coord
;
778 enum stone color
= m
->color
;
779 /* Check ko: Capture at a position of ko capture one move ago */
780 if (unlikely(color
== board
->ko
.color
&& coord_eq(coord
, board
->ko
.coord
))) {
782 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
784 } else if (DEBUGL(6)) {
785 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
786 color
, coord_x(coord
, board
), coord_y(coord
, board
),
787 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
790 struct move ko
= { pass
, S_NONE
};
792 int captured_groups
= 0;
794 foreach_neighbor(board
, coord
, {
795 group_t g
= group_at(board
, c
);
797 fprintf(stderr
, "board_check: group %d has %d libs\n",
798 g
, board_group_info(board
, g
).libs
);
799 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
802 if (likely(captured_groups
== 0)) {
805 board_print(board
, stderr
);
806 fprintf(stderr
, "board_check: one-stone suicide\n");
812 board
->f
[f
] = board
->f
[--board
->flen
];
814 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
816 foreach_neighbor(board
, coord
, {
817 inc_neighbor_count_at(board
, c
, color
);
819 group_t group
= group_at(board
, c
);
823 board_group_rmlib(board
, group
, coord
);
825 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
828 if (board_group_captured(board
, group
)) {
829 if (board_group_capture(board
, group
) == 1) {
830 /* If we captured multiple groups at once,
831 * we can't be fighting ko so we don't need
832 * to check for that. */
833 ko
.color
= stone_other(color
);
836 board
->last_ko_age
= board
->moves
;
838 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
843 board_at(board
, coord
) = color
;
845 board
->last_move2
= board
->last_move
;
846 board
->last_move
= *m
;
848 board_hash_update(board
, coord
, color
);
849 board_hash_commit(board
);
850 board_symmetry_update(board
, &board
->symmetry
, coord
);
853 return !!new_group(board
, coord
);
856 static int __attribute__((flatten
))
857 board_play_f(struct board
*board
, struct move
*m
, int f
)
860 fprintf(stderr
, "board_play(): ---- Playing %d,%d\n", coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
862 if (likely(!board_is_eyelike(board
, &m
->coord
, stone_other(m
->color
)))) {
863 /* NOT playing in an eye. Thus this move has to succeed. (This
864 * is thanks to New Zealand rules. Otherwise, multi-stone
865 * suicide might fail.) */
866 group_t group
= board_play_outside(board
, m
, f
);
867 if (unlikely(board_group_captured(board
, group
))) {
868 board_group_capture(board
, group
);
870 board_hash_commit(board
);
873 return board_play_in_eye(board
, m
, f
);
878 board_play(struct board
*board
, struct move
*m
)
880 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
881 board
->last_move2
= board
->last_move
;
882 board
->last_move
= *m
;
887 for (f
= 0; f
< board
->flen
; f
++)
888 if (board
->f
[f
] == coord_raw(m
->coord
))
889 return board_play_f(board
, m
, f
);
892 fprintf(stderr
, "board_check: stone exists\n");
898 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
900 coord_raw(*coord
) = b
->f
[f
];
901 if (unlikely(is_pass(*coord
)))
903 struct move m
= { *coord
, color
};
905 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
906 return (likely(!board_is_one_point_eye(b
, coord
, color
)) /* bad idea to play into one, usually */
907 && board_is_valid_move(b
, &m
)
908 && (!permit
|| permit(permit_data
, b
, &m
))
909 && likely(board_play_f(b
, &m
, f
) >= 0));
913 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
915 int base
= fast_random(b
->flen
);
916 coord_pos(*coord
, base
, b
);
917 if (likely(board_try_random_move(b
, color
, coord
, base
, permit
, permit_data
)))
921 for (f
= base
+ 1; f
< b
->flen
; f
++)
922 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
924 for (f
= 0; f
< base
; f
++)
925 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
933 board_is_false_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
935 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
937 /* XXX: We attempt false eye detection but we will yield false
938 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
940 foreach_diag_neighbor(board
, *coord
) {
941 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
942 } foreach_diag_neighbor_end
;
943 /* For false eye, we need two enemy stones diagonally in the
944 * middle of the board, or just one enemy stone at the edge
945 * or in the corner. */
946 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
947 return color_diag_libs
[stone_other(eye_color
)] >= 2;
951 board_is_one_point_eye(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
953 return board_is_eyelike(board
, coord
, eye_color
)
954 && !board_is_false_eyelike(board
, coord
, eye_color
);
958 board_get_one_point_eye(struct board
*board
, coord_t
*coord
)
960 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
962 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
969 int profiling_noinline
970 board_group_capture(struct board
*board
, group_t group
)
974 foreach_in_group(board
, group
) {
975 board
->captures
[stone_other(board_at(board
, c
))]++;
976 board_remove_stone(board
, c
);
978 } foreach_in_group_end
;
980 if (board_group_info(board
, group
).libs
== 1)
981 board_capturable_rm(board
, group
);
982 memset(&board_group_info(board
, group
), 0, sizeof(struct group
));
989 board_fast_score(struct board
*board
)
992 memset(scores
, 0, sizeof(scores
));
994 foreach_point(board
) {
995 enum stone color
= board_at(board
, c
);
997 color
= board_get_one_point_eye(board
, &c
);
999 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1000 } foreach_point_end
;
1002 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1005 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1007 /* One flood-fill iteration; returns true if next iteration
1010 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1012 bool needs_update
= false;
1013 foreach_point(board
) {
1014 /* Ignore occupied and already-dame positions. */
1015 if (board_at(board
, c
) != S_NONE
|| ownermap
[c
] == 3)
1017 /* Count neighbors. */
1019 foreach_neighbor(board
, c
, {
1022 /* If we have neighbors of both colors, or dame,
1023 * we are dame too. */
1024 if ((nei
[1] && nei
[2]) || nei
[3]) {
1026 /* Speed up the propagation. */
1027 foreach_neighbor(board
, c
, {
1028 if (board_at(board
, c
) == S_NONE
)
1031 needs_update
= true;
1034 /* If we have neighbors of one color, we are owned
1035 * by that color, too. */
1036 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1037 int newowner
= nei
[1] ? 1 : 2;
1038 ownermap
[c
] = newowner
;
1039 /* Speed up the propagation. */
1040 foreach_neighbor(board
, c
, {
1041 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1042 ownermap
[c
] = newowner
;
1044 needs_update
= true;
1047 } foreach_point_end
;
1048 return needs_update
;
1051 /* Tromp-Taylor Counting */
1053 board_official_score(struct board
*board
)
1056 /* A point P, not colored C, is said to reach C, if there is a path of
1057 * (vertically or horizontally) adjacent points of P's color from P to
1058 * a point of color C.
1060 * A player's score is the number of points of her color, plus the
1061 * number of empty points that reach only her color. */
1063 int ownermap
[board_size2(board
)];
1065 foreach_point(board
) {
1066 int o
[4] = {0, 1, 2, 0};
1067 ownermap
[c
] = o
[board_at(board
, c
)];
1068 s
[board_at(board
, c
)]++;
1069 } foreach_point_end
;
1071 /* We need to special-case empty board. */
1072 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1073 return board
->komi
+ board
->handicap
;
1075 while (board_tromp_taylor_iter(board
, ownermap
))
1076 /* Flood-fill... */;
1079 memset(scores
, 0, sizeof(scores
));
1081 foreach_point(board
) {
1082 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1083 if (ownermap
[c
] == 3)
1085 scores
[ownermap
[c
]]++;
1086 } foreach_point_end
;
1088 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];