16 #include "patternsp.h"
22 static void board_trait_recompute(struct board
*board
, coord_t coord
);
23 #include "tactics/selfatari.h"
28 #define profiling_noinline __attribute__((noinline))
30 #define profiling_noinline
33 #define gi_granularity 4
34 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
38 board_setup(struct board
*b
)
40 memset(b
, 0, sizeof(*b
));
42 struct move m
= { pass
, S_NONE
};
43 b
->last_move
= b
->last_move2
= b
->last_move3
= b
->last_move4
= b
->last_ko
= b
->ko
= m
;
47 board_init(char *fbookfile
)
49 struct board
*b
= malloc2(sizeof(struct board
));
52 b
->fbookfile
= fbookfile
;
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
);
96 int cdsize
= board_size2(board
) * sizeof(*board
->coord
);
98 size_t size
= bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ cdsize
;
99 void *x
= malloc2(size
);
101 /* board->b must come first */
102 board
->b
= x
; x
+= bsize
;
103 board
->g
= x
; x
+= gsize
;
104 board
->f
= x
; x
+= fsize
;
105 board
->p
= x
; x
+= psize
;
106 board
->n
= x
; x
+= nsize
;
107 board
->h
= x
; x
+= hsize
;
108 board
->gi
= x
; x
+= gisize
;
110 board
->c
= x
; x
+= csize
;
112 #ifdef BOARD_SPATHASH
113 board
->spathash
= x
; x
+= ssize
;
116 board
->pat3
= x
; x
+= p3size
;
119 board
->t
= x
; x
+= tsize
;
120 board
->tq
= x
; x
+= tqsize
;
122 board
->coord
= x
; x
+= cdsize
;
128 board_copy(struct board
*b2
, struct board
*b1
)
130 memcpy(b2
, b1
, sizeof(struct board
));
132 size_t size
= board_alloc(b2
);
133 memcpy(b2
->b
, b1
->b
, size
);
135 b2
->fbook
= NULL
; // XXX: Special semantics.
137 /* This is not 100% correct, but we can do away without
138 * locking as libmap cannot go away in the course of
139 * copy - b1 will still keep holding refcount at 1
141 __sync_fetch_and_add(&b2
->libmap
->refcount
, 1);
148 board_done_noalloc(struct board
*board
)
150 if (board
->b
) free(board
->b
);
151 if (board
->fbook
) fbook_done(board
->fbook
);
152 if (board
->libmap
) libmap_put(board
->libmap
);
156 board_done(struct board
*board
)
158 board_done_noalloc(board
);
163 board_resize(struct board
*board
, int size
)
166 assert(board_size(board
) == size
+ 2);
168 assert(size
<= BOARD_MAX_SIZE
);
169 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
170 board
->size2
= board_size(board
) * board_size(board
);
173 while ((1 << board
->bits2
) < board
->size2
) board
->bits2
++;
178 size_t asize
= board_alloc(board
);
179 memset(board
->b
, 0, asize
);
183 board_init_data(struct board
*board
)
185 int size
= board_size(board
);
188 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
190 /* Setup neighborhood iterators */
191 board
->nei8
[0] = -size
- 1; // (-1,-1)
194 board
->nei8
[3] = size
- 2; // (-1,0)
196 board
->nei8
[5] = size
- 2; // (-1,1)
199 board
->dnei
[0] = -size
- 1;
201 board
->dnei
[2] = size
*2 - 2;
204 /* Setup initial symmetry */
206 board
->symmetry
.d
= 1;
207 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
208 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
209 board
->symmetry
.type
= SYM_FULL
;
211 /* TODO: We do not handle board symmetry on boards
212 * with no tengen yet. */
213 board
->symmetry
.d
= 0;
214 board
->symmetry
.x1
= board
->symmetry
.y1
= 1;
215 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
216 board
->symmetry
.type
= SYM_NONE
;
219 /* Set up coordinate cache */
220 foreach_point(board
) {
221 board
->coord
[c
][0] = c
% board_size(board
);
222 board
->coord
[c
][1] = c
/ board_size(board
);
225 /* Draw the offboard margin */
226 int top_row
= board_size2(board
) - board_size(board
);
228 for (i
= 0; i
< board_size(board
); i
++)
229 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
230 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
231 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
233 foreach_point(board
) {
235 if (board_at(board
, coord
) == S_OFFBOARD
)
237 foreach_neighbor(board
, c
, {
238 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
242 /* All positions are free! Except the margin. */
243 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
244 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
245 board
->f
[board
->flen
++] = i
;
247 /* Initialize zobrist hashtable. */
248 /* We will need these to be stable across Pachi runs for
249 * certain kinds of pattern matching, thus we do not use
250 * fast_random() for this. */
251 hash_t hseed
= 0x3121110101112131;
252 foreach_point(board
) {
253 board
->h
[c
* 2] = (hseed
*= 16807);
254 if (!board
->h
[c
* 2])
256 /* And once again for white */
257 board
->h
[c
* 2 + 1] = (hseed
*= 16807);
258 if (!board
->h
[c
* 2 + 1])
259 board
->h
[c
* 2 + 1] = 1;
262 #ifdef BOARD_SPATHASH
263 /* Initialize spatial hashes. */
264 foreach_point(board
) {
265 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
266 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
267 ptcoords_at(x
, y
, c
, board
, j
);
268 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
269 pthashes
[0][j
][board_at(board
, c
)];
270 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
271 pthashes
[0][j
][stone_other(board_at(board
, c
))];
277 /* Initialize 3x3 pattern codes. */
278 foreach_point(board
) {
279 if (board_at(board
, c
) == S_NONE
)
280 board
->pat3
[c
] = pattern3_hash(board
, c
);
284 /* Initialize traits. */
285 foreach_point(board
) {
286 trait_at(board
, c
, S_BLACK
).cap
= 0;
287 trait_at(board
, c
, S_WHITE
).cap
= 0;
288 trait_at(board
, c
, S_BLACK
).cap1
= 0;
289 trait_at(board
, c
, S_WHITE
).cap1
= 0;
290 #ifdef BOARD_TRAIT_SAFE
291 trait_at(board
, c
, S_BLACK
).safe
= true;
292 trait_at(board
, c
, S_WHITE
).safe
= true;
299 board_clear(struct board
*board
)
301 int size
= board_size(board
);
302 floating_t komi
= board
->komi
;
303 char *fbookfile
= board
->fbookfile
;
304 enum go_ruleset rules
= board
->rules
;
306 board_done_noalloc(board
);
308 static struct board bcache
[BOARD_MAX_SIZE
+ 2];
309 assert(size
> 0 && size
<= BOARD_MAX_SIZE
+ 2);
310 if (bcache
[size
- 1].size
== size
) {
311 board_copy(board
, &bcache
[size
- 1]);
313 board_init_data(board
);
314 board_copy(&bcache
[size
- 1], board
);
318 board
->fbookfile
= fbookfile
;
319 board
->rules
= rules
;
321 if (board
->fbookfile
) {
322 board
->fbook
= fbook_init(board
->fbookfile
, board
);
325 libmap_put(board
->libmap
);
326 board
->libmap
= NULL
;
331 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
333 for (int i
= 0; i
< c
; i
++) {
334 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
335 s
+= snprintf(s
, end
- s
, " ");
336 for (int x
= 1; x
< board_size(board
) - 1; x
++)
337 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
338 s
+= snprintf(s
, end
-s
, " ");
340 s
+= snprintf(s
, end
- s
, "\n");
341 for (int i
= 0; i
< c
; i
++) {
342 s
+= snprintf(s
, end
- s
, " +-");
343 for (int x
= 1; x
< board_size(board
) - 1; x
++)
344 s
+= snprintf(s
, end
- s
, "--");
345 s
+= snprintf(s
, end
- s
, "+");
347 s
+= snprintf(s
, end
- s
, "\n");
352 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
354 for (int i
= 0; i
< c
; i
++) {
355 s
+= snprintf(s
, end
- s
, " +-");
356 for (int x
= 1; x
< board_size(board
) - 1; x
++)
357 s
+= snprintf(s
, end
- s
, "--");
358 s
+= snprintf(s
, end
- s
, "+");
360 s
+= snprintf(s
, end
- s
, "\n");
365 board_print_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
)
367 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
368 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
369 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
370 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
372 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
374 s
+= snprintf(s
, end
- s
, "|");
376 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
377 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
378 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
);
380 s
+= snprintf(s
, end
- s
, "|");
382 s
+= snprintf(s
, end
- s
, "\n");
387 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
391 char *end
= buf
+ sizeof(buf
);
392 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
393 board
->moves
, board
->komi
, board
->handicap
,
394 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
395 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
396 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
397 s
= board_print_row(board
, y
, s
, end
, cprint
);
398 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
399 fprintf(f
, "%s\n", buf
);
403 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
)
405 s
+= snprintf(s
, end
- s
, "%d ", group_base(group_at(board
, c
)));
410 board_print(struct board
*board
, FILE *f
)
412 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
418 #if BOARD_TRAIT_SAFE == 1
420 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
422 return board_safe_to_play(board
, coord
, color
);
424 #elif BOARD_TRAIT_SAFE == 2
426 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
428 return !is_bad_selfatari(board
, color
, coord
);
433 board_trait_recompute(struct board
*board
, coord_t coord
)
435 int sfb
= -1, sfw
= -1;
436 #ifdef BOARD_TRAIT_SAFE
437 sfb
= trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);
438 sfw
= trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
441 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
442 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
443 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).cap1
, sfb
,
444 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).cap1
, sfw
);
449 /* Recompute traits for dirty points that we have previously touched
450 * somehow (libs of their neighbors changed or so). */
452 board_traits_recompute(struct board
*board
)
455 for (int i
= 0; i
< board
->tqlen
; i
++) {
456 coord_t coord
= board
->tq
[i
];
457 trait_at(board
, coord
, S_BLACK
).dirty
= false;
458 if (board_at(board
, coord
) != S_NONE
)
460 board_trait_recompute(board
, coord
);
466 /* Queue traits of given point for recomputing. */
468 board_trait_queue(struct board
*board
, coord_t coord
)
471 if (trait_at(board
, coord
, S_BLACK
).dirty
)
473 board
->tq
[board
->tqlen
++] = coord
;
474 trait_at(board
, coord
, S_BLACK
).dirty
= true;
479 /* Update board hash with given coordinate. */
480 static void profiling_noinline
481 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
483 board
->hash
^= hash_at(board
, coord
, color
);
484 board
->qhash
[coord_quadrant(coord
, board
)] ^= hash_at(board
, coord
, color
);
486 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
);
488 #ifdef BOARD_SPATHASH
489 /* Gridcular metric is reflective, so we update all hashes
490 * of appropriate ditance in OUR circle. */
491 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
492 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
493 ptcoords_at(x
, y
, coord
, board
, j
);
494 /* We either changed from S_NONE to color
495 * or vice versa; doesn't matter. */
496 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
497 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
498 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
499 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
504 #if defined(BOARD_PAT3)
505 /* @color is not what we need in case of capture. */
506 static const int ataribits
[8] = { -1, 0, -1, 1, 2, -1, 3, -1 };
507 enum stone new_color
= board_at(board
, coord
);
508 bool in_atari
= false;
509 if (new_color
== S_NONE
) {
510 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
512 in_atari
= (board_group_info(board
, group_at(board
, coord
)).libs
== 1);
514 foreach_8neighbor(board
, coord
) {
515 /* Internally, the loop uses fn__i=[0..7]. We can use
516 * it directly to address bits within the bitmap of the
517 * neighbors since the bitmap order is reverse to the
519 if (board_at(board
, c
) != S_NONE
)
521 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
522 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
523 if (ataribits
[fn__i
] >= 0) {
524 board
->pat3
[c
] &= ~(1 << (16 + ataribits
[fn__i
]));
525 board
->pat3
[c
] |= in_atari
<< (16 + ataribits
[fn__i
]);
527 #if defined(BOARD_TRAITS)
528 board_trait_queue(board
, c
);
530 } foreach_8neighbor_end
;
534 /* Commit current board hash to history. */
535 static void profiling_noinline
536 board_hash_commit(struct board
*board
)
539 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
540 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
541 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
543 hash_t i
= board
->hash
;
544 while (board
->history_hash
[i
& history_hash_mask
]) {
545 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
547 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
548 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
549 board
->superko_violation
= true;
552 i
= history_hash_next(i
);
554 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
560 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
562 if (likely(symmetry
->type
== SYM_NONE
)) {
563 /* Fully degenerated already. We do not support detection
564 * of restoring of symmetry, assuming that this is too rare
565 * a case to handle. */
569 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
570 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
572 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
573 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
574 symmetry
->d
, symmetry
->type
, x
, y
);
577 switch (symmetry
->type
) {
579 if (x
== t
&& y
== t
) {
580 /* Tengen keeps full symmetry. */
583 /* New symmetry now? */
585 symmetry
->type
= SYM_DIAG_UP
;
586 symmetry
->x1
= symmetry
->y1
= 1;
587 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
589 } else if (dx
== y
) {
590 symmetry
->type
= SYM_DIAG_DOWN
;
591 symmetry
->x1
= symmetry
->y1
= 1;
592 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
595 symmetry
->type
= SYM_HORIZ
;
597 symmetry
->y2
= board_size(b
) - 1;
600 symmetry
->type
= SYM_VERT
;
602 symmetry
->x2
= board_size(b
) - 1;
606 symmetry
->type
= SYM_NONE
;
607 symmetry
->x1
= symmetry
->y1
= 1;
608 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
634 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
635 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
636 symmetry
->d
, symmetry
->type
);
643 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
646 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
648 board_play(board
, &m
);
649 /* Simulate white passing; otherwise, UCT search can get confused since
650 * tree depth parity won't match the color to move. */
653 char *str
= coord2str(m
.coord
, board
);
655 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
656 if (f
) fprintf(f
, "%s ", str
);
661 board_handicap(struct board
*board
, int stones
, FILE *f
)
663 int margin
= 3 + (board_size(board
) >= 13);
665 int mid
= board_size(board
) / 2;
666 int max
= board_size(board
) - 1 - margin
;
667 const int places
[][2] = {
668 { min
, min
}, { max
, max
}, { min
, max
}, { max
, min
},
669 { min
, mid
}, { max
, mid
},
670 { mid
, min
}, { mid
, max
},
674 board
->handicap
= stones
;
676 if (stones
== 5 || stones
== 7) {
677 board_handicap_stone(board
, mid
, mid
, f
);
682 for (i
= 0; i
< stones
; i
++)
683 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
687 static void __attribute__((noinline
))
688 check_libs_consistency(struct board
*board
, group_t g
)
692 struct group
*gi
= &board_group_info(board
, g
);
693 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
694 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
695 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
702 check_pat3_consistency(struct board
*board
, coord_t coord
)
705 foreach_8neighbor(board
, coord
) {
706 if (board_at(board
, c
) == S_NONE
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
707 board_print(board
, stderr
);
708 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
);
711 } foreach_8neighbor_end
;
716 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
718 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
720 /* Increase capturable count trait of my last lib. */
721 enum stone capturing_color
= stone_other(board_at(board
, group
));
722 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
723 foreach_neighbor(board
, lib
, {
724 if (DEBUGL(8) && group_at(board
, c
) == group
)
725 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
);
726 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
727 trait_at(board
, lib
, capturing_color
).cap1
+= (group_at(board
, c
) == group
&& onestone
);
729 board_trait_queue(board
, lib
);
734 foreach_neighbor(board
, lib
, {
735 board
->pat3
[lib
] |= (group_at(board
, c
) == group
) << (16 + 3 - fn__i
);
741 /* Update the list of capturable groups. */
743 assert(board
->clen
< board_size2(board
));
744 board
->c
[board
->clen
++] = group
;
748 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
750 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
752 /* Decrease capturable count trait of my previously-last lib. */
753 enum stone capturing_color
= stone_other(board_at(board
, group
));
754 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
755 foreach_neighbor(board
, lib
, {
756 if (DEBUGL(8) && group_at(board
, c
) == group
)
757 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
);
758 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
759 trait_at(board
, lib
, capturing_color
).cap1
-= (group_at(board
, c
) == group
&& onestone
);
761 board_trait_queue(board
, lib
);
766 foreach_neighbor(board
, lib
, {
767 board
->pat3
[lib
] &= ~((group_at(board
, c
) == group
) << (16 + 3 - fn__i
));
773 /* Update the list of capturable groups. */
774 for (int i
= 0; i
< board
->clen
; i
++) {
775 if (unlikely(board
->c
[i
] == group
)) {
776 board
->c
[i
] = board
->c
[--board
->clen
];
780 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
786 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
789 board_trait_queue(board
, lib1
);
790 board_trait_queue(board
, lib2
);
794 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
797 board_trait_queue(board
, lib1
);
798 board_trait_queue(board
, lib2
);
803 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
806 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
807 group_base(group
), coord2sstr(group_base(group
), board
),
808 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
811 check_libs_consistency(board
, group
);
813 struct group
*gi
= &board_group_info(board
, group
);
814 bool onestone
= group_is_onestone(board
, group
);
815 if (gi
->libs
< GROUP_KEEP_LIBS
) {
816 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
818 /* Seems extra branch just slows it down */
822 if (unlikely(gi
->lib
[i
] == coord
))
826 board_capturable_add(board
, group
, coord
, onestone
);
827 } else if (gi
->libs
== 1) {
828 board_capturable_rm(board
, group
, gi
->lib
[0], onestone
);
829 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
830 } else if (gi
->libs
== 2) {
831 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
833 gi
->lib
[gi
->libs
++] = coord
;
836 check_libs_consistency(board
, group
);
840 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
842 /* Add extra liberty from the board to our liberty list. */
843 unsigned char watermark
[board_size2(board
) / 8];
844 memset(watermark
, 0, sizeof(watermark
));
845 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
846 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
848 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
849 watermark_set(gi
->lib
[i
]);
850 watermark_set(avoid
);
852 foreach_in_group(board
, group
) {
854 foreach_neighbor(board
, coord2
, {
855 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
858 gi
->lib
[gi
->libs
++] = c
;
859 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
862 } foreach_in_group_end
;
868 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
871 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
872 group_base(group
), coord2sstr(group_base(group
), board
),
873 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
876 struct group
*gi
= &board_group_info(board
, group
);
877 bool onestone
= group_is_onestone(board
, group
);
878 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
880 /* Seems extra branch just slows it down */
884 if (likely(gi
->lib
[i
] != coord
))
887 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
888 gi
->lib
[gi
->libs
] = 0;
890 check_libs_consistency(board
, group
);
892 /* Postpone refilling lib[] until we need to. */
893 assert(GROUP_REFILL_LIBS
> 1);
894 if (gi
->libs
> GROUP_REFILL_LIBS
)
896 if (gi
->libs
== GROUP_REFILL_LIBS
)
897 board_group_find_extra_libs(board
, group
, gi
, coord
);
900 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
901 } else if (gi
->libs
== 1) {
902 board_capturable_add(board
, group
, gi
->lib
[0], onestone
);
903 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
904 } else if (gi
->libs
== 0)
905 board_capturable_rm(board
, group
, lib
, onestone
);
909 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
910 * can call this multiple times per coord. */
911 check_libs_consistency(board
, group
);
916 /* This is a low-level routine that doesn't maintain consistency
917 * of all the board data structures. */
919 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
921 enum stone color
= board_at(board
, c
);
922 board_at(board
, c
) = S_NONE
;
923 group_at(board
, c
) = 0;
924 board_hash_update(board
, c
, color
);
926 /* We mark as cannot-capture now. If this is a ko/snapback,
927 * we will get incremented later in board_group_addlib(). */
928 trait_at(board
, c
, S_BLACK
).cap
= trait_at(board
, c
, S_BLACK
).cap1
= 0;
929 trait_at(board
, c
, S_WHITE
).cap
= trait_at(board
, c
, S_WHITE
).cap1
= 0;
930 board_trait_queue(board
, c
);
933 /* Increase liberties of surrounding groups */
935 foreach_neighbor(board
, coord
, {
936 dec_neighbor_count_at(board
, c
, color
);
937 board_trait_queue(board
, c
);
938 group_t g
= group_at(board
, c
);
940 board_group_addlib(board
, g
, coord
);
944 /* board_hash_update() might have seen the freed up point as able
945 * to capture another group in atari that only after the loop
946 * above gained enough liberties. Reset pat3 again. */
947 board
->pat3
[c
] = pattern3_hash(board
, c
);
951 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
952 board
->f
[board
->flen
++] = c
;
955 static int profiling_noinline
956 board_group_capture(struct board
*board
, group_t group
)
960 foreach_in_group(board
, group
) {
961 board
->captures
[stone_other(board_at(board
, c
))]++;
962 board_remove_stone(board
, group
, c
);
964 } foreach_in_group_end
;
966 struct group
*gi
= &board_group_info(board
, group
);
967 assert(gi
->libs
== 0);
968 memset(gi
, 0, sizeof(*gi
));
974 static void profiling_noinline
975 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
978 struct group
*gi
= &board_group_info(board
, group
);
979 bool onestone
= group_is_onestone(board
, group
);
982 /* Our group is temporarily in atari; make sure the capturable
983 * counts also correspond to the newly added stone before we
984 * start adding liberties again so bump-dump ops match. */
985 enum stone capturing_color
= stone_other(board_at(board
, group
));
986 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
988 coord_t lib
= board_group_info(board
, group
).lib
[0];
989 if (coord_is_adjecent(lib
, coord
, board
)) {
991 fprintf(stderr
, "add_to_group %s: %s[%d] bump\n", coord2sstr(group
, board
), coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
992 trait_at(board
, lib
, capturing_color
).cap
++;
993 /* This is never a 1-stone group, obviously. */
994 board_trait_queue(board
, lib
);
998 /* We are not 1-stone group anymore, update the cap1
999 * counter specifically. */
1000 foreach_neighbor(board
, group
, {
1001 if (board_at(board
, c
) != S_NONE
) continue;
1002 trait_at(board
, c
, capturing_color
).cap1
--;
1003 board_trait_queue(board
, c
);
1009 group_at(board
, coord
) = group
;
1010 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
1011 groupnext_at(board
, prevstone
) = coord
;
1013 foreach_neighbor(board
, coord
, {
1014 if (board_at(board
, c
) == S_NONE
)
1015 board_group_addlib(board
, group
, c
);
1019 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1020 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
1021 coord_x(coord
, board
), coord_y(coord
, board
),
1022 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
1026 static void profiling_noinline
1027 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
1030 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
1031 group_base(group_from
), group_base(group_to
));
1032 struct group
*gi_from
= &board_group_info(board
, group_from
);
1033 struct group
*gi_to
= &board_group_info(board
, group_to
);
1034 bool onestone_from
= group_is_onestone(board
, group_from
);
1035 bool onestone_to
= group_is_onestone(board
, group_to
);
1037 /* We do this early before the group info is rewritten. */
1038 if (gi_from
->libs
== 2)
1039 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
1040 else if (gi_from
->libs
== 1)
1041 board_capturable_rm(board
, group_from
, gi_from
->lib
[0], onestone_from
);
1044 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
1046 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
1047 for (int i
= 0; i
< gi_from
->libs
; i
++) {
1048 for (int j
= 0; j
< gi_to
->libs
; j
++)
1049 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
1051 if (gi_to
->libs
== 0) {
1052 board_capturable_add(board
, group_to
, gi_from
->lib
[i
], onestone_to
);
1053 } else if (gi_to
->libs
== 1) {
1054 board_capturable_rm(board
, group_to
, gi_to
->lib
[0], onestone_to
);
1055 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
1056 } else if (gi_to
->libs
== 2) {
1057 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
1059 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
1060 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
1066 if (gi_to
->libs
== 1) {
1067 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
1069 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
1070 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1072 /* Our group is currently in atari; make sure we properly
1073 * count in even the neighbors from the other group in the
1074 * capturable counter. */
1075 foreach_neighbor(board
, lib
, {
1076 if (DEBUGL(8) && group_at(board
, c
) == group_from
)
1077 fprintf(stderr
, "%s[%d] cap bump\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
1078 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
1079 /* This is never a 1-stone group, obviously. */
1081 board_trait_queue(board
, lib
);
1084 /* We are not 1-stone group anymore, update the cap1
1085 * counter specifically. */
1086 foreach_neighbor(board
, group_to
, {
1087 if (board_at(board
, c
) != S_NONE
) continue;
1088 trait_at(board
, c
, capturing_color
).cap1
--;
1089 board_trait_queue(board
, c
);
1094 if (gi_from
->libs
== 1) {
1095 /* We removed group_from from capturable groups,
1096 * therefore switching the atari flag off.
1097 * We need to set it again since group_to is also
1100 foreach_neighbor(board
, lib
, {
1101 board
->pat3
[lib
] |= (group_at(board
, c
) == group_from
) << (16 + 3 - fn__i
);
1108 coord_t last_in_group
;
1109 foreach_in_group(board
, group_from
) {
1111 group_at(board
, c
) = group_to
;
1112 } foreach_in_group_end
;
1113 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1114 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1115 memset(gi_from
, 0, sizeof(struct group
));
1118 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1119 group_base(group_to
));
1122 static group_t profiling_noinline
1123 new_group(struct board
*board
, coord_t coord
)
1125 group_t group
= coord
;
1126 struct group
*gi
= &board_group_info(board
, group
);
1127 foreach_neighbor(board
, coord
, {
1128 if (board_at(board
, c
) == S_NONE
)
1129 /* board_group_addlib is ridiculously expensive for us */
1130 #if GROUP_KEEP_LIBS < 4
1131 if (gi
->libs
< GROUP_KEEP_LIBS
)
1133 gi
->lib
[gi
->libs
++] = c
;
1136 group_at(board
, coord
) = group
;
1137 groupnext_at(board
, coord
) = 0;
1140 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1141 else if (gi
->libs
== 1)
1142 board_capturable_add(board
, group
, gi
->lib
[0], true);
1143 check_libs_consistency(board
, group
);
1146 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1147 coord_x(coord
, board
), coord_y(coord
, board
),
1153 static inline group_t
1154 play_one_neighbor(struct board
*board
,
1155 coord_t coord
, enum stone color
, enum stone other_color
,
1156 coord_t c
, group_t group
)
1158 enum stone ncolor
= board_at(board
, c
);
1159 group_t ngroup
= group_at(board
, c
);
1161 inc_neighbor_count_at(board
, c
, color
);
1162 /* We can be S_NONE, in that case we need to update the safety
1163 * trait since we might be left with only one liberty. */
1164 board_trait_queue(board
, c
);
1169 board_group_rmlib(board
, ngroup
, coord
);
1171 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1172 group_base(ngroup
), ncolor
, color
, other_color
);
1174 if (ncolor
== color
&& ngroup
!= group
) {
1177 add_to_group(board
, group
, c
, coord
);
1179 merge_groups(board
, group
, ngroup
);
1181 } else if (ncolor
== other_color
) {
1183 struct group
*gi
= &board_group_info(board
, ngroup
);
1184 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1185 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1186 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1187 fprintf(stderr
, "\n");
1189 if (unlikely(board_group_captured(board
, ngroup
)))
1190 board_group_capture(board
, ngroup
);
1195 /* We played on a place with at least one liberty. We will become a member of
1196 * some group for sure. */
1197 static group_t profiling_noinline
1198 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1200 coord_t coord
= m
->coord
;
1201 enum stone color
= m
->color
;
1202 enum stone other_color
= stone_other(color
);
1205 board
->f
[f
] = board
->f
[--board
->flen
];
1207 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1209 #if defined(BOARD_TRAITS) && defined(DEBUG)
1210 /* Sanity check that cap matches reality. */
1213 foreach_neighbor(board
, coord
, {
1214 group_t g
= group_at(board
, c
);
1215 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1216 b
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1) && group_is_onestone(board
, g
);
1218 assert(a
== trait_at(board
, coord
, color
).cap
);
1219 assert(b
== trait_at(board
, coord
, color
).cap1
);
1220 #ifdef BOARD_TRAIT_SAFE
1221 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1225 foreach_neighbor(board
, coord
, {
1226 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1229 board_at(board
, coord
) = color
;
1230 if (unlikely(!group
))
1231 group
= new_group(board
, coord
);
1233 board
->last_move2
= board
->last_move
;
1234 board
->last_move
= *m
;
1236 board_hash_update(board
, coord
, color
);
1237 board_symmetry_update(board
, &board
->symmetry
, coord
);
1238 struct move ko
= { pass
, S_NONE
};
1241 check_pat3_consistency(board
, coord
);
1246 /* We played in an eye-like shape. Either we capture at least one of the eye
1247 * sides in the process of playing, or return -1. */
1248 static int profiling_noinline
1249 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1251 coord_t coord
= m
->coord
;
1252 enum stone color
= m
->color
;
1253 /* Check ko: Capture at a position of ko capture one move ago */
1254 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1256 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1258 } else if (DEBUGL(6)) {
1259 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1260 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1261 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1264 struct move ko
= { pass
, S_NONE
};
1266 int captured_groups
= 0;
1268 foreach_neighbor(board
, coord
, {
1269 group_t g
= group_at(board
, c
);
1271 fprintf(stderr
, "board_check: group %d has %d libs\n",
1272 g
, board_group_info(board
, g
).libs
);
1273 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1276 if (likely(captured_groups
== 0)) {
1279 board_print(board
, stderr
);
1280 fprintf(stderr
, "board_check: one-stone suicide\n");
1286 /* We _will_ for sure capture something. */
1287 assert(trait_at(board
, coord
, color
).cap
> 0);
1288 #ifdef BOARD_TRAIT_SAFE
1289 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1293 board
->f
[f
] = board
->f
[--board
->flen
];
1295 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1298 coord_t cap_at
= pass
;
1299 foreach_neighbor(board
, coord
, {
1300 inc_neighbor_count_at(board
, c
, color
);
1301 /* Originally, this could not have changed any trait
1302 * since no neighbors were S_NONE, however by now some
1303 * of them might be removed from the board. */
1304 board_trait_queue(board
, c
);
1306 group_t group
= group_at(board
, c
);
1310 board_group_rmlib(board
, group
, coord
);
1312 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1315 if (board_group_captured(board
, group
)) {
1316 ko_caps
+= board_group_capture(board
, group
);
1321 ko
.color
= stone_other(color
);
1322 ko
.coord
= cap_at
; // unique
1323 board
->last_ko
= ko
;
1324 board
->last_ko_age
= board
->moves
;
1326 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1329 board_at(board
, coord
) = color
;
1330 group_t group
= new_group(board
, coord
);
1332 board
->last_move2
= board
->last_move
;
1333 board
->last_move
= *m
;
1335 board_hash_update(board
, coord
, color
);
1336 board_hash_commit(board
);
1337 board_traits_recompute(board
);
1338 board_symmetry_update(board
, &board
->symmetry
, coord
);
1341 check_pat3_consistency(board
, coord
);
1346 static int __attribute__((flatten
))
1347 board_play_f(struct board
*board
, struct move
*m
, int f
)
1350 fprintf(stderr
, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m
->coord
, board
), coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1352 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1353 /* NOT playing in an eye. Thus this move has to succeed. (This
1354 * is thanks to New Zealand rules. Otherwise, multi-stone
1355 * suicide might fail.) */
1356 group_t group
= board_play_outside(board
, m
, f
);
1357 if (unlikely(board_group_captured(board
, group
))) {
1358 board_group_capture(board
, group
);
1360 board_hash_commit(board
);
1361 board_traits_recompute(board
);
1364 return board_play_in_eye(board
, m
, f
);
1369 board_play(struct board
*board
, struct move
*m
)
1371 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1372 if (is_pass(m
->coord
) && board
->rules
== RULES_SIMING
) {
1373 /* On pass, the player gives a pass stone
1374 * to the opponent. */
1375 board
->captures
[stone_other(m
->color
)]++;
1377 struct move nomove
= { pass
, S_NONE
};
1379 board
->last_move4
= board
->last_move3
;
1380 board
->last_move3
= board
->last_move2
;
1381 board
->last_move2
= board
->last_move
;
1382 board
->last_move
= *m
;
1387 for (f
= 0; f
< board
->flen
; f
++)
1388 if (board
->f
[f
] == m
->coord
)
1389 return board_play_f(board
, m
, f
);
1392 fprintf(stderr
, "board_check: stone exists\n");
1396 /* Undo, supported only for pass moves. This form of undo is required by KGS
1397 * to settle disputes on dead groups. (Undo of real moves would be more complex
1398 * particularly for capturing moves.) */
1399 int board_undo(struct board
*board
)
1401 if (!is_pass(board
->last_move
.coord
))
1403 if (board
->rules
== RULES_SIMING
) {
1404 /* Return pass stone to the passing player. */
1405 board
->captures
[stone_other(board
->last_move
.color
)]--;
1407 board
->last_move
= board
->last_move2
;
1408 board
->last_move2
= board
->last_move3
;
1409 board
->last_move3
= board
->last_move4
;
1410 if (board
->last_ko_age
== board
->moves
)
1411 board
->ko
= board
->last_ko
;
1416 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1419 struct move m
= { *coord
, color
};
1421 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
));
1422 if (unlikely(board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1423 || !board_is_valid_move(b
, &m
)
1424 || (permit
&& !permit(permit_data
, b
, &m
)))
1426 if (m
.coord
== *coord
) {
1427 return likely(board_play_f(b
, &m
, f
) >= 0);
1429 *coord
= m
.coord
; // permit modified the coordinate
1430 return likely(board_play(b
, &m
) >= 0);
1435 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1437 if (unlikely(b
->flen
== 0))
1440 int base
= fast_random(b
->flen
), f
;
1441 for (f
= base
; f
< b
->flen
; f
++)
1442 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1444 for (f
= 0; f
< base
; f
++)
1445 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1450 struct move m
= { pass
, color
};
1456 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1458 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1460 /* XXX: We attempt false eye detection but we will yield false
1461 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1463 foreach_diag_neighbor(board
, coord
) {
1464 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1465 } foreach_diag_neighbor_end
;
1466 /* For false eye, we need two enemy stones diagonally in the
1467 * middle of the board, or just one enemy stone at the edge
1468 * or in the corner. */
1469 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1470 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1474 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1476 return board_is_eyelike(board
, coord
, eye_color
)
1477 && !board_is_false_eyelike(board
, coord
, eye_color
);
1481 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1483 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1485 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1493 board_fast_score(struct board
*board
)
1496 memset(scores
, 0, sizeof(scores
));
1498 foreach_point(board
) {
1499 enum stone color
= board_at(board
, c
);
1500 if (color
== S_NONE
&& board
->rules
!= RULES_STONES_ONLY
)
1501 color
= board_get_one_point_eye(board
, c
);
1503 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1504 } foreach_point_end
;
1506 return board
->komi
+ (board
->rules
!= RULES_SIMING
? board
->handicap
: 0) + scores
[S_WHITE
] - scores
[S_BLACK
];
1509 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1511 /* One flood-fill iteration; returns true if next iteration
1514 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1516 bool needs_update
= false;
1517 foreach_free_point(board
) {
1518 /* Ignore occupied and already-dame positions. */
1519 assert(board_at(board
, c
) == S_NONE
);
1520 if (board
->rules
== RULES_STONES_ONLY
)
1522 if (ownermap
[c
] == 3)
1524 /* Count neighbors. */
1526 foreach_neighbor(board
, c
, {
1529 /* If we have neighbors of both colors, or dame,
1530 * we are dame too. */
1531 if ((nei
[1] && nei
[2]) || nei
[3]) {
1533 /* Speed up the propagation. */
1534 foreach_neighbor(board
, c
, {
1535 if (board_at(board
, c
) == S_NONE
)
1538 needs_update
= true;
1541 /* If we have neighbors of one color, we are owned
1542 * by that color, too. */
1543 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1544 int newowner
= nei
[1] ? 1 : 2;
1545 ownermap
[c
] = newowner
;
1546 /* Speed up the propagation. */
1547 foreach_neighbor(board
, c
, {
1548 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1549 ownermap
[c
] = newowner
;
1551 needs_update
= true;
1554 } foreach_free_point_end
;
1555 return needs_update
;
1558 /* Tromp-Taylor Counting */
1560 board_official_score(struct board
*board
, struct move_queue
*q
)
1563 /* A point P, not colored C, is said to reach C, if there is a path of
1564 * (vertically or horizontally) adjacent points of P's color from P to
1565 * a point of color C.
1567 * A player's score is the number of points of her color, plus the
1568 * number of empty points that reach only her color. */
1570 int ownermap
[board_size2(board
)];
1572 const int o
[4] = {0, 1, 2, 0};
1573 foreach_point(board
) {
1574 ownermap
[c
] = o
[board_at(board
, c
)];
1575 s
[board_at(board
, c
)]++;
1576 } foreach_point_end
;
1579 /* Process dead groups. */
1580 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
1581 foreach_in_group(board
, q
->move
[i
]) {
1582 enum stone color
= board_at(board
, c
);
1583 ownermap
[c
] = o
[stone_other(color
)];
1584 s
[color
]--; s
[stone_other(color
)]++;
1585 } foreach_in_group_end
;
1589 /* We need to special-case empty board. */
1590 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1593 while (board_tromp_taylor_iter(board
, ownermap
))
1594 /* Flood-fill... */;
1597 memset(scores
, 0, sizeof(scores
));
1599 foreach_point(board
) {
1600 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1601 if (ownermap
[c
] == 3)
1603 scores
[ownermap
[c
]]++;
1604 } foreach_point_end
;
1606 return board
->komi
+ (board
->rules
!= RULES_SIMING
? board
->handicap
: 0) + scores
[S_WHITE
] - scores
[S_BLACK
];
1610 board_set_rules(struct board
*board
, char *name
)
1612 if (!strcasecmp(name
, "japanese")) {
1613 board
->rules
= RULES_JAPANESE
;
1614 } else if (!strcasecmp(name
, "chinese")) {
1615 board
->rules
= RULES_CHINESE
;
1616 } else if (!strcasecmp(name
, "aga")) {
1617 board
->rules
= RULES_AGA
;
1618 } else if (!strcasecmp(name
, "new_zealand")) {
1619 board
->rules
= RULES_NEW_ZEALAND
;
1620 } else if (!strcasecmp(name
, "siming") || !strcasecmp(name
, "simplified_ing")) {
1621 board
->rules
= RULES_SIMING
;