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
);
80 int spsize
= board_size2(board
) * sizeof(*board
->spatprob
);
86 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
91 int tsize
= board_size2(board
) * sizeof(*board
->t
);
92 int tqsize
= board_size2(board
) * sizeof(*board
->t
);
97 int cdsize
= board_size2(board
) * sizeof(*board
->coord
);
99 size_t size
= bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ spsize
+ p3size
+ tsize
+ tqsize
+ cdsize
;
100 void *x
= malloc2(size
);
102 /* board->b must come first */
103 board
->b
= x
; x
+= bsize
;
104 board
->g
= x
; x
+= gsize
;
105 board
->f
= x
; x
+= fsize
;
106 board
->p
= x
; x
+= psize
;
107 board
->n
= x
; x
+= nsize
;
108 board
->h
= x
; x
+= hsize
;
109 board
->gi
= x
; x
+= gisize
;
111 board
->c
= x
; x
+= csize
;
113 #ifdef BOARD_SPATHASH
114 board
->spathash
= x
; x
+= ssize
;
115 board
->spatprob
= x
; x
+= spsize
;
118 board
->pat3
= x
; x
+= p3size
;
121 board
->t
= x
; x
+= tsize
;
122 board
->tq
= x
; x
+= tqsize
;
124 board
->coord
= x
; x
+= cdsize
;
130 board_copy(struct board
*b2
, struct board
*b1
)
132 memcpy(b2
, b1
, sizeof(struct board
));
134 size_t size
= board_alloc(b2
);
135 memcpy(b2
->b
, b1
->b
, size
);
137 // XXX: Special semantics.
144 board_done_noalloc(struct board
*board
)
146 if (board
->b
) free(board
->b
);
147 if (board
->fbook
) fbook_done(board
->fbook
);
151 board_done(struct board
*board
)
153 board_done_noalloc(board
);
158 board_resize(struct board
*board
, int size
)
161 assert(board_size(board
) == size
+ 2);
163 assert(size
<= BOARD_MAX_SIZE
);
164 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
165 board
->size2
= board_size(board
) * board_size(board
);
168 while ((1 << board
->bits2
) < board
->size2
) board
->bits2
++;
173 size_t asize
= board_alloc(board
);
174 memset(board
->b
, 0, asize
);
178 board_init_data(struct board
*board
)
180 int size
= board_size(board
);
183 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
185 /* Setup neighborhood iterators */
186 board
->nei8
[0] = -size
- 1; // (-1,-1)
189 board
->nei8
[3] = size
- 2; // (-1,0)
191 board
->nei8
[5] = size
- 2; // (-1,1)
194 board
->dnei
[0] = -size
- 1;
196 board
->dnei
[2] = size
*2 - 2;
199 /* Setup initial symmetry */
201 board
->symmetry
.d
= 1;
202 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
203 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
204 board
->symmetry
.type
= SYM_FULL
;
206 /* TODO: We do not handle board symmetry on boards
207 * with no tengen yet. */
208 board
->symmetry
.d
= 0;
209 board
->symmetry
.x1
= board
->symmetry
.y1
= 1;
210 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
211 board
->symmetry
.type
= SYM_NONE
;
214 /* Set up coordinate cache */
215 foreach_point(board
) {
216 board
->coord
[c
][0] = c
% board_size(board
);
217 board
->coord
[c
][1] = c
/ board_size(board
);
220 /* Draw the offboard margin */
221 int top_row
= board_size2(board
) - board_size(board
);
223 for (i
= 0; i
< board_size(board
); i
++)
224 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
225 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
226 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
228 foreach_point(board
) {
230 if (board_at(board
, coord
) == S_OFFBOARD
)
232 foreach_neighbor(board
, c
, {
233 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
237 /* All positions are free! Except the margin. */
238 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
239 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
240 board
->f
[board
->flen
++] = i
;
242 /* Initialize zobrist hashtable. */
243 /* We will need these to be stable across Pachi runs for
244 * certain kinds of pattern matching, thus we do not use
245 * fast_random() for this. */
246 hash_t hseed
= 0x3121110101112131;
247 foreach_point(board
) {
248 board
->h
[c
* 2] = (hseed
*= 16807);
249 if (!board
->h
[c
* 2])
251 /* And once again for white */
252 board
->h
[c
* 2 + 1] = (hseed
*= 16807);
253 if (!board
->h
[c
* 2 + 1])
254 board
->h
[c
* 2 + 1] = 1;
257 #ifdef BOARD_SPATHASH
258 /* Initialize spatial hashes. */
259 foreach_point(board
) {
260 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
261 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
262 ptcoords_at(x
, y
, c
, board
, j
);
263 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
264 pthashes
[0][j
][board_at(board
, c
)];
265 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
266 pthashes
[0][j
][stone_other(board_at(board
, c
))];
269 board
->spatprob
[c
][0] = NAN
;
270 board
->spatprob
[c
][1] = NAN
;
274 /* Initialize 3x3 pattern codes. */
275 foreach_point(board
) {
276 if (board_at(board
, c
) == S_NONE
)
277 board
->pat3
[c
] = pattern3_hash(board
, c
);
281 /* Initialize traits. */
282 foreach_point(board
) {
283 trait_at(board
, c
, S_BLACK
).cap
= 0;
284 trait_at(board
, c
, S_WHITE
).cap
= 0;
285 trait_at(board
, c
, S_BLACK
).cap1
= 0;
286 trait_at(board
, c
, S_WHITE
).cap1
= 0;
287 #ifdef BOARD_TRAIT_SAFE
288 trait_at(board
, c
, S_BLACK
).safe
= true;
289 trait_at(board
, c
, S_WHITE
).safe
= true;
296 board_clear(struct board
*board
)
298 int size
= board_size(board
);
299 floating_t komi
= board
->komi
;
300 char *fbookfile
= board
->fbookfile
;
301 enum go_ruleset rules
= board
->rules
;
303 board_done_noalloc(board
);
305 static struct board bcache
[BOARD_MAX_SIZE
+ 2];
306 assert(size
> 0 && size
<= BOARD_MAX_SIZE
+ 2);
307 if (bcache
[size
- 1].size
== size
) {
308 board_copy(board
, &bcache
[size
- 1]);
310 board_init_data(board
);
311 board_copy(&bcache
[size
- 1], board
);
315 board
->fbookfile
= fbookfile
;
316 board
->rules
= rules
;
318 if (board
->fbookfile
) {
319 board
->fbook
= fbook_init(board
->fbookfile
, board
);
324 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
326 for (int i
= 0; i
< c
; i
++) {
327 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
328 s
+= snprintf(s
, end
- s
, " ");
329 for (int x
= 1; x
< board_size(board
) - 1; x
++)
330 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
331 s
+= snprintf(s
, end
-s
, " ");
333 s
+= snprintf(s
, end
- s
, "\n");
334 for (int i
= 0; i
< c
; i
++) {
335 s
+= snprintf(s
, end
- s
, " +-");
336 for (int x
= 1; x
< board_size(board
) - 1; x
++)
337 s
+= snprintf(s
, end
- s
, "--");
338 s
+= snprintf(s
, end
- s
, "+");
340 s
+= snprintf(s
, end
- s
, "\n");
345 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
347 for (int i
= 0; i
< c
; i
++) {
348 s
+= snprintf(s
, end
- s
, " +-");
349 for (int x
= 1; x
< board_size(board
) - 1; x
++)
350 s
+= snprintf(s
, end
- s
, "--");
351 s
+= snprintf(s
, end
- s
, "+");
353 s
+= snprintf(s
, end
- s
, "\n");
358 board_print_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
)
360 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
361 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
362 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
363 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
365 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
367 s
+= snprintf(s
, end
- s
, "|");
369 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
370 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
371 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
);
373 s
+= snprintf(s
, end
- s
, "|");
375 s
+= snprintf(s
, end
- s
, "\n");
380 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
384 char *end
= buf
+ sizeof(buf
);
385 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
386 board
->moves
, board
->komi
, board
->handicap
,
387 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
388 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
389 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
390 s
= board_print_row(board
, y
, s
, end
, cprint
);
391 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
392 fprintf(f
, "%s\n", buf
);
396 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
)
398 s
+= snprintf(s
, end
- s
, "%d ", group_base(group_at(board
, c
)));
403 board_print(struct board
*board
, FILE *f
)
405 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
411 #if BOARD_TRAIT_SAFE == 1
413 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
415 return board_safe_to_play(board
, coord
, color
);
417 #elif BOARD_TRAIT_SAFE == 2
419 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
421 return !is_bad_selfatari(board
, color
, coord
);
426 board_trait_recompute(struct board
*board
, coord_t coord
)
428 int sfb
= -1, sfw
= -1;
429 #ifdef BOARD_TRAIT_SAFE
430 sfb
= trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);
431 sfw
= trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
434 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
435 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
436 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).cap1
, sfb
,
437 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).cap1
, sfw
);
442 /* Recompute traits for dirty points that we have previously touched
443 * somehow (libs of their neighbors changed or so). */
445 board_traits_recompute(struct board
*board
)
448 for (int i
= 0; i
< board
->tqlen
; i
++) {
449 coord_t coord
= board
->tq
[i
];
450 trait_at(board
, coord
, S_BLACK
).dirty
= false;
451 if (board_at(board
, coord
) != S_NONE
)
453 board_trait_recompute(board
, coord
);
459 /* Queue traits of given point for recomputing. */
461 board_trait_queue(struct board
*board
, coord_t coord
)
464 if (trait_at(board
, coord
, S_BLACK
).dirty
)
466 board
->tq
[board
->tqlen
++] = coord
;
467 trait_at(board
, coord
, S_BLACK
).dirty
= true;
472 /* Update board hash with given coordinate. */
473 static void profiling_noinline
474 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
476 board
->hash
^= hash_at(board
, coord
, color
);
477 board
->qhash
[coord_quadrant(coord
, board
)] ^= hash_at(board
, coord
, color
);
479 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
);
481 #ifdef BOARD_SPATHASH
482 /* Gridcular metric is reflective, so we update all hashes
483 * of appropriate ditance in OUR circle. */
484 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
485 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
486 ptcoords_at(x
, y
, coord
, board
, j
);
487 /* We either changed from S_NONE to color
488 * or vice versa; doesn't matter. */
489 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
490 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
491 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
492 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
493 board
->spatprob
[coord_xy(board
, x
, y
)][0] = NAN
;
494 board
->spatprob
[coord_xy(board
, x
, y
)][1] = NAN
;
499 #if defined(BOARD_PAT3)
500 /* @color is not what we need in case of capture. */
501 static const int ataribits
[8] = { -1, 0, -1, 1, 2, -1, 3, -1 };
502 enum stone new_color
= board_at(board
, coord
);
503 bool in_atari
= false;
504 if (new_color
== S_NONE
) {
505 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
507 in_atari
= (board_group_info(board
, group_at(board
, coord
)).libs
== 1);
509 foreach_8neighbor(board
, coord
) {
510 /* Internally, the loop uses fn__i=[0..7]. We can use
511 * it directly to address bits within the bitmap of the
512 * neighbors since the bitmap order is reverse to the
514 if (board_at(board
, c
) != S_NONE
)
516 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
517 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
518 if (ataribits
[fn__i
] >= 0) {
519 board
->pat3
[c
] &= ~(1 << (16 + ataribits
[fn__i
]));
520 board
->pat3
[c
] |= in_atari
<< (16 + ataribits
[fn__i
]);
522 #if defined(BOARD_TRAITS)
523 board_trait_queue(board
, c
);
525 } foreach_8neighbor_end
;
529 /* Commit current board hash to history. */
530 static void profiling_noinline
531 board_hash_commit(struct board
*board
)
534 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
535 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
536 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
538 hash_t i
= board
->hash
;
539 while (board
->history_hash
[i
& history_hash_mask
]) {
540 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
542 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
543 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
544 board
->superko_violation
= true;
547 i
= history_hash_next(i
);
549 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
555 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
557 if (likely(symmetry
->type
== SYM_NONE
)) {
558 /* Fully degenerated already. We do not support detection
559 * of restoring of symmetry, assuming that this is too rare
560 * a case to handle. */
564 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
565 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
567 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
568 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
569 symmetry
->d
, symmetry
->type
, x
, y
);
572 switch (symmetry
->type
) {
574 if (x
== t
&& y
== t
) {
575 /* Tengen keeps full symmetry. */
578 /* New symmetry now? */
580 symmetry
->type
= SYM_DIAG_UP
;
581 symmetry
->x1
= symmetry
->y1
= 1;
582 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
584 } else if (dx
== y
) {
585 symmetry
->type
= SYM_DIAG_DOWN
;
586 symmetry
->x1
= symmetry
->y1
= 1;
587 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
590 symmetry
->type
= SYM_HORIZ
;
592 symmetry
->y2
= board_size(b
) - 1;
595 symmetry
->type
= SYM_VERT
;
597 symmetry
->x2
= board_size(b
) - 1;
601 symmetry
->type
= SYM_NONE
;
602 symmetry
->x1
= symmetry
->y1
= 1;
603 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
629 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
630 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
631 symmetry
->d
, symmetry
->type
);
638 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
641 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
643 board_play(board
, &m
);
644 /* Simulate white passing; otherwise, UCT search can get confused since
645 * tree depth parity won't match the color to move. */
648 char *str
= coord2str(m
.coord
, board
);
650 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
651 if (f
) fprintf(f
, "%s ", str
);
656 board_handicap(struct board
*board
, int stones
, FILE *f
)
658 int margin
= 3 + (board_size(board
) >= 13);
660 int mid
= board_size(board
) / 2;
661 int max
= board_size(board
) - 1 - margin
;
662 const int places
[][2] = {
663 { min
, min
}, { max
, max
}, { min
, max
}, { max
, min
},
664 { min
, mid
}, { max
, mid
},
665 { mid
, min
}, { mid
, max
},
669 board
->handicap
= stones
;
671 if (stones
== 5 || stones
== 7) {
672 board_handicap_stone(board
, mid
, mid
, f
);
677 for (i
= 0; i
< stones
; i
++)
678 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
682 static void __attribute__((noinline
))
683 check_libs_consistency(struct board
*board
, group_t g
)
687 struct group
*gi
= &board_group_info(board
, g
);
688 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
689 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
690 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
697 check_pat3_consistency(struct board
*board
, coord_t coord
)
700 foreach_8neighbor(board
, coord
) {
701 if (board_at(board
, c
) == S_NONE
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
702 board_print(board
, stderr
);
703 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
);
706 } foreach_8neighbor_end
;
711 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
713 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
715 /* Increase capturable count trait of my last lib. */
716 enum stone capturing_color
= stone_other(board_at(board
, group
));
717 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
718 foreach_neighbor(board
, lib
, {
719 if (DEBUGL(8) && group_at(board
, c
) == group
)
720 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
);
721 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
722 trait_at(board
, lib
, capturing_color
).cap1
+= (group_at(board
, c
) == group
&& onestone
);
724 board_trait_queue(board
, lib
);
729 foreach_neighbor(board
, lib
, {
730 board
->pat3
[lib
] |= (group_at(board
, c
) == group
) << (16 + 3 - fn__i
);
736 /* Update the list of capturable groups. */
738 assert(board
->clen
< board_size2(board
));
739 board
->c
[board
->clen
++] = group
;
743 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
745 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
747 /* Decrease capturable count trait of my previously-last lib. */
748 enum stone capturing_color
= stone_other(board_at(board
, group
));
749 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
750 foreach_neighbor(board
, lib
, {
751 if (DEBUGL(8) && group_at(board
, c
) == group
)
752 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
);
753 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
754 trait_at(board
, lib
, capturing_color
).cap1
-= (group_at(board
, c
) == group
&& onestone
);
756 board_trait_queue(board
, lib
);
761 foreach_neighbor(board
, lib
, {
762 board
->pat3
[lib
] &= ~((group_at(board
, c
) == group
) << (16 + 3 - fn__i
));
768 /* Update the list of capturable groups. */
769 for (int i
= 0; i
< board
->clen
; i
++) {
770 if (unlikely(board
->c
[i
] == group
)) {
771 board
->c
[i
] = board
->c
[--board
->clen
];
775 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
781 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
784 board_trait_queue(board
, lib1
);
785 board_trait_queue(board
, lib2
);
789 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
792 board_trait_queue(board
, lib1
);
793 board_trait_queue(board
, lib2
);
798 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
801 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
802 group_base(group
), coord2sstr(group_base(group
), board
),
803 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
806 check_libs_consistency(board
, group
);
808 struct group
*gi
= &board_group_info(board
, group
);
809 bool onestone
= group_is_onestone(board
, group
);
810 if (gi
->libs
< GROUP_KEEP_LIBS
) {
811 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
813 /* Seems extra branch just slows it down */
817 if (unlikely(gi
->lib
[i
] == coord
))
821 board_capturable_add(board
, group
, coord
, onestone
);
822 } else if (gi
->libs
== 1) {
823 board_capturable_rm(board
, group
, gi
->lib
[0], onestone
);
824 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
825 } else if (gi
->libs
== 2) {
826 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
828 gi
->lib
[gi
->libs
++] = coord
;
831 check_libs_consistency(board
, group
);
835 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
837 /* Add extra liberty from the board to our liberty list. */
838 unsigned char watermark
[board_size2(board
) / 8];
839 memset(watermark
, 0, sizeof(watermark
));
840 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
841 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
843 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
844 watermark_set(gi
->lib
[i
]);
845 watermark_set(avoid
);
847 foreach_in_group(board
, group
) {
849 foreach_neighbor(board
, coord2
, {
850 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
853 gi
->lib
[gi
->libs
++] = c
;
854 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
857 } foreach_in_group_end
;
863 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
866 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
867 group_base(group
), coord2sstr(group_base(group
), board
),
868 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
871 struct group
*gi
= &board_group_info(board
, group
);
872 bool onestone
= group_is_onestone(board
, group
);
873 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
875 /* Seems extra branch just slows it down */
879 if (likely(gi
->lib
[i
] != coord
))
882 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
883 gi
->lib
[gi
->libs
] = 0;
885 check_libs_consistency(board
, group
);
887 /* Postpone refilling lib[] until we need to. */
888 assert(GROUP_REFILL_LIBS
> 1);
889 if (gi
->libs
> GROUP_REFILL_LIBS
)
891 if (gi
->libs
== GROUP_REFILL_LIBS
)
892 board_group_find_extra_libs(board
, group
, gi
, coord
);
895 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
896 } else if (gi
->libs
== 1) {
897 board_capturable_add(board
, group
, gi
->lib
[0], onestone
);
898 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
899 } else if (gi
->libs
== 0)
900 board_capturable_rm(board
, group
, lib
, onestone
);
904 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
905 * can call this multiple times per coord. */
906 check_libs_consistency(board
, group
);
911 /* This is a low-level routine that doesn't maintain consistency
912 * of all the board data structures. */
914 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
916 enum stone color
= board_at(board
, c
);
917 board_at(board
, c
) = S_NONE
;
918 group_at(board
, c
) = 0;
919 board_hash_update(board
, c
, color
);
921 /* We mark as cannot-capture now. If this is a ko/snapback,
922 * we will get incremented later in board_group_addlib(). */
923 trait_at(board
, c
, S_BLACK
).cap
= trait_at(board
, c
, S_BLACK
).cap1
= 0;
924 trait_at(board
, c
, S_WHITE
).cap
= trait_at(board
, c
, S_WHITE
).cap1
= 0;
925 board_trait_queue(board
, c
);
928 /* Increase liberties of surrounding groups */
930 foreach_neighbor(board
, coord
, {
931 dec_neighbor_count_at(board
, c
, color
);
932 board_trait_queue(board
, c
);
933 group_t g
= group_at(board
, c
);
935 board_group_addlib(board
, g
, coord
);
939 /* board_hash_update() might have seen the freed up point as able
940 * to capture another group in atari that only after the loop
941 * above gained enough liberties. Reset pat3 again. */
942 board
->pat3
[c
] = pattern3_hash(board
, c
);
946 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
947 board
->f
[board
->flen
++] = c
;
950 static int profiling_noinline
951 board_group_capture(struct board
*board
, group_t group
)
955 foreach_in_group(board
, group
) {
956 board
->captures
[stone_other(board_at(board
, c
))]++;
957 board_remove_stone(board
, group
, c
);
959 } foreach_in_group_end
;
961 struct group
*gi
= &board_group_info(board
, group
);
962 assert(gi
->libs
== 0);
963 memset(gi
, 0, sizeof(*gi
));
969 static void profiling_noinline
970 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
973 struct group
*gi
= &board_group_info(board
, group
);
974 bool onestone
= group_is_onestone(board
, group
);
977 /* Our group is temporarily in atari; make sure the capturable
978 * counts also correspond to the newly added stone before we
979 * start adding liberties again so bump-dump ops match. */
980 enum stone capturing_color
= stone_other(board_at(board
, group
));
981 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
983 coord_t lib
= board_group_info(board
, group
).lib
[0];
984 if (coord_is_adjecent(lib
, coord
, board
)) {
986 fprintf(stderr
, "add_to_group %s: %s[%d] bump\n", coord2sstr(group
, board
), coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
987 trait_at(board
, lib
, capturing_color
).cap
++;
988 /* This is never a 1-stone group, obviously. */
989 board_trait_queue(board
, lib
);
993 /* We are not 1-stone group anymore, update the cap1
994 * counter specifically. */
995 foreach_neighbor(board
, group
, {
996 if (board_at(board
, c
) != S_NONE
) continue;
997 trait_at(board
, c
, capturing_color
).cap1
--;
998 board_trait_queue(board
, c
);
1004 group_at(board
, coord
) = group
;
1005 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
1006 groupnext_at(board
, prevstone
) = coord
;
1008 foreach_neighbor(board
, coord
, {
1009 if (board_at(board
, c
) == S_NONE
)
1010 board_group_addlib(board
, group
, c
);
1014 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1015 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
1016 coord_x(coord
, board
), coord_y(coord
, board
),
1017 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
1021 static void profiling_noinline
1022 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
1025 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
1026 group_base(group_from
), group_base(group_to
));
1027 struct group
*gi_from
= &board_group_info(board
, group_from
);
1028 struct group
*gi_to
= &board_group_info(board
, group_to
);
1029 bool onestone_from
= group_is_onestone(board
, group_from
);
1030 bool onestone_to
= group_is_onestone(board
, group_to
);
1032 /* We do this early before the group info is rewritten. */
1033 if (gi_from
->libs
== 2)
1034 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
1035 else if (gi_from
->libs
== 1)
1036 board_capturable_rm(board
, group_from
, gi_from
->lib
[0], onestone_from
);
1039 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
1041 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
1042 for (int i
= 0; i
< gi_from
->libs
; i
++) {
1043 for (int j
= 0; j
< gi_to
->libs
; j
++)
1044 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
1046 if (gi_to
->libs
== 0) {
1047 board_capturable_add(board
, group_to
, gi_from
->lib
[i
], onestone_to
);
1048 } else if (gi_to
->libs
== 1) {
1049 board_capturable_rm(board
, group_to
, gi_to
->lib
[0], onestone_to
);
1050 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
1051 } else if (gi_to
->libs
== 2) {
1052 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
1054 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
1055 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
1061 if (gi_to
->libs
== 1) {
1062 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
1064 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
1065 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1067 /* Our group is currently in atari; make sure we properly
1068 * count in even the neighbors from the other group in the
1069 * capturable counter. */
1070 foreach_neighbor(board
, lib
, {
1071 if (DEBUGL(8) && group_at(board
, c
) == group_from
)
1072 fprintf(stderr
, "%s[%d] cap bump\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
1073 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
1074 /* This is never a 1-stone group, obviously. */
1076 board_trait_queue(board
, lib
);
1079 /* We are not 1-stone group anymore, update the cap1
1080 * counter specifically. */
1081 foreach_neighbor(board
, group_to
, {
1082 if (board_at(board
, c
) != S_NONE
) continue;
1083 trait_at(board
, c
, capturing_color
).cap1
--;
1084 board_trait_queue(board
, c
);
1089 if (gi_from
->libs
== 1) {
1090 /* We removed group_from from capturable groups,
1091 * therefore switching the atari flag off.
1092 * We need to set it again since group_to is also
1095 foreach_neighbor(board
, lib
, {
1096 board
->pat3
[lib
] |= (group_at(board
, c
) == group_from
) << (16 + 3 - fn__i
);
1103 coord_t last_in_group
;
1104 foreach_in_group(board
, group_from
) {
1106 group_at(board
, c
) = group_to
;
1107 } foreach_in_group_end
;
1108 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1109 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1110 memset(gi_from
, 0, sizeof(struct group
));
1113 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1114 group_base(group_to
));
1117 static group_t profiling_noinline
1118 new_group(struct board
*board
, coord_t coord
)
1120 group_t group
= coord
;
1121 struct group
*gi
= &board_group_info(board
, group
);
1122 foreach_neighbor(board
, coord
, {
1123 if (board_at(board
, c
) == S_NONE
)
1124 /* board_group_addlib is ridiculously expensive for us */
1125 #if GROUP_KEEP_LIBS < 4
1126 if (gi
->libs
< GROUP_KEEP_LIBS
)
1128 gi
->lib
[gi
->libs
++] = c
;
1131 group_at(board
, coord
) = group
;
1132 groupnext_at(board
, coord
) = 0;
1135 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1136 else if (gi
->libs
== 1)
1137 board_capturable_add(board
, group
, gi
->lib
[0], true);
1138 check_libs_consistency(board
, group
);
1141 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1142 coord_x(coord
, board
), coord_y(coord
, board
),
1148 static inline group_t
1149 play_one_neighbor(struct board
*board
,
1150 coord_t coord
, enum stone color
, enum stone other_color
,
1151 coord_t c
, group_t group
)
1153 enum stone ncolor
= board_at(board
, c
);
1154 group_t ngroup
= group_at(board
, c
);
1156 inc_neighbor_count_at(board
, c
, color
);
1157 /* We can be S_NONE, in that case we need to update the safety
1158 * trait since we might be left with only one liberty. */
1159 board_trait_queue(board
, c
);
1164 board_group_rmlib(board
, ngroup
, coord
);
1166 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1167 group_base(ngroup
), ncolor
, color
, other_color
);
1169 if (ncolor
== color
&& ngroup
!= group
) {
1172 add_to_group(board
, group
, c
, coord
);
1174 merge_groups(board
, group
, ngroup
);
1176 } else if (ncolor
== other_color
) {
1178 struct group
*gi
= &board_group_info(board
, ngroup
);
1179 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1180 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1181 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1182 fprintf(stderr
, "\n");
1184 if (unlikely(board_group_captured(board
, ngroup
)))
1185 board_group_capture(board
, ngroup
);
1190 /* We played on a place with at least one liberty. We will become a member of
1191 * some group for sure. */
1192 static group_t profiling_noinline
1193 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1195 coord_t coord
= m
->coord
;
1196 enum stone color
= m
->color
;
1197 enum stone other_color
= stone_other(color
);
1200 board
->f
[f
] = board
->f
[--board
->flen
];
1202 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1204 #if defined(BOARD_TRAITS) && defined(DEBUG)
1205 /* Sanity check that cap matches reality. */
1208 foreach_neighbor(board
, coord
, {
1209 group_t g
= group_at(board
, c
);
1210 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1211 b
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1) && group_is_onestone(board
, g
);
1213 assert(a
== trait_at(board
, coord
, color
).cap
);
1214 assert(b
== trait_at(board
, coord
, color
).cap1
);
1215 #ifdef BOARD_TRAIT_SAFE
1216 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1220 foreach_neighbor(board
, coord
, {
1221 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1224 board_at(board
, coord
) = color
;
1225 if (unlikely(!group
))
1226 group
= new_group(board
, coord
);
1228 board
->last_move2
= board
->last_move
;
1229 board
->last_move
= *m
;
1231 board_hash_update(board
, coord
, color
);
1232 board_symmetry_update(board
, &board
->symmetry
, coord
);
1233 struct move ko
= { pass
, S_NONE
};
1236 check_pat3_consistency(board
, coord
);
1241 /* We played in an eye-like shape. Either we capture at least one of the eye
1242 * sides in the process of playing, or return -1. */
1243 static int profiling_noinline
1244 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1246 coord_t coord
= m
->coord
;
1247 enum stone color
= m
->color
;
1248 /* Check ko: Capture at a position of ko capture one move ago */
1249 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1251 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1253 } else if (DEBUGL(6)) {
1254 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1255 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1256 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1259 struct move ko
= { pass
, S_NONE
};
1261 int captured_groups
= 0;
1263 foreach_neighbor(board
, coord
, {
1264 group_t g
= group_at(board
, c
);
1266 fprintf(stderr
, "board_check: group %d has %d libs\n",
1267 g
, board_group_info(board
, g
).libs
);
1268 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1271 if (likely(captured_groups
== 0)) {
1274 board_print(board
, stderr
);
1275 fprintf(stderr
, "board_check: one-stone suicide\n");
1281 /* We _will_ for sure capture something. */
1282 assert(trait_at(board
, coord
, color
).cap
> 0);
1283 #ifdef BOARD_TRAIT_SAFE
1284 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1288 board
->f
[f
] = board
->f
[--board
->flen
];
1290 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1293 coord_t cap_at
= pass
;
1294 foreach_neighbor(board
, coord
, {
1295 inc_neighbor_count_at(board
, c
, color
);
1296 /* Originally, this could not have changed any trait
1297 * since no neighbors were S_NONE, however by now some
1298 * of them might be removed from the board. */
1299 board_trait_queue(board
, c
);
1301 group_t group
= group_at(board
, c
);
1305 board_group_rmlib(board
, group
, coord
);
1307 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1310 if (board_group_captured(board
, group
)) {
1311 ko_caps
+= board_group_capture(board
, group
);
1316 ko
.color
= stone_other(color
);
1317 ko
.coord
= cap_at
; // unique
1318 board
->last_ko
= ko
;
1319 board
->last_ko_age
= board
->moves
;
1321 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1324 board_at(board
, coord
) = color
;
1325 group_t group
= new_group(board
, coord
);
1327 board
->last_move2
= board
->last_move
;
1328 board
->last_move
= *m
;
1330 board_hash_update(board
, coord
, color
);
1331 board_hash_commit(board
);
1332 board_traits_recompute(board
);
1333 board_symmetry_update(board
, &board
->symmetry
, coord
);
1336 check_pat3_consistency(board
, coord
);
1341 static int __attribute__((flatten
))
1342 board_play_f(struct board
*board
, struct move
*m
, int f
)
1345 fprintf(stderr
, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m
->coord
, board
), coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1347 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1348 /* NOT playing in an eye. Thus this move has to succeed. (This
1349 * is thanks to New Zealand rules. Otherwise, multi-stone
1350 * suicide might fail.) */
1351 group_t group
= board_play_outside(board
, m
, f
);
1352 if (unlikely(board_group_captured(board
, group
))) {
1353 board_group_capture(board
, group
);
1355 board_hash_commit(board
);
1356 board_traits_recompute(board
);
1359 return board_play_in_eye(board
, m
, f
);
1364 board_play(struct board
*board
, struct move
*m
)
1366 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1367 if (is_pass(m
->coord
) && board
->rules
== RULES_SIMING
) {
1368 /* On pass, the player gives a pass stone
1369 * to the opponent. */
1370 board
->captures
[stone_other(m
->color
)]++;
1372 struct move nomove
= { pass
, S_NONE
};
1374 board
->last_move4
= board
->last_move3
;
1375 board
->last_move3
= board
->last_move2
;
1376 board
->last_move2
= board
->last_move
;
1377 board
->last_move
= *m
;
1382 for (f
= 0; f
< board
->flen
; f
++)
1383 if (board
->f
[f
] == m
->coord
)
1384 return board_play_f(board
, m
, f
);
1387 fprintf(stderr
, "board_check: stone exists\n");
1391 /* Undo, supported only for pass moves. This form of undo is required by KGS
1392 * to settle disputes on dead groups. (Undo of real moves would be more complex
1393 * particularly for capturing moves.) */
1394 int board_undo(struct board
*board
)
1396 if (!is_pass(board
->last_move
.coord
))
1398 if (board
->rules
== RULES_SIMING
) {
1399 /* Return pass stone to the passing player. */
1400 board
->captures
[stone_other(board
->last_move
.color
)]--;
1402 board
->last_move
= board
->last_move2
;
1403 board
->last_move2
= board
->last_move3
;
1404 board
->last_move3
= board
->last_move4
;
1405 if (board
->last_ko_age
== board
->moves
)
1406 board
->ko
= board
->last_ko
;
1411 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1414 struct move m
= { *coord
, color
};
1416 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
));
1417 if (unlikely(board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1418 || !board_is_valid_move(b
, &m
)
1419 || (permit
&& !permit(permit_data
, b
, &m
)))
1421 if (m
.coord
== *coord
) {
1422 return likely(board_play_f(b
, &m
, f
) >= 0);
1424 *coord
= m
.coord
; // permit modified the coordinate
1425 return likely(board_play(b
, &m
) >= 0);
1430 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1432 if (unlikely(b
->flen
== 0))
1435 int base
= fast_random(b
->flen
), f
;
1436 for (f
= base
; f
< b
->flen
; f
++)
1437 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1439 for (f
= 0; f
< base
; f
++)
1440 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1445 struct move m
= { pass
, color
};
1451 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1453 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1455 /* XXX: We attempt false eye detection but we will yield false
1456 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1458 foreach_diag_neighbor(board
, coord
) {
1459 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1460 } foreach_diag_neighbor_end
;
1461 /* For false eye, we need two enemy stones diagonally in the
1462 * middle of the board, or just one enemy stone at the edge
1463 * or in the corner. */
1464 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1465 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1469 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1471 return board_is_eyelike(board
, coord
, eye_color
)
1472 && !board_is_false_eyelike(board
, coord
, eye_color
);
1476 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1478 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1480 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1488 board_fast_score(struct board
*board
)
1491 memset(scores
, 0, sizeof(scores
));
1493 foreach_point(board
) {
1494 enum stone color
= board_at(board
, c
);
1495 if (color
== S_NONE
&& board
->rules
!= RULES_STONES_ONLY
)
1496 color
= board_get_one_point_eye(board
, c
);
1498 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1499 } foreach_point_end
;
1501 return board
->komi
+ (board
->rules
!= RULES_SIMING
? board
->handicap
: 0) + scores
[S_WHITE
] - scores
[S_BLACK
];
1504 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1506 /* One flood-fill iteration; returns true if next iteration
1509 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1511 bool needs_update
= false;
1512 foreach_free_point(board
) {
1513 /* Ignore occupied and already-dame positions. */
1514 assert(board_at(board
, c
) == S_NONE
);
1515 if (board
->rules
== RULES_STONES_ONLY
)
1517 if (ownermap
[c
] == 3)
1519 /* Count neighbors. */
1521 foreach_neighbor(board
, c
, {
1524 /* If we have neighbors of both colors, or dame,
1525 * we are dame too. */
1526 if ((nei
[1] && nei
[2]) || nei
[3]) {
1528 /* Speed up the propagation. */
1529 foreach_neighbor(board
, c
, {
1530 if (board_at(board
, c
) == S_NONE
)
1533 needs_update
= true;
1536 /* If we have neighbors of one color, we are owned
1537 * by that color, too. */
1538 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1539 int newowner
= nei
[1] ? 1 : 2;
1540 ownermap
[c
] = newowner
;
1541 /* Speed up the propagation. */
1542 foreach_neighbor(board
, c
, {
1543 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1544 ownermap
[c
] = newowner
;
1546 needs_update
= true;
1549 } foreach_free_point_end
;
1550 return needs_update
;
1553 /* Tromp-Taylor Counting */
1555 board_official_score(struct board
*board
, struct move_queue
*q
)
1558 /* A point P, not colored C, is said to reach C, if there is a path of
1559 * (vertically or horizontally) adjacent points of P's color from P to
1560 * a point of color C.
1562 * A player's score is the number of points of her color, plus the
1563 * number of empty points that reach only her color. */
1565 int ownermap
[board_size2(board
)];
1567 const int o
[4] = {0, 1, 2, 0};
1568 foreach_point(board
) {
1569 ownermap
[c
] = o
[board_at(board
, c
)];
1570 s
[board_at(board
, c
)]++;
1571 } foreach_point_end
;
1574 /* Process dead groups. */
1575 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
1576 foreach_in_group(board
, q
->move
[i
]) {
1577 enum stone color
= board_at(board
, c
);
1578 ownermap
[c
] = o
[stone_other(color
)];
1579 s
[color
]--; s
[stone_other(color
)]++;
1580 } foreach_in_group_end
;
1584 /* We need to special-case empty board. */
1585 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1588 while (board_tromp_taylor_iter(board
, ownermap
))
1589 /* Flood-fill... */;
1592 memset(scores
, 0, sizeof(scores
));
1594 foreach_point(board
) {
1595 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1596 if (ownermap
[c
] == 3)
1598 scores
[ownermap
[c
]]++;
1599 } foreach_point_end
;
1601 return board
->komi
+ (board
->rules
!= RULES_SIMING
? board
->handicap
: 0) + scores
[S_WHITE
] - scores
[S_BLACK
];
1605 board_set_rules(struct board
*board
, char *name
)
1607 if (!strcasecmp(name
, "japanese")) {
1608 board
->rules
= RULES_JAPANESE
;
1609 } else if (!strcasecmp(name
, "chinese")) {
1610 board
->rules
= RULES_CHINESE
;
1611 } else if (!strcasecmp(name
, "aga")) {
1612 board
->rules
= RULES_AGA
;
1613 } else if (!strcasecmp(name
, "new_zealand")) {
1614 board
->rules
= RULES_NEW_ZEALAND
;
1615 } else if (!strcasecmp(name
, "siming") || !strcasecmp(name
, "simplified_ing")) {
1616 board
->rules
= RULES_SIMING
;