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
->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 /* 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
);
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
) {
153 if (board_at(board
, coord
) == S_OFFBOARD
)
155 foreach_neighbor(board
, c
, {
156 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
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;
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
]);
196 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
197 for (x
= 1; x
< board_size(board
) - 1; x
++)
198 fprintf(f
, "%c ", asdf
[x
- 1]);
200 for (x
= 1; x
< board_size(board
) - 1; x
++)
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
)));
209 fprintf(f
, "%c ", stone2char(board_atxy(board
, x
, y
)));
213 for (x
= 1; x
< board_size(board
) - 1; x
++) {
214 fprintf(f
, "%d ", group_base(group_atxy(board
, x
, y
)));
220 for (x
= 1; x
< board_size(board
) - 1; x
++)
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
);
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
)
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
;
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
) {
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;
253 i
= history_hash_next(i
);
255 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
261 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
263 if (likely(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. */
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 */
273 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d+%d] update for %d,%d\n",
274 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
275 symmetry
->d
, symmetry
->type
, symmetry
->free
, x
, y
);
278 switch (symmetry
->type
) {
280 if (x
== t
&& y
== t
) {
281 /* Tengen keeps full symmetry. */
284 /* New symmetry now? */
286 symmetry
->type
= SYM_DIAG_UP
;
287 symmetry
->x1
= symmetry
->y1
= 1;
288 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
290 } else if (dx
== y
) {
291 symmetry
->type
= SYM_DIAG_DOWN
;
292 symmetry
->x1
= symmetry
->y1
= 1;
293 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
296 symmetry
->type
= SYM_HORIZ
;
298 symmetry
->y2
= board_size(b
) - 1;
301 symmetry
->type
= SYM_VERT
;
303 symmetry
->x2
= board_size(b
) - 1;
307 symmetry
->type
= SYM_NONE
;
308 symmetry
->x1
= symmetry
->y1
= 1;
309 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
316 if (symmetry
->d
> 0 ? x
< y
: x
> y
) {
317 /* Good side of symmetry. Just froze it. */
318 symmetry
->free
= false;
319 } else if (symmetry
->free
) {
321 symmetry
->d
= - symmetry
->d
;
322 symmetry
->free
= false;
324 /* Break symmetry. */
331 if (symmetry
->d
> 0 ? dx
< y
: dx
> y
) {
332 symmetry
->free
= false;
333 } else if (symmetry
->free
) {
334 symmetry
->d
= - symmetry
->d
;
335 symmetry
->free
= false;
343 if (symmetry
->x1
<= x
&& x
<= symmetry
->x2
) {
344 symmetry
->free
= false;
345 } else if (symmetry
->free
) {
347 symmetry
->x2
= board_size(b
) - 1;
348 symmetry
->free
= false;
354 if (symmetry
->y1
<= y
&& y
<= symmetry
->y2
) {
355 symmetry
->free
= false;
356 } else if (symmetry
->free
) {
358 symmetry
->y2
= board_size(b
) - 1;
359 symmetry
->free
= false;
370 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d+%d]\n",
371 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
372 symmetry
->d
, symmetry
->type
, symmetry
->free
);
379 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
383 coord_xy(m
.coord
, x
, y
, board
);
385 board_play(board
, &m
);
387 char *str
= coord2str(m
.coord
, board
);
389 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
390 fprintf(f
, "%s ", str
);
395 board_handicap(struct board
*board
, int stones
, FILE *f
)
397 int margin
= 3 + (board_size(board
) >= 13);
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
},
408 board
->handicap
= stones
;
410 if (stones
== 5 || stones
== 7) {
411 board_handicap_stone(board
, mid
, mid
, f
);
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
)
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
));
436 board_capturable_add(struct board
*board
, group_t group
)
439 //fprintf(stderr, "add of group %d (%d)\n", group_base(group), board->clen);
441 assert(board
->clen
< board_size2(board
));
442 board
->c
[board
->clen
++] = group
;
446 board_capturable_rm(struct board
*board
, group_t group
)
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
];
456 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
462 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
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
++) {
476 /* Seems extra branch just slows it down */
480 if (unlikely(gi
->lib
[i
] == coord
))
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
);
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
) {
506 foreach_neighbor(board
, coord2
, {
507 if (likely(watermark
[coord_raw(c
)] != S_NONE
))
509 watermark
[coord_raw(c
)] = S_OFFBOARD
;
510 gi
->lib
[gi
->libs
++] = c
;
511 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
514 } foreach_in_group_end
;
518 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
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
++) {
529 /* Seems extra branch just slows it down */
533 if (likely(gi
->lib
[i
] != coord
))
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
)
545 if (gi
->libs
== GROUP_REFILL_LIBS
)
546 board_group_find_extra_libs(board
, group
, gi
, coord
);
549 board_capturable_add(board
, group
);
550 else if (gi
->libs
== 0)
551 board_capturable_rm(board
, group
);
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
);
562 /* This is a low-level routine that doesn't maintain consistency
563 * of all the board data structures. Use board_group_capture() from
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 */
575 foreach_neighbor(board
, coord
, {
576 dec_neighbor_count_at(board
, c
, color
);
577 group_t g
= group_at(board
, c
);
579 board_group_addlib(board
, g
, coord
);
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
);
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
),
608 static void profiling_noinline
609 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
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
) {
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
])
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
)
641 if (gi_from
->libs
== 1)
642 board_capturable_rm(board
, group_from
);
643 memset(gi_from
, 0, sizeof(struct group
));
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
)
661 gi
->lib
[gi
->libs
++] = c
;
664 board_capturable_add(board
, group
);
665 check_libs_consistency(board
, group
);
667 group_at(board
, coord
) = group
;
668 groupnext_at(board
, coord
) = 0;
671 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
672 coord_x(coord
, board
), coord_y(coord
, board
),
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
);
691 board_group_rmlib(board
, ngroup
, coord
);
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
) {
699 add_to_group(board
, group
, c
, coord
);
701 merge_groups(board
, group
, ngroup
);
703 } else if (ncolor
== other_color
) {
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
);
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
);
727 board
->f
[f
] = board
->f
[--board
->flen
];
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
;
741 board_hash_update(board
, coord
, color
);
742 board_symmetry_update(board
, &board
->symmetry
, coord
);
743 struct move ko
= { pass
, S_NONE
};
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
))) {
759 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
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
);
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)) {
782 board_print(board
, stderr
);
783 fprintf(stderr
, "board_check: one-stone suicide\n");
789 board
->f
[f
] = board
->f
[--board
->flen
];
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
);
800 board_group_rmlib(board
, group
, coord
);
802 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
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
);
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
;
822 board_hash_update(board
, coord
, color
);
823 board_hash_commit(board
);
824 board_symmetry_update(board
, &board
->symmetry
, coord
);
827 return !!new_group(board
, coord
);
830 static int __attribute__((flatten
))
831 board_play_f(struct board
*board
, struct move
*m
, int f
)
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
);
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
;
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
);
865 fprintf(stderr
, "board_check: stone exists\n");
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
)))
876 struct move m
= { *coord
, color
};
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));
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
)))
892 for (f
= base
+ 1; f
< b
->flen
; f
++)
893 if (board_try_random_move(b
, color
, coord
, f
))
895 for (f
= 0; f
< base
; f
++)
896 if (board_try_random_move(b
, color
, coord
, f
))
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;
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)) {
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);
929 board_get_one_point_eye(struct board
*board
, coord_t
*coord
)
931 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
933 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
940 int profiling_noinline
941 board_group_capture(struct board
*board
, group_t group
)
945 foreach_in_group(board
, group
) {
946 board
->captures
[stone_other(board_at(board
, c
))]++;
947 board_remove_stone(board
, c
);
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
));
959 board_group_in_atari(struct board
*board
, group_t group
, coord_t
*lastlib
)
961 if (board_group_info(board
, group
).libs
!= 1)
963 *lastlib
= board_group_info(board
, group
).lib
[0];
968 board_group_can_atari(struct board
*board
, group_t group
, coord_t lastlib
[2])
970 if (board_group_info(board
, group
).libs
!= 2)
972 lastlib
[0] = board_group_info(board
, group
).lib
[0];
973 lastlib
[1] = board_group_info(board
, group
).lib
[1];
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) \
993 for (int i
= x
; i
> 0; i
--)
995 for (int i
= x
; i
< board_size(board
) - 1; i
++)
997 for (int i
= y
; i
> 0; i
--)
999 for (int i
= y
; i
< board_size(board
) - 1; i
++)
1004 /* Tromp-Taylor Counting */
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. */
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
);
1024 } foreach_point_end
;
1026 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1030 board_fast_score(struct board
*board
)
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
);
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
];
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:
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);
1068 board_stone_radar(struct board
*b
, coord_t coord
, int distance
)
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
++)
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));