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
);
82 int tsize
= board_size2(b2
) * sizeof(*b2
->t
);
86 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
);
87 memcpy(x
, b1
->b
, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
);
88 b2
->b
= x
; x
+= bsize
;
89 b2
->g
= x
; x
+= gsize
;
90 b2
->f
= x
; x
+= fsize
;
91 b2
->p
= x
; x
+= psize
;
92 b2
->n
= x
; x
+= nsize
;
93 b2
->h
= x
; x
+= hsize
;
94 b2
->gi
= x
; x
+= gisize
;
96 b2
->c
= x
; x
+= csize
;
99 b2
->spathash
= x
; x
+= ssize
;
102 b2
->pat3
= x
; x
+= p3size
;
105 b2
->t
= x
; x
+= tsize
;
112 board_done_noalloc(struct board
*board
)
114 if (board
->b
) free(board
->b
);
118 board_done(struct board
*board
)
120 board_done_noalloc(board
);
125 board_resize(struct board
*board
, int size
)
128 assert(board_size(board
) == size
+ 2);
130 board_size(board
) = size
+ 2 /* S_OFFBOARD margin */;
131 board_size2(board
) = board_size(board
) * board_size(board
);
136 int bsize
= board_size2(board
) * sizeof(*board
->b
);
137 int gsize
= board_size2(board
) * sizeof(*board
->g
);
138 int fsize
= board_size2(board
) * sizeof(*board
->f
);
139 int nsize
= board_size2(board
) * sizeof(*board
->n
);
140 int psize
= board_size2(board
) * sizeof(*board
->p
);
141 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
142 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
144 int csize
= board_size2(board
) * sizeof(*board
->c
);
148 #ifdef BOARD_SPATHASH
149 int ssize
= board_size2(board
) * sizeof(*board
->spathash
);
154 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
159 int tsize
= board_size2(board
) * sizeof(*board
->t
);
163 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
);
164 memset(x
, 0, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
);
165 board
->b
= x
; x
+= bsize
;
166 board
->g
= x
; x
+= gsize
;
167 board
->f
= x
; x
+= fsize
;
168 board
->p
= x
; x
+= psize
;
169 board
->n
= x
; x
+= nsize
;
170 board
->h
= x
; x
+= hsize
;
171 board
->gi
= x
; x
+= gisize
;
173 board
->c
= x
; x
+= csize
;
175 #ifdef BOARD_SPATHASH
176 board
->spathash
= x
; x
+= ssize
;
179 board
->pat3
= x
; x
+= p3size
;
182 board
->t
= x
; x
+= tsize
;
187 board_clear(struct board
*board
)
189 int size
= board_size(board
);
190 float komi
= board
->komi
;
192 board_done_noalloc(board
);
194 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
198 /* Setup neighborhood iterators */
199 board
->nei8
[0] = -size
- 1; // (-1,-1)
202 board
->nei8
[3] = size
- 2; // (-1,0)
204 board
->nei8
[5] = size
- 2; // (-1,1)
207 board
->dnei
[0] = -size
- 1;
209 board
->dnei
[2] = size
*2 - 2;
212 /* Setup initial symmetry */
213 board
->symmetry
.d
= 1;
214 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
215 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
216 board
->symmetry
.type
= SYM_FULL
;
218 /* Draw the offboard margin */
219 int top_row
= board_size2(board
) - board_size(board
);
221 for (i
= 0; i
< board_size(board
); i
++)
222 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
223 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
224 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
226 foreach_point(board
) {
228 if (board_at(board
, coord
) == S_OFFBOARD
)
230 foreach_neighbor(board
, c
, {
231 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
235 /* First, pass is always a free position. */
236 board
->f
[board
->flen
++] = coord_raw(pass
);
237 /* All positions are free! Except the margin. */
238 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
239 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
240 board
->f
[board
->flen
++] = i
;
242 /* Initialize zobrist hashtable. */
243 foreach_point(board
) {
244 int max
= (sizeof(hash_t
) << history_hash_bits
);
245 /* fast_random() is 16-bit only */
246 board
->h
[coord_raw(c
) * 2] = ((hash_t
) fast_random(max
))
247 | ((hash_t
) fast_random(max
) << 16)
248 | ((hash_t
) fast_random(max
) << 32)
249 | ((hash_t
) fast_random(max
) << 48);
250 if (!board
->h
[coord_raw(c
) * 2])
251 /* Would be kinda "oops". */
252 board
->h
[coord_raw(c
) * 2] = 1;
253 /* And once again for white */
254 board
->h
[coord_raw(c
) * 2 + 1] = ((hash_t
) fast_random(max
))
255 | ((hash_t
) fast_random(max
) << 16)
256 | ((hash_t
) fast_random(max
) << 32)
257 | ((hash_t
) fast_random(max
) << 48);
258 if (!board
->h
[coord_raw(c
) * 2 + 1])
259 board
->h
[coord_raw(c
) * 2 + 1] = 1;
262 #ifdef BOARD_SPATHASH
263 /* Initialize spatial hashes. */
264 foreach_point(board
) {
265 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
266 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
267 ptcoords_at(x
, y
, c
, board
, j
);
268 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
269 pthashes
[0][j
][board_at(board
, c
)];
270 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
271 pthashes
[0][j
][stone_other(board_at(board
, c
))];
277 /* Initialize 3x3 pattern codes. */
278 foreach_point(board
) {
279 if (board_at(board
, c
) == S_NONE
)
280 board
->pat3
[c
] = pattern3_hash(board
, c
);
283 /* We don't need to initialize traits, they are all zero
289 board_print_top(struct board
*board
, FILE *f
, int c
)
291 for (int i
= 0; i
< c
; i
++) {
292 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
294 for (int x
= 1; x
< board_size(board
) - 1; x
++)
295 fprintf(f
, "%c ", asdf
[x
- 1]);
299 for (int i
= 0; i
< c
; i
++) {
301 for (int x
= 1; x
< board_size(board
) - 1; x
++)
309 board_print_bottom(struct board
*board
, FILE *f
, int c
)
311 for (int i
= 0; i
< c
; i
++) {
313 for (int x
= 1; x
< board_size(board
) - 1; x
++)
321 board_print_row(struct board
*board
, int y
, FILE *f
, board_cprint cprint
)
323 fprintf(f
, " %2d | ", y
);
324 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
325 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
326 fprintf(f
, "%c)", stone2char(board_atxy(board
, x
, y
)));
328 fprintf(f
, "%c ", stone2char(board_atxy(board
, x
, y
)));
332 fprintf(f
, " %2d | ", y
);
333 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
334 cprint(board
, coord_xy(board
, x
, y
), f
);
342 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
344 fprintf(f
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
345 board
->moves
, board
->komi
, board
->handicap
,
346 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
347 board_print_top(board
, f
, 1 + !!cprint
);
348 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
349 board_print_row(board
, y
, f
, cprint
);
350 board_print_bottom(board
, f
, 1 + !!cprint
);
355 cprint_group(struct board
*board
, coord_t c
, FILE *f
)
357 fprintf(f
, "%d ", group_base(group_at(board
, c
)));
361 board_print(struct board
*board
, FILE *f
)
363 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
367 /* Update board hash with given coordinate. */
368 static void profiling_noinline
369 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
371 board
->hash
^= hash_at(board
, coord
, color
);
373 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
);
375 #ifdef BOARD_SPATHASH
376 /* Gridcular metric is reflective, so we update all hashes
377 * of appropriate ditance in OUR circle. */
378 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
379 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
380 ptcoords_at(x
, y
, coord
, board
, j
);
381 /* We either changed from S_NONE to color
382 * or vice versa; doesn't matter. */
383 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
384 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
385 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
386 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
391 #if defined(BOARD_PAT3)
392 /* @color is not what we need in case of capture. */
393 enum stone new_color
= board_at(board
, coord
);
394 if (new_color
== S_NONE
)
395 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
396 foreach_8neighbor(board
, coord
) { // internally, the loop uses fn__i=[0..7]
397 if (board_at(board
, c
) != S_NONE
)
399 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
400 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
402 if (board_at(board
, c
) != S_OFFBOARD
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
403 board_print(board
, stderr
);
404 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
);
408 } foreach_8neighbor_end
;
412 /* Commit current board hash to history. */
413 static void profiling_noinline
414 board_hash_commit(struct board
*board
)
417 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
418 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
419 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
421 hash_t i
= board
->hash
;
422 while (board
->history_hash
[i
& history_hash_mask
]) {
423 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
425 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
426 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
427 board
->superko_violation
= true;
430 i
= history_hash_next(i
);
432 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
438 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
440 if (likely(symmetry
->type
== SYM_NONE
)) {
441 /* Fully degenerated already. We do not support detection
442 * of restoring of symmetry, assuming that this is too rare
443 * a case to handle. */
447 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
448 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
450 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
451 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
452 symmetry
->d
, symmetry
->type
, x
, y
);
455 switch (symmetry
->type
) {
457 if (x
== t
&& y
== t
) {
458 /* Tengen keeps full symmetry. */
461 /* New symmetry now? */
463 symmetry
->type
= SYM_DIAG_UP
;
464 symmetry
->x1
= symmetry
->y1
= 1;
465 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
467 } else if (dx
== y
) {
468 symmetry
->type
= SYM_DIAG_DOWN
;
469 symmetry
->x1
= symmetry
->y1
= 1;
470 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
473 symmetry
->type
= SYM_HORIZ
;
475 symmetry
->y2
= board_size(b
) - 1;
478 symmetry
->type
= SYM_VERT
;
480 symmetry
->x2
= board_size(b
) - 1;
484 symmetry
->type
= SYM_NONE
;
485 symmetry
->x1
= symmetry
->y1
= 1;
486 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
512 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
513 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
514 symmetry
->d
, symmetry
->type
);
521 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
524 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
526 board_play(board
, &m
);
527 /* Simulate white passing; otherwise, UCT search can get confused since
528 * tree depth parity won't match the color to move. */
531 char *str
= coord2str(m
.coord
, board
);
533 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
534 fprintf(f
, "%s ", str
);
539 board_handicap(struct board
*board
, int stones
, FILE *f
)
541 int margin
= 3 + (board_size(board
) >= 13);
543 int mid
= board_size(board
) / 2;
544 int max
= board_size(board
) - 1 - margin
;
545 const int places
[][2] = {
546 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
547 { min
, mid
}, { max
, mid
},
548 { mid
, min
}, { mid
, max
},
552 board
->handicap
= stones
;
554 if (stones
== 5 || stones
== 7) {
555 board_handicap_stone(board
, mid
, mid
, f
);
560 for (i
= 0; i
< stones
; i
++)
561 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
565 static void __attribute__((noinline
))
566 check_libs_consistency(struct board
*board
, group_t g
)
570 struct group
*gi
= &board_group_info(board
, g
);
571 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
572 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
573 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
580 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
)
582 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
584 /* Increase capturable count trait of my last lib. */
585 enum stone capturing_color
= stone_other(board_at(board
, group
));
586 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
587 foreach_neighbor(board
, lib
, {
588 if (DEBUGL(8) && group_at(board
, c
) == group
)
589 fprintf(stderr
, "%s[%d] %s cap bump bc of %s(%d) member %s\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
, stone2str(capturing_color
), coord2sstr(group
, board
), board_group_info(board
, group
).libs
, coord2sstr(c
, board
));
590 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
595 /* Update the list of capturable groups. */
597 assert(board
->clen
< board_size2(board
));
598 board
->c
[board
->clen
++] = group
;
602 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
)
604 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
606 /* Decrease capturable count trait of my previously-last lib. */
607 enum stone capturing_color
= stone_other(board_at(board
, group
));
608 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
609 foreach_neighbor(board
, lib
, {
610 if (DEBUGL(8) && group_at(board
, c
) == group
)
611 fprintf(stderr
, "%s[%d] cap dump bc of %s(%d) member %s\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
, coord2sstr(group
, board
), board_group_info(board
, group
).libs
, coord2sstr(c
, board
));
612 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
617 /* Update the list of capturable groups. */
618 for (int i
= 0; i
< board
->clen
; i
++) {
619 if (unlikely(board
->c
[i
] == group
)) {
620 board
->c
[i
] = board
->c
[--board
->clen
];
624 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
630 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
633 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
634 group_base(group
), coord2sstr(group_base(group
), board
),
635 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
638 check_libs_consistency(board
, group
);
640 struct group
*gi
= &board_group_info(board
, group
);
641 if (gi
->libs
< GROUP_KEEP_LIBS
) {
642 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
644 /* Seems extra branch just slows it down */
648 if (unlikely(gi
->lib
[i
] == coord
))
652 board_capturable_add(board
, group
, coord
);
653 else if (gi
->libs
== 1)
654 board_capturable_rm(board
, group
, gi
->lib
[0]);
655 gi
->lib
[gi
->libs
++] = coord
;
658 check_libs_consistency(board
, group
);
662 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
664 /* Add extra liberty from the board to our liberty list. */
665 unsigned char watermark
[board_size2(board
) / 8];
666 memset(watermark
, 0, sizeof(watermark
));
667 #define watermark_get(c) (watermark[coord_raw(c) >> 3] & (1 << (coord_raw(c) & 7)))
668 #define watermark_set(c) watermark[coord_raw(c) >> 3] |= (1 << (coord_raw(c) & 7))
670 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
671 watermark_set(gi
->lib
[i
]);
672 watermark_set(avoid
);
674 foreach_in_group(board
, group
) {
676 foreach_neighbor(board
, coord2
, {
677 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
680 gi
->lib
[gi
->libs
++] = c
;
681 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
684 } foreach_in_group_end
;
690 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
693 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
694 group_base(group
), coord2sstr(group_base(group
), board
),
695 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
698 struct group
*gi
= &board_group_info(board
, group
);
699 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
701 /* Seems extra branch just slows it down */
705 if (likely(gi
->lib
[i
] != coord
))
708 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
709 gi
->lib
[gi
->libs
] = 0;
711 check_libs_consistency(board
, group
);
713 /* Postpone refilling lib[] until we need to. */
714 assert(GROUP_REFILL_LIBS
> 1);
715 if (gi
->libs
> GROUP_REFILL_LIBS
)
717 if (gi
->libs
== GROUP_REFILL_LIBS
)
718 board_group_find_extra_libs(board
, group
, gi
, coord
);
721 board_capturable_add(board
, group
, gi
->lib
[0]);
722 else if (gi
->libs
== 0)
723 board_capturable_rm(board
, group
, lib
);
727 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
728 * can call this multiple times per coord. */
729 check_libs_consistency(board
, group
);
734 /* This is a low-level routine that doesn't maintain consistency
735 * of all the board data structures. */
737 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
739 enum stone color
= board_at(board
, c
);
740 board_at(board
, c
) = S_NONE
;
741 group_at(board
, c
) = 0;
742 board_hash_update(board
, c
, color
);
744 /* We mark as cannot-capture now. If this is a ko/snapback,
745 * we will get incremented later in board_group_addlib(). */
746 trait_at(board
, c
, S_BLACK
).cap
= 0;
747 trait_at(board
, c
, S_WHITE
).cap
= 0;
750 /* Increase liberties of surrounding groups */
752 foreach_neighbor(board
, coord
, {
753 dec_neighbor_count_at(board
, c
, color
);
754 group_t g
= group_at(board
, c
);
756 board_group_addlib(board
, g
, coord
);
760 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
761 board
->f
[board
->flen
++] = coord_raw(c
);
764 static int profiling_noinline
765 board_group_capture(struct board
*board
, group_t group
)
769 foreach_in_group(board
, group
) {
770 board
->captures
[stone_other(board_at(board
, c
))]++;
771 board_remove_stone(board
, group
, c
);
773 } foreach_in_group_end
;
775 if (board_group_info(board
, group
).libs
== 1)
776 board_capturable_rm(board
, group
, board_group_info(board
, group
).lib
[0]);
777 memset(&board_group_info(board
, group
), 0, sizeof(struct group
));
783 static void profiling_noinline
784 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
786 group_at(board
, coord
) = group
;
787 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
788 groupnext_at(board
, prevstone
) = coord_raw(coord
);
791 if (board_group_info(board
, group
).libs
== 1) {
792 /* Our group is temporarily in atari; make sure the capturable
793 * counts also correspond to the newly added stone before we
794 * start adding liberties again so bump-dump ops match. */
795 enum stone capturing_color
= stone_other(board_at(board
, group
));
796 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
797 coord_t lib
= board_group_info(board
, group
).lib
[0];
798 if (coord_is_adjecent(lib
, coord
, board
)) {
799 if (DEBUGL(8)) fprintf(stderr
, "add_to_group %s: %s[%d] bump\n", coord2sstr(group
, board
), coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
800 trait_at(board
, lib
, capturing_color
).cap
++;
805 foreach_neighbor(board
, coord
, {
806 if (board_at(board
, c
) == S_NONE
)
807 board_group_addlib(board
, group
, c
);
811 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
812 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
813 coord_x(coord
, board
), coord_y(coord
, board
),
814 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
818 static void profiling_noinline
819 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
822 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
823 group_base(group_from
), group_base(group_to
));
824 struct group
*gi_from
= &board_group_info(board
, group_from
);
825 struct group
*gi_to
= &board_group_info(board
, group_to
);
827 /* We do this early before the group info is rewritten. */
828 if (gi_from
->libs
== 1)
829 board_capturable_rm(board
, group_from
, gi_from
->lib
[0]);
832 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
834 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
835 for (int i
= 0; i
< gi_from
->libs
; i
++) {
836 for (int j
= 0; j
< gi_to
->libs
; j
++)
837 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
839 if (gi_to
->libs
== 0)
840 board_capturable_add(board
, group_to
, gi_from
->lib
[i
]);
841 else if (gi_to
->libs
== 1)
842 board_capturable_rm(board
, group_to
, gi_to
->lib
[0]);
843 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
844 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
851 if (board_group_info(board
, group_to
).libs
== 1) {
852 /* Our group is currently in atari; make sure we properly
853 * count in even the neighbors from the other group in the
854 * capturable counter. */
855 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
856 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
857 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
858 foreach_neighbor(board
, lib
, {
859 if (DEBUGL(8) && group_at(board
, c
) == group_from
) fprintf(stderr
, "%s[%d] cap bump\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
860 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
865 coord_t last_in_group
;
866 foreach_in_group(board
, group_from
) {
868 group_at(board
, c
) = group_to
;
869 } foreach_in_group_end
;
870 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
871 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
872 memset(gi_from
, 0, sizeof(struct group
));
875 fprintf(stderr
, "board_play_raw: merged group: %d\n",
876 group_base(group_to
));
879 static group_t profiling_noinline
880 new_group(struct board
*board
, coord_t coord
)
882 group_t group
= coord_raw(coord
);
883 struct group
*gi
= &board_group_info(board
, group
);
884 foreach_neighbor(board
, coord
, {
885 if (board_at(board
, c
) == S_NONE
)
886 /* board_group_addlib is ridiculously expensive for us */
887 #if GROUP_KEEP_LIBS < 4
888 if (gi
->libs
< GROUP_KEEP_LIBS
)
890 gi
->lib
[gi
->libs
++] = c
;
893 group_at(board
, coord
) = group
;
894 groupnext_at(board
, coord
) = 0;
897 board_capturable_add(board
, group
, gi
->lib
[0]);
898 check_libs_consistency(board
, group
);
901 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
902 coord_x(coord
, board
), coord_y(coord
, board
),
908 static inline group_t
909 play_one_neighbor(struct board
*board
,
910 coord_t coord
, enum stone color
, enum stone other_color
,
911 coord_t c
, group_t group
)
913 enum stone ncolor
= board_at(board
, c
);
914 group_t ngroup
= group_at(board
, c
);
916 inc_neighbor_count_at(board
, c
, color
);
921 board_group_rmlib(board
, ngroup
, coord
);
923 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
924 group_base(ngroup
), ncolor
, color
, other_color
);
926 if (ncolor
== color
&& ngroup
!= group
) {
929 add_to_group(board
, group
, c
, coord
);
931 merge_groups(board
, group
, ngroup
);
933 } else if (ncolor
== other_color
) {
935 struct group
*gi
= &board_group_info(board
, ngroup
);
936 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
937 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
938 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
939 fprintf(stderr
, "\n");
941 if (unlikely(board_group_captured(board
, ngroup
)))
942 board_group_capture(board
, ngroup
);
947 /* We played on a place with at least one liberty. We will become a member of
948 * some group for sure. */
949 static group_t profiling_noinline
950 board_play_outside(struct board
*board
, struct move
*m
, int f
)
952 coord_t coord
= m
->coord
;
953 enum stone color
= m
->color
;
954 enum stone other_color
= stone_other(color
);
957 board
->f
[f
] = board
->f
[--board
->flen
];
959 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
961 #if defined(BOARD_TRAITS) && !defined(NDEBUG)
962 /* Sanity check that cap matches reality. */
965 foreach_neighbor(board
, coord
, {
966 group_t g
= group_at(board
, c
);
967 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
969 assert(a
== trait_at(board
, coord
, color
).cap
);
972 foreach_neighbor(board
, coord
, {
973 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
976 board_at(board
, coord
) = color
;
977 if (unlikely(!group
))
978 group
= new_group(board
, coord
);
980 board
->last_move2
= board
->last_move
;
981 board
->last_move
= *m
;
983 board_hash_update(board
, coord
, color
);
984 board_symmetry_update(board
, &board
->symmetry
, coord
);
985 struct move ko
= { pass
, S_NONE
};
991 /* We played in an eye-like shape. Either we capture at least one of the eye
992 * sides in the process of playing, or return -1. */
993 static int profiling_noinline
994 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
996 coord_t coord
= m
->coord
;
997 enum stone color
= m
->color
;
998 /* Check ko: Capture at a position of ko capture one move ago */
999 if (unlikely(color
== board
->ko
.color
&& coord_eq(coord
, board
->ko
.coord
))) {
1001 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1003 } else if (DEBUGL(6)) {
1004 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1005 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1006 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1009 struct move ko
= { pass
, S_NONE
};
1011 int captured_groups
= 0;
1013 foreach_neighbor(board
, coord
, {
1014 group_t g
= group_at(board
, c
);
1016 fprintf(stderr
, "board_check: group %d has %d libs\n",
1017 g
, board_group_info(board
, g
).libs
);
1018 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1021 if (likely(captured_groups
== 0)) {
1024 board_print(board
, stderr
);
1025 fprintf(stderr
, "board_check: one-stone suicide\n");
1031 /* We _will_ for sure capture something. */
1032 assert(trait_at(board
, coord
, color
).cap
> 0);
1035 board
->f
[f
] = board
->f
[--board
->flen
];
1037 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1039 foreach_neighbor(board
, coord
, {
1040 inc_neighbor_count_at(board
, c
, color
);
1042 group_t group
= group_at(board
, c
);
1046 board_group_rmlib(board
, group
, coord
);
1048 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1051 if (board_group_captured(board
, group
)) {
1052 if (board_group_capture(board
, group
) == 1) {
1053 /* If we captured multiple groups at once,
1054 * we can't be fighting ko so we don't need
1055 * to check for that. */
1056 ko
.color
= stone_other(color
);
1058 board
->last_ko
= ko
;
1059 board
->last_ko_age
= board
->moves
;
1061 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1066 board_at(board
, coord
) = color
;
1067 group_t group
= new_group(board
, coord
);
1069 board
->last_move2
= board
->last_move
;
1070 board
->last_move
= *m
;
1072 board_hash_update(board
, coord
, color
);
1073 board_hash_commit(board
);
1074 board_symmetry_update(board
, &board
->symmetry
, coord
);
1080 static int __attribute__((flatten
))
1081 board_play_f(struct board
*board
, struct move
*m
, int f
)
1084 fprintf(stderr
, "board_play(): ---- Playing %d,%d\n", coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1086 if (likely(!board_is_eyelike(board
, &m
->coord
, stone_other(m
->color
)))) {
1087 /* NOT playing in an eye. Thus this move has to succeed. (This
1088 * is thanks to New Zealand rules. Otherwise, multi-stone
1089 * suicide might fail.) */
1090 group_t group
= board_play_outside(board
, m
, f
);
1091 if (unlikely(board_group_captured(board
, group
))) {
1092 board_group_capture(board
, group
);
1094 board_hash_commit(board
);
1097 return board_play_in_eye(board
, m
, f
);
1102 board_play(struct board
*board
, struct move
*m
)
1104 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1105 board
->last_move2
= board
->last_move
;
1106 board
->last_move
= *m
;
1111 for (f
= 0; f
< board
->flen
; f
++)
1112 if (board
->f
[f
] == coord_raw(m
->coord
))
1113 return board_play_f(board
, m
, f
);
1116 fprintf(stderr
, "board_check: stone exists\n");
1122 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1124 coord_raw(*coord
) = b
->f
[f
];
1125 if (unlikely(is_pass(*coord
)))
1127 struct move m
= { *coord
, color
};
1129 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
1130 return (likely(!board_is_one_point_eye(b
, coord
, color
)) /* bad idea to play into one, usually */
1131 && board_is_valid_move(b
, &m
)
1132 && (!permit
|| permit(permit_data
, b
, &m
))
1133 && likely(board_play_f(b
, &m
, f
) >= 0));
1137 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1139 int base
= fast_random(b
->flen
);
1140 coord_pos(*coord
, base
, b
);
1141 if (likely(board_try_random_move(b
, color
, coord
, base
, permit
, permit_data
)))
1145 for (f
= base
+ 1; f
< b
->flen
; f
++)
1146 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1148 for (f
= 0; f
< base
; f
++)
1149 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1157 board_is_false_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
1159 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1161 /* XXX: We attempt false eye detection but we will yield false
1162 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1164 foreach_diag_neighbor(board
, *coord
) {
1165 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1166 } foreach_diag_neighbor_end
;
1167 /* For false eye, we need two enemy stones diagonally in the
1168 * middle of the board, or just one enemy stone at the edge
1169 * or in the corner. */
1170 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1171 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1175 board_is_one_point_eye(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
1177 return board_is_eyelike(board
, coord
, eye_color
)
1178 && !board_is_false_eyelike(board
, coord
, eye_color
);
1182 board_get_one_point_eye(struct board
*board
, coord_t
*coord
)
1184 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1186 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1194 board_fast_score(struct board
*board
)
1197 memset(scores
, 0, sizeof(scores
));
1199 foreach_point(board
) {
1200 enum stone color
= board_at(board
, c
);
1201 if (color
== S_NONE
)
1202 color
= board_get_one_point_eye(board
, &c
);
1204 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1205 } foreach_point_end
;
1207 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1210 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1212 /* One flood-fill iteration; returns true if next iteration
1215 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1217 bool needs_update
= false;
1218 foreach_point(board
) {
1219 /* Ignore occupied and already-dame positions. */
1220 if (board_at(board
, c
) != S_NONE
|| ownermap
[c
] == 3)
1222 /* Count neighbors. */
1224 foreach_neighbor(board
, c
, {
1227 /* If we have neighbors of both colors, or dame,
1228 * we are dame too. */
1229 if ((nei
[1] && nei
[2]) || nei
[3]) {
1231 /* Speed up the propagation. */
1232 foreach_neighbor(board
, c
, {
1233 if (board_at(board
, c
) == S_NONE
)
1236 needs_update
= true;
1239 /* If we have neighbors of one color, we are owned
1240 * by that color, too. */
1241 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1242 int newowner
= nei
[1] ? 1 : 2;
1243 ownermap
[c
] = newowner
;
1244 /* Speed up the propagation. */
1245 foreach_neighbor(board
, c
, {
1246 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1247 ownermap
[c
] = newowner
;
1249 needs_update
= true;
1252 } foreach_point_end
;
1253 return needs_update
;
1256 /* Tromp-Taylor Counting */
1258 board_official_score(struct board
*board
, struct move_queue
*q
)
1261 /* A point P, not colored C, is said to reach C, if there is a path of
1262 * (vertically or horizontally) adjacent points of P's color from P to
1263 * a point of color C.
1265 * A player's score is the number of points of her color, plus the
1266 * number of empty points that reach only her color. */
1268 int ownermap
[board_size2(board
)];
1270 const int o
[4] = {0, 1, 2, 0};
1271 foreach_point(board
) {
1272 ownermap
[c
] = o
[board_at(board
, c
)];
1273 s
[board_at(board
, c
)]++;
1274 } foreach_point_end
;
1277 /* Process dead groups. */
1278 for (int i
= 0; i
< q
->moves
; i
++) {
1279 foreach_in_group(board
, q
->move
[i
]) {
1280 enum stone color
= board_at(board
, c
);
1281 ownermap
[c
] = o
[stone_other(color
)];
1282 s
[color
]--; s
[stone_other(color
)]++;
1283 } foreach_in_group_end
;
1287 /* We need to special-case empty board. */
1288 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1289 return board
->komi
+ board
->handicap
;
1291 while (board_tromp_taylor_iter(board
, ownermap
))
1292 /* Flood-fill... */;
1295 memset(scores
, 0, sizeof(scores
));
1297 foreach_point(board
) {
1298 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1299 if (ownermap
[c
] == 3)
1301 scores
[ownermap
[c
]]++;
1302 } foreach_point_end
;
1304 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];