13 #include "patternsp.h"
19 bool random_pass
= false;
23 #define profiling_noinline __attribute__((noinline))
25 #define profiling_noinline
28 #define gi_granularity 4
29 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
33 board_setup(struct board
*b
)
35 memset(b
, 0, sizeof(*b
));
37 struct move m
= { pass
, S_NONE
};
38 b
->last_move
= b
->last_move2
= b
->last_ko
= b
->ko
= m
;
44 struct board
*b
= malloc(sizeof(struct board
));
55 board_copy(struct board
*b2
, struct board
*b1
)
57 memcpy(b2
, b1
, sizeof(struct board
));
59 int bsize
= board_size2(b2
) * sizeof(*b2
->b
);
60 int gsize
= board_size2(b2
) * sizeof(*b2
->g
);
61 int fsize
= board_size2(b2
) * sizeof(*b2
->f
);
62 int nsize
= board_size2(b2
) * sizeof(*b2
->n
);
63 int psize
= board_size2(b2
) * sizeof(*b2
->p
);
64 int hsize
= board_size2(b2
) * 2 * sizeof(*b2
->h
);
65 int gisize
= board_size2(b2
) * sizeof(*b2
->gi
);
67 int csize
= board_size2(b2
) * sizeof(*b2
->c
);
72 int ssize
= board_size2(b2
) * sizeof(*b2
->spathash
);
77 int p3size
= board_size2(b2
) * sizeof(*b2
->pat3
);
81 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
);
82 memcpy(x
, b1
->b
, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
);
83 b2
->b
= x
; x
+= bsize
;
84 b2
->g
= x
; x
+= gsize
;
85 b2
->f
= x
; x
+= fsize
;
86 b2
->p
= x
; x
+= psize
;
87 b2
->n
= x
; x
+= nsize
;
88 b2
->h
= x
; x
+= hsize
;
89 b2
->gi
= x
; x
+= gisize
;
91 b2
->c
= x
; x
+= csize
;
94 b2
->spathash
= x
; x
+= ssize
;
97 b2
->pat3
= x
; x
+= p3size
;
104 board_done_noalloc(struct board
*board
)
106 if (board
->b
) free(board
->b
);
110 board_done(struct board
*board
)
112 board_done_noalloc(board
);
117 board_resize(struct board
*board
, int size
)
120 assert(board_size(board
) == size
+ 2);
122 board_size(board
) = size
+ 2 /* S_OFFBOARD margin */;
123 board_size2(board
) = board_size(board
) * board_size(board
);
128 int bsize
= board_size2(board
) * sizeof(*board
->b
);
129 int gsize
= board_size2(board
) * sizeof(*board
->g
);
130 int fsize
= board_size2(board
) * sizeof(*board
->f
);
131 int nsize
= board_size2(board
) * sizeof(*board
->n
);
132 int psize
= board_size2(board
) * sizeof(*board
->p
);
133 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
134 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
136 int csize
= board_size2(board
) * sizeof(*board
->c
);
140 #ifdef BOARD_SPATHASH
141 int ssize
= board_size2(board
) * sizeof(*board
->spathash
);
146 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
150 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
);
151 memset(x
, 0, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
);
152 board
->b
= x
; x
+= bsize
;
153 board
->g
= x
; x
+= gsize
;
154 board
->f
= x
; x
+= fsize
;
155 board
->p
= x
; x
+= psize
;
156 board
->n
= x
; x
+= nsize
;
157 board
->h
= x
; x
+= hsize
;
158 board
->gi
= x
; x
+= gisize
;
160 board
->c
= x
; x
+= csize
;
162 #ifdef BOARD_SPATHASH
163 board
->spathash
= x
; x
+= ssize
;
166 board
->pat3
= x
; x
+= p3size
;
171 board_clear(struct board
*board
)
173 int size
= board_size(board
);
174 float komi
= board
->komi
;
176 board_done_noalloc(board
);
178 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
182 /* Setup neighborhood iterators */
183 board
->nei8
[0] = -size
- 1; // (-1,-1)
186 board
->nei8
[3] = size
- 2; // (-1,0)
188 board
->nei8
[5] = size
- 2; // (-1,1)
191 board
->dnei
[0] = -size
- 1;
193 board
->dnei
[2] = size
*2 - 2;
196 /* Setup initial symmetry */
197 board
->symmetry
.d
= 1;
198 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
199 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
200 board
->symmetry
.type
= SYM_FULL
;
202 /* Draw the offboard margin */
203 int top_row
= board_size2(board
) - board_size(board
);
205 for (i
= 0; i
< board_size(board
); i
++)
206 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
207 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
208 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
210 foreach_point(board
) {
212 if (board_at(board
, coord
) == S_OFFBOARD
)
214 foreach_neighbor(board
, c
, {
215 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
219 /* First, pass is always a free position. */
220 board
->f
[board
->flen
++] = coord_raw(pass
);
221 /* All positions are free! Except the margin. */
222 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
223 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
224 board
->f
[board
->flen
++] = i
;
226 /* Initialize zobrist hashtable. */
227 foreach_point(board
) {
228 int max
= (sizeof(hash_t
) << history_hash_bits
);
229 /* fast_random() is 16-bit only */
230 board
->h
[coord_raw(c
) * 2] = ((hash_t
) fast_random(max
))
231 | ((hash_t
) fast_random(max
) << 16)
232 | ((hash_t
) fast_random(max
) << 32)
233 | ((hash_t
) fast_random(max
) << 48);
234 if (!board
->h
[coord_raw(c
) * 2])
235 /* Would be kinda "oops". */
236 board
->h
[coord_raw(c
) * 2] = 1;
237 /* And once again for white */
238 board
->h
[coord_raw(c
) * 2 + 1] = ((hash_t
) fast_random(max
))
239 | ((hash_t
) fast_random(max
) << 16)
240 | ((hash_t
) fast_random(max
) << 32)
241 | ((hash_t
) fast_random(max
) << 48);
242 if (!board
->h
[coord_raw(c
) * 2 + 1])
243 board
->h
[coord_raw(c
) * 2 + 1] = 1;
246 #ifdef BOARD_SPATHASH
247 /* Initialize spatial hashes. */
248 foreach_point(board
) {
249 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
250 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
251 ptcoords_at(x
, y
, c
, board
, j
);
252 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
253 pthashes
[0][j
][board_at(board
, c
)];
254 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
255 pthashes
[0][j
][stone_other(board_at(board
, c
))];
261 /* Initialize 3x3 pattern codes. */
262 foreach_point(board
) {
263 if (board_at(board
, c
) == S_NONE
)
264 board
->pat3
[c
] = pattern3_hash(board
, c
);
271 board_print_top(struct board
*board
, FILE *f
, int c
)
273 for (int i
= 0; i
< c
; i
++) {
274 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
276 for (int x
= 1; x
< board_size(board
) - 1; x
++)
277 fprintf(f
, "%c ", asdf
[x
- 1]);
281 for (int i
= 0; i
< c
; i
++) {
283 for (int x
= 1; x
< board_size(board
) - 1; x
++)
291 board_print_bottom(struct board
*board
, FILE *f
, int c
)
293 for (int i
= 0; i
< c
; i
++) {
295 for (int x
= 1; x
< board_size(board
) - 1; x
++)
303 board_print_row(struct board
*board
, int y
, FILE *f
, board_cprint cprint
)
305 fprintf(f
, " %2d | ", y
);
306 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
307 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
308 fprintf(f
, "%c)", stone2char(board_atxy(board
, x
, y
)));
310 fprintf(f
, "%c ", stone2char(board_atxy(board
, x
, y
)));
314 fprintf(f
, " %2d | ", y
);
315 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
316 cprint(board
, coord_xy(board
, x
, y
), f
);
324 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
326 fprintf(f
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
327 board
->moves
, board
->komi
, board
->handicap
,
328 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
329 board_print_top(board
, f
, 1 + !!cprint
);
330 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
331 board_print_row(board
, y
, f
, cprint
);
332 board_print_bottom(board
, f
, 1 + !!cprint
);
337 cprint_group(struct board
*board
, coord_t c
, FILE *f
)
339 fprintf(f
, "%d ", group_base(group_at(board
, c
)));
343 board_print(struct board
*board
, FILE *f
)
345 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
349 /* Update board hash with given coordinate. */
350 static void profiling_noinline
351 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
353 board
->hash
^= hash_at(board
, coord
, color
);
355 fprintf(stderr
, "board_hash_update(%d,%d,%d) ^ %"PRIhash
" -> %"PRIhash
"\n", color
, coord_x(coord
, board
), coord_y(coord
, board
), hash_at(board
, coord
, color
), board
->hash
);
357 #ifdef BOARD_SPATHASH
358 /* Gridcular metric is reflective, so we update all hashes
359 * of appropriate ditance in OUR circle. */
360 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
361 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
362 ptcoords_at(x
, y
, coord
, board
, j
);
363 /* We either changed from S_NONE to color
364 * or vice versa; doesn't matter. */
365 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
366 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
367 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
368 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
374 /* @color is not what we need in case of capture. */
375 enum stone new_color
= board_at(board
, coord
);
376 if (new_color
== S_NONE
)
377 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
378 foreach_8neighbor(board
, coord
) { // internally, the loop uses fn__i=[0..7]
379 if (board_at(board
, c
) != S_NONE
)
381 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
382 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
384 if (board_at(board
, c
) != S_OFFBOARD
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
385 board_print(board
, stderr
);
386 fprintf(stderr
, "%s->%s %x != %x (%d-%d:%d)\n", coord2sstr(coord
, board
), coord2sstr(c
, board
), pattern3_hash(board
, c
), board
->pat3
[c
], coord
, c
, fn__i
);
390 } foreach_8neighbor_end
;
394 /* Commit current board hash to history. */
395 static void profiling_noinline
396 board_hash_commit(struct board
*board
)
399 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
400 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
401 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
403 hash_t i
= board
->hash
;
404 while (board
->history_hash
[i
& history_hash_mask
]) {
405 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
407 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
408 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
409 board
->superko_violation
= true;
412 i
= history_hash_next(i
);
414 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
420 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
422 if (likely(symmetry
->type
== SYM_NONE
)) {
423 /* Fully degenerated already. We do not support detection
424 * of restoring of symmetry, assuming that this is too rare
425 * a case to handle. */
429 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
430 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
432 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
433 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
434 symmetry
->d
, symmetry
->type
, x
, y
);
437 switch (symmetry
->type
) {
439 if (x
== t
&& y
== t
) {
440 /* Tengen keeps full symmetry. */
443 /* New symmetry now? */
445 symmetry
->type
= SYM_DIAG_UP
;
446 symmetry
->x1
= symmetry
->y1
= 1;
447 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
449 } else if (dx
== y
) {
450 symmetry
->type
= SYM_DIAG_DOWN
;
451 symmetry
->x1
= symmetry
->y1
= 1;
452 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
455 symmetry
->type
= SYM_HORIZ
;
457 symmetry
->y2
= board_size(b
) - 1;
460 symmetry
->type
= SYM_VERT
;
462 symmetry
->x2
= board_size(b
) - 1;
466 symmetry
->type
= SYM_NONE
;
467 symmetry
->x1
= symmetry
->y1
= 1;
468 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
494 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
495 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
496 symmetry
->d
, symmetry
->type
);
503 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
506 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
508 board_play(board
, &m
);
509 /* Simulate white passing; otherwise, UCT search can get confused since
510 * tree depth parity won't match the color to move. */
513 char *str
= coord2str(m
.coord
, board
);
515 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
516 fprintf(f
, "%s ", str
);
521 board_handicap(struct board
*board
, int stones
, FILE *f
)
523 int margin
= 3 + (board_size(board
) >= 13);
525 int mid
= board_size(board
) / 2;
526 int max
= board_size(board
) - 1 - margin
;
527 const int places
[][2] = {
528 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
529 { min
, mid
}, { max
, mid
},
530 { mid
, min
}, { mid
, max
},
534 board
->handicap
= stones
;
536 if (stones
== 5 || stones
== 7) {
537 board_handicap_stone(board
, mid
, mid
, f
);
542 for (i
= 0; i
< stones
; i
++)
543 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
547 static void __attribute__((noinline
))
548 check_libs_consistency(struct board
*board
, group_t g
)
552 struct group
*gi
= &board_group_info(board
, g
);
553 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
554 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
555 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
562 board_capturable_add(struct board
*board
, group_t group
)
565 //fprintf(stderr, "add of group %d (%d)\n", group_base(group), board->clen);
567 assert(board
->clen
< board_size2(board
));
568 board
->c
[board
->clen
++] = group
;
572 board_capturable_rm(struct board
*board
, group_t group
)
575 //fprintf(stderr, "rm of group %d\n", group_base(group));
576 for (int i
= 0; i
< board
->clen
; i
++) {
577 if (unlikely(board
->c
[i
] == group
)) {
578 board
->c
[i
] = board
->c
[--board
->clen
];
582 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
588 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
591 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
592 group_base(group
), coord2sstr(group_base(group
), board
),
593 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
596 check_libs_consistency(board
, group
);
598 struct group
*gi
= &board_group_info(board
, group
);
599 if (gi
->libs
< GROUP_KEEP_LIBS
) {
600 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
602 /* Seems extra branch just slows it down */
606 if (unlikely(gi
->lib
[i
] == coord
))
610 board_capturable_add(board
, group
);
611 else if (gi
->libs
== 1)
612 board_capturable_rm(board
, group
);
613 gi
->lib
[gi
->libs
++] = coord
;
616 check_libs_consistency(board
, group
);
620 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
622 /* Add extra liberty from the board to our liberty list. */
623 unsigned char watermark
[board_size2(board
) / 8];
624 memset(watermark
, 0, sizeof(watermark
));
625 #define watermark_get(c) (watermark[coord_raw(c) >> 3] & (1 << (coord_raw(c) & 7)))
626 #define watermark_set(c) watermark[coord_raw(c) >> 3] |= (1 << (coord_raw(c) & 7))
628 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
629 watermark_set(gi
->lib
[i
]);
630 watermark_set(avoid
);
632 foreach_in_group(board
, group
) {
634 foreach_neighbor(board
, coord2
, {
635 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
638 gi
->lib
[gi
->libs
++] = c
;
639 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
642 } foreach_in_group_end
;
648 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
651 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
652 group_base(group
), coord2sstr(group_base(group
), board
),
653 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
656 struct group
*gi
= &board_group_info(board
, group
);
657 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
659 /* Seems extra branch just slows it down */
663 if (likely(gi
->lib
[i
] != coord
))
666 gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
667 gi
->lib
[gi
->libs
] = 0;
669 check_libs_consistency(board
, group
);
671 /* Postpone refilling lib[] until we need to. */
672 assert(GROUP_REFILL_LIBS
> 1);
673 if (gi
->libs
> GROUP_REFILL_LIBS
)
675 if (gi
->libs
== GROUP_REFILL_LIBS
)
676 board_group_find_extra_libs(board
, group
, gi
, coord
);
679 board_capturable_add(board
, group
);
680 else if (gi
->libs
== 0)
681 board_capturable_rm(board
, group
);
685 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
686 * can call this multiple times per coord. */
687 check_libs_consistency(board
, group
);
692 /* This is a low-level routine that doesn't maintain consistency
693 * of all the board data structures. */
695 board_remove_stone(struct board
*board
, coord_t c
)
697 enum stone color
= board_at(board
, c
);
698 board_at(board
, c
) = S_NONE
;
699 group_at(board
, c
) = 0;
700 board_hash_update(board
, c
, color
);
702 /* Increase liberties of surrounding groups */
704 foreach_neighbor(board
, coord
, {
705 dec_neighbor_count_at(board
, c
, color
);
706 group_t g
= group_at(board
, c
);
708 board_group_addlib(board
, g
, coord
);
712 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
713 board
->f
[board
->flen
++] = coord_raw(c
);
716 static int profiling_noinline
717 board_group_capture(struct board
*board
, group_t group
)
721 foreach_in_group(board
, group
) {
722 board
->captures
[stone_other(board_at(board
, c
))]++;
723 board_remove_stone(board
, c
);
725 } foreach_in_group_end
;
727 if (board_group_info(board
, group
).libs
== 1)
728 board_capturable_rm(board
, group
);
729 memset(&board_group_info(board
, group
), 0, sizeof(struct group
));
735 static void profiling_noinline
736 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
738 foreach_neighbor(board
, coord
, {
739 if (board_at(board
, c
) == S_NONE
)
740 board_group_addlib(board
, group
, c
);
743 group_at(board
, coord
) = group
;
744 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
745 groupnext_at(board
, prevstone
) = coord_raw(coord
);
748 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
749 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
750 coord_x(coord
, board
), coord_y(coord
, board
),
751 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
755 static void profiling_noinline
756 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
759 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
760 group_base(group_from
), group_base(group_to
));
762 coord_t last_in_group
;
763 foreach_in_group(board
, group_from
) {
765 group_at(board
, c
) = group_to
;
766 } foreach_in_group_end
;
767 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
768 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
770 struct group
*gi_from
= &board_group_info(board
, group_from
);
771 struct group
*gi_to
= &board_group_info(board
, group_to
);
772 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
773 for (int i
= 0; i
< gi_from
->libs
; i
++) {
774 for (int j
= 0; j
< gi_to
->libs
; j
++)
775 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
777 if (gi_to
->libs
== 0)
778 board_capturable_add(board
, group_to
);
779 else if (gi_to
->libs
== 1)
780 board_capturable_rm(board
, group_to
);
781 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
782 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
788 if (gi_from
->libs
== 1)
789 board_capturable_rm(board
, group_from
);
790 memset(gi_from
, 0, sizeof(struct group
));
793 fprintf(stderr
, "board_play_raw: merged group: %d\n",
794 group_base(group_to
));
797 static group_t profiling_noinline
798 new_group(struct board
*board
, coord_t coord
)
800 group_t group
= coord_raw(coord
);
801 struct group
*gi
= &board_group_info(board
, group
);
802 foreach_neighbor(board
, coord
, {
803 if (board_at(board
, c
) == S_NONE
)
804 /* board_group_addlib is ridiculously expensive for us */
805 #if GROUP_KEEP_LIBS < 4
806 if (gi
->libs
< GROUP_KEEP_LIBS
)
808 gi
->lib
[gi
->libs
++] = c
;
811 board_capturable_add(board
, group
);
812 check_libs_consistency(board
, group
);
814 group_at(board
, coord
) = group
;
815 groupnext_at(board
, coord
) = 0;
818 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
819 coord_x(coord
, board
), coord_y(coord
, board
),
825 static inline group_t
826 play_one_neighbor(struct board
*board
,
827 coord_t coord
, enum stone color
, enum stone other_color
,
828 coord_t c
, group_t group
)
830 enum stone ncolor
= board_at(board
, c
);
831 group_t ngroup
= group_at(board
, c
);
833 inc_neighbor_count_at(board
, c
, color
);
838 board_group_rmlib(board
, ngroup
, coord
);
840 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
841 group_base(ngroup
), ncolor
, color
, other_color
);
843 if (ncolor
== color
&& ngroup
!= group
) {
846 add_to_group(board
, group
, c
, coord
);
848 merge_groups(board
, group
, ngroup
);
850 } else if (ncolor
== other_color
) {
852 struct group
*gi
= &board_group_info(board
, ngroup
);
853 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
854 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
855 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
856 fprintf(stderr
, "\n");
858 if (unlikely(board_group_captured(board
, ngroup
)))
859 board_group_capture(board
, ngroup
);
864 /* We played on a place with at least one liberty. We will become a member of
865 * some group for sure. */
866 static group_t profiling_noinline
867 board_play_outside(struct board
*board
, struct move
*m
, int f
)
869 coord_t coord
= m
->coord
;
870 enum stone color
= m
->color
;
871 enum stone other_color
= stone_other(color
);
874 board
->f
[f
] = board
->f
[--board
->flen
];
876 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
878 foreach_neighbor(board
, coord
, {
879 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
882 if (unlikely(!group
))
883 group
= new_group(board
, coord
);
885 board_at(board
, coord
) = color
;
886 board
->last_move2
= board
->last_move
;
887 board
->last_move
= *m
;
889 board_hash_update(board
, coord
, color
);
890 board_symmetry_update(board
, &board
->symmetry
, coord
);
891 struct move ko
= { pass
, S_NONE
};
897 /* We played in an eye-like shape. Either we capture at least one of the eye
898 * sides in the process of playing, or return -1. */
899 static int profiling_noinline
900 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
902 coord_t coord
= m
->coord
;
903 enum stone color
= m
->color
;
904 /* Check ko: Capture at a position of ko capture one move ago */
905 if (unlikely(color
== board
->ko
.color
&& coord_eq(coord
, board
->ko
.coord
))) {
907 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
909 } else if (DEBUGL(6)) {
910 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
911 color
, coord_x(coord
, board
), coord_y(coord
, board
),
912 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
915 struct move ko
= { pass
, S_NONE
};
917 int captured_groups
= 0;
919 foreach_neighbor(board
, coord
, {
920 group_t g
= group_at(board
, c
);
922 fprintf(stderr
, "board_check: group %d has %d libs\n",
923 g
, board_group_info(board
, g
).libs
);
924 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
927 if (likely(captured_groups
== 0)) {
930 board_print(board
, stderr
);
931 fprintf(stderr
, "board_check: one-stone suicide\n");
937 board
->f
[f
] = board
->f
[--board
->flen
];
939 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
941 foreach_neighbor(board
, coord
, {
942 inc_neighbor_count_at(board
, c
, color
);
944 group_t group
= group_at(board
, c
);
948 board_group_rmlib(board
, group
, coord
);
950 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
953 if (board_group_captured(board
, group
)) {
954 if (board_group_capture(board
, group
) == 1) {
955 /* If we captured multiple groups at once,
956 * we can't be fighting ko so we don't need
957 * to check for that. */
958 ko
.color
= stone_other(color
);
961 board
->last_ko_age
= board
->moves
;
963 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
968 board_at(board
, coord
) = color
;
970 board
->last_move2
= board
->last_move
;
971 board
->last_move
= *m
;
973 board_hash_update(board
, coord
, color
);
974 board_hash_commit(board
);
975 board_symmetry_update(board
, &board
->symmetry
, coord
);
978 return !!new_group(board
, coord
);
981 static int __attribute__((flatten
))
982 board_play_f(struct board
*board
, struct move
*m
, int f
)
985 fprintf(stderr
, "board_play(): ---- Playing %d,%d\n", coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
987 if (likely(!board_is_eyelike(board
, &m
->coord
, stone_other(m
->color
)))) {
988 /* NOT playing in an eye. Thus this move has to succeed. (This
989 * is thanks to New Zealand rules. Otherwise, multi-stone
990 * suicide might fail.) */
991 group_t group
= board_play_outside(board
, m
, f
);
992 if (unlikely(board_group_captured(board
, group
))) {
993 board_group_capture(board
, group
);
995 board_hash_commit(board
);
998 return board_play_in_eye(board
, m
, f
);
1003 board_play(struct board
*board
, struct move
*m
)
1005 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1006 board
->last_move2
= board
->last_move
;
1007 board
->last_move
= *m
;
1012 for (f
= 0; f
< board
->flen
; f
++)
1013 if (board
->f
[f
] == coord_raw(m
->coord
))
1014 return board_play_f(board
, m
, f
);
1017 fprintf(stderr
, "board_check: stone exists\n");
1023 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1025 coord_raw(*coord
) = b
->f
[f
];
1026 if (unlikely(is_pass(*coord
)))
1028 struct move m
= { *coord
, color
};
1030 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
1031 return (likely(!board_is_one_point_eye(b
, coord
, color
)) /* bad idea to play into one, usually */
1032 && board_is_valid_move(b
, &m
)
1033 && (!permit
|| permit(permit_data
, b
, &m
))
1034 && likely(board_play_f(b
, &m
, f
) >= 0));
1038 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1040 int base
= fast_random(b
->flen
);
1041 coord_pos(*coord
, base
, b
);
1042 if (likely(board_try_random_move(b
, color
, coord
, base
, permit
, permit_data
)))
1046 for (f
= base
+ 1; f
< b
->flen
; f
++)
1047 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1049 for (f
= 0; f
< base
; f
++)
1050 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1058 board_is_false_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
1060 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1062 /* XXX: We attempt false eye detection but we will yield false
1063 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1065 foreach_diag_neighbor(board
, *coord
) {
1066 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1067 } foreach_diag_neighbor_end
;
1068 /* For false eye, we need two enemy stones diagonally in the
1069 * middle of the board, or just one enemy stone at the edge
1070 * or in the corner. */
1071 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1072 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1076 board_is_one_point_eye(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
1078 return board_is_eyelike(board
, coord
, eye_color
)
1079 && !board_is_false_eyelike(board
, coord
, eye_color
);
1083 board_get_one_point_eye(struct board
*board
, coord_t
*coord
)
1085 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1087 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1095 board_fast_score(struct board
*board
)
1098 memset(scores
, 0, sizeof(scores
));
1100 foreach_point(board
) {
1101 enum stone color
= board_at(board
, c
);
1102 if (color
== S_NONE
)
1103 color
= board_get_one_point_eye(board
, &c
);
1105 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1106 } foreach_point_end
;
1108 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1111 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1113 /* One flood-fill iteration; returns true if next iteration
1116 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1118 bool needs_update
= false;
1119 foreach_point(board
) {
1120 /* Ignore occupied and already-dame positions. */
1121 if (board_at(board
, c
) != S_NONE
|| ownermap
[c
] == 3)
1123 /* Count neighbors. */
1125 foreach_neighbor(board
, c
, {
1128 /* If we have neighbors of both colors, or dame,
1129 * we are dame too. */
1130 if ((nei
[1] && nei
[2]) || nei
[3]) {
1132 /* Speed up the propagation. */
1133 foreach_neighbor(board
, c
, {
1134 if (board_at(board
, c
) == S_NONE
)
1137 needs_update
= true;
1140 /* If we have neighbors of one color, we are owned
1141 * by that color, too. */
1142 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1143 int newowner
= nei
[1] ? 1 : 2;
1144 ownermap
[c
] = newowner
;
1145 /* Speed up the propagation. */
1146 foreach_neighbor(board
, c
, {
1147 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1148 ownermap
[c
] = newowner
;
1150 needs_update
= true;
1153 } foreach_point_end
;
1154 return needs_update
;
1157 /* Tromp-Taylor Counting */
1159 board_official_score(struct board
*board
, struct move_queue
*q
)
1162 /* A point P, not colored C, is said to reach C, if there is a path of
1163 * (vertically or horizontally) adjacent points of P's color from P to
1164 * a point of color C.
1166 * A player's score is the number of points of her color, plus the
1167 * number of empty points that reach only her color. */
1169 int ownermap
[board_size2(board
)];
1171 const int o
[4] = {0, 1, 2, 0};
1172 foreach_point(board
) {
1173 ownermap
[c
] = o
[board_at(board
, c
)];
1174 s
[board_at(board
, c
)]++;
1175 } foreach_point_end
;
1178 /* Process dead groups. */
1179 for (int i
= 0; i
< q
->moves
; i
++) {
1180 foreach_in_group(board
, q
->move
[i
]) {
1181 enum stone color
= board_at(board
, c
);
1182 ownermap
[c
] = o
[stone_other(color
)];
1183 s
[color
]--; s
[stone_other(color
)]++;
1184 } foreach_in_group_end
;
1188 /* We need to special-case empty board. */
1189 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1190 return board
->komi
+ board
->handicap
;
1192 while (board_tromp_taylor_iter(board
, ownermap
))
1193 /* Flood-fill... */;
1196 memset(scores
, 0, sizeof(scores
));
1198 foreach_point(board
) {
1199 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1200 if (ownermap
[c
] == 3)
1202 scores
[ownermap
[c
]]++;
1203 } foreach_point_end
;
1205 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];