15 #include "patternsp.h"
21 static void board_trait_recompute(struct board
*board
, coord_t coord
);
22 #include "tactics/selfatari.h"
27 #define profiling_noinline __attribute__((noinline))
29 #define profiling_noinline
32 #define gi_granularity 4
33 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
37 board_setup(struct board
*b
)
39 memset(b
, 0, sizeof(*b
));
41 struct move m
= { pass
, S_NONE
};
42 b
->last_move
= b
->last_move2
= b
->last_move3
= b
->last_move4
= b
->last_ko
= b
->ko
= m
;
46 board_init(char *fbookfile
)
48 struct board
*b
= malloc2(sizeof(struct board
));
51 b
->fbookfile
= fbookfile
;
61 board_alloc(struct board
*board
)
63 /* We do not allocate the board structure itself but we allocate
64 * all the arrays with board contents. */
66 int bsize
= board_size2(board
) * sizeof(*board
->b
);
67 int gsize
= board_size2(board
) * sizeof(*board
->g
);
68 int fsize
= board_size2(board
) * sizeof(*board
->f
);
69 int nsize
= board_size2(board
) * sizeof(*board
->n
);
70 int psize
= board_size2(board
) * sizeof(*board
->p
);
71 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
72 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
74 int csize
= board_size2(board
) * sizeof(*board
->c
);
79 int ssize
= board_size2(board
) * sizeof(*board
->spathash
);
84 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
89 int tsize
= board_size2(board
) * sizeof(*board
->t
);
90 int tqsize
= board_size2(board
) * sizeof(*board
->t
);
95 int cdsize
= board_size2(board
) * sizeof(*board
->coord
);
97 size_t size
= bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ cdsize
;
98 void *x
= malloc2(size
);
100 /* board->b must come first */
101 board
->b
= x
; x
+= bsize
;
102 board
->g
= x
; x
+= gsize
;
103 board
->f
= x
; x
+= fsize
;
104 board
->p
= x
; x
+= psize
;
105 board
->n
= x
; x
+= nsize
;
106 board
->h
= x
; x
+= hsize
;
107 board
->gi
= x
; x
+= gisize
;
109 board
->c
= x
; x
+= csize
;
111 #ifdef BOARD_SPATHASH
112 board
->spathash
= x
; x
+= ssize
;
115 board
->pat3
= x
; x
+= p3size
;
118 board
->t
= x
; x
+= tsize
;
119 board
->tq
= x
; x
+= tqsize
;
121 board
->coord
= x
; x
+= cdsize
;
127 board_copy(struct board
*b2
, struct board
*b1
)
129 memcpy(b2
, b1
, sizeof(struct board
));
131 size_t size
= board_alloc(b2
);
132 memcpy(b2
->b
, b1
->b
, size
);
134 // XXX: Special semantics.
141 board_done_noalloc(struct board
*board
)
143 if (board
->b
) free(board
->b
);
144 if (board
->fbook
) fbook_done(board
->fbook
);
148 board_done(struct board
*board
)
150 board_done_noalloc(board
);
155 board_resize(struct board
*board
, int size
)
158 assert(board_size(board
) == size
+ 2);
160 assert(size
<= BOARD_MAX_SIZE
);
161 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
162 board
->size2
= board_size(board
) * board_size(board
);
165 while ((1 << board
->bits2
) < board
->size2
) board
->bits2
++;
170 size_t asize
= board_alloc(board
);
171 memset(board
->b
, 0, asize
);
175 board_init_data(struct board
*board
)
177 int size
= board_size(board
);
180 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
182 /* Setup neighborhood iterators */
183 board
->nei8
[0] = -size
- 1; // (-1,-1)
186 board
->nei8
[3] = size
- 2; // (-1,0)
188 board
->nei8
[5] = size
- 2; // (-1,1)
191 board
->dnei
[0] = -size
- 1;
193 board
->dnei
[2] = size
*2 - 2;
196 /* Setup initial symmetry */
198 board
->symmetry
.d
= 1;
199 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
200 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
201 board
->symmetry
.type
= SYM_FULL
;
203 /* TODO: We do not handle board symmetry on boards
204 * with no tengen yet. */
205 board
->symmetry
.d
= 0;
206 board
->symmetry
.x1
= board
->symmetry
.y1
= 1;
207 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
208 board
->symmetry
.type
= SYM_NONE
;
211 /* Set up coordinate cache */
212 foreach_point(board
) {
213 board
->coord
[c
][0] = c
% board_size(board
);
214 board
->coord
[c
][1] = c
/ board_size(board
);
217 /* Draw the offboard margin */
218 int top_row
= board_size2(board
) - board_size(board
);
220 for (i
= 0; i
< board_size(board
); i
++)
221 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
222 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
223 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
225 foreach_point(board
) {
227 if (board_at(board
, coord
) == S_OFFBOARD
)
229 foreach_neighbor(board
, c
, {
230 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
234 /* All positions are free! Except the margin. */
235 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
236 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
237 board
->f
[board
->flen
++] = i
;
239 /* Initialize zobrist hashtable. */
240 /* We will need these to be stable across Pachi runs for
241 * certain kinds of pattern matching, thus we do not use
242 * fast_random() for this. */
243 hash_t hseed
= 0x3121110101112131;
244 foreach_point(board
) {
245 board
->h
[c
* 2] = (hseed
*= 16807);
246 if (!board
->h
[c
* 2])
248 /* And once again for white */
249 board
->h
[c
* 2 + 1] = (hseed
*= 16807);
250 if (!board
->h
[c
* 2 + 1])
251 board
->h
[c
* 2 + 1] = 1;
254 #ifdef BOARD_SPATHASH
255 /* Initialize spatial hashes. */
256 foreach_point(board
) {
257 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
258 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
259 ptcoords_at(x
, y
, c
, board
, j
);
260 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
261 pthashes
[0][j
][board_at(board
, c
)];
262 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
263 pthashes
[0][j
][stone_other(board_at(board
, c
))];
269 /* Initialize 3x3 pattern codes. */
270 foreach_point(board
) {
271 if (board_at(board
, c
) == S_NONE
)
272 board
->pat3
[c
] = pattern3_hash(board
, c
);
276 /* Initialize traits. */
277 foreach_point(board
) {
278 trait_at(board
, c
, S_BLACK
).cap
= 0;
279 trait_at(board
, c
, S_WHITE
).cap
= 0;
280 trait_at(board
, c
, S_BLACK
).cap1
= 0;
281 trait_at(board
, c
, S_WHITE
).cap1
= 0;
282 #ifdef BOARD_TRAIT_SAFE
283 trait_at(board
, c
, S_BLACK
).safe
= true;
284 trait_at(board
, c
, S_WHITE
).safe
= true;
291 board_clear(struct board
*board
)
293 int size
= board_size(board
);
294 floating_t komi
= board
->komi
;
295 char *fbookfile
= board
->fbookfile
;
297 board_done_noalloc(board
);
299 static struct board bcache
[BOARD_MAX_SIZE
+ 2];
300 assert(size
> 0 && size
<= BOARD_MAX_SIZE
+ 2);
301 if (bcache
[size
- 1].size
== size
) {
302 board_copy(board
, &bcache
[size
- 1]);
304 board_init_data(board
);
305 board_copy(&bcache
[size
- 1], board
);
309 board
->fbookfile
= fbookfile
;
311 if (board
->fbookfile
) {
312 board
->fbook
= fbook_init(board
->fbookfile
, board
);
317 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
319 for (int i
= 0; i
< c
; i
++) {
320 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
321 s
+= snprintf(s
, end
- s
, " ");
322 for (int x
= 1; x
< board_size(board
) - 1; x
++)
323 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
324 s
+= snprintf(s
, end
-s
, " ");
326 s
+= snprintf(s
, end
- s
, "\n");
327 for (int i
= 0; i
< c
; i
++) {
328 s
+= snprintf(s
, end
- s
, " +-");
329 for (int x
= 1; x
< board_size(board
) - 1; x
++)
330 s
+= snprintf(s
, end
- s
, "--");
331 s
+= snprintf(s
, end
- s
, "+");
333 s
+= snprintf(s
, end
- s
, "\n");
338 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
340 for (int i
= 0; i
< c
; i
++) {
341 s
+= snprintf(s
, end
- s
, " +-");
342 for (int x
= 1; x
< board_size(board
) - 1; x
++)
343 s
+= snprintf(s
, end
- s
, "--");
344 s
+= snprintf(s
, end
- s
, "+");
346 s
+= snprintf(s
, end
- s
, "\n");
351 board_print_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
)
353 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
354 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
355 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
356 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
358 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
360 s
+= snprintf(s
, end
- s
, "|");
362 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
363 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
364 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
);
366 s
+= snprintf(s
, end
- s
, "|");
368 s
+= snprintf(s
, end
- s
, "\n");
373 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
377 char *end
= buf
+ sizeof(buf
);
378 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
379 board
->moves
, board
->komi
, board
->handicap
,
380 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
381 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
382 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
383 s
= board_print_row(board
, y
, s
, end
, cprint
);
384 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
385 fprintf(f
, "%s\n", buf
);
389 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
)
391 s
+= snprintf(s
, end
- s
, "%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
);
404 #if BOARD_TRAIT_SAFE == 1
406 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
408 return board_safe_to_play(board
, coord
, color
);
410 #elif BOARD_TRAIT_SAFE == 2
412 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
414 return !is_bad_selfatari(board
, color
, coord
);
419 board_trait_recompute(struct board
*board
, coord_t coord
)
421 int sfb
= -1, sfw
= -1;
422 #ifdef BOARD_TRAIT_SAFE
423 sfb
= trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);
424 sfw
= trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
427 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
428 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
429 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).cap1
, sfb
,
430 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).cap1
, sfw
);
435 /* Recompute traits for dirty points that we have previously touched
436 * somehow (libs of their neighbors changed or so). */
438 board_traits_recompute(struct board
*board
)
441 for (int i
= 0; i
< board
->tqlen
; i
++) {
442 coord_t coord
= board
->tq
[i
];
443 trait_at(board
, coord
, S_BLACK
).dirty
= false;
444 if (board_at(board
, coord
) != S_NONE
)
446 board_trait_recompute(board
, coord
);
452 /* Queue traits of given point for recomputing. */
454 board_trait_queue(struct board
*board
, coord_t coord
)
457 if (trait_at(board
, coord
, S_BLACK
).dirty
)
459 board
->tq
[board
->tqlen
++] = coord
;
460 trait_at(board
, coord
, S_BLACK
).dirty
= true;
465 /* Update board hash with given coordinate. */
466 static void profiling_noinline
467 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
469 board
->hash
^= hash_at(board
, coord
, color
);
470 board
->qhash
[coord_quadrant(coord
, board
)] ^= hash_at(board
, coord
, color
);
472 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
);
474 #ifdef BOARD_SPATHASH
475 /* Gridcular metric is reflective, so we update all hashes
476 * of appropriate ditance in OUR circle. */
477 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
478 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
479 ptcoords_at(x
, y
, coord
, board
, j
);
480 /* We either changed from S_NONE to color
481 * or vice versa; doesn't matter. */
482 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
483 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
484 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
485 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
490 #if defined(BOARD_PAT3)
491 /* @color is not what we need in case of capture. */
492 static const int ataribits
[8] = { -1, 0, -1, 1, 2, -1, 3, -1 };
493 enum stone new_color
= board_at(board
, coord
);
494 bool in_atari
= false;
495 if (new_color
== S_NONE
) {
496 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
498 in_atari
= (board_group_info(board
, group_at(board
, coord
)).libs
== 1);
500 foreach_8neighbor(board
, coord
) {
501 /* Internally, the loop uses fn__i=[0..7]. We can use
502 * it directly to address bits within the bitmap of the
503 * neighbors since the bitmap order is reverse to the
505 if (board_at(board
, c
) != S_NONE
)
507 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
508 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
509 if (ataribits
[fn__i
] >= 0) {
510 board
->pat3
[c
] &= ~(1 << (16 + ataribits
[fn__i
]));
511 board
->pat3
[c
] |= in_atari
<< (16 + ataribits
[fn__i
]);
513 #if defined(BOARD_TRAITS)
514 board_trait_queue(board
, c
);
516 } foreach_8neighbor_end
;
520 /* Commit current board hash to history. */
521 static void profiling_noinline
522 board_hash_commit(struct board
*board
)
525 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
526 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
527 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
529 hash_t i
= board
->hash
;
530 while (board
->history_hash
[i
& history_hash_mask
]) {
531 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
533 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
534 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
535 board
->superko_violation
= true;
538 i
= history_hash_next(i
);
540 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
546 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
548 if (likely(symmetry
->type
== SYM_NONE
)) {
549 /* Fully degenerated already. We do not support detection
550 * of restoring of symmetry, assuming that this is too rare
551 * a case to handle. */
555 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
556 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
558 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
559 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
560 symmetry
->d
, symmetry
->type
, x
, y
);
563 switch (symmetry
->type
) {
565 if (x
== t
&& y
== t
) {
566 /* Tengen keeps full symmetry. */
569 /* New symmetry now? */
571 symmetry
->type
= SYM_DIAG_UP
;
572 symmetry
->x1
= symmetry
->y1
= 1;
573 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
575 } else if (dx
== y
) {
576 symmetry
->type
= SYM_DIAG_DOWN
;
577 symmetry
->x1
= symmetry
->y1
= 1;
578 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
581 symmetry
->type
= SYM_HORIZ
;
583 symmetry
->y2
= board_size(b
) - 1;
586 symmetry
->type
= SYM_VERT
;
588 symmetry
->x2
= board_size(b
) - 1;
592 symmetry
->type
= SYM_NONE
;
593 symmetry
->x1
= symmetry
->y1
= 1;
594 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
620 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
621 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
622 symmetry
->d
, symmetry
->type
);
629 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
632 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
634 board_play(board
, &m
);
635 /* Simulate white passing; otherwise, UCT search can get confused since
636 * tree depth parity won't match the color to move. */
639 char *str
= coord2str(m
.coord
, board
);
641 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
642 if (f
) fprintf(f
, "%s ", str
);
647 board_handicap(struct board
*board
, int stones
, FILE *f
)
649 int margin
= 3 + (board_size(board
) >= 13);
651 int mid
= board_size(board
) / 2;
652 int max
= board_size(board
) - 1 - margin
;
653 const int places
[][2] = {
654 { min
, min
}, { max
, max
}, { min
, max
}, { max
, min
},
655 { min
, mid
}, { max
, mid
},
656 { mid
, min
}, { mid
, max
},
660 board
->handicap
= stones
;
662 if (stones
== 5 || stones
== 7) {
663 board_handicap_stone(board
, mid
, mid
, f
);
668 for (i
= 0; i
< stones
; i
++)
669 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
673 static void __attribute__((noinline
))
674 check_libs_consistency(struct board
*board
, group_t g
)
678 struct group
*gi
= &board_group_info(board
, g
);
679 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
680 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
681 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
688 check_pat3_consistency(struct board
*board
, coord_t coord
)
691 foreach_8neighbor(board
, coord
) {
692 if (board_at(board
, c
) == S_NONE
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
693 board_print(board
, stderr
);
694 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
);
697 } foreach_8neighbor_end
;
702 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
704 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
706 /* Increase capturable count trait of my last lib. */
707 enum stone capturing_color
= stone_other(board_at(board
, group
));
708 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
709 foreach_neighbor(board
, lib
, {
710 if (DEBUGL(8) && group_at(board
, c
) == group
)
711 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
);
712 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
713 trait_at(board
, lib
, capturing_color
).cap1
+= (group_at(board
, c
) == group
&& onestone
);
715 board_trait_queue(board
, lib
);
720 foreach_neighbor(board
, lib
, {
721 board
->pat3
[lib
] |= (group_at(board
, c
) == group
) << (16 + 3 - fn__i
);
727 /* Update the list of capturable groups. */
729 assert(board
->clen
< board_size2(board
));
730 board
->c
[board
->clen
++] = group
;
734 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
736 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
738 /* Decrease capturable count trait of my previously-last lib. */
739 enum stone capturing_color
= stone_other(board_at(board
, group
));
740 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
741 foreach_neighbor(board
, lib
, {
742 if (DEBUGL(8) && group_at(board
, c
) == group
)
743 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
);
744 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
745 trait_at(board
, lib
, capturing_color
).cap1
-= (group_at(board
, c
) == group
&& onestone
);
747 board_trait_queue(board
, lib
);
752 foreach_neighbor(board
, lib
, {
753 board
->pat3
[lib
] &= ~((group_at(board
, c
) == group
) << (16 + 3 - fn__i
));
759 /* Update the list of capturable groups. */
760 for (int i
= 0; i
< board
->clen
; i
++) {
761 if (unlikely(board
->c
[i
] == group
)) {
762 board
->c
[i
] = board
->c
[--board
->clen
];
766 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
772 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
775 board_trait_queue(board
, lib1
);
776 board_trait_queue(board
, lib2
);
780 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
783 board_trait_queue(board
, lib1
);
784 board_trait_queue(board
, lib2
);
789 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
792 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
793 group_base(group
), coord2sstr(group_base(group
), board
),
794 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
797 check_libs_consistency(board
, group
);
799 struct group
*gi
= &board_group_info(board
, group
);
800 bool onestone
= group_is_onestone(board
, group
);
801 if (gi
->libs
< GROUP_KEEP_LIBS
) {
802 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
804 /* Seems extra branch just slows it down */
808 if (unlikely(gi
->lib
[i
] == coord
))
812 board_capturable_add(board
, group
, coord
, onestone
);
813 } else if (gi
->libs
== 1) {
814 board_capturable_rm(board
, group
, gi
->lib
[0], onestone
);
815 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
816 } else if (gi
->libs
== 2) {
817 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
819 gi
->lib
[gi
->libs
++] = coord
;
822 check_libs_consistency(board
, group
);
826 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
828 /* Add extra liberty from the board to our liberty list. */
829 unsigned char watermark
[board_size2(board
) / 8];
830 memset(watermark
, 0, sizeof(watermark
));
831 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
832 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
834 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
835 watermark_set(gi
->lib
[i
]);
836 watermark_set(avoid
);
838 foreach_in_group(board
, group
) {
840 foreach_neighbor(board
, coord2
, {
841 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
844 gi
->lib
[gi
->libs
++] = c
;
845 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
848 } foreach_in_group_end
;
854 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
857 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
858 group_base(group
), coord2sstr(group_base(group
), board
),
859 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
862 struct group
*gi
= &board_group_info(board
, group
);
863 bool onestone
= group_is_onestone(board
, group
);
864 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
866 /* Seems extra branch just slows it down */
870 if (likely(gi
->lib
[i
] != coord
))
873 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
874 gi
->lib
[gi
->libs
] = 0;
876 check_libs_consistency(board
, group
);
878 /* Postpone refilling lib[] until we need to. */
879 assert(GROUP_REFILL_LIBS
> 1);
880 if (gi
->libs
> GROUP_REFILL_LIBS
)
882 if (gi
->libs
== GROUP_REFILL_LIBS
)
883 board_group_find_extra_libs(board
, group
, gi
, coord
);
886 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
887 } else if (gi
->libs
== 1) {
888 board_capturable_add(board
, group
, gi
->lib
[0], onestone
);
889 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
890 } else if (gi
->libs
== 0)
891 board_capturable_rm(board
, group
, lib
, onestone
);
895 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
896 * can call this multiple times per coord. */
897 check_libs_consistency(board
, group
);
902 /* This is a low-level routine that doesn't maintain consistency
903 * of all the board data structures. */
905 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
907 enum stone color
= board_at(board
, c
);
908 board_at(board
, c
) = S_NONE
;
909 group_at(board
, c
) = 0;
910 board_hash_update(board
, c
, color
);
912 /* We mark as cannot-capture now. If this is a ko/snapback,
913 * we will get incremented later in board_group_addlib(). */
914 trait_at(board
, c
, S_BLACK
).cap
= trait_at(board
, c
, S_BLACK
).cap1
= 0;
915 trait_at(board
, c
, S_WHITE
).cap
= trait_at(board
, c
, S_WHITE
).cap1
= 0;
916 board_trait_queue(board
, c
);
919 /* Increase liberties of surrounding groups */
921 foreach_neighbor(board
, coord
, {
922 dec_neighbor_count_at(board
, c
, color
);
923 board_trait_queue(board
, c
);
924 group_t g
= group_at(board
, c
);
926 board_group_addlib(board
, g
, coord
);
930 /* board_hash_update() might have seen the freed up point as able
931 * to capture another group in atari that only after the loop
932 * above gained enough liberties. Reset pat3 again. */
933 board
->pat3
[c
] = pattern3_hash(board
, c
);
937 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
938 board
->f
[board
->flen
++] = c
;
941 static int profiling_noinline
942 board_group_capture(struct board
*board
, group_t group
)
946 foreach_in_group(board
, group
) {
947 board
->captures
[stone_other(board_at(board
, c
))]++;
948 board_remove_stone(board
, group
, c
);
950 } foreach_in_group_end
;
952 struct group
*gi
= &board_group_info(board
, group
);
953 assert(gi
->libs
== 0);
954 memset(gi
, 0, sizeof(*gi
));
960 static void profiling_noinline
961 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
964 struct group
*gi
= &board_group_info(board
, group
);
965 bool onestone
= group_is_onestone(board
, group
);
968 /* Our group is temporarily in atari; make sure the capturable
969 * counts also correspond to the newly added stone before we
970 * start adding liberties again so bump-dump ops match. */
971 enum stone capturing_color
= stone_other(board_at(board
, group
));
972 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
974 coord_t lib
= board_group_info(board
, group
).lib
[0];
975 if (coord_is_adjecent(lib
, coord
, board
)) {
977 fprintf(stderr
, "add_to_group %s: %s[%d] bump\n", coord2sstr(group
, board
), coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
978 trait_at(board
, lib
, capturing_color
).cap
++;
979 /* This is never a 1-stone group, obviously. */
980 board_trait_queue(board
, lib
);
984 /* We are not 1-stone group anymore, update the cap1
985 * counter specifically. */
986 foreach_neighbor(board
, group
, {
987 if (board_at(board
, c
) != S_NONE
) continue;
988 trait_at(board
, c
, capturing_color
).cap1
--;
989 board_trait_queue(board
, c
);
995 group_at(board
, coord
) = group
;
996 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
997 groupnext_at(board
, prevstone
) = coord
;
999 foreach_neighbor(board
, coord
, {
1000 if (board_at(board
, c
) == S_NONE
)
1001 board_group_addlib(board
, group
, c
);
1005 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1006 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
1007 coord_x(coord
, board
), coord_y(coord
, board
),
1008 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
1012 static void profiling_noinline
1013 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
1016 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
1017 group_base(group_from
), group_base(group_to
));
1018 struct group
*gi_from
= &board_group_info(board
, group_from
);
1019 struct group
*gi_to
= &board_group_info(board
, group_to
);
1020 bool onestone_from
= group_is_onestone(board
, group_from
);
1021 bool onestone_to
= group_is_onestone(board
, group_to
);
1023 /* We do this early before the group info is rewritten. */
1024 if (gi_from
->libs
== 2)
1025 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
1026 else if (gi_from
->libs
== 1)
1027 board_capturable_rm(board
, group_from
, gi_from
->lib
[0], onestone_from
);
1030 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
1032 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
1033 for (int i
= 0; i
< gi_from
->libs
; i
++) {
1034 for (int j
= 0; j
< gi_to
->libs
; j
++)
1035 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
1037 if (gi_to
->libs
== 0) {
1038 board_capturable_add(board
, group_to
, gi_from
->lib
[i
], onestone_to
);
1039 } else if (gi_to
->libs
== 1) {
1040 board_capturable_rm(board
, group_to
, gi_to
->lib
[0], onestone_to
);
1041 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
1042 } else if (gi_to
->libs
== 2) {
1043 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
1045 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
1046 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
1052 if (gi_to
->libs
== 1) {
1053 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
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 foreach_neighbor(board
, lib
, {
1062 if (DEBUGL(8) && group_at(board
, c
) == group_from
)
1063 fprintf(stderr
, "%s[%d] cap bump\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
1064 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
1065 /* This is never a 1-stone group, obviously. */
1067 board_trait_queue(board
, lib
);
1070 /* We are not 1-stone group anymore, update the cap1
1071 * counter specifically. */
1072 foreach_neighbor(board
, group_to
, {
1073 if (board_at(board
, c
) != S_NONE
) continue;
1074 trait_at(board
, c
, capturing_color
).cap1
--;
1075 board_trait_queue(board
, c
);
1080 if (gi_from
->libs
== 1) {
1081 /* We removed group_from from capturable groups,
1082 * therefore switching the atari flag off.
1083 * We need to set it again since group_to is also
1086 foreach_neighbor(board
, lib
, {
1087 board
->pat3
[lib
] |= (group_at(board
, c
) == group_from
) << (16 + 3 - fn__i
);
1094 coord_t last_in_group
;
1095 foreach_in_group(board
, group_from
) {
1097 group_at(board
, c
) = group_to
;
1098 } foreach_in_group_end
;
1099 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1100 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1101 memset(gi_from
, 0, sizeof(struct group
));
1104 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1105 group_base(group_to
));
1108 static group_t profiling_noinline
1109 new_group(struct board
*board
, coord_t coord
)
1111 group_t group
= coord
;
1112 struct group
*gi
= &board_group_info(board
, group
);
1113 foreach_neighbor(board
, coord
, {
1114 if (board_at(board
, c
) == S_NONE
)
1115 /* board_group_addlib is ridiculously expensive for us */
1116 #if GROUP_KEEP_LIBS < 4
1117 if (gi
->libs
< GROUP_KEEP_LIBS
)
1119 gi
->lib
[gi
->libs
++] = c
;
1122 group_at(board
, coord
) = group
;
1123 groupnext_at(board
, coord
) = 0;
1126 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1127 else if (gi
->libs
== 1)
1128 board_capturable_add(board
, group
, gi
->lib
[0], true);
1129 check_libs_consistency(board
, group
);
1132 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1133 coord_x(coord
, board
), coord_y(coord
, board
),
1139 static inline group_t
1140 play_one_neighbor(struct board
*board
,
1141 coord_t coord
, enum stone color
, enum stone other_color
,
1142 coord_t c
, group_t group
)
1144 enum stone ncolor
= board_at(board
, c
);
1145 group_t ngroup
= group_at(board
, c
);
1147 inc_neighbor_count_at(board
, c
, color
);
1148 /* We can be S_NONE, in that case we need to update the safety
1149 * trait since we might be left with only one liberty. */
1150 board_trait_queue(board
, c
);
1155 board_group_rmlib(board
, ngroup
, coord
);
1157 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1158 group_base(ngroup
), ncolor
, color
, other_color
);
1160 if (ncolor
== color
&& ngroup
!= group
) {
1163 add_to_group(board
, group
, c
, coord
);
1165 merge_groups(board
, group
, ngroup
);
1167 } else if (ncolor
== other_color
) {
1169 struct group
*gi
= &board_group_info(board
, ngroup
);
1170 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1171 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1172 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1173 fprintf(stderr
, "\n");
1175 if (unlikely(board_group_captured(board
, ngroup
)))
1176 board_group_capture(board
, ngroup
);
1181 /* We played on a place with at least one liberty. We will become a member of
1182 * some group for sure. */
1183 static group_t profiling_noinline
1184 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1186 coord_t coord
= m
->coord
;
1187 enum stone color
= m
->color
;
1188 enum stone other_color
= stone_other(color
);
1191 board
->f
[f
] = board
->f
[--board
->flen
];
1193 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1195 #if defined(BOARD_TRAITS) && defined(DEBUG)
1196 /* Sanity check that cap matches reality. */
1199 foreach_neighbor(board
, coord
, {
1200 group_t g
= group_at(board
, c
);
1201 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1202 b
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1) && group_is_onestone(board
, g
);
1204 assert(a
== trait_at(board
, coord
, color
).cap
);
1205 assert(b
== trait_at(board
, coord
, color
).cap1
);
1206 #ifdef BOARD_TRAIT_SAFE
1207 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1211 foreach_neighbor(board
, coord
, {
1212 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1215 board_at(board
, coord
) = color
;
1216 if (unlikely(!group
))
1217 group
= new_group(board
, coord
);
1219 board
->last_move2
= board
->last_move
;
1220 board
->last_move
= *m
;
1222 board_hash_update(board
, coord
, color
);
1223 board_symmetry_update(board
, &board
->symmetry
, coord
);
1224 struct move ko
= { pass
, S_NONE
};
1227 check_pat3_consistency(board
, coord
);
1232 /* We played in an eye-like shape. Either we capture at least one of the eye
1233 * sides in the process of playing, or return -1. */
1234 static int profiling_noinline
1235 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1237 coord_t coord
= m
->coord
;
1238 enum stone color
= m
->color
;
1239 /* Check ko: Capture at a position of ko capture one move ago */
1240 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1242 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1244 } else if (DEBUGL(6)) {
1245 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1246 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1247 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1250 struct move ko
= { pass
, S_NONE
};
1252 int captured_groups
= 0;
1254 foreach_neighbor(board
, coord
, {
1255 group_t g
= group_at(board
, c
);
1257 fprintf(stderr
, "board_check: group %d has %d libs\n",
1258 g
, board_group_info(board
, g
).libs
);
1259 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1262 if (likely(captured_groups
== 0)) {
1265 board_print(board
, stderr
);
1266 fprintf(stderr
, "board_check: one-stone suicide\n");
1272 /* We _will_ for sure capture something. */
1273 assert(trait_at(board
, coord
, color
).cap
> 0);
1274 #ifdef BOARD_TRAIT_SAFE
1275 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1279 board
->f
[f
] = board
->f
[--board
->flen
];
1281 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1284 coord_t cap_at
= pass
;
1285 foreach_neighbor(board
, coord
, {
1286 inc_neighbor_count_at(board
, c
, color
);
1287 /* Originally, this could not have changed any trait
1288 * since no neighbors were S_NONE, however by now some
1289 * of them might be removed from the board. */
1290 board_trait_queue(board
, c
);
1292 group_t group
= group_at(board
, c
);
1296 board_group_rmlib(board
, group
, coord
);
1298 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1301 if (board_group_captured(board
, group
)) {
1302 ko_caps
+= board_group_capture(board
, group
);
1307 ko
.color
= stone_other(color
);
1308 ko
.coord
= cap_at
; // unique
1309 board
->last_ko
= ko
;
1310 board
->last_ko_age
= board
->moves
;
1312 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1315 board_at(board
, coord
) = color
;
1316 group_t group
= new_group(board
, coord
);
1318 board
->last_move2
= board
->last_move
;
1319 board
->last_move
= *m
;
1321 board_hash_update(board
, coord
, color
);
1322 board_hash_commit(board
);
1323 board_traits_recompute(board
);
1324 board_symmetry_update(board
, &board
->symmetry
, coord
);
1327 check_pat3_consistency(board
, coord
);
1332 static int __attribute__((flatten
))
1333 board_play_f(struct board
*board
, struct move
*m
, int f
)
1336 fprintf(stderr
, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m
->coord
, board
), coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1338 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1339 /* NOT playing in an eye. Thus this move has to succeed. (This
1340 * is thanks to New Zealand rules. Otherwise, multi-stone
1341 * suicide might fail.) */
1342 group_t group
= board_play_outside(board
, m
, f
);
1343 if (unlikely(board_group_captured(board
, group
))) {
1344 board_group_capture(board
, group
);
1346 board_hash_commit(board
);
1347 board_traits_recompute(board
);
1350 return board_play_in_eye(board
, m
, f
);
1355 board_play(struct board
*board
, struct move
*m
)
1357 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1358 struct move nomove
= { pass
, S_NONE
};
1360 board
->last_move4
= board
->last_move3
;
1361 board
->last_move3
= board
->last_move2
;
1362 board
->last_move2
= board
->last_move
;
1363 board
->last_move
= *m
;
1368 for (f
= 0; f
< board
->flen
; f
++)
1369 if (board
->f
[f
] == m
->coord
)
1370 return board_play_f(board
, m
, f
);
1373 fprintf(stderr
, "board_check: stone exists\n");
1377 /* Undo, supported only for pass moves. This form of undo is required by KGS
1378 * to settle disputes on dead groups. (Undo of real moves would be more complex
1379 * particularly for capturing moves.) */
1380 int board_undo(struct board
*board
)
1382 if (!is_pass(board
->last_move
.coord
))
1384 board
->last_move
= board
->last_move2
;
1385 board
->last_move2
= board
->last_move3
;
1386 board
->last_move3
= board
->last_move4
;
1387 if (board
->last_ko_age
== board
->moves
)
1388 board
->ko
= board
->last_ko
;
1393 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1396 struct move m
= { *coord
, color
};
1398 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
));
1399 if (unlikely(board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1400 || !board_is_valid_move(b
, &m
)
1401 || (permit
&& !permit(permit_data
, b
, &m
)))
1403 if (m
.coord
== *coord
) {
1404 return likely(board_play_f(b
, &m
, f
) >= 0);
1406 *coord
= m
.coord
; // permit modified the coordinate
1407 return likely(board_play(b
, &m
) >= 0);
1412 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1414 if (unlikely(b
->flen
== 0))
1417 int base
= fast_random(b
->flen
), f
;
1418 for (f
= base
; f
< b
->flen
; f
++)
1419 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1421 for (f
= 0; f
< base
; f
++)
1422 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1427 struct move m
= { pass
, color
};
1433 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1435 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1437 /* XXX: We attempt false eye detection but we will yield false
1438 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1440 foreach_diag_neighbor(board
, coord
) {
1441 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1442 } foreach_diag_neighbor_end
;
1443 /* For false eye, we need two enemy stones diagonally in the
1444 * middle of the board, or just one enemy stone at the edge
1445 * or in the corner. */
1446 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1447 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1451 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1453 return board_is_eyelike(board
, coord
, eye_color
)
1454 && !board_is_false_eyelike(board
, coord
, eye_color
);
1458 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1460 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1462 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1470 board_fast_score(struct board
*board
)
1473 memset(scores
, 0, sizeof(scores
));
1475 foreach_point(board
) {
1476 enum stone color
= board_at(board
, c
);
1477 if (color
== S_NONE
&& board
->rules
!= RULES_STONES_ONLY
)
1478 color
= board_get_one_point_eye(board
, c
);
1480 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1481 } foreach_point_end
;
1483 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1486 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1488 /* One flood-fill iteration; returns true if next iteration
1491 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1493 bool needs_update
= false;
1494 foreach_free_point(board
) {
1495 /* Ignore occupied and already-dame positions. */
1496 assert(board_at(board
, c
) == S_NONE
);
1497 if (board
->rules
== RULES_STONES_ONLY
)
1499 if (ownermap
[c
] == 3)
1501 /* Count neighbors. */
1503 foreach_neighbor(board
, c
, {
1506 /* If we have neighbors of both colors, or dame,
1507 * we are dame too. */
1508 if ((nei
[1] && nei
[2]) || nei
[3]) {
1510 /* Speed up the propagation. */
1511 foreach_neighbor(board
, c
, {
1512 if (board_at(board
, c
) == S_NONE
)
1515 needs_update
= true;
1518 /* If we have neighbors of one color, we are owned
1519 * by that color, too. */
1520 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1521 int newowner
= nei
[1] ? 1 : 2;
1522 ownermap
[c
] = newowner
;
1523 /* Speed up the propagation. */
1524 foreach_neighbor(board
, c
, {
1525 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1526 ownermap
[c
] = newowner
;
1528 needs_update
= true;
1531 } foreach_free_point_end
;
1532 return needs_update
;
1535 /* Tromp-Taylor Counting */
1537 board_official_score(struct board
*board
, struct move_queue
*q
)
1540 /* A point P, not colored C, is said to reach C, if there is a path of
1541 * (vertically or horizontally) adjacent points of P's color from P to
1542 * a point of color C.
1544 * A player's score is the number of points of her color, plus the
1545 * number of empty points that reach only her color. */
1547 int ownermap
[board_size2(board
)];
1549 const int o
[4] = {0, 1, 2, 0};
1550 foreach_point(board
) {
1551 ownermap
[c
] = o
[board_at(board
, c
)];
1552 s
[board_at(board
, c
)]++;
1553 } foreach_point_end
;
1556 /* Process dead groups. */
1557 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
1558 foreach_in_group(board
, q
->move
[i
]) {
1559 enum stone color
= board_at(board
, c
);
1560 ownermap
[c
] = o
[stone_other(color
)];
1561 s
[color
]--; s
[stone_other(color
)]++;
1562 } foreach_in_group_end
;
1566 /* We need to special-case empty board. */
1567 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1568 return board
->komi
+ board
->handicap
;
1570 while (board_tromp_taylor_iter(board
, ownermap
))
1571 /* Flood-fill... */;
1574 memset(scores
, 0, sizeof(scores
));
1576 foreach_point(board
) {
1577 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1578 if (ownermap
[c
] == 3)
1580 scores
[ownermap
[c
]]++;
1581 } foreach_point_end
;
1583 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];