13 #include "patternsp.h"
22 bool random_pass
= false;
26 #define profiling_noinline __attribute__((noinline))
28 #define profiling_noinline
31 #define gi_granularity 4
32 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
36 board_setup(struct board
*b
)
38 memset(b
, 0, sizeof(*b
));
40 struct move m
= { pass
, S_NONE
};
41 b
->last_move
= b
->last_move2
= b
->last_ko
= b
->ko
= m
;
47 struct board
*b
= malloc(sizeof(struct board
));
58 board_copy(struct board
*b2
, struct board
*b1
)
60 memcpy(b2
, b1
, sizeof(struct board
));
62 int bsize
= board_size2(b2
) * sizeof(*b2
->b
);
63 int gsize
= board_size2(b2
) * sizeof(*b2
->g
);
64 int fsize
= board_size2(b2
) * sizeof(*b2
->f
);
65 int nsize
= board_size2(b2
) * sizeof(*b2
->n
);
66 int psize
= board_size2(b2
) * sizeof(*b2
->p
);
67 int hsize
= board_size2(b2
) * 2 * sizeof(*b2
->h
);
68 int gisize
= board_size2(b2
) * sizeof(*b2
->gi
);
70 int csize
= board_size2(b2
) * sizeof(*b2
->c
);
75 int ssize
= board_size2(b2
) * sizeof(*b2
->spathash
);
80 int p3size
= board_size2(b2
) * sizeof(*b2
->pat3
);
85 int tsize
= board_size2(b2
) * sizeof(*b2
->t
);
90 int pbsize
= board_size2(b2
) * sizeof(*b2
->prob
[0].items
);
94 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ pbsize
* 2);
95 memcpy(x
, b1
->b
, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ pbsize
* 2);
96 b2
->b
= x
; x
+= bsize
;
97 b2
->g
= x
; x
+= gsize
;
98 b2
->f
= x
; x
+= fsize
;
99 b2
->p
= x
; x
+= psize
;
100 b2
->n
= x
; x
+= nsize
;
101 b2
->h
= x
; x
+= hsize
;
102 b2
->gi
= x
; x
+= gisize
;
104 b2
->c
= x
; x
+= csize
;
106 #ifdef BOARD_SPATHASH
107 b2
->spathash
= x
; x
+= ssize
;
110 b2
->pat3
= x
; x
+= p3size
;
113 b2
->t
= x
; x
+= tsize
;
116 b2
->prob
[0].items
= x
; x
+= pbsize
;
117 b2
->prob
[1].items
= x
; x
+= pbsize
;
124 board_done_noalloc(struct board
*board
)
126 if (board
->b
) free(board
->b
);
130 board_done(struct board
*board
)
132 board_done_noalloc(board
);
137 board_resize(struct board
*board
, int size
)
140 assert(board_size(board
) == size
+ 2);
142 board_size(board
) = size
+ 2 /* S_OFFBOARD margin */;
143 board_size2(board
) = board_size(board
) * board_size(board
);
148 int bsize
= board_size2(board
) * sizeof(*board
->b
);
149 int gsize
= board_size2(board
) * sizeof(*board
->g
);
150 int fsize
= board_size2(board
) * sizeof(*board
->f
);
151 int nsize
= board_size2(board
) * sizeof(*board
->n
);
152 int psize
= board_size2(board
) * sizeof(*board
->p
);
153 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
154 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
156 int csize
= board_size2(board
) * sizeof(*board
->c
);
160 #ifdef BOARD_SPATHASH
161 int ssize
= board_size2(board
) * sizeof(*board
->spathash
);
166 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
171 int tsize
= board_size2(board
) * sizeof(*board
->t
);
176 int pbsize
= board_size2(board
) * sizeof(*board
->prob
[0].items
);
180 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ pbsize
* 2);
181 memset(x
, 0, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ pbsize
* 2);
182 board
->b
= x
; x
+= bsize
;
183 board
->g
= x
; x
+= gsize
;
184 board
->f
= x
; x
+= fsize
;
185 board
->p
= x
; x
+= psize
;
186 board
->n
= x
; x
+= nsize
;
187 board
->h
= x
; x
+= hsize
;
188 board
->gi
= x
; x
+= gisize
;
190 board
->c
= x
; x
+= csize
;
192 #ifdef BOARD_SPATHASH
193 board
->spathash
= x
; x
+= ssize
;
196 board
->pat3
= x
; x
+= p3size
;
199 board
->t
= x
; x
+= tsize
;
202 board
->prob
[0].items
= x
; x
+= pbsize
;
203 board
->prob
[1].items
= x
; x
+= pbsize
;
208 board_clear(struct board
*board
)
210 int size
= board_size(board
);
211 float komi
= board
->komi
;
213 board_done_noalloc(board
);
215 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
219 /* Setup neighborhood iterators */
220 board
->nei8
[0] = -size
- 1; // (-1,-1)
223 board
->nei8
[3] = size
- 2; // (-1,0)
225 board
->nei8
[5] = size
- 2; // (-1,1)
228 board
->dnei
[0] = -size
- 1;
230 board
->dnei
[2] = size
*2 - 2;
233 /* Setup initial symmetry */
234 board
->symmetry
.d
= 1;
235 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
236 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
237 board
->symmetry
.type
= SYM_FULL
;
239 /* Draw the offboard margin */
240 int top_row
= board_size2(board
) - board_size(board
);
242 for (i
= 0; i
< board_size(board
); i
++)
243 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
244 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
245 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
247 foreach_point(board
) {
249 if (board_at(board
, coord
) == S_OFFBOARD
)
251 foreach_neighbor(board
, c
, {
252 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
256 /* First, pass is always a free position. */
257 board
->f
[board
->flen
++] = coord_raw(pass
);
258 /* All positions are free! Except the margin. */
259 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
260 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
261 board
->f
[board
->flen
++] = i
;
263 /* Initialize zobrist hashtable. */
264 foreach_point(board
) {
265 int max
= (sizeof(hash_t
) << history_hash_bits
);
266 /* fast_random() is 16-bit only */
267 board
->h
[coord_raw(c
) * 2] = ((hash_t
) fast_random(max
))
268 | ((hash_t
) fast_random(max
) << 16)
269 | ((hash_t
) fast_random(max
) << 32)
270 | ((hash_t
) fast_random(max
) << 48);
271 if (!board
->h
[coord_raw(c
) * 2])
272 /* Would be kinda "oops". */
273 board
->h
[coord_raw(c
) * 2] = 1;
274 /* And once again for white */
275 board
->h
[coord_raw(c
) * 2 + 1] = ((hash_t
) fast_random(max
))
276 | ((hash_t
) fast_random(max
) << 16)
277 | ((hash_t
) fast_random(max
) << 32)
278 | ((hash_t
) fast_random(max
) << 48);
279 if (!board
->h
[coord_raw(c
) * 2 + 1])
280 board
->h
[coord_raw(c
) * 2 + 1] = 1;
283 #ifdef BOARD_SPATHASH
284 /* Initialize spatial hashes. */
285 foreach_point(board
) {
286 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
287 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
288 ptcoords_at(x
, y
, c
, board
, j
);
289 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
290 pthashes
[0][j
][board_at(board
, c
)];
291 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
292 pthashes
[0][j
][stone_other(board_at(board
, c
))];
298 /* Initialize 3x3 pattern codes. */
299 foreach_point(board
) {
300 if (board_at(board
, c
) == S_NONE
)
301 board
->pat3
[c
] = pattern3_hash(board
, c
);
305 /* Initialize traits. */
306 foreach_point(board
) {
307 trait_at(board
, c
, S_BLACK
).cap
= 0;
308 trait_at(board
, c
, S_BLACK
).safe
= true;
309 trait_at(board
, c
, S_WHITE
).cap
= 0;
310 trait_at(board
, c
, S_WHITE
).safe
= true;
314 board
->prob
[0].n
= board
->prob
[1].n
= board_size2(board
);
315 foreach_point(board
) {
316 probdist_set(&board
->prob
[0], c
, (board_at(board
, c
) == S_NONE
) * 1.0f
);
317 probdist_set(&board
->prob
[1], c
, (board_at(board
, c
) == S_NONE
) * 1.0f
);
324 board_print_top(struct board
*board
, FILE *f
, int c
)
326 for (int i
= 0; i
< c
; i
++) {
327 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
329 for (int x
= 1; x
< board_size(board
) - 1; x
++)
330 fprintf(f
, "%c ", asdf
[x
- 1]);
334 for (int i
= 0; i
< c
; i
++) {
336 for (int x
= 1; x
< board_size(board
) - 1; x
++)
344 board_print_bottom(struct board
*board
, FILE *f
, int c
)
346 for (int i
= 0; i
< c
; i
++) {
348 for (int x
= 1; x
< board_size(board
) - 1; x
++)
356 board_print_row(struct board
*board
, int y
, FILE *f
, board_cprint cprint
)
358 fprintf(f
, " %2d | ", y
);
359 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
360 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
361 fprintf(f
, "%c)", stone2char(board_atxy(board
, x
, y
)));
363 fprintf(f
, "%c ", stone2char(board_atxy(board
, x
, y
)));
367 fprintf(f
, " %2d | ", y
);
368 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
369 cprint(board
, coord_xy(board
, x
, y
), f
);
377 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
379 fprintf(f
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
380 board
->moves
, board
->komi
, board
->handicap
,
381 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
382 board_print_top(board
, f
, 1 + !!cprint
);
383 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
384 board_print_row(board
, y
, f
, cprint
);
385 board_print_bottom(board
, f
, 1 + !!cprint
);
390 cprint_group(struct board
*board
, coord_t c
, FILE *f
)
392 fprintf(f
, "%d ", group_base(group_at(board
, c
)));
396 board_print(struct board
*board
, FILE *f
)
398 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
402 /* Update the probability distribution we maintain incrementally. */
404 board_gamma_update(struct board
*board
, coord_t coord
, enum stone color
)
407 assert(board
->gamma
);
408 /* Punch out invalid moves and moves filling our own eyes. */
409 if (board_at(board
, coord
) != S_NONE
410 || (board_is_eyelike(board
, &coord
, stone_other(color
))
411 && !trait_at(board
, coord
, color
).cap
)
412 || (board_is_one_point_eye(board
, &coord
, color
))) {
413 probdist_set(&board
->prob
[color
- 1], coord
, 0);
417 /* We just quickly replicate the general pattern matcher stuff
418 * here in the most bare-bone way. */
420 if (trait_at(board
, coord
, color
).cap
)
421 value
*= board
->gamma
->gamma
[FEAT_CAPTURE
][0];
422 if (trait_at(board
, coord
, stone_other(color
)).cap
423 && trait_at(board
, coord
, color
).safe
)
424 value
*= board
->gamma
->gamma
[FEAT_AESCAPE
][0];
425 if (!trait_at(board
, coord
, color
).safe
)
426 value
*= board
->gamma
->gamma
[FEAT_SELFATARI
][0];
427 probdist_set(&board
->prob
[color
- 1], coord
, value
);
431 /* Recompute some of the traits for given point from scratch. Note that
432 * some traits are updated incrementally elsewhere. */
434 board_trait_recompute(struct board
*board
, coord_t coord
)
437 trait_at(board
, coord
, S_BLACK
).safe
= board_safe_to_play(board
, coord
, S_BLACK
);
438 trait_at(board
, coord
, S_WHITE
).safe
= board_safe_to_play(board
, coord
, S_WHITE
);
440 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d safe=%d) (white cap=%d safe=%d)\n",
441 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
442 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).safe
,
443 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).safe
);
446 board_gamma_update(board
, coord
, S_BLACK
);
447 board_gamma_update(board
, coord
, S_WHITE
);
450 /* Update board hash with given coordinate. */
451 static void profiling_noinline
452 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
454 board
->hash
^= hash_at(board
, coord
, color
);
456 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
);
458 #ifdef BOARD_SPATHASH
459 /* Gridcular metric is reflective, so we update all hashes
460 * of appropriate ditance in OUR circle. */
461 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
462 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
463 ptcoords_at(x
, y
, coord
, board
, j
);
464 /* We either changed from S_NONE to color
465 * or vice versa; doesn't matter. */
466 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
467 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
468 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
469 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
474 #if defined(BOARD_PAT3)
475 /* @color is not what we need in case of capture. */
476 enum stone new_color
= board_at(board
, coord
);
477 if (new_color
== S_NONE
)
478 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
479 foreach_8neighbor(board
, coord
) { // internally, the loop uses fn__i=[0..7]
480 if (board_at(board
, c
) != S_NONE
)
482 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
483 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
485 if (board_at(board
, c
) != S_OFFBOARD
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
486 board_print(board
, stderr
);
487 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
);
491 board_gamma_update(board
, c
, S_BLACK
);
492 board_gamma_update(board
, c
, S_WHITE
);
493 } foreach_8neighbor_end
;
497 /* Commit current board hash to history. */
498 static void profiling_noinline
499 board_hash_commit(struct board
*board
)
502 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
503 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
504 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
506 hash_t i
= board
->hash
;
507 while (board
->history_hash
[i
& history_hash_mask
]) {
508 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
510 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
511 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
512 board
->superko_violation
= true;
515 i
= history_hash_next(i
);
517 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
523 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
525 if (likely(symmetry
->type
== SYM_NONE
)) {
526 /* Fully degenerated already. We do not support detection
527 * of restoring of symmetry, assuming that this is too rare
528 * a case to handle. */
532 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
533 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
535 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
536 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
537 symmetry
->d
, symmetry
->type
, x
, y
);
540 switch (symmetry
->type
) {
542 if (x
== t
&& y
== t
) {
543 /* Tengen keeps full symmetry. */
546 /* New symmetry now? */
548 symmetry
->type
= SYM_DIAG_UP
;
549 symmetry
->x1
= symmetry
->y1
= 1;
550 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
552 } else if (dx
== y
) {
553 symmetry
->type
= SYM_DIAG_DOWN
;
554 symmetry
->x1
= symmetry
->y1
= 1;
555 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
558 symmetry
->type
= SYM_HORIZ
;
560 symmetry
->y2
= board_size(b
) - 1;
563 symmetry
->type
= SYM_VERT
;
565 symmetry
->x2
= board_size(b
) - 1;
569 symmetry
->type
= SYM_NONE
;
570 symmetry
->x1
= symmetry
->y1
= 1;
571 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
597 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
598 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
599 symmetry
->d
, symmetry
->type
);
606 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
609 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
611 board_play(board
, &m
);
612 /* Simulate white passing; otherwise, UCT search can get confused since
613 * tree depth parity won't match the color to move. */
616 char *str
= coord2str(m
.coord
, board
);
618 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
619 fprintf(f
, "%s ", str
);
624 board_handicap(struct board
*board
, int stones
, FILE *f
)
626 int margin
= 3 + (board_size(board
) >= 13);
628 int mid
= board_size(board
) / 2;
629 int max
= board_size(board
) - 1 - margin
;
630 const int places
[][2] = {
631 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
632 { min
, mid
}, { max
, mid
},
633 { mid
, min
}, { mid
, max
},
637 board
->handicap
= stones
;
639 if (stones
== 5 || stones
== 7) {
640 board_handicap_stone(board
, mid
, mid
, f
);
645 for (i
= 0; i
< stones
; i
++)
646 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
650 static void __attribute__((noinline
))
651 check_libs_consistency(struct board
*board
, group_t g
)
655 struct group
*gi
= &board_group_info(board
, g
);
656 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
657 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
658 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
665 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
)
667 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
669 /* Increase capturable count trait of my last lib. */
670 enum stone capturing_color
= stone_other(board_at(board
, group
));
671 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
672 foreach_neighbor(board
, lib
, {
673 if (DEBUGL(8) && group_at(board
, c
) == group
)
674 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
));
675 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
677 board_trait_recompute(board
, lib
);
681 /* Update the list of capturable groups. */
683 assert(board
->clen
< board_size2(board
));
684 board
->c
[board
->clen
++] = group
;
688 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
)
690 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
692 /* Decrease capturable count trait of my previously-last lib. */
693 enum stone capturing_color
= stone_other(board_at(board
, group
));
694 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
695 foreach_neighbor(board
, lib
, {
696 if (DEBUGL(8) && group_at(board
, c
) == group
)
697 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
));
698 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
700 board_trait_recompute(board
, lib
);
704 /* Update the list of capturable groups. */
705 for (int i
= 0; i
< board
->clen
; i
++) {
706 if (unlikely(board
->c
[i
] == group
)) {
707 board
->c
[i
] = board
->c
[--board
->clen
];
711 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
717 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
720 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
721 group_base(group
), coord2sstr(group_base(group
), board
),
722 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
725 check_libs_consistency(board
, group
);
727 struct group
*gi
= &board_group_info(board
, group
);
728 if (gi
->libs
< GROUP_KEEP_LIBS
) {
729 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
731 /* Seems extra branch just slows it down */
735 if (unlikely(gi
->lib
[i
] == coord
))
739 board_capturable_add(board
, group
, coord
);
740 else if (gi
->libs
== 1)
741 board_capturable_rm(board
, group
, gi
->lib
[0]);
742 gi
->lib
[gi
->libs
++] = coord
;
745 check_libs_consistency(board
, group
);
749 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
751 /* Add extra liberty from the board to our liberty list. */
752 unsigned char watermark
[board_size2(board
) / 8];
753 memset(watermark
, 0, sizeof(watermark
));
754 #define watermark_get(c) (watermark[coord_raw(c) >> 3] & (1 << (coord_raw(c) & 7)))
755 #define watermark_set(c) watermark[coord_raw(c) >> 3] |= (1 << (coord_raw(c) & 7))
757 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
758 watermark_set(gi
->lib
[i
]);
759 watermark_set(avoid
);
761 foreach_in_group(board
, group
) {
763 foreach_neighbor(board
, coord2
, {
764 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
767 gi
->lib
[gi
->libs
++] = c
;
768 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
771 } foreach_in_group_end
;
777 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
780 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
781 group_base(group
), coord2sstr(group_base(group
), board
),
782 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
785 struct group
*gi
= &board_group_info(board
, group
);
786 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
788 /* Seems extra branch just slows it down */
792 if (likely(gi
->lib
[i
] != coord
))
795 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
796 gi
->lib
[gi
->libs
] = 0;
798 check_libs_consistency(board
, group
);
800 /* Postpone refilling lib[] until we need to. */
801 assert(GROUP_REFILL_LIBS
> 1);
802 if (gi
->libs
> GROUP_REFILL_LIBS
)
804 if (gi
->libs
== GROUP_REFILL_LIBS
)
805 board_group_find_extra_libs(board
, group
, gi
, coord
);
808 board_capturable_add(board
, group
, gi
->lib
[0]);
809 else if (gi
->libs
== 0)
810 board_capturable_rm(board
, group
, lib
);
814 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
815 * can call this multiple times per coord. */
816 check_libs_consistency(board
, group
);
821 /* This is a low-level routine that doesn't maintain consistency
822 * of all the board data structures. */
824 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
826 enum stone color
= board_at(board
, c
);
827 board_at(board
, c
) = S_NONE
;
828 group_at(board
, c
) = 0;
829 board_hash_update(board
, c
, color
);
831 /* We mark as cannot-capture now. If this is a ko/snapback,
832 * we will get incremented later in board_group_addlib(). */
833 trait_at(board
, c
, S_BLACK
).cap
= 0;
834 trait_at(board
, c
, S_WHITE
).cap
= 0;
835 /* However, we do decide safety statically; we might get
836 * over-paranoid, but in that case the neighbor loop for
837 * stones removed next will repair the flag. */
838 /* We must do this update after the loop when our neighbor count is correct. */
839 board_trait_recompute(board
, c
);
842 /* Increase liberties of surrounding groups */
844 foreach_neighbor(board
, coord
, {
845 dec_neighbor_count_at(board
, c
, color
);
846 board_trait_recompute(board
, c
);
847 group_t g
= group_at(board
, c
);
849 board_group_addlib(board
, g
, coord
);
853 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
854 board
->f
[board
->flen
++] = coord_raw(c
);
857 static int profiling_noinline
858 board_group_capture(struct board
*board
, group_t group
)
862 foreach_in_group(board
, group
) {
863 board
->captures
[stone_other(board_at(board
, c
))]++;
864 board_remove_stone(board
, group
, c
);
866 } foreach_in_group_end
;
868 if (board_group_info(board
, group
).libs
== 1)
869 board_capturable_rm(board
, group
, board_group_info(board
, group
).lib
[0]);
870 memset(&board_group_info(board
, group
), 0, sizeof(struct group
));
876 static void profiling_noinline
877 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
879 group_at(board
, coord
) = group
;
880 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
881 groupnext_at(board
, prevstone
) = coord_raw(coord
);
884 if (board_group_info(board
, group
).libs
== 1) {
885 /* Our group is temporarily in atari; make sure the capturable
886 * counts also correspond to the newly added stone before we
887 * start adding liberties again so bump-dump ops match. */
888 enum stone capturing_color
= stone_other(board_at(board
, group
));
889 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
890 coord_t lib
= board_group_info(board
, group
).lib
[0];
891 if (coord_is_adjecent(lib
, coord
, board
)) {
892 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
);
893 trait_at(board
, lib
, capturing_color
).cap
++;
894 board_trait_recompute(board
, lib
);
899 foreach_neighbor(board
, coord
, {
900 if (board_at(board
, c
) == S_NONE
)
901 board_group_addlib(board
, group
, c
);
905 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
906 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
907 coord_x(coord
, board
), coord_y(coord
, board
),
908 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
912 static void profiling_noinline
913 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
916 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
917 group_base(group_from
), group_base(group_to
));
918 struct group
*gi_from
= &board_group_info(board
, group_from
);
919 struct group
*gi_to
= &board_group_info(board
, group_to
);
921 /* We do this early before the group info is rewritten. */
922 if (gi_from
->libs
== 1)
923 board_capturable_rm(board
, group_from
, gi_from
->lib
[0]);
926 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
928 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
929 for (int i
= 0; i
< gi_from
->libs
; i
++) {
930 for (int j
= 0; j
< gi_to
->libs
; j
++)
931 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
933 if (gi_to
->libs
== 0)
934 board_capturable_add(board
, group_to
, gi_from
->lib
[i
]);
935 else if (gi_to
->libs
== 1)
936 board_capturable_rm(board
, group_to
, gi_to
->lib
[0]);
937 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
938 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
945 if (board_group_info(board
, group_to
).libs
== 1) {
946 /* Our group is currently in atari; make sure we properly
947 * count in even the neighbors from the other group in the
948 * capturable counter. */
949 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
950 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
951 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
952 foreach_neighbor(board
, lib
, {
953 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
);
954 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
956 board_trait_recompute(board
, lib
);
960 coord_t last_in_group
;
961 foreach_in_group(board
, group_from
) {
963 group_at(board
, c
) = group_to
;
964 } foreach_in_group_end
;
965 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
966 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
967 memset(gi_from
, 0, sizeof(struct group
));
970 fprintf(stderr
, "board_play_raw: merged group: %d\n",
971 group_base(group_to
));
974 static group_t profiling_noinline
975 new_group(struct board
*board
, coord_t coord
)
977 group_t group
= coord_raw(coord
);
978 struct group
*gi
= &board_group_info(board
, group
);
979 foreach_neighbor(board
, coord
, {
980 if (board_at(board
, c
) == S_NONE
)
981 /* board_group_addlib is ridiculously expensive for us */
982 #if GROUP_KEEP_LIBS < 4
983 if (gi
->libs
< GROUP_KEEP_LIBS
)
985 gi
->lib
[gi
->libs
++] = c
;
988 group_at(board
, coord
) = group
;
989 groupnext_at(board
, coord
) = 0;
992 board_capturable_add(board
, group
, gi
->lib
[0]);
993 check_libs_consistency(board
, group
);
996 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
997 coord_x(coord
, board
), coord_y(coord
, board
),
1003 static inline group_t
1004 play_one_neighbor(struct board
*board
,
1005 coord_t coord
, enum stone color
, enum stone other_color
,
1006 coord_t c
, group_t group
)
1008 enum stone ncolor
= board_at(board
, c
);
1009 group_t ngroup
= group_at(board
, c
);
1011 inc_neighbor_count_at(board
, c
, color
);
1012 /* We can be S_NONE, in that case we need to update the safety
1013 * trait since we might be left with only one liberty. */
1014 board_trait_recompute(board
, c
);
1019 board_group_rmlib(board
, ngroup
, coord
);
1021 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1022 group_base(ngroup
), ncolor
, color
, other_color
);
1024 if (ncolor
== color
&& ngroup
!= group
) {
1027 add_to_group(board
, group
, c
, coord
);
1029 merge_groups(board
, group
, ngroup
);
1031 } else if (ncolor
== other_color
) {
1033 struct group
*gi
= &board_group_info(board
, ngroup
);
1034 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1035 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1036 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1037 fprintf(stderr
, "\n");
1039 if (unlikely(board_group_captured(board
, ngroup
)))
1040 board_group_capture(board
, ngroup
);
1045 /* We played on a place with at least one liberty. We will become a member of
1046 * some group for sure. */
1047 static group_t profiling_noinline
1048 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1050 coord_t coord
= m
->coord
;
1051 enum stone color
= m
->color
;
1052 enum stone other_color
= stone_other(color
);
1055 board
->f
[f
] = board
->f
[--board
->flen
];
1057 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1059 #if defined(BOARD_TRAITS) && !defined(NDEBUG)
1060 /* Sanity check that cap matches reality. */
1063 foreach_neighbor(board
, coord
, {
1064 group_t g
= group_at(board
, c
);
1065 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1067 assert(a
== trait_at(board
, coord
, color
).cap
);
1068 assert(board_safe_to_play(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1071 foreach_neighbor(board
, coord
, {
1072 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1075 board_at(board
, coord
) = color
;
1076 if (unlikely(!group
))
1077 group
= new_group(board
, coord
);
1078 board_gamma_update(board
, coord
, S_BLACK
);
1079 board_gamma_update(board
, coord
, S_WHITE
);
1081 board
->last_move2
= board
->last_move
;
1082 board
->last_move
= *m
;
1084 board_hash_update(board
, coord
, color
);
1085 board_symmetry_update(board
, &board
->symmetry
, coord
);
1086 struct move ko
= { pass
, S_NONE
};
1092 /* We played in an eye-like shape. Either we capture at least one of the eye
1093 * sides in the process of playing, or return -1. */
1094 static int profiling_noinline
1095 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1097 coord_t coord
= m
->coord
;
1098 enum stone color
= m
->color
;
1099 /* Check ko: Capture at a position of ko capture one move ago */
1100 if (unlikely(color
== board
->ko
.color
&& coord_eq(coord
, board
->ko
.coord
))) {
1102 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1104 } else if (DEBUGL(6)) {
1105 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1106 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1107 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1110 struct move ko
= { pass
, S_NONE
};
1112 int captured_groups
= 0;
1114 foreach_neighbor(board
, coord
, {
1115 group_t g
= group_at(board
, c
);
1117 fprintf(stderr
, "board_check: group %d has %d libs\n",
1118 g
, board_group_info(board
, g
).libs
);
1119 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1122 if (likely(captured_groups
== 0)) {
1125 board_print(board
, stderr
);
1126 fprintf(stderr
, "board_check: one-stone suicide\n");
1132 /* We _will_ for sure capture something. */
1133 assert(trait_at(board
, coord
, color
).cap
> 0);
1134 assert(trait_at(board
, coord
, color
).safe
== board_safe_to_play(board
, coord
, color
));
1137 board
->f
[f
] = board
->f
[--board
->flen
];
1139 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1141 foreach_neighbor(board
, coord
, {
1142 inc_neighbor_count_at(board
, c
, color
);
1143 /* Originally, this could not have changed any trait
1144 * since no neighbors were S_NONE, however by now some
1145 * of them might be removed from the board. */
1146 board_trait_recompute(board
, c
);
1148 group_t group
= group_at(board
, c
);
1152 board_group_rmlib(board
, group
, coord
);
1154 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1157 if (board_group_captured(board
, group
)) {
1158 if (board_group_capture(board
, group
) == 1) {
1159 /* If we captured multiple groups at once,
1160 * we can't be fighting ko so we don't need
1161 * to check for that. */
1162 ko
.color
= stone_other(color
);
1164 board
->last_ko
= ko
;
1165 board
->last_ko_age
= board
->moves
;
1167 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1172 board_at(board
, coord
) = color
;
1173 group_t group
= new_group(board
, coord
);
1174 board_gamma_update(board
, coord
, S_BLACK
);
1175 board_gamma_update(board
, coord
, S_WHITE
);
1177 board
->last_move2
= board
->last_move
;
1178 board
->last_move
= *m
;
1180 board_hash_update(board
, coord
, color
);
1181 board_hash_commit(board
);
1182 board_symmetry_update(board
, &board
->symmetry
, coord
);
1188 static int __attribute__((flatten
))
1189 board_play_f(struct board
*board
, struct move
*m
, int f
)
1192 fprintf(stderr
, "board_play(): ---- Playing %d,%d\n", coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1194 if (likely(!board_is_eyelike(board
, &m
->coord
, stone_other(m
->color
)))) {
1195 /* NOT playing in an eye. Thus this move has to succeed. (This
1196 * is thanks to New Zealand rules. Otherwise, multi-stone
1197 * suicide might fail.) */
1198 group_t group
= board_play_outside(board
, m
, f
);
1199 if (unlikely(board_group_captured(board
, group
))) {
1200 board_group_capture(board
, group
);
1202 board_hash_commit(board
);
1205 return board_play_in_eye(board
, m
, f
);
1210 board_play(struct board
*board
, struct move
*m
)
1212 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1213 struct move nomove
= { pass
, S_NONE
};
1215 board
->last_move2
= board
->last_move
;
1216 board
->last_move
= *m
;
1221 for (f
= 0; f
< board
->flen
; f
++)
1222 if (board
->f
[f
] == coord_raw(m
->coord
))
1223 return board_play_f(board
, m
, f
);
1226 fprintf(stderr
, "board_check: stone exists\n");
1232 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1234 coord_raw(*coord
) = b
->f
[f
];
1235 if (unlikely(is_pass(*coord
)))
1237 struct move m
= { *coord
, color
};
1239 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
1240 return (likely(!board_is_one_point_eye(b
, coord
, color
)) /* bad idea to play into one, usually */
1241 && board_is_valid_move(b
, &m
)
1242 && (!permit
|| permit(permit_data
, b
, &m
))
1243 && likely(board_play_f(b
, &m
, f
) >= 0));
1247 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1249 int base
= fast_random(b
->flen
);
1250 coord_pos(*coord
, base
, b
);
1251 if (likely(board_try_random_move(b
, color
, coord
, base
, permit
, permit_data
)))
1255 for (f
= base
+ 1; f
< b
->flen
; f
++)
1256 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1258 for (f
= 0; f
< base
; f
++)
1259 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1263 struct move m
= { pass
, color
};
1269 board_is_false_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
1271 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1273 /* XXX: We attempt false eye detection but we will yield false
1274 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1276 foreach_diag_neighbor(board
, *coord
) {
1277 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1278 } foreach_diag_neighbor_end
;
1279 /* For false eye, we need two enemy stones diagonally in the
1280 * middle of the board, or just one enemy stone at the edge
1281 * or in the corner. */
1282 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1283 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1287 board_is_one_point_eye(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
1289 return board_is_eyelike(board
, coord
, eye_color
)
1290 && !board_is_false_eyelike(board
, coord
, eye_color
);
1294 board_get_one_point_eye(struct board
*board
, coord_t
*coord
)
1296 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1298 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1306 board_fast_score(struct board
*board
)
1309 memset(scores
, 0, sizeof(scores
));
1311 foreach_point(board
) {
1312 enum stone color
= board_at(board
, c
);
1313 if (color
== S_NONE
)
1314 color
= board_get_one_point_eye(board
, &c
);
1316 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1317 } foreach_point_end
;
1319 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1322 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1324 /* One flood-fill iteration; returns true if next iteration
1327 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1329 bool needs_update
= false;
1330 foreach_point(board
) {
1331 /* Ignore occupied and already-dame positions. */
1332 if (board_at(board
, c
) != S_NONE
|| ownermap
[c
] == 3)
1334 /* Count neighbors. */
1336 foreach_neighbor(board
, c
, {
1339 /* If we have neighbors of both colors, or dame,
1340 * we are dame too. */
1341 if ((nei
[1] && nei
[2]) || nei
[3]) {
1343 /* Speed up the propagation. */
1344 foreach_neighbor(board
, c
, {
1345 if (board_at(board
, c
) == S_NONE
)
1348 needs_update
= true;
1351 /* If we have neighbors of one color, we are owned
1352 * by that color, too. */
1353 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1354 int newowner
= nei
[1] ? 1 : 2;
1355 ownermap
[c
] = newowner
;
1356 /* Speed up the propagation. */
1357 foreach_neighbor(board
, c
, {
1358 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1359 ownermap
[c
] = newowner
;
1361 needs_update
= true;
1364 } foreach_point_end
;
1365 return needs_update
;
1368 /* Tromp-Taylor Counting */
1370 board_official_score(struct board
*board
, struct move_queue
*q
)
1373 /* A point P, not colored C, is said to reach C, if there is a path of
1374 * (vertically or horizontally) adjacent points of P's color from P to
1375 * a point of color C.
1377 * A player's score is the number of points of her color, plus the
1378 * number of empty points that reach only her color. */
1380 int ownermap
[board_size2(board
)];
1382 const int o
[4] = {0, 1, 2, 0};
1383 foreach_point(board
) {
1384 ownermap
[c
] = o
[board_at(board
, c
)];
1385 s
[board_at(board
, c
)]++;
1386 } foreach_point_end
;
1389 /* Process dead groups. */
1390 for (int i
= 0; i
< q
->moves
; i
++) {
1391 foreach_in_group(board
, q
->move
[i
]) {
1392 enum stone color
= board_at(board
, c
);
1393 ownermap
[c
] = o
[stone_other(color
)];
1394 s
[color
]--; s
[stone_other(color
)]++;
1395 } foreach_in_group_end
;
1399 /* We need to special-case empty board. */
1400 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1401 return board
->komi
+ board
->handicap
;
1403 while (board_tromp_taylor_iter(board
, ownermap
))
1404 /* Flood-fill... */;
1407 memset(scores
, 0, sizeof(scores
));
1409 foreach_point(board
) {
1410 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1411 if (ownermap
[c
] == 3)
1413 scores
[ownermap
[c
]]++;
1414 } foreach_point_end
;
1416 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];