15 #include "patternsp.h"
21 static void board_trait_recompute(struct board
*board
, coord_t coord
);
22 #include "tactics/selfatari.h"
30 #define profiling_noinline __attribute__((noinline))
32 #define profiling_noinline
35 #define gi_granularity 4
36 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
40 board_setup(struct board
*b
)
42 memset(b
, 0, sizeof(*b
));
44 struct move m
= { pass
, S_NONE
};
45 b
->last_move
= b
->last_move2
= b
->last_ko
= b
->ko
= m
;
51 struct board
*b
= malloc2(sizeof(struct board
));
62 board_alloc(struct board
*board
)
64 /* We do not allocate the board structure itself but we allocate
65 * all the arrays with board contents. */
67 int bsize
= board_size2(board
) * sizeof(*board
->b
);
68 int gsize
= board_size2(board
) * sizeof(*board
->g
);
69 int fsize
= board_size2(board
) * sizeof(*board
->f
);
70 int nsize
= board_size2(board
) * sizeof(*board
->n
);
71 int psize
= board_size2(board
) * sizeof(*board
->p
);
72 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
73 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
75 int csize
= board_size2(board
) * sizeof(*board
->c
);
80 int ssize
= board_size2(board
) * sizeof(*board
->spathash
);
85 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
90 int tsize
= board_size2(board
) * sizeof(*board
->t
);
91 int tqsize
= board_size2(board
) * sizeof(*board
->t
);
97 int pbsize
= board_size2(board
) * sizeof(*board
->prob
[0].items
);
98 int rowpbsize
= board_size(board
) * sizeof(*board
->prob
[0].rowtotals
);
103 int cdsize
= board_size2(board
) * sizeof(*board
->coord
);
105 size_t size
= bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ (pbsize
+ rowpbsize
) * 2 + cdsize
;
106 void *x
= malloc2(size
);
108 /* board->b must come first */
109 board
->b
= x
; x
+= bsize
;
110 board
->g
= x
; x
+= gsize
;
111 board
->f
= x
; x
+= fsize
;
112 board
->p
= x
; x
+= psize
;
113 board
->n
= x
; x
+= nsize
;
114 board
->h
= x
; x
+= hsize
;
115 board
->gi
= x
; x
+= gisize
;
117 board
->c
= x
; x
+= csize
;
119 #ifdef BOARD_SPATHASH
120 board
->spathash
= x
; x
+= ssize
;
123 board
->pat3
= x
; x
+= p3size
;
126 board
->t
= x
; x
+= tsize
;
127 board
->tq
= x
; x
+= tqsize
;
130 board
->prob
[0].items
= x
; x
+= pbsize
;
131 board
->prob
[1].items
= x
; x
+= pbsize
;
132 board
->prob
[0].rowtotals
= x
; x
+= rowpbsize
;
133 board
->prob
[1].rowtotals
= x
; x
+= rowpbsize
;
135 board
->coord
= x
; x
+= cdsize
;
141 board_copy(struct board
*b2
, struct board
*b1
)
143 memcpy(b2
, b1
, sizeof(struct board
));
145 size_t size
= board_alloc(b2
);
146 memcpy(b2
->b
, b1
->b
, size
);
152 board_done_noalloc(struct board
*board
)
154 if (board
->b
) free(board
->b
);
158 board_done(struct board
*board
)
160 board_done_noalloc(board
);
165 board_resize(struct board
*board
, int size
)
168 assert(board_size(board
) == size
+ 2);
170 assert(size
<= BOARD_MAX_SIZE
);
171 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
172 board
->size2
= board_size(board
) * board_size(board
);
175 while ((1 << board
->bits2
) < board
->size2
) board
->bits2
++;
180 size_t asize
= board_alloc(board
);
181 memset(board
->b
, 0, asize
);
185 board_clear(struct board
*board
)
187 int size
= board_size(board
);
188 float komi
= board
->komi
;
190 board_done_noalloc(board
);
192 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
196 /* Setup neighborhood iterators */
197 board
->nei8
[0] = -size
- 1; // (-1,-1)
200 board
->nei8
[3] = size
- 2; // (-1,0)
202 board
->nei8
[5] = size
- 2; // (-1,1)
205 board
->dnei
[0] = -size
- 1;
207 board
->dnei
[2] = size
*2 - 2;
210 /* Setup initial symmetry */
211 board
->symmetry
.d
= 1;
212 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
213 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
214 board
->symmetry
.type
= SYM_FULL
;
216 /* Set up coordinate cache */
217 foreach_point(board
) {
218 board
->coord
[c
][0] = c
% board_size(board
);
219 board
->coord
[c
][1] = c
/ board_size(board
);
222 /* Draw the offboard margin */
223 int top_row
= board_size2(board
) - board_size(board
);
225 for (i
= 0; i
< board_size(board
); i
++)
226 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
227 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
228 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
230 foreach_point(board
) {
232 if (board_at(board
, coord
) == S_OFFBOARD
)
234 foreach_neighbor(board
, c
, {
235 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
239 /* All positions are free! Except the margin. */
240 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
241 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
242 board
->f
[board
->flen
++] = i
;
244 /* Initialize zobrist hashtable. */
245 /* We will need these to be stable across Pachi runs for
246 * certain kinds of pattern matching, thus we do not use
247 * fast_random() for this. */
248 hash_t hseed
= 0x3121110101112131;
249 foreach_point(board
) {
250 board
->h
[c
* 2] = (hseed
*= 16807);
251 if (!board
->h
[c
* 2])
253 /* And once again for white */
254 board
->h
[c
* 2 + 1] = (hseed
*= 16807);
255 if (!board
->h
[c
* 2 + 1])
256 board
->h
[c
* 2 + 1] = 1;
259 #ifdef BOARD_SPATHASH
260 /* Initialize spatial hashes. */
261 foreach_point(board
) {
262 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
263 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
264 ptcoords_at(x
, y
, c
, board
, j
);
265 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
266 pthashes
[0][j
][board_at(board
, c
)];
267 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
268 pthashes
[0][j
][stone_other(board_at(board
, c
))];
274 /* Initialize 3x3 pattern codes. */
275 foreach_point(board
) {
276 if (board_at(board
, c
) == S_NONE
)
277 board
->pat3
[c
] = pattern3_hash(board
, c
);
281 /* Initialize traits. */
282 foreach_point(board
) {
283 trait_at(board
, c
, S_BLACK
).cap
= 0;
284 trait_at(board
, c
, S_BLACK
).cap1
= 0;
285 trait_at(board
, c
, S_BLACK
).safe
= true;
286 trait_at(board
, c
, S_WHITE
).cap
= 0;
287 trait_at(board
, c
, S_WHITE
).cap1
= 0;
288 trait_at(board
, c
, S_WHITE
).safe
= true;
292 board
->prob
[0].b
= board
->prob
[1].b
= board
;
293 foreach_point(board
) {
294 probdist_set(&board
->prob
[0], c
, double_to_fixp((board_at(board
, c
) == S_NONE
) * 1.0f
));
295 probdist_set(&board
->prob
[1], c
, double_to_fixp((board_at(board
, c
) == S_NONE
) * 1.0f
));
301 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
303 for (int i
= 0; i
< c
; i
++) {
304 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
305 s
+= snprintf(s
, end
- s
, " ");
306 for (int x
= 1; x
< board_size(board
) - 1; x
++)
307 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
308 s
+= snprintf(s
, end
-s
, " ");
310 s
+= snprintf(s
, end
- s
, "\n");
311 for (int i
= 0; i
< c
; i
++) {
312 s
+= snprintf(s
, end
- s
, " +-");
313 for (int x
= 1; x
< board_size(board
) - 1; x
++)
314 s
+= snprintf(s
, end
- s
, "--");
315 s
+= snprintf(s
, end
- s
, "+");
317 s
+= snprintf(s
, end
- s
, "\n");
322 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
324 for (int i
= 0; i
< c
; i
++) {
325 s
+= snprintf(s
, end
- s
, " +-");
326 for (int x
= 1; x
< board_size(board
) - 1; x
++)
327 s
+= snprintf(s
, end
- s
, "--");
328 s
+= snprintf(s
, end
- s
, "+");
330 s
+= snprintf(s
, end
- s
, "\n");
335 board_print_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
)
337 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
338 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
339 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
340 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
342 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
344 s
+= snprintf(s
, end
- s
, "|");
346 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
347 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
348 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
);
350 s
+= snprintf(s
, end
- s
, "|");
352 s
+= snprintf(s
, end
- s
, "\n");
357 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
361 char *end
= buf
+ sizeof(buf
);
362 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
363 board
->moves
, board
->komi
, board
->handicap
,
364 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
365 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
366 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
367 s
= board_print_row(board
, y
, s
, end
, cprint
);
368 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
369 fprintf(f
, "%s\n", buf
);
373 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
)
375 s
+= snprintf(s
, end
- s
, "%d ", group_base(group_at(board
, c
)));
380 board_print(struct board
*board
, FILE *f
)
382 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
386 board_gamma_set(struct board
*b
, struct features_gamma
*gamma
, bool precise_selfatari
)
390 b
->precise_selfatari
= precise_selfatari
;
392 for (int i
= 0; i
< b
->flen
; i
++) {
393 board_trait_recompute(b
, b
->f
[i
]);
400 /* Update the probability distribution we maintain incrementally. */
402 board_gamma_update(struct board
*board
, coord_t coord
, enum stone color
)
404 #if defined(BOARD_GAMMA) && defined(BOARD_TRAITS)
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 hash3_t pat
= board
->pat3
[coord
];
418 if (color
== S_WHITE
) {
419 /* We work with the pattern3s as black-to-play. */
420 pat
= pattern3_reverse(pat
);
423 /* We just quickly replicate the general pattern matcher stuff
424 * here in the most bare-bone way. */
425 double value
= board
->gamma
->gamma
[FEAT_PATTERN3
][pat
];
426 if (trait_at(board
, coord
, color
).cap
) {
428 i
|= (trait_at(board
, coord
, color
).cap1
== trait_at(board
, coord
, color
).cap
) << PF_CAPTURE_1STONE
;
429 i
|= (!trait_at(board
, coord
, stone_other(color
)).safe
) << PF_CAPTURE_TRAPPED
;
430 i
|= (trait_at(board
, coord
, color
).cap
< neighbor_count_at(board
, coord
, stone_other(color
))) << PF_CAPTURE_CONNECTION
;
431 value
*= board
->gamma
->gamma
[FEAT_CAPTURE
][i
];
433 if (trait_at(board
, coord
, stone_other(color
)).cap
) {
435 i
|= (trait_at(board
, coord
, stone_other(color
)).cap1
== trait_at(board
, coord
, stone_other(color
)).cap
) << PF_AESCAPE_1STONE
;
436 i
|= (!trait_at(board
, coord
, color
).safe
) << PF_AESCAPE_TRAPPED
;
437 i
|= (trait_at(board
, coord
, stone_other(color
)).cap
< neighbor_count_at(board
, coord
, color
)) << PF_AESCAPE_CONNECTION
;
438 value
*= board
->gamma
->gamma
[FEAT_AESCAPE
][i
];
440 if (!trait_at(board
, coord
, color
).safe
)
441 value
*= board
->gamma
->gamma
[FEAT_SELFATARI
][1 + board
->precise_selfatari
];
442 probdist_set(&board
->prob
[color
- 1], coord
, double_to_fixp(value
));
448 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
450 if (board
->precise_selfatari
)
451 return !is_bad_selfatari(board
, color
, coord
);
453 return board_safe_to_play(board
, coord
, color
);
457 board_trait_recompute(struct board
*board
, coord_t coord
)
459 trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);;
460 trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
462 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
463 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
464 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).cap1
, trait_at(board
, coord
, S_BLACK
).safe
,
465 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).cap1
, trait_at(board
, coord
, S_WHITE
).safe
);
467 board_gamma_update(board
, coord
, S_BLACK
);
468 board_gamma_update(board
, coord
, S_WHITE
);
472 /* Recompute traits for dirty points that we have previously touched
473 * somehow (libs of their neighbors changed or so). */
475 board_traits_recompute(struct board
*board
)
478 for (int i
= 0; i
< board
->tqlen
; i
++) {
479 coord_t coord
= board
->tq
[i
];
480 trait_at(board
, coord
, S_BLACK
).dirty
= false;
481 if (board_at(board
, coord
) != S_NONE
)
483 board_trait_recompute(board
, coord
);
489 /* Queue traits of given point for recomputing. */
491 board_trait_queue(struct board
*board
, coord_t coord
)
494 if (trait_at(board
, coord
, S_BLACK
).dirty
)
496 board
->tq
[board
->tqlen
++] = coord
;
497 trait_at(board
, coord
, S_BLACK
).dirty
= true;
502 /* Update board hash with given coordinate. */
503 static void profiling_noinline
504 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
506 board
->hash
^= hash_at(board
, coord
, color
);
507 board
->qhash
[coord_quadrant(coord
, board
)] ^= hash_at(board
, coord
, color
);
509 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
);
511 #ifdef BOARD_SPATHASH
512 /* Gridcular metric is reflective, so we update all hashes
513 * of appropriate ditance in OUR circle. */
514 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
515 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
516 ptcoords_at(x
, y
, coord
, board
, j
);
517 /* We either changed from S_NONE to color
518 * or vice versa; doesn't matter. */
519 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
520 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
521 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
522 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
527 #if defined(BOARD_PAT3)
528 /* @color is not what we need in case of capture. */
529 enum stone new_color
= board_at(board
, coord
);
530 if (new_color
== S_NONE
)
531 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
532 foreach_8neighbor(board
, coord
) { // internally, the loop uses fn__i=[0..7]
533 if (board_at(board
, c
) != S_NONE
)
535 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
536 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
538 if (board_at(board
, c
) != S_OFFBOARD
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
539 board_print(board
, stderr
);
540 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
);
544 #if defined(BOARD_TRAITS)
545 board_trait_queue(board
, c
);
546 #elif defined(BOARD_GAMMA)
548 hash3_t pat
= board
->pat3
[c
];
549 if (color
== S_WHITE
) pat
= pattern3_reverse(pat
);
550 double value
= board
->gamma
->gamma
[FEAT_PATTERN3
][pat
];
551 probdist_set(&board
->prob
[color
- 1], c
, double_to_fixp(value
));
554 } foreach_8neighbor_end
;
558 /* Commit current board hash to history. */
559 static void profiling_noinline
560 board_hash_commit(struct board
*board
)
563 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
564 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
565 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
567 hash_t i
= board
->hash
;
568 while (board
->history_hash
[i
& history_hash_mask
]) {
569 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
571 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
572 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
573 board
->superko_violation
= true;
576 i
= history_hash_next(i
);
578 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
584 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
586 if (likely(symmetry
->type
== SYM_NONE
)) {
587 /* Fully degenerated already. We do not support detection
588 * of restoring of symmetry, assuming that this is too rare
589 * a case to handle. */
593 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
594 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
596 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
597 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
598 symmetry
->d
, symmetry
->type
, x
, y
);
601 switch (symmetry
->type
) {
603 if (x
== t
&& y
== t
) {
604 /* Tengen keeps full symmetry. */
607 /* New symmetry now? */
609 symmetry
->type
= SYM_DIAG_UP
;
610 symmetry
->x1
= symmetry
->y1
= 1;
611 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
613 } else if (dx
== y
) {
614 symmetry
->type
= SYM_DIAG_DOWN
;
615 symmetry
->x1
= symmetry
->y1
= 1;
616 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
619 symmetry
->type
= SYM_HORIZ
;
621 symmetry
->y2
= board_size(b
) - 1;
624 symmetry
->type
= SYM_VERT
;
626 symmetry
->x2
= board_size(b
) - 1;
630 symmetry
->type
= SYM_NONE
;
631 symmetry
->x1
= symmetry
->y1
= 1;
632 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
658 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
659 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
660 symmetry
->d
, symmetry
->type
);
667 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
670 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
672 board_play(board
, &m
);
673 /* Simulate white passing; otherwise, UCT search can get confused since
674 * tree depth parity won't match the color to move. */
677 char *str
= coord2str(m
.coord
, board
);
679 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
680 if (f
) fprintf(f
, "%s ", str
);
685 board_handicap(struct board
*board
, int stones
, FILE *f
)
687 int margin
= 3 + (board_size(board
) >= 13);
689 int mid
= board_size(board
) / 2;
690 int max
= board_size(board
) - 1 - margin
;
691 const int places
[][2] = {
692 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
693 { min
, mid
}, { max
, mid
},
694 { mid
, min
}, { mid
, max
},
698 board
->handicap
= stones
;
700 if (stones
== 5 || stones
== 7) {
701 board_handicap_stone(board
, mid
, mid
, f
);
706 for (i
= 0; i
< stones
; i
++)
707 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
711 static void __attribute__((noinline
))
712 check_libs_consistency(struct board
*board
, group_t g
)
716 struct group
*gi
= &board_group_info(board
, g
);
717 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
718 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
719 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
726 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
728 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
730 /* Increase capturable count trait of my last lib. */
731 enum stone capturing_color
= stone_other(board_at(board
, group
));
732 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
733 foreach_neighbor(board
, lib
, {
734 if (DEBUGL(8) && group_at(board
, c
) == group
)
735 fprintf(stderr
, "%s[%d] %s cap bump bc of %s(%d) member %s onestone %d\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
), onestone
);
736 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
737 trait_at(board
, lib
, capturing_color
).cap1
+= (group_at(board
, c
) == group
&& onestone
);
739 board_trait_queue(board
, lib
);
743 /* Update the list of capturable groups. */
745 assert(board
->clen
< board_size2(board
));
746 board
->c
[board
->clen
++] = group
;
750 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
752 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
754 /* Decrease capturable count trait of my previously-last lib. */
755 enum stone capturing_color
= stone_other(board_at(board
, group
));
756 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
757 foreach_neighbor(board
, lib
, {
758 if (DEBUGL(8) && group_at(board
, c
) == group
)
759 fprintf(stderr
, "%s[%d] cap dump bc of %s(%d) member %s onestone %d\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
, coord2sstr(group
, board
), board_group_info(board
, group
).libs
, coord2sstr(c
, board
), onestone
);
760 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
761 trait_at(board
, lib
, capturing_color
).cap1
-= (group_at(board
, c
) == group
&& onestone
);
763 board_trait_queue(board
, lib
);
767 /* Update the list of capturable groups. */
768 for (int i
= 0; i
< board
->clen
; i
++) {
769 if (unlikely(board
->c
[i
] == group
)) {
770 board
->c
[i
] = board
->c
[--board
->clen
];
774 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
780 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
783 board_trait_queue(board
, lib1
);
784 board_trait_queue(board
, lib2
);
788 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
791 board_trait_queue(board
, lib1
);
792 board_trait_queue(board
, lib2
);
797 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
800 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
801 group_base(group
), coord2sstr(group_base(group
), board
),
802 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
805 check_libs_consistency(board
, group
);
807 struct group
*gi
= &board_group_info(board
, group
);
808 bool onestone
= group_is_onestone(board
, group
);
809 if (gi
->libs
< GROUP_KEEP_LIBS
) {
810 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
812 /* Seems extra branch just slows it down */
816 if (unlikely(gi
->lib
[i
] == coord
))
820 board_capturable_add(board
, group
, coord
, onestone
);
821 } else if (gi
->libs
== 1) {
822 board_capturable_rm(board
, group
, gi
->lib
[0], onestone
);
823 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
824 } else if (gi
->libs
== 2) {
825 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
827 gi
->lib
[gi
->libs
++] = coord
;
830 check_libs_consistency(board
, group
);
834 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
836 /* Add extra liberty from the board to our liberty list. */
837 unsigned char watermark
[board_size2(board
) / 8];
838 memset(watermark
, 0, sizeof(watermark
));
839 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
840 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
842 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
843 watermark_set(gi
->lib
[i
]);
844 watermark_set(avoid
);
846 foreach_in_group(board
, group
) {
848 foreach_neighbor(board
, coord2
, {
849 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
852 gi
->lib
[gi
->libs
++] = c
;
853 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
856 } foreach_in_group_end
;
862 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
865 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
866 group_base(group
), coord2sstr(group_base(group
), board
),
867 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
870 struct group
*gi
= &board_group_info(board
, group
);
871 bool onestone
= group_is_onestone(board
, group
);
872 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
874 /* Seems extra branch just slows it down */
878 if (likely(gi
->lib
[i
] != coord
))
881 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
882 gi
->lib
[gi
->libs
] = 0;
884 check_libs_consistency(board
, group
);
886 /* Postpone refilling lib[] until we need to. */
887 assert(GROUP_REFILL_LIBS
> 1);
888 if (gi
->libs
> GROUP_REFILL_LIBS
)
890 if (gi
->libs
== GROUP_REFILL_LIBS
)
891 board_group_find_extra_libs(board
, group
, gi
, coord
);
894 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
895 } else if (gi
->libs
== 1) {
896 board_capturable_add(board
, group
, gi
->lib
[0], onestone
);
897 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
898 } else if (gi
->libs
== 0)
899 board_capturable_rm(board
, group
, lib
, onestone
);
903 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
904 * can call this multiple times per coord. */
905 check_libs_consistency(board
, group
);
910 /* This is a low-level routine that doesn't maintain consistency
911 * of all the board data structures. */
913 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
915 enum stone color
= board_at(board
, c
);
916 board_at(board
, c
) = S_NONE
;
917 group_at(board
, c
) = 0;
918 board_hash_update(board
, c
, color
);
920 /* We mark as cannot-capture now. If this is a ko/snapback,
921 * we will get incremented later in board_group_addlib(). */
922 trait_at(board
, c
, S_BLACK
).cap
= trait_at(board
, c
, S_BLACK
).cap1
= 0;
923 trait_at(board
, c
, S_WHITE
).cap
= trait_at(board
, c
, S_WHITE
).cap1
= 0;
924 board_trait_queue(board
, c
);
927 /* Increase liberties of surrounding groups */
929 foreach_neighbor(board
, coord
, {
930 dec_neighbor_count_at(board
, c
, color
);
931 board_trait_queue(board
, c
);
932 group_t g
= group_at(board
, c
);
934 board_group_addlib(board
, g
, coord
);
938 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
939 board
->f
[board
->flen
++] = c
;
942 static int profiling_noinline
943 board_group_capture(struct board
*board
, group_t group
)
947 foreach_in_group(board
, group
) {
948 board
->captures
[stone_other(board_at(board
, c
))]++;
949 board_remove_stone(board
, group
, c
);
951 } foreach_in_group_end
;
953 struct group
*gi
= &board_group_info(board
, group
);
954 assert(gi
->libs
== 0);
955 memset(gi
, 0, sizeof(*gi
));
961 static void profiling_noinline
962 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
965 struct group
*gi
= &board_group_info(board
, group
);
966 bool onestone
= group_is_onestone(board
, group
);
969 /* Our group is temporarily in atari; make sure the capturable
970 * counts also correspond to the newly added stone before we
971 * start adding liberties again so bump-dump ops match. */
972 enum stone capturing_color
= stone_other(board_at(board
, group
));
973 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
975 coord_t lib
= board_group_info(board
, group
).lib
[0];
976 if (coord_is_adjecent(lib
, coord
, board
)) {
978 fprintf(stderr
, "add_to_group %s: %s[%d] bump\n", coord2sstr(group
, board
), coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
979 trait_at(board
, lib
, capturing_color
).cap
++;
980 /* This is never a 1-stone group, obviously. */
981 board_trait_queue(board
, lib
);
985 /* We are not 1-stone group anymore, update the cap1
986 * counter specifically. */
987 foreach_neighbor(board
, group
, {
988 if (board_at(board
, c
) != S_NONE
) continue;
989 trait_at(board
, c
, capturing_color
).cap1
--;
990 board_trait_queue(board
, c
);
996 group_at(board
, coord
) = group
;
997 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
998 groupnext_at(board
, prevstone
) = coord
;
1000 foreach_neighbor(board
, coord
, {
1001 if (board_at(board
, c
) == S_NONE
)
1002 board_group_addlib(board
, group
, c
);
1006 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1007 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
1008 coord_x(coord
, board
), coord_y(coord
, board
),
1009 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
1013 static void profiling_noinline
1014 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
1017 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
1018 group_base(group_from
), group_base(group_to
));
1019 struct group
*gi_from
= &board_group_info(board
, group_from
);
1020 struct group
*gi_to
= &board_group_info(board
, group_to
);
1021 bool onestone_from
= group_is_onestone(board
, group_from
);
1022 bool onestone_to
= group_is_onestone(board
, group_to
);
1024 /* We do this early before the group info is rewritten. */
1025 if (gi_from
->libs
== 2)
1026 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
1027 else if (gi_from
->libs
== 1)
1028 board_capturable_rm(board
, group_from
, gi_from
->lib
[0], onestone_from
);
1031 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
1033 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
1034 for (int i
= 0; i
< gi_from
->libs
; i
++) {
1035 for (int j
= 0; j
< gi_to
->libs
; j
++)
1036 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
1038 if (gi_to
->libs
== 0) {
1039 board_capturable_add(board
, group_to
, gi_from
->lib
[i
], onestone_to
);
1040 } else if (gi_to
->libs
== 1) {
1041 board_capturable_rm(board
, group_to
, gi_to
->lib
[0], onestone_to
);
1042 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
1043 } else if (gi_to
->libs
== 2) {
1044 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
1046 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
1047 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
1054 if (gi_to
->libs
== 1) {
1055 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
1056 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1058 /* Our group is currently in atari; make sure we properly
1059 * count in even the neighbors from the other group in the
1060 * capturable counter. */
1061 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
1062 foreach_neighbor(board
, lib
, {
1063 if (DEBUGL(8) && group_at(board
, c
) == group_from
)
1064 fprintf(stderr
, "%s[%d] cap bump\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
1065 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
1066 /* This is never a 1-stone group, obviously. */
1068 board_trait_queue(board
, lib
);
1071 /* We are not 1-stone group anymore, update the cap1
1072 * counter specifically. */
1073 foreach_neighbor(board
, group_to
, {
1074 if (board_at(board
, c
) != S_NONE
) continue;
1075 trait_at(board
, c
, capturing_color
).cap1
--;
1076 board_trait_queue(board
, c
);
1082 coord_t last_in_group
;
1083 foreach_in_group(board
, group_from
) {
1085 group_at(board
, c
) = group_to
;
1086 } foreach_in_group_end
;
1087 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1088 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1089 memset(gi_from
, 0, sizeof(struct group
));
1092 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1093 group_base(group_to
));
1096 static group_t profiling_noinline
1097 new_group(struct board
*board
, coord_t coord
)
1099 group_t group
= coord
;
1100 struct group
*gi
= &board_group_info(board
, group
);
1101 foreach_neighbor(board
, coord
, {
1102 if (board_at(board
, c
) == S_NONE
)
1103 /* board_group_addlib is ridiculously expensive for us */
1104 #if GROUP_KEEP_LIBS < 4
1105 if (gi
->libs
< GROUP_KEEP_LIBS
)
1107 gi
->lib
[gi
->libs
++] = c
;
1110 group_at(board
, coord
) = group
;
1111 groupnext_at(board
, coord
) = 0;
1114 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1115 else if (gi
->libs
== 1)
1116 board_capturable_add(board
, group
, gi
->lib
[0], true);
1117 check_libs_consistency(board
, group
);
1120 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1121 coord_x(coord
, board
), coord_y(coord
, board
),
1127 static inline group_t
1128 play_one_neighbor(struct board
*board
,
1129 coord_t coord
, enum stone color
, enum stone other_color
,
1130 coord_t c
, group_t group
)
1132 enum stone ncolor
= board_at(board
, c
);
1133 group_t ngroup
= group_at(board
, c
);
1135 inc_neighbor_count_at(board
, c
, color
);
1136 /* We can be S_NONE, in that case we need to update the safety
1137 * trait since we might be left with only one liberty. */
1138 board_trait_queue(board
, c
);
1143 board_group_rmlib(board
, ngroup
, coord
);
1145 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1146 group_base(ngroup
), ncolor
, color
, other_color
);
1148 if (ncolor
== color
&& ngroup
!= group
) {
1151 add_to_group(board
, group
, c
, coord
);
1153 merge_groups(board
, group
, ngroup
);
1155 } else if (ncolor
== other_color
) {
1157 struct group
*gi
= &board_group_info(board
, ngroup
);
1158 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1159 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1160 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1161 fprintf(stderr
, "\n");
1163 if (unlikely(board_group_captured(board
, ngroup
)))
1164 board_group_capture(board
, ngroup
);
1169 /* We played on a place with at least one liberty. We will become a member of
1170 * some group for sure. */
1171 static group_t profiling_noinline
1172 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1174 coord_t coord
= m
->coord
;
1175 enum stone color
= m
->color
;
1176 enum stone other_color
= stone_other(color
);
1179 board
->f
[f
] = board
->f
[--board
->flen
];
1181 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1183 #if defined(BOARD_TRAITS) && defined(DEBUG)
1184 /* Sanity check that cap matches reality. */
1187 foreach_neighbor(board
, coord
, {
1188 group_t g
= group_at(board
, c
);
1189 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1190 b
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1) && group_is_onestone(board
, g
);
1192 assert(a
== trait_at(board
, coord
, color
).cap
);
1193 assert(b
== trait_at(board
, coord
, color
).cap1
);
1194 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1197 foreach_neighbor(board
, coord
, {
1198 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1201 board_at(board
, coord
) = color
;
1202 if (unlikely(!group
))
1203 group
= new_group(board
, coord
);
1204 board_gamma_update(board
, coord
, S_BLACK
);
1205 board_gamma_update(board
, coord
, S_WHITE
);
1207 board
->last_move2
= board
->last_move
;
1208 board
->last_move
= *m
;
1210 board_hash_update(board
, coord
, color
);
1211 board_symmetry_update(board
, &board
->symmetry
, coord
);
1212 struct move ko
= { pass
, S_NONE
};
1218 /* We played in an eye-like shape. Either we capture at least one of the eye
1219 * sides in the process of playing, or return -1. */
1220 static int profiling_noinline
1221 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1223 coord_t coord
= m
->coord
;
1224 enum stone color
= m
->color
;
1225 /* Check ko: Capture at a position of ko capture one move ago */
1226 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1228 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1230 } else if (DEBUGL(6)) {
1231 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1232 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1233 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1236 struct move ko
= { pass
, S_NONE
};
1238 int captured_groups
= 0;
1240 foreach_neighbor(board
, coord
, {
1241 group_t g
= group_at(board
, c
);
1243 fprintf(stderr
, "board_check: group %d has %d libs\n",
1244 g
, board_group_info(board
, g
).libs
);
1245 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1248 if (likely(captured_groups
== 0)) {
1251 board_print(board
, stderr
);
1252 fprintf(stderr
, "board_check: one-stone suicide\n");
1258 /* We _will_ for sure capture something. */
1259 assert(trait_at(board
, coord
, color
).cap
> 0);
1260 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1263 board
->f
[f
] = board
->f
[--board
->flen
];
1265 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1267 foreach_neighbor(board
, coord
, {
1268 inc_neighbor_count_at(board
, c
, color
);
1269 /* Originally, this could not have changed any trait
1270 * since no neighbors were S_NONE, however by now some
1271 * of them might be removed from the board. */
1272 board_trait_queue(board
, c
);
1274 group_t group
= group_at(board
, c
);
1278 board_group_rmlib(board
, group
, coord
);
1280 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1283 if (board_group_captured(board
, group
)) {
1284 if (board_group_capture(board
, group
) == 1) {
1285 /* If we captured multiple groups at once,
1286 * we can't be fighting ko so we don't need
1287 * to check for that. */
1288 ko
.color
= stone_other(color
);
1290 board
->last_ko
= ko
;
1291 board
->last_ko_age
= board
->moves
;
1293 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1298 board_at(board
, coord
) = color
;
1299 group_t group
= new_group(board
, coord
);
1300 board_gamma_update(board
, coord
, S_BLACK
);
1301 board_gamma_update(board
, coord
, S_WHITE
);
1303 board
->last_move2
= board
->last_move
;
1304 board
->last_move
= *m
;
1306 board_hash_update(board
, coord
, color
);
1307 board_hash_commit(board
);
1308 board_traits_recompute(board
);
1309 board_symmetry_update(board
, &board
->symmetry
, coord
);
1315 static int __attribute__((flatten
))
1316 board_play_f(struct board
*board
, struct move
*m
, int f
)
1319 fprintf(stderr
, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m
->coord
, board
), coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1321 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1322 /* NOT playing in an eye. Thus this move has to succeed. (This
1323 * is thanks to New Zealand rules. Otherwise, multi-stone
1324 * suicide might fail.) */
1325 group_t group
= board_play_outside(board
, m
, f
);
1326 if (unlikely(board_group_captured(board
, group
))) {
1327 board_group_capture(board
, group
);
1329 board_hash_commit(board
);
1330 board_traits_recompute(board
);
1333 return board_play_in_eye(board
, m
, f
);
1338 board_play(struct board
*board
, struct move
*m
)
1340 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1341 struct move nomove
= { pass
, S_NONE
};
1343 board
->last_move2
= board
->last_move
;
1344 board
->last_move
= *m
;
1349 for (f
= 0; f
< board
->flen
; f
++)
1350 if (board
->f
[f
] == m
->coord
)
1351 return board_play_f(board
, m
, f
);
1354 fprintf(stderr
, "board_check: stone exists\n");
1360 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1363 struct move m
= { *coord
, color
};
1365 fprintf(stderr
, "trying random move %d: %d,%d %s %d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
), coord2sstr(*coord
, b
), board_is_valid_move(b
, &m
));
1366 if (unlikely(board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1367 || !board_is_valid_move(b
, &m
)
1368 || (permit
&& !permit(permit_data
, b
, &m
)))
1370 if (m
.coord
== *coord
) {
1371 return likely(board_play_f(b
, &m
, f
) >= 0);
1373 *coord
= m
.coord
; // permit modified the coordinate
1374 return likely(board_play(b
, &m
) >= 0);
1379 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1381 if (unlikely(b
->flen
== 0))
1384 int base
= fast_random(b
->flen
), f
;
1385 for (f
= base
; f
< b
->flen
; f
++)
1386 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1388 for (f
= 0; f
< base
; f
++)
1389 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1394 struct move m
= { pass
, color
};
1400 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1402 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1404 /* XXX: We attempt false eye detection but we will yield false
1405 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1407 foreach_diag_neighbor(board
, coord
) {
1408 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1409 } foreach_diag_neighbor_end
;
1410 /* For false eye, we need two enemy stones diagonally in the
1411 * middle of the board, or just one enemy stone at the edge
1412 * or in the corner. */
1413 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1414 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1418 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1420 return board_is_eyelike(board
, coord
, eye_color
)
1421 && !board_is_false_eyelike(board
, coord
, eye_color
);
1425 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1427 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1429 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1437 board_fast_score(struct board
*board
)
1440 memset(scores
, 0, sizeof(scores
));
1442 foreach_point(board
) {
1443 enum stone color
= board_at(board
, c
);
1444 if (color
== S_NONE
)
1445 color
= board_get_one_point_eye(board
, c
);
1447 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1448 } foreach_point_end
;
1450 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1453 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1455 /* One flood-fill iteration; returns true if next iteration
1458 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1460 bool needs_update
= false;
1461 foreach_free_point(board
) {
1462 /* Ignore occupied and already-dame positions. */
1463 assert(board_at(board
, c
) == S_NONE
);
1464 if (ownermap
[c
] == 3)
1466 /* Count neighbors. */
1468 foreach_neighbor(board
, c
, {
1471 /* If we have neighbors of both colors, or dame,
1472 * we are dame too. */
1473 if ((nei
[1] && nei
[2]) || nei
[3]) {
1475 /* Speed up the propagation. */
1476 foreach_neighbor(board
, c
, {
1477 if (board_at(board
, c
) == S_NONE
)
1480 needs_update
= true;
1483 /* If we have neighbors of one color, we are owned
1484 * by that color, too. */
1485 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1486 int newowner
= nei
[1] ? 1 : 2;
1487 ownermap
[c
] = newowner
;
1488 /* Speed up the propagation. */
1489 foreach_neighbor(board
, c
, {
1490 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1491 ownermap
[c
] = newowner
;
1493 needs_update
= true;
1496 } foreach_free_point_end
;
1497 return needs_update
;
1500 /* Tromp-Taylor Counting */
1502 board_official_score(struct board
*board
, struct move_queue
*q
)
1505 /* A point P, not colored C, is said to reach C, if there is a path of
1506 * (vertically or horizontally) adjacent points of P's color from P to
1507 * a point of color C.
1509 * A player's score is the number of points of her color, plus the
1510 * number of empty points that reach only her color. */
1512 int ownermap
[board_size2(board
)];
1514 const int o
[4] = {0, 1, 2, 0};
1515 foreach_point(board
) {
1516 ownermap
[c
] = o
[board_at(board
, c
)];
1517 s
[board_at(board
, c
)]++;
1518 } foreach_point_end
;
1521 /* Process dead groups. */
1522 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
1523 foreach_in_group(board
, q
->move
[i
]) {
1524 enum stone color
= board_at(board
, c
);
1525 ownermap
[c
] = o
[stone_other(color
)];
1526 s
[color
]--; s
[stone_other(color
)]++;
1527 } foreach_in_group_end
;
1531 /* We need to special-case empty board. */
1532 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1533 return board
->komi
+ board
->handicap
;
1535 while (board_tromp_taylor_iter(board
, ownermap
))
1536 /* Flood-fill... */;
1539 memset(scores
, 0, sizeof(scores
));
1541 foreach_point(board
) {
1542 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1543 if (ownermap
[c
] == 3)
1545 scores
[ownermap
[c
]]++;
1546 } foreach_point_end
;
1548 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];