16 #include "patternsp.h"
22 static void board_trait_recompute(struct board
*board
, coord_t coord
);
23 #include "tactics/selfatari.h"
31 #define profiling_noinline __attribute__((noinline))
33 #define profiling_noinline
36 #define gi_granularity 4
37 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
41 board_setup(struct board
*b
)
43 char *fbookfile
= b
->fbookfile
;
45 memset(b
, 0, sizeof(*b
));
47 b
->fbookfile
= fbookfile
;
49 struct move m
= { pass
, S_NONE
};
50 b
->last_move
= b
->last_move2
= b
->last_ko
= b
->ko
= m
;
54 board_init(char *fbookfile
)
56 struct board
*b
= malloc2(sizeof(struct board
));
59 b
->fbookfile
= fbookfile
;
69 board_alloc(struct board
*board
)
71 /* We do not allocate the board structure itself but we allocate
72 * all the arrays with board contents. */
74 int bsize
= board_size2(board
) * sizeof(*board
->b
);
75 int gsize
= board_size2(board
) * sizeof(*board
->g
);
76 int fsize
= board_size2(board
) * sizeof(*board
->f
);
77 int nsize
= board_size2(board
) * sizeof(*board
->n
);
78 int psize
= board_size2(board
) * sizeof(*board
->p
);
79 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
80 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
82 int csize
= board_size2(board
) * sizeof(*board
->c
);
87 int ssize
= board_size2(board
) * sizeof(*board
->spathash
);
92 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
97 int tsize
= board_size2(board
) * sizeof(*board
->t
);
98 int tqsize
= board_size2(board
) * sizeof(*board
->t
);
104 int pbsize
= board_size2(board
) * sizeof(*board
->prob
[0].items
);
105 int rowpbsize
= board_size(board
) * sizeof(*board
->prob
[0].rowtotals
);
110 int cdsize
= board_size2(board
) * sizeof(*board
->coord
);
112 size_t size
= bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ (pbsize
+ rowpbsize
) * 2 + cdsize
;
113 void *x
= malloc2(size
);
115 /* board->b must come first */
116 board
->b
= x
; x
+= bsize
;
117 board
->g
= x
; x
+= gsize
;
118 board
->f
= x
; x
+= fsize
;
119 board
->p
= x
; x
+= psize
;
120 board
->n
= x
; x
+= nsize
;
121 board
->h
= x
; x
+= hsize
;
122 board
->gi
= x
; x
+= gisize
;
124 board
->c
= x
; x
+= csize
;
126 #ifdef BOARD_SPATHASH
127 board
->spathash
= x
; x
+= ssize
;
130 board
->pat3
= x
; x
+= p3size
;
133 board
->t
= x
; x
+= tsize
;
134 board
->tq
= x
; x
+= tqsize
;
137 board
->prob
[0].items
= x
; x
+= pbsize
;
138 board
->prob
[1].items
= x
; x
+= pbsize
;
139 board
->prob
[0].rowtotals
= x
; x
+= rowpbsize
;
140 board
->prob
[1].rowtotals
= x
; x
+= rowpbsize
;
142 board
->coord
= x
; x
+= cdsize
;
148 board_copy(struct board
*b2
, struct board
*b1
)
150 memcpy(b2
, b1
, sizeof(struct board
));
152 size_t size
= board_alloc(b2
);
153 memcpy(b2
->b
, b1
->b
, size
);
155 // XXX: Special semantics.
162 board_done_noalloc(struct board
*board
)
164 if (board
->b
) free(board
->b
);
165 if (board
->fbook
) fbook_done(board
->fbook
);
169 board_done(struct board
*board
)
171 board_done_noalloc(board
);
176 board_resize(struct board
*board
, int size
)
179 assert(board_size(board
) == size
+ 2);
181 assert(size
<= BOARD_MAX_SIZE
);
182 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
183 board
->size2
= board_size(board
) * board_size(board
);
186 while ((1 << board
->bits2
) < board
->size2
) board
->bits2
++;
191 size_t asize
= board_alloc(board
);
192 memset(board
->b
, 0, asize
);
196 board_clear(struct board
*board
)
198 int size
= board_size(board
);
199 float komi
= board
->komi
;
201 board_done_noalloc(board
);
203 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
207 /* Setup neighborhood iterators */
208 board
->nei8
[0] = -size
- 1; // (-1,-1)
211 board
->nei8
[3] = size
- 2; // (-1,0)
213 board
->nei8
[5] = size
- 2; // (-1,1)
216 board
->dnei
[0] = -size
- 1;
218 board
->dnei
[2] = size
*2 - 2;
221 /* Setup initial symmetry */
222 board
->symmetry
.d
= 1;
223 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
224 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
225 board
->symmetry
.type
= SYM_FULL
;
227 /* Set up coordinate cache */
228 foreach_point(board
) {
229 board
->coord
[c
][0] = c
% board_size(board
);
230 board
->coord
[c
][1] = c
/ board_size(board
);
233 /* Draw the offboard margin */
234 int top_row
= board_size2(board
) - board_size(board
);
236 for (i
= 0; i
< board_size(board
); i
++)
237 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
238 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
239 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
241 foreach_point(board
) {
243 if (board_at(board
, coord
) == S_OFFBOARD
)
245 foreach_neighbor(board
, c
, {
246 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
250 /* All positions are free! Except the margin. */
251 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
252 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
253 board
->f
[board
->flen
++] = i
;
255 /* Initialize zobrist hashtable. */
256 /* We will need these to be stable across Pachi runs for
257 * certain kinds of pattern matching, thus we do not use
258 * fast_random() for this. */
259 hash_t hseed
= 0x3121110101112131;
260 foreach_point(board
) {
261 board
->h
[c
* 2] = (hseed
*= 16807);
262 if (!board
->h
[c
* 2])
264 /* And once again for white */
265 board
->h
[c
* 2 + 1] = (hseed
*= 16807);
266 if (!board
->h
[c
* 2 + 1])
267 board
->h
[c
* 2 + 1] = 1;
270 #ifdef BOARD_SPATHASH
271 /* Initialize spatial hashes. */
272 foreach_point(board
) {
273 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
274 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
275 ptcoords_at(x
, y
, c
, board
, j
);
276 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
277 pthashes
[0][j
][board_at(board
, c
)];
278 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
279 pthashes
[0][j
][stone_other(board_at(board
, c
))];
285 /* Initialize 3x3 pattern codes. */
286 foreach_point(board
) {
287 if (board_at(board
, c
) == S_NONE
)
288 board
->pat3
[c
] = pattern3_hash(board
, c
);
292 /* Initialize traits. */
293 foreach_point(board
) {
294 trait_at(board
, c
, S_BLACK
).cap
= 0;
295 trait_at(board
, c
, S_BLACK
).cap1
= 0;
296 trait_at(board
, c
, S_BLACK
).safe
= true;
297 trait_at(board
, c
, S_WHITE
).cap
= 0;
298 trait_at(board
, c
, S_WHITE
).cap1
= 0;
299 trait_at(board
, c
, S_WHITE
).safe
= true;
303 board
->prob
[0].b
= board
->prob
[1].b
= board
;
304 foreach_point(board
) {
305 probdist_set(&board
->prob
[0], c
, double_to_fixp((board_at(board
, c
) == S_NONE
) * 1.0f
));
306 probdist_set(&board
->prob
[1], c
, double_to_fixp((board_at(board
, c
) == S_NONE
) * 1.0f
));
310 if (board
->fbookfile
) {
311 board
->fbook
= fbook_init(board
->fbookfile
, board
);
316 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
318 for (int i
= 0; i
< c
; i
++) {
319 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
320 s
+= snprintf(s
, end
- s
, " ");
321 for (int x
= 1; x
< board_size(board
) - 1; x
++)
322 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
323 s
+= snprintf(s
, end
-s
, " ");
325 s
+= snprintf(s
, end
- s
, "\n");
326 for (int i
= 0; i
< c
; i
++) {
327 s
+= snprintf(s
, end
- s
, " +-");
328 for (int x
= 1; x
< board_size(board
) - 1; x
++)
329 s
+= snprintf(s
, end
- s
, "--");
330 s
+= snprintf(s
, end
- s
, "+");
332 s
+= snprintf(s
, end
- s
, "\n");
337 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
339 for (int i
= 0; i
< c
; i
++) {
340 s
+= snprintf(s
, end
- s
, " +-");
341 for (int x
= 1; x
< board_size(board
) - 1; x
++)
342 s
+= snprintf(s
, end
- s
, "--");
343 s
+= snprintf(s
, end
- s
, "+");
345 s
+= snprintf(s
, end
- s
, "\n");
350 board_print_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
)
352 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
353 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
354 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
355 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
357 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
359 s
+= snprintf(s
, end
- s
, "|");
361 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
362 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
363 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
);
365 s
+= snprintf(s
, end
- s
, "|");
367 s
+= snprintf(s
, end
- s
, "\n");
372 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
376 char *end
= buf
+ sizeof(buf
);
377 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
378 board
->moves
, board
->komi
, board
->handicap
,
379 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
380 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
381 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
382 s
= board_print_row(board
, y
, s
, end
, cprint
);
383 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
384 fprintf(f
, "%s\n", buf
);
388 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
)
390 s
+= snprintf(s
, end
- s
, "%d ", group_base(group_at(board
, c
)));
395 board_print(struct board
*board
, FILE *f
)
397 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
401 board_gamma_set(struct board
*b
, struct features_gamma
*gamma
, bool precise_selfatari
)
405 b
->precise_selfatari
= precise_selfatari
;
407 for (int i
= 0; i
< b
->flen
; i
++) {
408 board_trait_recompute(b
, b
->f
[i
]);
415 /* Update the probability distribution we maintain incrementally. */
417 board_gamma_update(struct board
*board
, coord_t coord
, enum stone color
)
419 #if defined(BOARD_GAMMA) && defined(BOARD_TRAITS)
423 /* Punch out invalid moves and moves filling our own eyes. */
424 if (board_at(board
, coord
) != S_NONE
425 || (board_is_eyelike(board
, coord
, stone_other(color
))
426 && !trait_at(board
, coord
, color
).cap
)
427 || (board_is_one_point_eye(board
, coord
, color
))) {
428 probdist_set(&board
->prob
[color
- 1], coord
, 0);
432 hash3_t pat
= board
->pat3
[coord
];
433 if (color
== S_WHITE
) {
434 /* We work with the pattern3s as black-to-play. */
435 pat
= pattern3_reverse(pat
);
438 /* We just quickly replicate the general pattern matcher stuff
439 * here in the most bare-bone way. */
440 double value
= board
->gamma
->gamma
[FEAT_PATTERN3
][pat
];
441 if (trait_at(board
, coord
, color
).cap
) {
443 i
|= (trait_at(board
, coord
, color
).cap1
== trait_at(board
, coord
, color
).cap
) << PF_CAPTURE_1STONE
;
444 i
|= (!trait_at(board
, coord
, stone_other(color
)).safe
) << PF_CAPTURE_TRAPPED
;
445 i
|= (trait_at(board
, coord
, color
).cap
< neighbor_count_at(board
, coord
, stone_other(color
))) << PF_CAPTURE_CONNECTION
;
446 value
*= board
->gamma
->gamma
[FEAT_CAPTURE
][i
];
448 if (trait_at(board
, coord
, stone_other(color
)).cap
) {
450 i
|= (trait_at(board
, coord
, stone_other(color
)).cap1
== trait_at(board
, coord
, stone_other(color
)).cap
) << PF_AESCAPE_1STONE
;
451 i
|= (!trait_at(board
, coord
, color
).safe
) << PF_AESCAPE_TRAPPED
;
452 i
|= (trait_at(board
, coord
, stone_other(color
)).cap
< neighbor_count_at(board
, coord
, color
)) << PF_AESCAPE_CONNECTION
;
453 value
*= board
->gamma
->gamma
[FEAT_AESCAPE
][i
];
455 if (!trait_at(board
, coord
, color
).safe
)
456 value
*= board
->gamma
->gamma
[FEAT_SELFATARI
][1 + board
->precise_selfatari
];
457 probdist_set(&board
->prob
[color
- 1], coord
, double_to_fixp(value
));
463 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
465 if (board
->precise_selfatari
)
466 return !is_bad_selfatari(board
, color
, coord
);
468 return board_safe_to_play(board
, coord
, color
);
472 board_trait_recompute(struct board
*board
, coord_t coord
)
474 trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);;
475 trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
477 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
478 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
479 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).cap1
, trait_at(board
, coord
, S_BLACK
).safe
,
480 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).cap1
, trait_at(board
, coord
, S_WHITE
).safe
);
482 board_gamma_update(board
, coord
, S_BLACK
);
483 board_gamma_update(board
, coord
, S_WHITE
);
487 /* Recompute traits for dirty points that we have previously touched
488 * somehow (libs of their neighbors changed or so). */
490 board_traits_recompute(struct board
*board
)
493 for (int i
= 0; i
< board
->tqlen
; i
++) {
494 coord_t coord
= board
->tq
[i
];
495 trait_at(board
, coord
, S_BLACK
).dirty
= false;
496 if (board_at(board
, coord
) != S_NONE
)
498 board_trait_recompute(board
, coord
);
504 /* Queue traits of given point for recomputing. */
506 board_trait_queue(struct board
*board
, coord_t coord
)
509 if (trait_at(board
, coord
, S_BLACK
).dirty
)
511 board
->tq
[board
->tqlen
++] = coord
;
512 trait_at(board
, coord
, S_BLACK
).dirty
= true;
517 /* Update board hash with given coordinate. */
518 static void profiling_noinline
519 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
521 board
->hash
^= hash_at(board
, coord
, color
);
522 board
->qhash
[coord_quadrant(coord
, board
)] ^= hash_at(board
, coord
, color
);
524 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
);
526 #ifdef BOARD_SPATHASH
527 /* Gridcular metric is reflective, so we update all hashes
528 * of appropriate ditance in OUR circle. */
529 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
530 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
531 ptcoords_at(x
, y
, coord
, board
, j
);
532 /* We either changed from S_NONE to color
533 * or vice versa; doesn't matter. */
534 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
535 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
536 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
537 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
542 #if defined(BOARD_PAT3)
543 /* @color is not what we need in case of capture. */
544 static const int ataribits
[8] = { -1, 0, -1, 1, 2, -1, 3, -1 };
545 enum stone new_color
= board_at(board
, coord
);
546 bool in_atari
= false;
547 if (new_color
== S_NONE
) {
548 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
550 in_atari
= (board_group_info(board
, group_at(board
, coord
)).libs
== 1);
552 foreach_8neighbor(board
, coord
) {
553 /* Internally, the loop uses fn__i=[0..7]. We can use
554 * it directly to address bits within the bitmap of the
555 * neighbors since the bitmap order is reverse to the
557 if (board_at(board
, c
) != S_NONE
)
559 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
560 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
561 if (ataribits
[fn__i
] >= 0) {
562 board
->pat3
[c
] &= ~(1 << (16 + ataribits
[fn__i
]));
563 board
->pat3
[c
] |= in_atari
<< (16 + ataribits
[fn__i
]);
565 #if defined(BOARD_TRAITS)
566 board_trait_queue(board
, c
);
567 #elif defined(BOARD_GAMMA)
569 hash3_t pat
= board
->pat3
[c
];
570 if (color
== S_WHITE
) pat
= pattern3_reverse(pat
);
571 double value
= board
->gamma
->gamma
[FEAT_PATTERN3
][pat
];
572 probdist_set(&board
->prob
[color
- 1], c
, double_to_fixp(value
));
575 } foreach_8neighbor_end
;
579 /* Commit current board hash to history. */
580 static void profiling_noinline
581 board_hash_commit(struct board
*board
)
584 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
585 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
586 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
588 hash_t i
= board
->hash
;
589 while (board
->history_hash
[i
& history_hash_mask
]) {
590 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
592 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
593 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
594 board
->superko_violation
= true;
597 i
= history_hash_next(i
);
599 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
605 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
607 if (likely(symmetry
->type
== SYM_NONE
)) {
608 /* Fully degenerated already. We do not support detection
609 * of restoring of symmetry, assuming that this is too rare
610 * a case to handle. */
614 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
615 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
617 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
618 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
619 symmetry
->d
, symmetry
->type
, x
, y
);
622 switch (symmetry
->type
) {
624 if (x
== t
&& y
== t
) {
625 /* Tengen keeps full symmetry. */
628 /* New symmetry now? */
630 symmetry
->type
= SYM_DIAG_UP
;
631 symmetry
->x1
= symmetry
->y1
= 1;
632 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
634 } else if (dx
== y
) {
635 symmetry
->type
= SYM_DIAG_DOWN
;
636 symmetry
->x1
= symmetry
->y1
= 1;
637 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
640 symmetry
->type
= SYM_HORIZ
;
642 symmetry
->y2
= board_size(b
) - 1;
645 symmetry
->type
= SYM_VERT
;
647 symmetry
->x2
= board_size(b
) - 1;
651 symmetry
->type
= SYM_NONE
;
652 symmetry
->x1
= symmetry
->y1
= 1;
653 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
679 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
680 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
681 symmetry
->d
, symmetry
->type
);
688 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
691 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
693 board_play(board
, &m
);
694 /* Simulate white passing; otherwise, UCT search can get confused since
695 * tree depth parity won't match the color to move. */
698 char *str
= coord2str(m
.coord
, board
);
700 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
701 if (f
) fprintf(f
, "%s ", str
);
706 board_handicap(struct board
*board
, int stones
, FILE *f
)
708 int margin
= 3 + (board_size(board
) >= 13);
710 int mid
= board_size(board
) / 2;
711 int max
= board_size(board
) - 1 - margin
;
712 const int places
[][2] = {
713 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
714 { min
, mid
}, { max
, mid
},
715 { mid
, min
}, { mid
, max
},
719 board
->handicap
= stones
;
721 if (stones
== 5 || stones
== 7) {
722 board_handicap_stone(board
, mid
, mid
, f
);
727 for (i
= 0; i
< stones
; i
++)
728 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
732 static void __attribute__((noinline
))
733 check_libs_consistency(struct board
*board
, group_t g
)
737 struct group
*gi
= &board_group_info(board
, g
);
738 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
739 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
740 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
747 check_pat3_consistency(struct board
*board
, coord_t coord
)
750 foreach_8neighbor(board
, coord
) {
751 if (board_at(board
, c
) == S_NONE
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
752 board_print(board
, stderr
);
753 fprintf(stderr
, "%s(%d)->%s(%d) computed %x != stored %x (%d)\n", coord2sstr(coord
, board
), coord
, coord2sstr(c
, board
), c
, pattern3_hash(board
, c
), board
->pat3
[c
], fn__i
);
756 } foreach_8neighbor_end
;
761 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
763 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
765 /* Increase capturable count trait of my last lib. */
766 enum stone capturing_color
= stone_other(board_at(board
, group
));
767 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
768 foreach_neighbor(board
, lib
, {
769 if (DEBUGL(8) && group_at(board
, c
) == group
)
770 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
);
771 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
772 trait_at(board
, lib
, capturing_color
).cap1
+= (group_at(board
, c
) == group
&& onestone
);
774 board_trait_queue(board
, lib
);
779 foreach_neighbor(board
, lib
, {
780 board
->pat3
[lib
] |= (group_at(board
, c
) == group
) << (16 + 3 - fn__i
);
786 /* Update the list of capturable groups. */
788 assert(board
->clen
< board_size2(board
));
789 board
->c
[board
->clen
++] = group
;
793 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
795 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
797 /* Decrease capturable count trait of my previously-last lib. */
798 enum stone capturing_color
= stone_other(board_at(board
, group
));
799 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
800 foreach_neighbor(board
, lib
, {
801 if (DEBUGL(8) && group_at(board
, c
) == group
)
802 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
);
803 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
804 trait_at(board
, lib
, capturing_color
).cap1
-= (group_at(board
, c
) == group
&& onestone
);
806 board_trait_queue(board
, lib
);
811 foreach_neighbor(board
, lib
, {
812 board
->pat3
[lib
] &= ~((group_at(board
, c
) == group
) << (16 + 3 - fn__i
));
818 /* Update the list of capturable groups. */
819 for (int i
= 0; i
< board
->clen
; i
++) {
820 if (unlikely(board
->c
[i
] == group
)) {
821 board
->c
[i
] = board
->c
[--board
->clen
];
825 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
831 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
834 board_trait_queue(board
, lib1
);
835 board_trait_queue(board
, lib2
);
839 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
842 board_trait_queue(board
, lib1
);
843 board_trait_queue(board
, lib2
);
848 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
851 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
852 group_base(group
), coord2sstr(group_base(group
), board
),
853 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
856 check_libs_consistency(board
, group
);
858 struct group
*gi
= &board_group_info(board
, group
);
859 bool onestone
= group_is_onestone(board
, group
);
860 if (gi
->libs
< GROUP_KEEP_LIBS
) {
861 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
863 /* Seems extra branch just slows it down */
867 if (unlikely(gi
->lib
[i
] == coord
))
871 board_capturable_add(board
, group
, coord
, onestone
);
872 } else if (gi
->libs
== 1) {
873 board_capturable_rm(board
, group
, gi
->lib
[0], onestone
);
874 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
875 } else if (gi
->libs
== 2) {
876 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
878 gi
->lib
[gi
->libs
++] = coord
;
881 check_libs_consistency(board
, group
);
885 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
887 /* Add extra liberty from the board to our liberty list. */
888 unsigned char watermark
[board_size2(board
) / 8];
889 memset(watermark
, 0, sizeof(watermark
));
890 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
891 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
893 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
894 watermark_set(gi
->lib
[i
]);
895 watermark_set(avoid
);
897 foreach_in_group(board
, group
) {
899 foreach_neighbor(board
, coord2
, {
900 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
903 gi
->lib
[gi
->libs
++] = c
;
904 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
907 } foreach_in_group_end
;
913 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
916 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
917 group_base(group
), coord2sstr(group_base(group
), board
),
918 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
921 struct group
*gi
= &board_group_info(board
, group
);
922 bool onestone
= group_is_onestone(board
, group
);
923 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
925 /* Seems extra branch just slows it down */
929 if (likely(gi
->lib
[i
] != coord
))
932 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
933 gi
->lib
[gi
->libs
] = 0;
935 check_libs_consistency(board
, group
);
937 /* Postpone refilling lib[] until we need to. */
938 assert(GROUP_REFILL_LIBS
> 1);
939 if (gi
->libs
> GROUP_REFILL_LIBS
)
941 if (gi
->libs
== GROUP_REFILL_LIBS
)
942 board_group_find_extra_libs(board
, group
, gi
, coord
);
945 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
946 } else if (gi
->libs
== 1) {
947 board_capturable_add(board
, group
, gi
->lib
[0], onestone
);
948 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
949 } else if (gi
->libs
== 0)
950 board_capturable_rm(board
, group
, lib
, onestone
);
954 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
955 * can call this multiple times per coord. */
956 check_libs_consistency(board
, group
);
961 /* This is a low-level routine that doesn't maintain consistency
962 * of all the board data structures. */
964 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
966 enum stone color
= board_at(board
, c
);
967 board_at(board
, c
) = S_NONE
;
968 group_at(board
, c
) = 0;
969 board_hash_update(board
, c
, color
);
971 /* We mark as cannot-capture now. If this is a ko/snapback,
972 * we will get incremented later in board_group_addlib(). */
973 trait_at(board
, c
, S_BLACK
).cap
= trait_at(board
, c
, S_BLACK
).cap1
= 0;
974 trait_at(board
, c
, S_WHITE
).cap
= trait_at(board
, c
, S_WHITE
).cap1
= 0;
975 board_trait_queue(board
, c
);
978 /* Increase liberties of surrounding groups */
980 foreach_neighbor(board
, coord
, {
981 dec_neighbor_count_at(board
, c
, color
);
982 board_trait_queue(board
, c
);
983 group_t g
= group_at(board
, c
);
985 board_group_addlib(board
, g
, coord
);
989 /* board_hash_update() might have seen the freed up point as able
990 * to capture another group in atari that only after the loop
991 * above gained enough liberties. Reset pat3 again. */
992 board
->pat3
[c
] = pattern3_hash(board
, c
);
996 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
997 board
->f
[board
->flen
++] = c
;
1000 static int profiling_noinline
1001 board_group_capture(struct board
*board
, group_t group
)
1005 foreach_in_group(board
, group
) {
1006 board
->captures
[stone_other(board_at(board
, c
))]++;
1007 board_remove_stone(board
, group
, c
);
1009 } foreach_in_group_end
;
1011 struct group
*gi
= &board_group_info(board
, group
);
1012 assert(gi
->libs
== 0);
1013 memset(gi
, 0, sizeof(*gi
));
1019 static void profiling_noinline
1020 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
1023 struct group
*gi
= &board_group_info(board
, group
);
1024 bool onestone
= group_is_onestone(board
, group
);
1026 if (gi
->libs
== 1) {
1027 /* Our group is temporarily in atari; make sure the capturable
1028 * counts also correspond to the newly added stone before we
1029 * start adding liberties again so bump-dump ops match. */
1030 enum stone capturing_color
= stone_other(board_at(board
, group
));
1031 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1033 coord_t lib
= board_group_info(board
, group
).lib
[0];
1034 if (coord_is_adjecent(lib
, coord
, board
)) {
1036 fprintf(stderr
, "add_to_group %s: %s[%d] bump\n", coord2sstr(group
, board
), coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
1037 trait_at(board
, lib
, capturing_color
).cap
++;
1038 /* This is never a 1-stone group, obviously. */
1039 board_trait_queue(board
, lib
);
1043 /* We are not 1-stone group anymore, update the cap1
1044 * counter specifically. */
1045 foreach_neighbor(board
, group
, {
1046 if (board_at(board
, c
) != S_NONE
) continue;
1047 trait_at(board
, c
, capturing_color
).cap1
--;
1048 board_trait_queue(board
, c
);
1054 group_at(board
, coord
) = group
;
1055 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
1056 groupnext_at(board
, prevstone
) = coord
;
1058 foreach_neighbor(board
, coord
, {
1059 if (board_at(board
, c
) == S_NONE
)
1060 board_group_addlib(board
, group
, c
);
1064 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1065 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
1066 coord_x(coord
, board
), coord_y(coord
, board
),
1067 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
1071 static void profiling_noinline
1072 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
1075 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
1076 group_base(group_from
), group_base(group_to
));
1077 struct group
*gi_from
= &board_group_info(board
, group_from
);
1078 struct group
*gi_to
= &board_group_info(board
, group_to
);
1079 bool onestone_from
= group_is_onestone(board
, group_from
);
1080 bool onestone_to
= group_is_onestone(board
, group_to
);
1082 /* We do this early before the group info is rewritten. */
1083 if (gi_from
->libs
== 2)
1084 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
1085 else if (gi_from
->libs
== 1)
1086 board_capturable_rm(board
, group_from
, gi_from
->lib
[0], onestone_from
);
1089 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
1091 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
1092 for (int i
= 0; i
< gi_from
->libs
; i
++) {
1093 for (int j
= 0; j
< gi_to
->libs
; j
++)
1094 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
1096 if (gi_to
->libs
== 0) {
1097 board_capturable_add(board
, group_to
, gi_from
->lib
[i
], onestone_to
);
1098 } else if (gi_to
->libs
== 1) {
1099 board_capturable_rm(board
, group_to
, gi_to
->lib
[0], onestone_to
);
1100 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
1101 } else if (gi_to
->libs
== 2) {
1102 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
1104 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
1105 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
1111 if (gi_to
->libs
== 1) {
1112 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
1114 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
1115 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1117 /* Our group is currently in atari; make sure we properly
1118 * count in even the neighbors from the other group in the
1119 * capturable counter. */
1120 foreach_neighbor(board
, lib
, {
1121 if (DEBUGL(8) && group_at(board
, c
) == group_from
)
1122 fprintf(stderr
, "%s[%d] cap bump\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
1123 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
1124 /* This is never a 1-stone group, obviously. */
1126 board_trait_queue(board
, lib
);
1129 /* We are not 1-stone group anymore, update the cap1
1130 * counter specifically. */
1131 foreach_neighbor(board
, group_to
, {
1132 if (board_at(board
, c
) != S_NONE
) continue;
1133 trait_at(board
, c
, capturing_color
).cap1
--;
1134 board_trait_queue(board
, c
);
1139 if (gi_from
->libs
== 1) {
1140 /* We removed group_from from capturable groups,
1141 * therefore switching the atari flag off.
1142 * We need to set it again since group_to is also
1145 foreach_neighbor(board
, lib
, {
1146 board
->pat3
[lib
] |= (group_at(board
, c
) == group_from
) << (16 + 3 - fn__i
);
1153 coord_t last_in_group
;
1154 foreach_in_group(board
, group_from
) {
1156 group_at(board
, c
) = group_to
;
1157 } foreach_in_group_end
;
1158 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1159 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1160 memset(gi_from
, 0, sizeof(struct group
));
1163 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1164 group_base(group_to
));
1167 static group_t profiling_noinline
1168 new_group(struct board
*board
, coord_t coord
)
1170 group_t group
= coord
;
1171 struct group
*gi
= &board_group_info(board
, group
);
1172 foreach_neighbor(board
, coord
, {
1173 if (board_at(board
, c
) == S_NONE
)
1174 /* board_group_addlib is ridiculously expensive for us */
1175 #if GROUP_KEEP_LIBS < 4
1176 if (gi
->libs
< GROUP_KEEP_LIBS
)
1178 gi
->lib
[gi
->libs
++] = c
;
1181 group_at(board
, coord
) = group
;
1182 groupnext_at(board
, coord
) = 0;
1185 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1186 else if (gi
->libs
== 1)
1187 board_capturable_add(board
, group
, gi
->lib
[0], true);
1188 check_libs_consistency(board
, group
);
1191 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1192 coord_x(coord
, board
), coord_y(coord
, board
),
1198 static inline group_t
1199 play_one_neighbor(struct board
*board
,
1200 coord_t coord
, enum stone color
, enum stone other_color
,
1201 coord_t c
, group_t group
)
1203 enum stone ncolor
= board_at(board
, c
);
1204 group_t ngroup
= group_at(board
, c
);
1206 inc_neighbor_count_at(board
, c
, color
);
1207 /* We can be S_NONE, in that case we need to update the safety
1208 * trait since we might be left with only one liberty. */
1209 board_trait_queue(board
, c
);
1214 board_group_rmlib(board
, ngroup
, coord
);
1216 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1217 group_base(ngroup
), ncolor
, color
, other_color
);
1219 if (ncolor
== color
&& ngroup
!= group
) {
1222 add_to_group(board
, group
, c
, coord
);
1224 merge_groups(board
, group
, ngroup
);
1226 } else if (ncolor
== other_color
) {
1228 struct group
*gi
= &board_group_info(board
, ngroup
);
1229 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1230 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1231 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1232 fprintf(stderr
, "\n");
1234 if (unlikely(board_group_captured(board
, ngroup
)))
1235 board_group_capture(board
, ngroup
);
1240 /* We played on a place with at least one liberty. We will become a member of
1241 * some group for sure. */
1242 static group_t profiling_noinline
1243 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1245 coord_t coord
= m
->coord
;
1246 enum stone color
= m
->color
;
1247 enum stone other_color
= stone_other(color
);
1250 board
->f
[f
] = board
->f
[--board
->flen
];
1252 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1254 #if defined(BOARD_TRAITS) && defined(DEBUG)
1255 /* Sanity check that cap matches reality. */
1258 foreach_neighbor(board
, coord
, {
1259 group_t g
= group_at(board
, c
);
1260 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1261 b
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1) && group_is_onestone(board
, g
);
1263 assert(a
== trait_at(board
, coord
, color
).cap
);
1264 assert(b
== trait_at(board
, coord
, color
).cap1
);
1265 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1268 foreach_neighbor(board
, coord
, {
1269 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1272 board_at(board
, coord
) = color
;
1273 if (unlikely(!group
))
1274 group
= new_group(board
, coord
);
1275 board_gamma_update(board
, coord
, S_BLACK
);
1276 board_gamma_update(board
, coord
, S_WHITE
);
1278 board
->last_move2
= board
->last_move
;
1279 board
->last_move
= *m
;
1281 board_hash_update(board
, coord
, color
);
1282 board_symmetry_update(board
, &board
->symmetry
, coord
);
1283 struct move ko
= { pass
, S_NONE
};
1286 check_pat3_consistency(board
, coord
);
1291 /* We played in an eye-like shape. Either we capture at least one of the eye
1292 * sides in the process of playing, or return -1. */
1293 static int profiling_noinline
1294 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1296 coord_t coord
= m
->coord
;
1297 enum stone color
= m
->color
;
1298 /* Check ko: Capture at a position of ko capture one move ago */
1299 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1301 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1303 } else if (DEBUGL(6)) {
1304 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1305 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1306 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1309 struct move ko
= { pass
, S_NONE
};
1311 int captured_groups
= 0;
1313 foreach_neighbor(board
, coord
, {
1314 group_t g
= group_at(board
, c
);
1316 fprintf(stderr
, "board_check: group %d has %d libs\n",
1317 g
, board_group_info(board
, g
).libs
);
1318 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1321 if (likely(captured_groups
== 0)) {
1324 board_print(board
, stderr
);
1325 fprintf(stderr
, "board_check: one-stone suicide\n");
1331 /* We _will_ for sure capture something. */
1332 assert(trait_at(board
, coord
, color
).cap
> 0);
1333 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1336 board
->f
[f
] = board
->f
[--board
->flen
];
1338 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1340 foreach_neighbor(board
, coord
, {
1341 inc_neighbor_count_at(board
, c
, color
);
1342 /* Originally, this could not have changed any trait
1343 * since no neighbors were S_NONE, however by now some
1344 * of them might be removed from the board. */
1345 board_trait_queue(board
, c
);
1347 group_t group
= group_at(board
, c
);
1351 board_group_rmlib(board
, group
, coord
);
1353 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1356 if (board_group_captured(board
, group
)) {
1357 if (board_group_capture(board
, group
) == 1) {
1358 /* If we captured multiple groups at once,
1359 * we can't be fighting ko so we don't need
1360 * to check for that. */
1361 ko
.color
= stone_other(color
);
1363 board
->last_ko
= ko
;
1364 board
->last_ko_age
= board
->moves
;
1366 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1371 board_at(board
, coord
) = color
;
1372 group_t group
= new_group(board
, coord
);
1373 board_gamma_update(board
, coord
, S_BLACK
);
1374 board_gamma_update(board
, coord
, S_WHITE
);
1376 board
->last_move2
= board
->last_move
;
1377 board
->last_move
= *m
;
1379 board_hash_update(board
, coord
, color
);
1380 board_hash_commit(board
);
1381 board_traits_recompute(board
);
1382 board_symmetry_update(board
, &board
->symmetry
, coord
);
1385 check_pat3_consistency(board
, coord
);
1390 static int __attribute__((flatten
))
1391 board_play_f(struct board
*board
, struct move
*m
, int f
)
1394 fprintf(stderr
, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m
->coord
, board
), coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1396 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1397 /* NOT playing in an eye. Thus this move has to succeed. (This
1398 * is thanks to New Zealand rules. Otherwise, multi-stone
1399 * suicide might fail.) */
1400 group_t group
= board_play_outside(board
, m
, f
);
1401 if (unlikely(board_group_captured(board
, group
))) {
1402 board_group_capture(board
, group
);
1404 board_hash_commit(board
);
1405 board_traits_recompute(board
);
1408 return board_play_in_eye(board
, m
, f
);
1413 board_play(struct board
*board
, struct move
*m
)
1415 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1416 struct move nomove
= { pass
, S_NONE
};
1418 board
->last_move2
= board
->last_move
;
1419 board
->last_move
= *m
;
1424 for (f
= 0; f
< board
->flen
; f
++)
1425 if (board
->f
[f
] == m
->coord
)
1426 return board_play_f(board
, m
, f
);
1429 fprintf(stderr
, "board_check: stone exists\n");
1435 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1438 struct move m
= { *coord
, color
};
1440 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
));
1441 if (unlikely(board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1442 || !board_is_valid_move(b
, &m
)
1443 || (permit
&& !permit(permit_data
, b
, &m
)))
1445 if (m
.coord
== *coord
) {
1446 return likely(board_play_f(b
, &m
, f
) >= 0);
1448 *coord
= m
.coord
; // permit modified the coordinate
1449 return likely(board_play(b
, &m
) >= 0);
1454 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1456 if (unlikely(b
->flen
== 0))
1459 int base
= fast_random(b
->flen
), f
;
1460 for (f
= base
; f
< b
->flen
; f
++)
1461 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1463 for (f
= 0; f
< base
; f
++)
1464 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1469 struct move m
= { pass
, color
};
1475 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1477 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1479 /* XXX: We attempt false eye detection but we will yield false
1480 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1482 foreach_diag_neighbor(board
, coord
) {
1483 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1484 } foreach_diag_neighbor_end
;
1485 /* For false eye, we need two enemy stones diagonally in the
1486 * middle of the board, or just one enemy stone at the edge
1487 * or in the corner. */
1488 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1489 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1493 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1495 return board_is_eyelike(board
, coord
, eye_color
)
1496 && !board_is_false_eyelike(board
, coord
, eye_color
);
1500 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1502 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1504 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1512 board_fast_score(struct board
*board
)
1515 memset(scores
, 0, sizeof(scores
));
1517 foreach_point(board
) {
1518 enum stone color
= board_at(board
, c
);
1519 if (color
== S_NONE
)
1520 color
= board_get_one_point_eye(board
, c
);
1522 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1523 } foreach_point_end
;
1525 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1528 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1530 /* One flood-fill iteration; returns true if next iteration
1533 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1535 bool needs_update
= false;
1536 foreach_free_point(board
) {
1537 /* Ignore occupied and already-dame positions. */
1538 assert(board_at(board
, c
) == S_NONE
);
1539 if (ownermap
[c
] == 3)
1541 /* Count neighbors. */
1543 foreach_neighbor(board
, c
, {
1546 /* If we have neighbors of both colors, or dame,
1547 * we are dame too. */
1548 if ((nei
[1] && nei
[2]) || nei
[3]) {
1550 /* Speed up the propagation. */
1551 foreach_neighbor(board
, c
, {
1552 if (board_at(board
, c
) == S_NONE
)
1555 needs_update
= true;
1558 /* If we have neighbors of one color, we are owned
1559 * by that color, too. */
1560 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1561 int newowner
= nei
[1] ? 1 : 2;
1562 ownermap
[c
] = newowner
;
1563 /* Speed up the propagation. */
1564 foreach_neighbor(board
, c
, {
1565 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1566 ownermap
[c
] = newowner
;
1568 needs_update
= true;
1571 } foreach_free_point_end
;
1572 return needs_update
;
1575 /* Tromp-Taylor Counting */
1577 board_official_score(struct board
*board
, struct move_queue
*q
)
1580 /* A point P, not colored C, is said to reach C, if there is a path of
1581 * (vertically or horizontally) adjacent points of P's color from P to
1582 * a point of color C.
1584 * A player's score is the number of points of her color, plus the
1585 * number of empty points that reach only her color. */
1587 int ownermap
[board_size2(board
)];
1589 const int o
[4] = {0, 1, 2, 0};
1590 foreach_point(board
) {
1591 ownermap
[c
] = o
[board_at(board
, c
)];
1592 s
[board_at(board
, c
)]++;
1593 } foreach_point_end
;
1596 /* Process dead groups. */
1597 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
1598 foreach_in_group(board
, q
->move
[i
]) {
1599 enum stone color
= board_at(board
, c
);
1600 ownermap
[c
] = o
[stone_other(color
)];
1601 s
[color
]--; s
[stone_other(color
)]++;
1602 } foreach_in_group_end
;
1606 /* We need to special-case empty board. */
1607 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1608 return board
->komi
+ board
->handicap
;
1610 while (board_tromp_taylor_iter(board
, ownermap
))
1611 /* Flood-fill... */;
1614 memset(scores
, 0, sizeof(scores
));
1616 foreach_point(board
) {
1617 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1618 if (ownermap
[c
] == 3)
1620 scores
[ownermap
[c
]]++;
1621 } foreach_point_end
;
1623 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];