11 int board_group_capture(struct board
*board
, int 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
->ko
= m
;
38 struct board
*b
= malloc(sizeof(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
);
56 int csize
= board_size2(b2
) * sizeof(*b2
->c
);
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
;
70 b2
->c
= x
; x
+= csize
;
77 board_done_noalloc(struct board
*board
)
79 if (board
->b
) free(board
->b
);
83 board_done(struct board
*board
)
85 board_done_noalloc(board
);
90 board_resize(struct board
*board
, int size
)
93 assert(board_size(board
) == size
+ 2);
95 board_size(board
) = size
+ 2 /* S_OFFBOARD margin */;
96 board_size2(board
) = board_size(board
) * board_size(board
);
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
);
109 int csize
= board_size2(board
) * sizeof(*board
->c
);
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
;
123 board
->c
= x
; x
+= csize
;
128 board_clear(struct board
*board
)
130 int size
= board_size(board
);
132 board_done_noalloc(board
);
134 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
136 /* Draw the offboard margin */
137 int top_row
= board_size2(board
) - board_size(board
);
139 for (i
= 0; i
< board_size(board
); i
++)
140 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
141 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
142 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
144 foreach_point(board
) {
146 if (board_at(board
, coord
) == S_OFFBOARD
)
148 foreach_neighbor(board
, c
, {
149 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
153 /* First, pass is always a free position. */
154 board
->f
[board
->flen
++] = coord_raw(pass
);
155 /* All positions are free! Except the margin. */
156 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
157 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
158 board
->f
[board
->flen
++] = i
;
160 /* Initialize zobrist hashtable. */
161 foreach_point(board
) {
162 int max
= (sizeof(hash_t
) << history_hash_bits
);
163 /* fast_random() is 16-bit only */
164 board
->h
[coord_raw(c
) * 2] = ((hash_t
) fast_random(max
))
165 | ((hash_t
) fast_random(max
) << 16)
166 | ((hash_t
) fast_random(max
) << 32)
167 | ((hash_t
) fast_random(max
) << 48);
168 if (!board
->h
[coord_raw(c
) * 2])
169 /* Would be kinda "oops". */
170 board
->h
[coord_raw(c
) * 2] = 1;
171 /* And once again for white */
172 board
->h
[coord_raw(c
) * 2 + 1] = ((hash_t
) fast_random(max
))
173 | ((hash_t
) fast_random(max
) << 16)
174 | ((hash_t
) fast_random(max
) << 32)
175 | ((hash_t
) fast_random(max
) << 48);
176 if (!board
->h
[coord_raw(c
) * 2 + 1])
177 board
->h
[coord_raw(c
) * 2 + 1] = 1;
183 board_print(struct board
*board
, FILE *f
)
185 fprintf(f
, "Move: % 3d Komi: %2.1f Captures B: %d W: %d\n ",
186 board
->moves
, board
->komi
,
187 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
189 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
190 for (x
= 1; x
< board_size(board
) - 1; x
++)
191 fprintf(f
, "%c ", asdf
[x
- 1]);
193 for (x
= 1; x
< board_size(board
) - 1; x
++)
196 for (y
= board_size(board
) - 2; y
>= 1; y
--) {
197 fprintf(f
, "%2d | ", y
);
198 for (x
= 1; x
< board_size(board
) - 1; x
++) {
199 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
200 fprintf(f
, "%c)", stone2char(board_atxy(board
, x
, y
)));
202 fprintf(f
, "%c ", stone2char(board_atxy(board
, x
, y
)));
206 for (x
= 1; x
< board_size(board
) - 1; x
++) {
207 fprintf(f
, "%d ", group_atxy(board
, x
, y
));
213 for (x
= 1; x
< board_size(board
) - 1; x
++)
219 /* Update board hash with given coordinate. */
220 static void profiling_noinline
221 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
223 board
->hash
^= hash_at(board
, coord
, color
);
225 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
);
228 /* Commit current board hash to history. */
229 static void profiling_noinline
230 board_hash_commit(struct board
*board
)
233 fprintf(stderr
, "board_hash_commit %llx\n", board
->hash
);
234 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
235 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
237 hash_t i
= board
->hash
;
238 while (board
->history_hash
[i
& history_hash_mask
]) {
239 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
241 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
242 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
243 board
->superko_violation
= true;
246 i
= history_hash_next(i
);
248 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
254 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
258 coord_xy(m
.coord
, x
, y
, board
);
260 board_play(board
, &m
);
262 char *str
= coord2str(m
.coord
, board
);
264 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
265 fprintf(f
, "%s ", str
);
270 board_handicap(struct board
*board
, int stones
, FILE *f
)
272 int margin
= 3 + (board_size(board
) >= 13);
274 int mid
= board_size(board
) / 2;
275 int max
= board_size(board
) - 1 - margin
;
276 const int places
[][2] = {
277 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
278 { min
, mid
}, { max
, mid
},
279 { mid
, min
}, { mid
, max
},
283 board
->handicap
= stones
;
285 if (stones
== 5 || stones
== 7) {
286 board_handicap_stone(board
, mid
, mid
, f
);
291 for (i
= 0; i
< stones
; i
++)
292 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
296 static void __attribute__((noinline
))
297 check_libs_consistency(struct board
*board
, group_t g
)
301 struct group
*gi
= &board_group_info(board
, g
);
302 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
303 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
304 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(g
, board
));
311 board_capturable_add(struct board
*board
, group_t group
)
314 //fprintf(stderr, "add of group %d (%d)\n", group, board->clen);
315 assert(board
->clen
< board_size2(board
));
316 board
->c
[board
->clen
++] = group
;
320 board_capturable_rm(struct board
*board
, group_t group
)
323 //fprintf(stderr, "rm of group %d\n", group);
324 for (int i
= 0; i
< board
->clen
; i
++) {
325 if (unlikely(board
->c
[i
] == group
)) {
326 board
->c
[i
] = board
->c
[--board
->clen
];
330 fprintf(stderr
, "rm of bad group %d\n", group
);
336 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
, bool fresh
)
339 fprintf(stderr
, "Group %d[%s]: Adding liberty %s\n",
340 group
, coord2sstr(group
, board
), coord2sstr(coord
, board
));
343 check_libs_consistency(board
, group
);
345 struct group
*gi
= &board_group_info(board
, group
);
346 if (gi
->libs
< GROUP_KEEP_LIBS
) {
348 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
350 /* Seems extra branch just slows it down */
354 if (unlikely(gi
->lib
[i
] == coord
))
358 board_capturable_add(board
, group
);
359 else if (gi
->libs
== 1)
360 board_capturable_rm(board
, group
);
361 gi
->lib
[gi
->libs
++] = coord
;
364 check_libs_consistency(board
, group
);
368 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
371 fprintf(stderr
, "Group %d[%s]: Removing liberty %s\n",
372 group
, coord2sstr(group
, board
), coord2sstr(coord
, board
));
375 struct group
*gi
= &board_group_info(board
, group
);
376 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
378 /* Seems extra branch just slows it down */
382 if (likely(gi
->lib
[i
] != coord
))
386 for (i
++; i
< GROUP_KEEP_LIBS
; i
++)
388 break; /* Unfilled liberties. */
389 gi
->lib
[j
] = gi
->lib
[i
- 1];
390 gi
->lib
[--gi
->libs
] = 0;
392 check_libs_consistency(board
, group
);
393 if (gi
->libs
< GROUP_KEEP_LIBS
- 1) {
395 board_capturable_add(board
, group
);
396 else if (gi
->libs
== 0)
397 board_capturable_rm(board
, group
);
400 /* Postpone refilling lib[] until we need to. */
401 if (i
> GROUP_REFILL_LIBS
)
406 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
407 * can call this multiple times per coord. */
408 check_libs_consistency(board
, group
);
411 /* Add extra liberty from the board to our liberty list. */
413 bool watermark
[board_size2(board
)];
414 memset(watermark
, 0, sizeof(watermark
));
416 foreach_in_group(board
, group
) {
418 foreach_neighbor(board
, coord2
, {
419 if (likely(watermark
[coord_raw(c
)]))
421 watermark
[coord_raw(c
)] = true;
422 if (c
!= coord
&& board_at(board
, c
) == S_NONE
) {
424 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++) {
425 if (gi
->lib
[i
] == c
) {
431 gi
->lib
[gi
->libs
++] = c
;
432 if (gi
->libs
>= GROUP_KEEP_LIBS
)
437 } foreach_in_group_end
;
441 /* This is a low-level routine that doesn't maintain consistency
442 * of all the board data structures. Use board_group_capture() from
445 board_remove_stone(struct board
*board
, coord_t c
)
447 enum stone color
= board_at(board
, c
);
448 board_at(board
, c
) = S_NONE
;
449 group_at(board
, c
) = 0;
450 board_hash_update(board
, c
, color
);
452 /* Increase liberties of surrounding groups */
454 foreach_neighbor(board
, coord
, {
455 dec_neighbor_count_at(board
, c
, color
);
456 board_group_addlib(board
, group_at(board
, c
), coord
, true);
460 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
461 board
->f
[board
->flen
++] = coord_raw(c
);
465 static void profiling_noinline
466 add_to_group(struct board
*board
, int gid
, coord_t prevstone
, coord_t coord
)
468 foreach_neighbor(board
, coord
, {
469 if (board_at(board
, c
) == S_NONE
)
470 board_group_addlib(board
, gid
, c
, false);
473 group_at(board
, coord
) = gid
;
474 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
475 groupnext_at(board
, prevstone
) = coord_raw(coord
);
478 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
479 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
480 coord_x(coord
, board
), coord_y(coord
, board
),
481 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
485 static void profiling_noinline
486 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
489 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
490 group_from
, group_to
);
492 coord_t last_in_group
;
493 foreach_in_group(board
, group_from
) {
495 group_at(board
, c
) = group_to
;
496 } foreach_in_group_end
;
497 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_to
);
498 groupnext_at(board
, group_to
) = group_from
;
500 struct group
*gi_from
= &board_group_info(board
, group_from
);
501 struct group
*gi_to
= &board_group_info(board
, group_to
);
502 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
503 for (int i
= 0; i
< gi_from
->libs
; i
++) {
504 for (int j
= 0; j
< gi_to
->libs
; j
++)
505 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
507 if (gi_to
->libs
== 0)
508 board_capturable_add(board
, group_to
);
509 else if (gi_to
->libs
== 1)
510 board_capturable_rm(board
, group_to
);
511 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
512 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
518 if (gi_from
->libs
== 1)
519 board_capturable_rm(board
, group_from
);
520 memset(gi_from
, 0, sizeof(struct group
));
523 fprintf(stderr
, "board_play_raw: merged group: %d\n",
527 static group_t profiling_noinline
528 new_group(struct board
*board
, coord_t coord
)
530 group_t gid
= coord_raw(coord
);
531 struct group
*gi
= &board_group_info(board
, gid
);
532 foreach_neighbor(board
, coord
, {
533 if (board_at(board
, c
) == S_NONE
)
534 /* board_group_addlib is ridiculously expensive for us */
535 #if GROUP_KEEP_LIBS < 4
536 if (gi
->libs
< GROUP_KEEP_LIBS
)
538 gi
->lib
[gi
->libs
++] = c
;
541 board_capturable_add(board
, gid
);
542 check_libs_consistency(board
, gid
);
544 group_at(board
, coord
) = gid
;
545 groupnext_at(board
, coord
) = 0;
548 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
549 coord_x(coord
, board
), coord_y(coord
, board
),
556 play_one_neighbor(struct board
*board
,
557 coord_t coord
, enum stone color
, enum stone other_color
,
560 enum stone ncolor
= board_at(board
, c
);
561 group_t ngroup
= group_at(board
, c
);
563 inc_neighbor_count_at(board
, c
, color
);
568 board_group_rmlib(board
, ngroup
, coord
);
570 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
571 ngroup
, ncolor
, color
, other_color
);
573 if (ncolor
== color
&& ngroup
!= gid
) {
576 add_to_group(board
, gid
, c
, coord
);
578 merge_groups(board
, gid
, ngroup
);
580 } else if (ncolor
== other_color
) {
582 struct group
*gi
= &board_group_info(board
, ngroup
);
583 fprintf(stderr
, "testing captured group %d[%s]: ", ngroup
, coord2sstr(ngroup
, board
));
584 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
585 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
586 fprintf(stderr
, "\n");
588 if (unlikely(board_group_captured(board
, ngroup
)))
589 board_group_capture(board
, ngroup
);
594 /* We played on a place with at least one liberty. We will become a member of
595 * some group for sure. */
596 static int profiling_noinline
597 board_play_outside(struct board
*board
, struct move
*m
, int f
)
599 coord_t coord
= m
->coord
;
600 enum stone color
= m
->color
;
601 enum stone other_color
= stone_other(color
);
604 board
->f
[f
] = board
->f
[--board
->flen
];
606 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
608 foreach_neighbor(board
, coord
, {
609 gid
= play_one_neighbor(board
, coord
, color
, other_color
, c
, gid
);
612 if (unlikely(gid
<= 0))
613 gid
= new_group(board
, coord
);
615 board_at(board
, coord
) = color
;
616 board
->last_move
= *m
;
618 board_hash_update(board
, coord
, color
);
619 struct move ko
= { pass
, S_NONE
};
625 /* We played in an eye-like shape. Either we capture at least one of the eye
626 * sides in the process of playing, or return -1. */
627 static int profiling_noinline
628 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
630 coord_t coord
= m
->coord
;
631 enum stone color
= m
->color
;
632 /* Check ko: Capture at a position of ko capture one move ago */
633 if (unlikely(color
== board
->ko
.color
&& coord_eq(coord
, board
->ko
.coord
))) {
635 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
637 } else if (DEBUGL(6)) {
638 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
639 color
, coord_x(coord
, board
), coord_y(coord
, board
),
640 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
643 struct move ko
= { pass
, S_NONE
};
645 int captured_groups
= 0;
647 foreach_neighbor(board
, coord
, {
648 captured_groups
+= (board_group_info(board
, group_at(board
, c
)).libs
== 1);
651 if (likely(captured_groups
== 0)) {
654 board_print(board
, stderr
);
655 fprintf(stderr
, "board_check: one-stone suicide\n");
661 board
->f
[f
] = board
->f
[--board
->flen
];
663 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
665 foreach_neighbor(board
, coord
, {
666 inc_neighbor_count_at(board
, c
, color
);
668 group_t group
= group_at(board
, c
);
672 board_group_rmlib(board
, group
, coord
);
674 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
677 if (board_group_captured(board
, group
)) {
678 if (board_group_capture(board
, group
) == 1) {
679 /* If we captured multiple groups at once,
680 * we can't be fighting ko so we don't need
681 * to check for that. */
682 ko
.color
= stone_other(color
);
685 fprintf(stderr
, "guarding ko at %d,%d,%d\n", ko
.color
, coord_x(ko
.coord
, board
), coord_y(ko
.coord
, board
));
690 board_at(board
, coord
) = color
;
692 board
->last_move
= *m
;
694 board_hash_update(board
, coord
, color
);
695 board_hash_commit(board
);
698 return new_group(board
, coord
);
701 static int __attribute__((flatten
))
702 board_play_f(struct board
*board
, struct move
*m
, int f
)
705 fprintf(stderr
, "board_play(): ---- Playing %d,%d\n", coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
707 if (likely(!board_is_eyelike(board
, &m
->coord
, stone_other(m
->color
)))) {
708 /* NOT playing in an eye. Thus this move has to succeed. (This
709 * is thanks to New Zealand rules. Otherwise, multi-stone
710 * suicide might fail.) */
711 int gid
= board_play_outside(board
, m
, f
);
712 if (unlikely(board_group_captured(board
, gid
))) {
713 board_group_capture(board
, gid
);
715 board_hash_commit(board
);
718 return board_play_in_eye(board
, m
, f
);
723 board_play(struct board
*board
, struct move
*m
)
725 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
726 board
->last_move
= *m
;
731 for (f
= 0; f
< board
->flen
; f
++)
732 if (board
->f
[f
] == coord_raw(m
->coord
))
733 return board_play_f(board
, m
, f
);
736 fprintf(stderr
, "board_check: stone exists\n");
742 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
)
744 coord_raw(*coord
) = b
->f
[f
];
747 struct move m
= { *coord
, color
};
749 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
750 return (!board_is_one_point_eye(b
, coord
, color
) /* bad idea to play into one, usually */
751 && board_play_f(b
, &m
, f
) >= 0);
755 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
)
757 int base
= fast_random(b
->flen
);
758 coord_pos(*coord
, base
, b
);
759 if (likely(board_try_random_move(b
, color
, coord
, base
)))
763 for (f
= base
+ 1; f
< b
->flen
; f
++)
764 if (board_try_random_move(b
, color
, coord
, f
))
766 for (f
= 0; f
< base
; f
++)
767 if (board_try_random_move(b
, color
, coord
, f
))
775 board_is_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
777 return (neighbor_count_at(board
, *coord
, eye_color
) + neighbor_count_at(board
, *coord
, S_OFFBOARD
)) == 4;
781 board_is_one_point_eye(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
783 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
785 if (likely(neighbor_count_at(board
, *coord
, eye_color
) + neighbor_count_at(board
, *coord
, S_OFFBOARD
) < 4)) {
789 /* XXX: We attempt false eye detection but we will yield false
790 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
792 foreach_diag_neighbor(board
, *coord
) {
793 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
794 } foreach_diag_neighbor_end
;
795 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
796 return likely(color_diag_libs
[stone_other(eye_color
)] < 2);
800 board_get_one_point_eye(struct board
*board
, coord_t
*coord
)
802 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
804 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
811 int profiling_noinline
812 board_group_capture(struct board
*board
, int group
)
816 foreach_in_group(board
, group
) {
817 board
->captures
[stone_other(board_at(board
, c
))]++;
818 board_remove_stone(board
, c
);
820 } foreach_in_group_end
;
822 if (board_group_info(board
, group
).libs
== 1)
823 board_capturable_rm(board
, group
);
824 memset(&board_group_info(board
, group
), 0, sizeof(struct group
));
830 board_group_in_atari(struct board
*board
, group_t group
, coord_t
*lastlib
)
832 if (board_group_info(board
, group
).libs
!= 1)
834 *lastlib
= board_group_info(board
, group
).lib
[0];
839 board_group_can_atari(struct board
*board
, group_t group
, coord_t lastlib
[2])
841 if (board_group_info(board
, group
).libs
!= 2)
843 lastlib
[0] = board_group_info(board
, group
).lib
[0];
844 lastlib
[1] = board_group_info(board
, group
).lib
[1];
850 board_tromp_taylor_owner(struct board
*board
, coord_t c
)
852 int x
= coord_x(c
, board
), y
= coord_y(c
, board
);
853 enum stone color
= S_NONE
;
854 #define TEST_REACH(xx, yy) \
856 enum stone c2 = board_atxy(board, xx, yy); \
857 if (c2 != S_NONE) { \
858 if (color != S_NONE && color != c2) \
864 for (int i
= x
; i
> 0; i
--)
866 for (int i
= x
; i
< board_size(board
) - 1; i
++)
868 for (int i
= y
; i
> 0; i
--)
870 for (int i
= y
; i
< board_size(board
) - 1; i
++)
875 /* Tromp-Taylor Counting */
877 board_official_score(struct board
*board
)
880 /* A point P, not colored C, is said to reach C, if there is a path of
881 * (vertically or horizontally) adjacent points of P's color from P to
882 * a point of color C.
884 * A player's score is the number of points of her color, plus the
885 * number of empty points that reach only her color. */
888 memset(scores
, 0, sizeof(scores
));
890 foreach_point(board
) {
891 enum stone color
= board_at(board
, c
);
893 color
= board_tromp_taylor_owner(board
, c
);
897 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
901 board_fast_score(struct board
*board
)
904 memset(scores
, 0, sizeof(scores
));
906 foreach_point(board
) {
907 enum stone color
= board_at(board
, c
);
909 color
= board_get_one_point_eye(board
, &c
);
911 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
914 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
919 valid_escape_route(struct board
*b
, enum stone color
, coord_t to
)
921 /* Assess if we actually gain any liberties by this escape route.
922 * Note that this is not 100% as we cannot check whether we are
923 * connecting out or just to ourselves. */
924 /* Also, we prohibit 1-1 here:
931 int friends
= neighbor_count_at(b
, to
, color
);
932 int borders
= neighbor_count_at(b
, to
, S_OFFBOARD
);
933 int libs
= immediate_liberty_count(b
, to
);
934 return (friends
> 1 && friends
+ borders
< 4) || (libs
> 1);
939 board_stone_radar(struct board
*b
, coord_t coord
, int distance
)
942 coord_x(coord
, b
) - distance
,
943 coord_y(coord
, b
) - distance
,
944 coord_x(coord
, b
) + distance
,
945 coord_y(coord
, b
) + distance
947 for (int i
= 0; i
< 4; i
++)
950 else if (bounds
[i
] > board_size(b
) - 2)
951 bounds
[i
] = board_size(b
) - 2;
952 for (int x
= bounds
[0]; x
<= bounds
[2]; x
++)
953 for (int y
= bounds
[1]; y
<= bounds
[3]; y
++)
954 if (board_atxy(b
, x
, y
) != S_NONE
) {
955 //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));