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_cmp(struct board
*b1
, struct board
*b2
)
129 void **p1
= (void**)b1
, **p2
= (void**)b2
;
130 for (unsigned int i
= 0; i
< sizeof(struct board
) / sizeof(void*); i
++)
131 if (p1
[i
] != p2
[i
] &&
132 &p1
[i
] != (void**)&b1
->b
&&
133 &p1
[i
] != (void**)&b1
->g
&&
134 &p1
[i
] != (void**)&b1
->f
&&
135 &p1
[i
] != (void**)&b1
->n
&&
136 &p1
[i
] != (void**)&b1
->p
&&
137 &p1
[i
] != (void**)&b1
->h
&&
138 &p1
[i
] != (void**)&b1
->gi
&&
140 &p1
[i
] != (void**)&b1
->c
&&
142 #ifdef BOARD_SPATHASH
143 &p1
[i
] != (void**)&b1
->spathash
&&
146 &p1
[i
] != (void**)&b1
->pat3
&&
149 &p1
[i
] != (void**)&b1
->t
&&
150 &p1
[i
] != (void**)&b1
->tq
&&
152 &p1
[i
] != (void**)&b1
->coord
)
155 /* Find alloc size */
158 size_t size
= board_alloc(&tmp
);
159 board_done_noalloc(&tmp
);
161 return memcmp(b1
->b
, b2
->b
, size
);
165 board_quick_cmp(struct board
*b1
, struct board
*b2
)
167 if (b1
->size
!= b2
->size
||
168 b1
->size2
!= b2
->size2
||
169 b1
->bits2
!= b2
->bits2
||
170 b1
->captures
[S_BLACK
] != b2
->captures
[S_BLACK
] ||
171 b1
->captures
[S_WHITE
] != b2
->captures
[S_WHITE
] ||
172 b1
->moves
!= b2
->moves
) {
173 fprintf(stderr
, "differs in main vars\n");
176 if (move_cmp(&b1
->last_move
, &b2
->last_move
) ||
177 move_cmp(&b1
->last_move2
, &b2
->last_move2
)) {
178 fprintf(stderr
, "differs in last_move\n");
181 if (move_cmp(&b1
->ko
, &b2
->ko
) ||
182 move_cmp(&b1
->last_ko
, &b2
->last_ko
) ||
183 b1
->last_ko_age
!= b2
->last_ko_age
) {
184 fprintf(stderr
, "differs in ko\n");
188 int bsize
= board_size2(b1
) * sizeof(*b1
->b
);
189 int gsize
= board_size2(b1
) * sizeof(*b1
->g
);
190 //int fsize = board_size2(b1) * sizeof(*b1->f);
191 int nsize
= board_size2(b1
) * sizeof(*b1
->n
);
192 int psize
= board_size2(b1
) * sizeof(*b1
->p
);
193 //int hsize = board_size2(b1) * 2 * sizeof(*b1->h);
194 int gisize
= board_size2(b1
) * sizeof(*b1
->gi
);
195 //int csize = board_size2(board) * sizeof(*b1->c);
196 //int ssize = board_size2(board) * sizeof(*b1->spathash);
197 //int p3size = board_size2(board) * sizeof(*b1->pat3);
198 //int tsize = board_size2(board) * sizeof(*b1->t);
199 //int tqsize = board_size2(board) * sizeof(*b1->t);
201 //int cdsize = board_size2(b1) * sizeof(*b1->coord);
203 if (memcmp(b1
->b
, b2
->b
, bsize
)) {
204 fprintf(stderr
, "differs in b\n"); return 1; }
205 if (memcmp(b1
->g
, b2
->g
, gsize
)) {
206 fprintf(stderr
, "differs in g\n"); return 1; }
207 if (memcmp(b1
->n
, b2
->n
, nsize
)) {
208 fprintf(stderr
, "differs in n\n"); return 1; }
209 if (memcmp(b1
->p
, b2
->p
, psize
)) {
210 fprintf(stderr
, "differs in p\n"); return 1; }
211 if (memcmp(b1
->gi
, b2
->gi
, gisize
)) {
212 fprintf(stderr
, "differs in gi\n"); return 1; }
219 board_copy(struct board
*b2
, struct board
*b1
)
221 memcpy(b2
, b1
, sizeof(struct board
));
223 size_t size
= board_alloc(b2
);
224 memcpy(b2
->b
, b1
->b
, size
);
226 // XXX: Special semantics.
233 board_done_noalloc(struct board
*board
)
235 if (board
->b
) free(board
->b
);
236 if (board
->fbook
) fbook_done(board
->fbook
);
240 board_done(struct board
*board
)
242 board_done_noalloc(board
);
247 board_resize(struct board
*board
, int size
)
250 assert(board_size(board
) == size
+ 2);
252 assert(size
<= BOARD_MAX_SIZE
);
253 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
254 board
->size2
= board_size(board
) * board_size(board
);
257 while ((1 << board
->bits2
) < board
->size2
) board
->bits2
++;
262 size_t asize
= board_alloc(board
);
263 memset(board
->b
, 0, asize
);
267 board_init_data(struct board
*board
)
269 int size
= board_size(board
);
272 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
274 /* Setup neighborhood iterators */
275 board
->nei8
[0] = -size
- 1; // (-1,-1)
278 board
->nei8
[3] = size
- 2; // (-1,0)
280 board
->nei8
[5] = size
- 2; // (-1,1)
283 board
->dnei
[0] = -size
- 1;
285 board
->dnei
[2] = size
*2 - 2;
288 /* Setup initial symmetry */
290 board
->symmetry
.d
= 1;
291 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
292 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
293 board
->symmetry
.type
= SYM_FULL
;
295 /* TODO: We do not handle board symmetry on boards
296 * with no tengen yet. */
297 board
->symmetry
.d
= 0;
298 board
->symmetry
.x1
= board
->symmetry
.y1
= 1;
299 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
300 board
->symmetry
.type
= SYM_NONE
;
303 /* Set up coordinate cache */
304 foreach_point(board
) {
305 board
->coord
[c
][0] = c
% board_size(board
);
306 board
->coord
[c
][1] = c
/ board_size(board
);
309 /* Draw the offboard margin */
310 int top_row
= board_size2(board
) - board_size(board
);
312 for (i
= 0; i
< board_size(board
); i
++)
313 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
314 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
315 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
317 foreach_point(board
) {
319 if (board_at(board
, coord
) == S_OFFBOARD
)
321 foreach_neighbor(board
, c
, {
322 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
326 /* All positions are free! Except the margin. */
327 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
328 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
329 board
->f
[board
->flen
++] = i
;
331 /* Initialize zobrist hashtable. */
332 /* We will need these to be stable across Pachi runs for
333 * certain kinds of pattern matching, thus we do not use
334 * fast_random() for this. */
335 hash_t hseed
= 0x3121110101112131;
336 foreach_point(board
) {
337 board
->h
[c
* 2] = (hseed
*= 16807);
338 if (!board
->h
[c
* 2])
340 /* And once again for white */
341 board
->h
[c
* 2 + 1] = (hseed
*= 16807);
342 if (!board
->h
[c
* 2 + 1])
343 board
->h
[c
* 2 + 1] = 1;
346 #ifdef BOARD_SPATHASH
347 /* Initialize spatial hashes. */
348 foreach_point(board
) {
349 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
350 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
351 ptcoords_at(x
, y
, c
, board
, j
);
352 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
353 pthashes
[0][j
][board_at(board
, c
)];
354 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
355 pthashes
[0][j
][stone_other(board_at(board
, c
))];
361 /* Initialize 3x3 pattern codes. */
362 foreach_point(board
) {
363 if (board_at(board
, c
) == S_NONE
)
364 board
->pat3
[c
] = pattern3_hash(board
, c
);
368 /* Initialize traits. */
369 foreach_point(board
) {
370 trait_at(board
, c
, S_BLACK
).cap
= 0;
371 trait_at(board
, c
, S_WHITE
).cap
= 0;
372 trait_at(board
, c
, S_BLACK
).cap1
= 0;
373 trait_at(board
, c
, S_WHITE
).cap1
= 0;
374 #ifdef BOARD_TRAIT_SAFE
375 trait_at(board
, c
, S_BLACK
).safe
= true;
376 trait_at(board
, c
, S_WHITE
).safe
= true;
383 board_clear(struct board
*board
)
385 int size
= board_size(board
);
386 floating_t komi
= board
->komi
;
387 char *fbookfile
= board
->fbookfile
;
388 enum go_ruleset rules
= board
->rules
;
390 board_done_noalloc(board
);
392 static struct board bcache
[BOARD_MAX_SIZE
+ 2];
393 assert(size
> 0 && size
<= BOARD_MAX_SIZE
+ 2);
394 if (bcache
[size
- 1].size
== size
) {
395 board_copy(board
, &bcache
[size
- 1]);
397 board_init_data(board
);
398 board_copy(&bcache
[size
- 1], board
);
402 board
->fbookfile
= fbookfile
;
403 board
->rules
= rules
;
405 if (board
->fbookfile
) {
406 board
->fbook
= fbook_init(board
->fbookfile
, board
);
411 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
413 for (int i
= 0; i
< c
; i
++) {
414 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
415 s
+= snprintf(s
, end
- s
, " ");
416 for (int x
= 1; x
< board_size(board
) - 1; x
++)
417 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
418 s
+= snprintf(s
, end
-s
, " ");
420 s
+= snprintf(s
, end
- s
, "\n");
421 for (int i
= 0; i
< c
; i
++) {
422 s
+= snprintf(s
, end
- s
, " +-");
423 for (int x
= 1; x
< board_size(board
) - 1; x
++)
424 s
+= snprintf(s
, end
- s
, "--");
425 s
+= snprintf(s
, end
- s
, "+");
427 s
+= snprintf(s
, end
- s
, "\n");
432 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
434 for (int i
= 0; i
< c
; i
++) {
435 s
+= snprintf(s
, end
- s
, " +-");
436 for (int x
= 1; x
< board_size(board
) - 1; x
++)
437 s
+= snprintf(s
, end
- s
, "--");
438 s
+= snprintf(s
, end
- s
, "+");
440 s
+= snprintf(s
, end
- s
, "\n");
445 board_print_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
, void *data
)
447 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
448 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
449 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
450 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
452 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
454 s
+= snprintf(s
, end
- s
, "|");
456 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
457 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
458 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
, data
);
460 s
+= snprintf(s
, end
- s
, "|");
462 s
+= snprintf(s
, end
- s
, "\n");
467 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
, void *data
)
471 char *end
= buf
+ sizeof(buf
);
472 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
473 board
->moves
, board
->komi
, board
->handicap
,
474 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
475 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
476 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
477 s
= board_print_row(board
, y
, s
, end
, cprint
, data
);
478 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
479 fprintf(f
, "%s\n", buf
);
483 board_hprint_row(struct board
*board
, int y
, char *s
, char *end
, board_print_handler handler
, void *data
)
485 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
486 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
487 char *stone_str
= handler(board
, coord_xy(board
, x
, y
), data
);
488 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
489 s
+= snprintf(s
, end
- s
, "%s)", stone_str
);
491 s
+= snprintf(s
, end
- s
, "%s ", stone_str
);
493 s
+= snprintf(s
, end
- s
, "|");
494 s
+= snprintf(s
, end
- s
, "\n");
499 board_hprint(struct board
*board
, FILE *f
, board_print_handler handler
, void *data
)
503 char *end
= buf
+ sizeof(buf
);
504 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
505 board
->moves
, board
->komi
, board
->handicap
,
506 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
507 s
= board_print_top(board
, s
, end
, 1);
508 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
509 s
= board_hprint_row(board
, y
, s
, end
, handler
, data
);
510 board_print_bottom(board
, s
, end
, 1);
511 fprintf(f
, "%s\n", buf
);
515 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
, void *data
)
517 s
+= snprintf(s
, end
- s
, "%d ", group_base(group_at(board
, c
)));
522 board_print(struct board
*board
, FILE *f
)
524 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
, NULL
);
530 #if BOARD_TRAIT_SAFE == 1
532 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
534 return board_safe_to_play(board
, coord
, color
);
536 #elif BOARD_TRAIT_SAFE == 2
538 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
540 return !is_bad_selfatari(board
, color
, coord
);
545 board_trait_recompute(struct board
*board
, coord_t coord
)
547 int sfb
= -1, sfw
= -1;
548 #ifdef BOARD_TRAIT_SAFE
549 sfb
= trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);
550 sfw
= trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
553 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
554 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
555 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).cap1
, sfb
,
556 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).cap1
, sfw
);
561 /* Recompute traits for dirty points that we have previously touched
562 * somehow (libs of their neighbors changed or so). */
564 board_traits_recompute(struct board
*board
)
567 for (int i
= 0; i
< board
->tqlen
; i
++) {
568 coord_t coord
= board
->tq
[i
];
569 trait_at(board
, coord
, S_BLACK
).dirty
= false;
570 if (board_at(board
, coord
) != S_NONE
)
572 board_trait_recompute(board
, coord
);
578 /* Queue traits of given point for recomputing. */
580 board_trait_queue(struct board
*board
, coord_t coord
)
583 if (trait_at(board
, coord
, S_BLACK
).dirty
)
585 board
->tq
[board
->tqlen
++] = coord
;
586 trait_at(board
, coord
, S_BLACK
).dirty
= true;
591 /* Update board hash with given coordinate. */
592 static void profiling_noinline
593 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
595 board
->hash
^= hash_at(board
, coord
, color
);
596 board
->qhash
[coord_quadrant(coord
, board
)] ^= hash_at(board
, coord
, color
);
598 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
);
600 #ifdef BOARD_SPATHASH
601 /* Gridcular metric is reflective, so we update all hashes
602 * of appropriate ditance in OUR circle. */
603 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
604 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
605 ptcoords_at(x
, y
, coord
, board
, j
);
606 /* We either changed from S_NONE to color
607 * or vice versa; doesn't matter. */
608 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
609 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
610 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
611 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
616 #if defined(BOARD_PAT3)
617 /* @color is not what we need in case of capture. */
618 static const int ataribits
[8] = { -1, 0, -1, 1, 2, -1, 3, -1 };
619 enum stone new_color
= board_at(board
, coord
);
620 bool in_atari
= false;
621 if (new_color
== S_NONE
) {
622 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
624 in_atari
= (board_group_info(board
, group_at(board
, coord
)).libs
== 1);
626 foreach_8neighbor(board
, coord
) {
627 /* Internally, the loop uses fn__i=[0..7]. We can use
628 * it directly to address bits within the bitmap of the
629 * neighbors since the bitmap order is reverse to the
631 if (board_at(board
, c
) != S_NONE
)
633 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
634 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
635 if (ataribits
[fn__i
] >= 0) {
636 board
->pat3
[c
] &= ~(1 << (16 + ataribits
[fn__i
]));
637 board
->pat3
[c
] |= in_atari
<< (16 + ataribits
[fn__i
]);
639 #if defined(BOARD_TRAITS)
640 board_trait_queue(board
, c
);
642 } foreach_8neighbor_end
;
646 /* Commit current board hash to history. */
647 static void profiling_noinline
648 board_hash_commit(struct board
*board
)
651 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
652 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
653 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
655 hash_t i
= board
->hash
;
656 while (board
->history_hash
[i
& history_hash_mask
]) {
657 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
659 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
660 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
661 board
->superko_violation
= true;
664 i
= history_hash_next(i
);
666 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
672 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
674 if (likely(symmetry
->type
== SYM_NONE
)) {
675 /* Fully degenerated already. We do not support detection
676 * of restoring of symmetry, assuming that this is too rare
677 * a case to handle. */
681 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
682 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
684 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
685 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
686 symmetry
->d
, symmetry
->type
, x
, y
);
689 switch (symmetry
->type
) {
691 if (x
== t
&& y
== t
) {
692 /* Tengen keeps full symmetry. */
695 /* New symmetry now? */
697 symmetry
->type
= SYM_DIAG_UP
;
698 symmetry
->x1
= symmetry
->y1
= 1;
699 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
701 } else if (dx
== y
) {
702 symmetry
->type
= SYM_DIAG_DOWN
;
703 symmetry
->x1
= symmetry
->y1
= 1;
704 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
707 symmetry
->type
= SYM_HORIZ
;
709 symmetry
->y2
= board_size(b
) - 1;
712 symmetry
->type
= SYM_VERT
;
714 symmetry
->x2
= board_size(b
) - 1;
718 symmetry
->type
= SYM_NONE
;
719 symmetry
->x1
= symmetry
->y1
= 1;
720 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
746 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
747 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
748 symmetry
->d
, symmetry
->type
);
755 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
758 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
760 board_play(board
, &m
);
761 /* Simulate white passing; otherwise, UCT search can get confused since
762 * tree depth parity won't match the color to move. */
765 char *str
= coord2str(m
.coord
, board
);
767 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
768 if (f
) fprintf(f
, "%s ", str
);
773 board_handicap(struct board
*board
, int stones
, FILE *f
)
775 int margin
= 3 + (board_size(board
) >= 13);
777 int mid
= board_size(board
) / 2;
778 int max
= board_size(board
) - 1 - margin
;
779 const int places
[][2] = {
780 { min
, min
}, { max
, max
}, { min
, max
}, { max
, min
},
781 { min
, mid
}, { max
, mid
},
782 { mid
, min
}, { mid
, max
},
786 board
->handicap
= stones
;
788 if (stones
== 5 || stones
== 7) {
789 board_handicap_stone(board
, mid
, mid
, f
);
794 for (i
= 0; i
< stones
; i
++)
795 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
799 static void __attribute__((noinline
))
800 check_libs_consistency(struct board
*board
, group_t g
)
804 struct group
*gi
= &board_group_info(board
, g
);
805 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
806 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
807 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
814 check_pat3_consistency(struct board
*board
, coord_t coord
)
817 foreach_8neighbor(board
, coord
) {
818 if (board_at(board
, c
) == S_NONE
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
819 board_print(board
, stderr
);
820 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
);
823 } foreach_8neighbor_end
;
828 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
830 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
832 /* Increase capturable count trait of my last lib. */
833 enum stone capturing_color
= stone_other(board_at(board
, group
));
834 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
835 foreach_neighbor(board
, lib
, {
836 if (DEBUGL(8) && group_at(board
, c
) == group
)
837 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
);
838 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
839 trait_at(board
, lib
, capturing_color
).cap1
+= (group_at(board
, c
) == group
&& onestone
);
841 board_trait_queue(board
, lib
);
846 foreach_neighbor(board
, lib
, {
847 board
->pat3
[lib
] |= (group_at(board
, c
) == group
) << (16 + 3 - fn__i
);
853 /* Update the list of capturable groups. */
855 assert(board
->clen
< board_size2(board
));
856 board
->c
[board
->clen
++] = group
;
860 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
862 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
864 /* Decrease capturable count trait of my previously-last lib. */
865 enum stone capturing_color
= stone_other(board_at(board
, group
));
866 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
867 foreach_neighbor(board
, lib
, {
868 if (DEBUGL(8) && group_at(board
, c
) == group
)
869 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
);
870 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
871 trait_at(board
, lib
, capturing_color
).cap1
-= (group_at(board
, c
) == group
&& onestone
);
873 board_trait_queue(board
, lib
);
878 foreach_neighbor(board
, lib
, {
879 board
->pat3
[lib
] &= ~((group_at(board
, c
) == group
) << (16 + 3 - fn__i
));
885 /* Update the list of capturable groups. */
886 for (int i
= 0; i
< board
->clen
; i
++) {
887 if (unlikely(board
->c
[i
] == group
)) {
888 board
->c
[i
] = board
->c
[--board
->clen
];
892 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
898 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
901 board_trait_queue(board
, lib1
);
902 board_trait_queue(board
, lib2
);
906 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
909 board_trait_queue(board
, lib1
);
910 board_trait_queue(board
, lib2
);
915 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
, struct board_undo
*u
)
918 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
919 group_base(group
), coord2sstr(group_base(group
), board
),
920 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
923 if (!u
) check_libs_consistency(board
, group
);
925 struct group
*gi
= &board_group_info(board
, group
);
926 bool onestone
= group_is_onestone(board
, group
);
927 if (gi
->libs
< GROUP_KEEP_LIBS
) {
928 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
930 /* Seems extra branch just slows it down */
934 if (unlikely(gi
->lib
[i
] == coord
))
939 board_capturable_add(board
, group
, coord
, onestone
);
940 } else if (gi
->libs
== 1) {
941 board_capturable_rm(board
, group
, gi
->lib
[0], onestone
);
942 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
943 } else if (gi
->libs
== 2) {
944 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
947 gi
->lib
[gi
->libs
++] = coord
;
950 if (!u
) check_libs_consistency(board
, group
);
954 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
956 /* Add extra liberty from the board to our liberty list. */
957 unsigned char watermark
[board_size2(board
) / 8];
958 memset(watermark
, 0, sizeof(watermark
));
959 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
960 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
962 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
963 watermark_set(gi
->lib
[i
]);
964 watermark_set(avoid
);
966 foreach_in_group(board
, group
) {
968 foreach_neighbor(board
, coord2
, {
969 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
972 gi
->lib
[gi
->libs
++] = c
;
973 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
976 } foreach_in_group_end
;
982 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
, struct board_undo
*u
)
985 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
986 group_base(group
), coord2sstr(group_base(group
), board
),
987 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
990 struct group
*gi
= &board_group_info(board
, group
);
991 bool onestone
= group_is_onestone(board
, group
);
992 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
994 /* Seems extra branch just slows it down */
998 if (likely(gi
->lib
[i
] != coord
))
1001 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
1002 gi
->lib
[gi
->libs
] = 0;
1004 if (!u
) check_libs_consistency(board
, group
);
1006 /* Postpone refilling lib[] until we need to. */
1007 assert(GROUP_REFILL_LIBS
> 1);
1008 if (gi
->libs
> GROUP_REFILL_LIBS
)
1010 if (gi
->libs
== GROUP_REFILL_LIBS
)
1011 board_group_find_extra_libs(board
, group
, gi
, coord
);
1014 if (gi
->libs
== 2) {
1015 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1016 } else if (gi
->libs
== 1) {
1017 board_capturable_add(board
, group
, gi
->lib
[0], onestone
);
1018 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
1019 } else if (gi
->libs
== 0)
1020 board_capturable_rm(board
, group
, lib
, onestone
);
1024 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
1025 * can call this multiple times per coord. */
1026 if (!u
) check_libs_consistency(board
, group
);
1031 /* This is a low-level routine that doesn't maintain consistency
1032 * of all the board data structures. */
1034 board_remove_stone(struct board
*board
, group_t group
, coord_t c
, struct board_undo
*u
)
1036 enum stone color
= board_at(board
, c
);
1037 board_at(board
, c
) = S_NONE
;
1038 group_at(board
, c
) = 0;
1040 board_hash_update(board
, c
, color
);
1042 /* We mark as cannot-capture now. If this is a ko/snapback,
1043 * we will get incremented later in board_group_addlib(). */
1044 trait_at(board
, c
, S_BLACK
).cap
= trait_at(board
, c
, S_BLACK
).cap1
= 0;
1045 trait_at(board
, c
, S_WHITE
).cap
= trait_at(board
, c
, S_WHITE
).cap1
= 0;
1046 board_trait_queue(board
, c
);
1050 /* Increase liberties of surrounding groups */
1052 foreach_neighbor(board
, coord
, {
1053 dec_neighbor_count_at(board
, c
, color
);
1054 if (!u
) board_trait_queue(board
, c
);
1055 group_t g
= group_at(board
, c
);
1056 if (g
&& g
!= group
)
1057 board_group_addlib(board
, g
, coord
, u
);
1062 /* board_hash_update() might have seen the freed up point as able
1063 * to capture another group in atari that only after the loop
1064 * above gained enough liberties. Reset pat3 again. */
1065 board
->pat3
[c
] = pattern3_hash(board
, c
);
1069 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
1070 board
->f
[board
->flen
++] = c
;
1073 static int profiling_noinline
1074 board_group_capture(struct board
*board
, group_t group
, struct board_undo
*u
)
1078 foreach_in_group(board
, group
) {
1079 board
->captures
[stone_other(board_at(board
, c
))]++;
1080 board_remove_stone(board
, group
, c
, u
);
1082 } foreach_in_group_end
;
1084 struct group
*gi
= &board_group_info(board
, group
);
1085 assert(gi
->libs
== 0);
1086 memset(gi
, 0, sizeof(*gi
));
1092 static void profiling_noinline
1093 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
, struct board_undo
*u
)
1096 struct group
*gi
= &board_group_info(board
, group
);
1097 bool onestone
= group_is_onestone(board
, group
);
1099 if (!u
&& gi
->libs
== 1) {
1100 /* Our group is temporarily in atari; make sure the capturable
1101 * counts also correspond to the newly added stone before we
1102 * start adding liberties again so bump-dump ops match. */
1103 enum stone capturing_color
= stone_other(board_at(board
, group
));
1104 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1106 coord_t lib
= board_group_info(board
, group
).lib
[0];
1107 if (coord_is_adjecent(lib
, coord
, board
)) {
1109 fprintf(stderr
, "add_to_group %s: %s[%d] bump\n", coord2sstr(group
, board
), coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
1110 trait_at(board
, lib
, capturing_color
).cap
++;
1111 /* This is never a 1-stone group, obviously. */
1112 board_trait_queue(board
, lib
);
1116 /* We are not 1-stone group anymore, update the cap1
1117 * counter specifically. */
1118 foreach_neighbor(board
, group
, {
1119 if (board_at(board
, c
) != S_NONE
) continue;
1120 trait_at(board
, c
, capturing_color
).cap1
--;
1121 board_trait_queue(board
, c
);
1127 group_at(board
, coord
) = group
;
1128 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
1129 groupnext_at(board
, prevstone
) = coord
;
1131 foreach_neighbor(board
, coord
, {
1132 if (board_at(board
, c
) == S_NONE
)
1133 board_group_addlib(board
, group
, c
, u
);
1137 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1138 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
1139 coord_x(coord
, board
), coord_y(coord
, board
),
1140 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
1144 static void profiling_noinline
1145 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
, struct board_undo
*u
)
1148 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
1149 group_base(group_from
), group_base(group_to
));
1150 struct group
*gi_from
= &board_group_info(board
, group_from
);
1151 struct group
*gi_to
= &board_group_info(board
, group_to
);
1152 bool onestone_from
= group_is_onestone(board
, group_from
);
1153 bool onestone_to
= group_is_onestone(board
, group_to
);
1156 /* We do this early before the group info is rewritten. */
1157 if (gi_from
->libs
== 2)
1158 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
1159 else if (gi_from
->libs
== 1)
1160 board_capturable_rm(board
, group_from
, gi_from
->lib
[0], onestone_from
);
1164 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
1166 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
1167 for (int i
= 0; i
< gi_from
->libs
; i
++) {
1168 for (int j
= 0; j
< gi_to
->libs
; j
++)
1169 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
1172 if (gi_to
->libs
== 0) {
1173 board_capturable_add(board
, group_to
, gi_from
->lib
[i
], onestone_to
);
1174 } else if (gi_to
->libs
== 1) {
1175 board_capturable_rm(board
, group_to
, gi_to
->lib
[0], onestone_to
);
1176 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
1177 } else if (gi_to
->libs
== 2) {
1178 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
1181 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
1182 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
1188 if (!u
&& gi_to
->libs
== 1) {
1189 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
1191 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
1192 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1194 /* Our group is currently in atari; make sure we properly
1195 * count in even the neighbors from the other group in the
1196 * capturable counter. */
1197 foreach_neighbor(board
, lib
, {
1198 if (DEBUGL(8) && group_at(board
, c
) == group_from
)
1199 fprintf(stderr
, "%s[%d] cap bump\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
1200 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
1201 /* This is never a 1-stone group, obviously. */
1203 board_trait_queue(board
, lib
);
1206 /* We are not 1-stone group anymore, update the cap1
1207 * counter specifically. */
1208 foreach_neighbor(board
, group_to
, {
1209 if (board_at(board
, c
) != S_NONE
) continue;
1210 trait_at(board
, c
, capturing_color
).cap1
--;
1211 board_trait_queue(board
, c
);
1216 if (gi_from
->libs
== 1) {
1217 /* We removed group_from from capturable groups,
1218 * therefore switching the atari flag off.
1219 * We need to set it again since group_to is also
1222 foreach_neighbor(board
, lib
, {
1223 board
->pat3
[lib
] |= (group_at(board
, c
) == group_from
) << (16 + 3 - fn__i
);
1230 coord_t last_in_group
;
1231 foreach_in_group(board
, group_from
) {
1233 group_at(board
, c
) = group_to
;
1234 } foreach_in_group_end
;
1236 if (u
) u
->merged
[++u
->nmerged_tmp
].last
= last_in_group
;
1237 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1238 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1239 memset(gi_from
, 0, sizeof(struct group
));
1242 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1243 group_base(group_to
));
1246 static group_t profiling_noinline
1247 new_group(struct board
*board
, coord_t coord
, struct board_undo
*u
)
1249 group_t group
= coord
;
1250 struct group
*gi
= &board_group_info(board
, group
);
1251 foreach_neighbor(board
, coord
, {
1252 if (board_at(board
, c
) == S_NONE
)
1253 /* board_group_addlib is ridiculously expensive for us */
1254 #if GROUP_KEEP_LIBS < 4
1255 if (gi
->libs
< GROUP_KEEP_LIBS
)
1257 gi
->lib
[gi
->libs
++] = c
;
1260 group_at(board
, coord
) = group
;
1261 groupnext_at(board
, coord
) = 0;
1265 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1266 else if (gi
->libs
== 1)
1267 board_capturable_add(board
, group
, gi
->lib
[0], true);
1268 check_libs_consistency(board
, group
);
1272 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1273 coord_x(coord
, board
), coord_y(coord
, board
),
1280 undo_save_merge(struct board
*b
, struct board_undo
*u
, group_t g
, coord_t c
)
1282 if (g
== u
->merged
[0].group
|| g
== u
->merged
[1].group
||
1283 g
== u
->merged
[2].group
|| g
== u
->merged
[3].group
)
1286 int i
= u
->nmerged
++;
1289 u
->merged
[i
].group
= g
;
1290 u
->merged
[i
].last
= 0; // can remove
1291 u
->merged
[i
].info
= board_group_info(b
, g
);
1295 undo_save_enemy(struct board
*b
, struct board_undo
*u
, group_t g
)
1297 if (g
== u
->enemies
[0].group
|| g
== u
->enemies
[1].group
||
1298 g
== u
->enemies
[2].group
|| g
== u
->enemies
[3].group
)
1301 int i
= u
->nenemies
++;
1302 u
->enemies
[i
].group
= g
;
1303 u
->enemies
[i
].info
= board_group_info(b
, g
);
1306 coord_t
*stones
= u
->enemies
[i
].stones
;
1307 if (board_group_info(b
, g
).libs
<= 1) { // Will be captured
1308 foreach_in_group(b
, g
) {
1310 } foreach_in_group_end
;
1317 undo_save_group_info(struct board
*b
, coord_t coord
, enum stone color
, struct board_undo
*u
)
1319 u
->next_at
= groupnext_at(b
, coord
);
1321 foreach_neighbor(b
, coord
, {
1322 group_t g
= group_at(b
, c
);
1324 if (board_at(b
, c
) == color
)
1325 undo_save_merge(b
, u
, g
, c
);
1326 else if (board_at(b
, c
) == stone_other(color
))
1327 undo_save_enemy(b
, u
, g
);
1332 undo_save_suicide(struct board
*b
, coord_t coord
, enum stone color
, struct board_undo
*u
)
1334 foreach_neighbor(b
, coord
, {
1335 if (board_at(b
, c
) == color
) {
1336 // Handle suicide as a capture ...
1337 undo_save_enemy(b
, u
, group_at(b
, c
));
1344 static inline group_t
1345 play_one_neighbor(struct board
*board
,
1346 coord_t coord
, enum stone color
, enum stone other_color
,
1347 coord_t c
, group_t group
, struct board_undo
*u
)
1349 enum stone ncolor
= board_at(board
, c
);
1350 group_t ngroup
= group_at(board
, c
);
1352 inc_neighbor_count_at(board
, c
, color
);
1353 /* We can be S_NONE, in that case we need to update the safety
1354 * trait since we might be left with only one liberty. */
1355 if (!u
) board_trait_queue(board
, c
);
1360 board_group_rmlib(board
, ngroup
, coord
, u
);
1362 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1363 group_base(ngroup
), ncolor
, color
, other_color
);
1365 if (ncolor
== color
&& ngroup
!= group
) {
1368 add_to_group(board
, group
, c
, coord
, u
);
1370 merge_groups(board
, group
, ngroup
, u
);
1372 } else if (ncolor
== other_color
) {
1374 struct group
*gi
= &board_group_info(board
, ngroup
);
1375 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1376 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1377 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1378 fprintf(stderr
, "\n");
1380 if (unlikely(board_group_captured(board
, ngroup
)))
1381 board_group_capture(board
, ngroup
, u
);
1386 /* We played on a place with at least one liberty. We will become a member of
1387 * some group for sure. */
1388 static group_t profiling_noinline
1389 board_play_outside(struct board
*board
, struct move
*m
, int f
, struct board_undo
*u
)
1391 coord_t coord
= m
->coord
;
1392 enum stone color
= m
->color
;
1393 enum stone other_color
= stone_other(color
);
1397 undo_save_group_info(board
, coord
, color
, u
);
1399 board
->f
[f
] = board
->f
[--board
->flen
];
1401 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1403 #if defined(BOARD_TRAITS) && defined(DEBUG)
1404 /* Sanity check that cap matches reality. */
1407 foreach_neighbor(board
, coord
, {
1408 group_t g
= group_at(board
, c
);
1409 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1410 b
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1) && group_is_onestone(board
, g
);
1412 assert(a
== trait_at(board
, coord
, color
).cap
);
1413 assert(b
== trait_at(board
, coord
, color
).cap1
);
1414 #ifdef BOARD_TRAIT_SAFE
1415 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1420 foreach_neighbor(board
, coord
, {
1421 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
, u
);
1424 board_at(board
, coord
) = color
;
1425 if (unlikely(!group
))
1426 group
= new_group(board
, coord
, u
);
1429 board
->last_move4
= board
->last_move3
;
1430 board
->last_move3
= board
->last_move2
;
1432 board
->last_move2
= board
->last_move
;
1433 board
->last_move
= *m
;
1436 board_hash_update(board
, coord
, color
);
1437 board_symmetry_update(board
, &board
->symmetry
, coord
);
1439 struct move ko
= { pass
, S_NONE
};
1442 if (!u
) check_pat3_consistency(board
, coord
);
1447 /* We played in an eye-like shape. Either we capture at least one of the eye
1448 * sides in the process of playing, or return -1. */
1449 static int profiling_noinline
1450 board_play_in_eye(struct board
*board
, struct move
*m
, int f
, struct board_undo
*u
)
1452 coord_t coord
= m
->coord
;
1453 enum stone color
= m
->color
;
1454 /* Check ko: Capture at a position of ko capture one move ago */
1455 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1457 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1459 } else if (DEBUGL(6)) {
1460 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1461 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1462 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1465 struct move ko
= { pass
, S_NONE
};
1467 int captured_groups
= 0;
1469 foreach_neighbor(board
, coord
, {
1470 group_t g
= group_at(board
, c
);
1472 fprintf(stderr
, "board_check: group %d has %d libs\n",
1473 g
, board_group_info(board
, g
).libs
);
1474 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1477 if (likely(captured_groups
== 0)) {
1480 board_print(board
, stderr
);
1481 fprintf(stderr
, "board_check: one-stone suicide\n");
1489 /* We _will_ for sure capture something. */
1490 assert(trait_at(board
, coord
, color
).cap
> 0);
1491 #ifdef BOARD_TRAIT_SAFE
1492 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1496 board
->f
[f
] = board
->f
[--board
->flen
];
1498 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1501 undo_save_group_info(board
, coord
, color
, u
);
1504 coord_t cap_at
= pass
;
1505 foreach_neighbor(board
, coord
, {
1506 inc_neighbor_count_at(board
, c
, color
);
1507 /* Originally, this could not have changed any trait
1508 * since no neighbors were S_NONE, however by now some
1509 * of them might be removed from the board. */
1510 if (!u
) board_trait_queue(board
, c
);
1512 group_t group
= group_at(board
, c
);
1516 board_group_rmlib(board
, group
, coord
, u
);
1518 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1521 if (board_group_captured(board
, group
)) {
1522 ko_caps
+= board_group_capture(board
, group
, u
);
1527 ko
.color
= stone_other(color
);
1528 ko
.coord
= cap_at
; // unique
1529 board
->last_ko
= ko
;
1530 board
->last_ko_age
= board
->moves
;
1532 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1535 board_at(board
, coord
) = color
;
1536 group_t group
= new_group(board
, coord
, u
);
1539 board
->last_move4
= board
->last_move3
;
1540 board
->last_move3
= board
->last_move2
;
1542 board
->last_move2
= board
->last_move
;
1543 board
->last_move
= *m
;
1546 board_hash_update(board
, coord
, color
);
1547 board_hash_commit(board
);
1548 board_traits_recompute(board
);
1549 board_symmetry_update(board
, &board
->symmetry
, coord
);
1553 if (!u
) check_pat3_consistency(board
, coord
);
1558 static int __attribute__((flatten
))
1559 board_play_f(struct board
*board
, struct move
*m
, int f
, struct board_undo
*u
)
1562 fprintf(stderr
, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m
->coord
, board
), coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1564 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1565 /* NOT playing in an eye. Thus this move has to succeed. (This
1566 * is thanks to New Zealand rules. Otherwise, multi-stone
1567 * suicide might fail.) */
1568 group_t group
= board_play_outside(board
, m
, f
, u
);
1569 if (unlikely(board_group_captured(board
, group
))) {
1570 if (u
) undo_save_suicide(board
, m
->coord
, m
->color
, u
);
1571 board_group_capture(board
, group
, u
);
1574 board_hash_commit(board
);
1575 board_traits_recompute(board
);
1579 return board_play_in_eye(board
, m
, f
, u
);
1584 undo_init(struct board
*b
, struct move
*m
, struct board_undo
*u
)
1586 // Paranoid uninitialized mem test
1587 // memset(u, 0xff, sizeof(*u));
1589 u
->last_move2
= b
->last_move2
;
1591 u
->last_ko
= b
->last_ko
;
1592 u
->last_ko_age
= b
->last_ko_age
;
1595 u
->nmerged
= u
->nmerged_tmp
= u
->nenemies
= 0;
1596 for (int i
= 0; i
< 4; i
++)
1597 u
->merged
[i
].group
= u
->enemies
[i
].group
= 0;
1601 board_play_(struct board
*board
, struct move
*m
, struct board_undo
*u
)
1603 #ifdef BOARD_UNDO_CHECKS
1604 assert(u
|| !board
->quicked
);
1607 if (u
) undo_init(board
, m
, u
);
1609 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1610 if (is_pass(m
->coord
) && board
->rules
== RULES_SIMING
) {
1611 /* On pass, the player gives a pass stone
1612 * to the opponent. */
1613 board
->captures
[stone_other(m
->color
)]++;
1615 struct move nomove
= { pass
, S_NONE
};
1618 board
->last_move4
= board
->last_move3
;
1619 board
->last_move3
= board
->last_move2
;
1621 board
->last_move2
= board
->last_move
;
1622 board
->last_move
= *m
;
1627 return board_play_f(board
, m
, -1, u
);
1630 for (f
= 0; f
< board
->flen
; f
++)
1631 if (board
->f
[f
] == m
->coord
)
1632 return board_play_f(board
, m
, f
, u
);
1635 fprintf(stderr
, "board_check: stone exists\n");
1640 board_play(struct board
*board
, struct move
*m
)
1642 return board_play_(board
, m
, NULL
);
1646 board_quick_play(struct board
*board
, struct move
*m
, struct board_undo
*u
)
1648 int r
= board_play_(board
, m
, u
);
1649 #ifdef BOARD_UNDO_CHECKS
1657 undo_merge(struct board
*b
, struct board_undo
*u
, struct move
*m
)
1659 coord_t coord
= m
->coord
;
1660 group_t group
= group_at(b
, coord
);
1661 struct undo_merge
*merged
= u
->merged
;
1663 // Others groups, in reverse order ...
1664 for (int i
= u
->nmerged
- 1; i
> 0; i
--) {
1665 group_t old_group
= merged
[i
].group
;
1667 board_group_info(b
, old_group
) = merged
[i
].info
;
1669 groupnext_at(b
, group_base(group
)) = groupnext_at(b
, merged
[i
].last
);
1670 groupnext_at(b
, merged
[i
].last
) = 0;
1673 printf("merged_group[%i]: (last: %s)", i
, coord2sstr(merged
[i
].last
, b
));
1674 foreach_in_group(b
, old_group
) {
1675 printf("%s ", coord2sstr(c
, b
));
1676 } foreach_in_group_end
;
1680 foreach_in_group(b
, old_group
) {
1681 group_at(b
, c
) = old_group
;
1682 } foreach_in_group_end
;
1685 // Restore first group
1686 groupnext_at(b
, u
->inserted
) = groupnext_at(b
, coord
);
1687 board_group_info(b
, merged
[0].group
) = merged
[0].info
;
1690 printf("merged_group[0]: ");
1691 foreach_in_group(b
, merged
[0].group
) {
1692 printf("%s ", coord2sstr(c
, b
));
1693 } foreach_in_group_end
;
1700 restore_enemies(struct board
*b
, struct board_undo
*u
, struct move
*m
)
1702 enum stone color
= m
->color
;
1703 enum stone other_color
= stone_other(m
->color
);
1705 struct undo_enemy
*enemy
= u
->enemies
;
1706 for (int i
= 0; i
< u
->nenemies
; i
++) {
1707 group_t old_group
= enemy
[i
].group
;
1709 board_group_info(b
, old_group
) = enemy
[i
].info
;
1711 coord_t
*stones
= enemy
[i
].stones
;
1712 for (int j
= 0; stones
[j
]; j
++) {
1713 board_at(b
, stones
[j
]) = other_color
;
1714 group_at(b
, stones
[j
]) = old_group
;
1715 groupnext_at(b
, stones
[j
]) = stones
[j
+ 1];
1717 foreach_neighbor(b
, stones
[j
], {
1718 inc_neighbor_count_at(b
, c
, other_color
);
1721 // Update liberties of neighboring groups
1722 foreach_neighbor(b
, stones
[j
], {
1723 if (board_at(b
, c
) != color
)
1725 group_t g
= group_at(b
, c
);
1726 if (g
== u
->merged
[0].group
|| g
== u
->merged
[1].group
|| g
== u
->merged
[2].group
|| g
== u
->merged
[3].group
)
1728 board_group_rmlib(b
, g
, stones
[j
], u
);
1735 board_undo_stone(struct board
*b
, struct board_undo
*u
, struct move
*m
)
1737 coord_t coord
= m
->coord
;
1738 enum stone color
= m
->color
;
1740 * - put captures back
1743 //printf("nmerged: %i\n", u->nmerged);
1745 // Restore merged groups
1747 undo_merge(b
, u
, m
);
1748 else // Single stone group undo
1749 memset(&board_group_info(b
, group_at(b
, coord
)), 0, sizeof(struct group
));
1751 board_at(b
, coord
) = S_NONE
;
1752 group_at(b
, coord
) = 0;
1753 groupnext_at(b
, coord
) = u
->next_at
;
1755 foreach_neighbor(b
, coord
, {
1756 dec_neighbor_count_at(b
, c
, color
);
1759 // Restore enemy groups
1761 b
->captures
[color
] -= u
->captures
;
1762 restore_enemies(b
, u
, m
);
1767 restore_suicide(struct board
*b
, struct board_undo
*u
, struct move
*m
)
1769 enum stone color
= m
->color
;
1770 enum stone other_color
= stone_other(m
->color
);
1772 struct undo_enemy
*enemy
= u
->enemies
;
1773 for (int i
= 0; i
< u
->nenemies
; i
++) {
1774 group_t old_group
= enemy
[i
].group
;
1776 board_group_info(b
, old_group
) = enemy
[i
].info
;
1778 coord_t
*stones
= enemy
[i
].stones
;
1779 for (int j
= 0; stones
[j
]; j
++) {
1780 board_at(b
, stones
[j
]) = other_color
;
1781 group_at(b
, stones
[j
]) = old_group
;
1782 groupnext_at(b
, stones
[j
]) = stones
[j
+ 1];
1784 foreach_neighbor(b
, stones
[j
], {
1785 inc_neighbor_count_at(b
, c
, other_color
);
1788 // Update liberties of neighboring groups
1789 foreach_neighbor(b
, stones
[j
], {
1790 if (board_at(b
, c
) != color
)
1792 group_t g
= group_at(b
, c
);
1793 if (g
== u
->enemies
[0].group
|| g
== u
->enemies
[1].group
||
1794 g
== u
->enemies
[2].group
|| g
== u
->enemies
[3].group
)
1796 board_group_rmlib(b
, g
, stones
[j
], u
);
1804 board_undo_suicide(struct board
*b
, struct board_undo
*u
, struct move
*m
)
1806 coord_t coord
= m
->coord
;
1807 enum stone other_color
= stone_other(m
->color
);
1809 // Pretend it's capture ...
1810 struct move m2
= { .coord
= m
->coord
, .color
= other_color
};
1811 b
->captures
[other_color
] -= u
->captures
;
1813 restore_suicide(b
, u
, &m2
);
1815 undo_merge(b
, u
, m
);
1817 board_at(b
, coord
) = S_NONE
;
1818 group_at(b
, coord
) = 0;
1819 groupnext_at(b
, coord
) = u
->next_at
;
1821 foreach_neighbor(b
, coord
, {
1822 dec_neighbor_count_at(b
, c
, m
->color
);
1829 board_quick_undo(struct board
*b
, struct move
*m
, struct board_undo
*u
)
1831 #ifdef BOARD_UNDO_CHECKS
1835 b
->last_move
= b
->last_move2
;
1836 b
->last_move2
= u
->last_move2
;
1838 b
->last_ko
= u
->last_ko
;
1839 b
->last_ko_age
= u
->last_ko_age
;
1841 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
)))
1846 if (likely(board_at(b
, m
->coord
) == m
->color
))
1847 board_undo_stone(b
, u
, m
);
1848 else if (board_at(b
, m
->coord
) == S_NONE
)
1849 board_undo_suicide(b
, u
, m
);
1851 assert(0); /* Anything else doesn't make sense */
1855 /* Undo, supported only for pass moves. This form of undo is required by KGS
1856 * to settle disputes on dead groups. See also fast_board_undo() */
1857 int board_undo(struct board
*board
)
1859 if (!is_pass(board
->last_move
.coord
))
1861 if (board
->rules
== RULES_SIMING
) {
1862 /* Return pass stone to the passing player. */
1863 board
->captures
[stone_other(board
->last_move
.color
)]--;
1865 board
->last_move
= board
->last_move2
;
1866 board
->last_move2
= board
->last_move3
;
1867 board
->last_move3
= board
->last_move4
;
1868 if (board
->last_ko_age
== board
->moves
)
1869 board
->ko
= board
->last_ko
;
1874 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1877 struct move m
= { *coord
, color
};
1879 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
));
1880 if (unlikely(board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1881 || !board_is_valid_move(b
, &m
)
1882 || (permit
&& !permit(permit_data
, b
, &m
)))
1884 if (m
.coord
== *coord
) {
1885 return likely(board_play_f(b
, &m
, f
, NULL
) >= 0);
1887 *coord
= m
.coord
; // permit modified the coordinate
1888 return likely(board_play(b
, &m
) >= 0);
1893 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1895 if (unlikely(b
->flen
== 0))
1898 int base
= fast_random(b
->flen
), f
;
1899 for (f
= base
; f
< b
->flen
; f
++)
1900 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1902 for (f
= 0; f
< base
; f
++)
1903 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1908 struct move m
= { pass
, color
};
1914 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1916 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1918 /* XXX: We attempt false eye detection but we will yield false
1919 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1921 foreach_diag_neighbor(board
, coord
) {
1922 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1923 } foreach_diag_neighbor_end
;
1924 /* For false eye, we need two enemy stones diagonally in the
1925 * middle of the board, or just one enemy stone at the edge
1926 * or in the corner. */
1927 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1928 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1932 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1934 return board_is_eyelike(board
, coord
, eye_color
)
1935 && !board_is_false_eyelike(board
, coord
, eye_color
);
1939 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1941 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1943 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1951 board_fast_score(struct board
*board
)
1954 memset(scores
, 0, sizeof(scores
));
1956 foreach_point(board
) {
1957 enum stone color
= board_at(board
, c
);
1958 if (color
== S_NONE
&& board
->rules
!= RULES_STONES_ONLY
)
1959 color
= board_get_one_point_eye(board
, c
);
1961 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1962 } foreach_point_end
;
1964 return board
->komi
+ (board
->rules
!= RULES_SIMING
? board
->handicap
: 0) + scores
[S_WHITE
] - scores
[S_BLACK
];
1967 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1969 /* One flood-fill iteration; returns true if next iteration
1972 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1974 bool needs_update
= false;
1975 foreach_free_point(board
) {
1976 /* Ignore occupied and already-dame positions. */
1977 assert(board_at(board
, c
) == S_NONE
);
1978 if (board
->rules
== RULES_STONES_ONLY
)
1980 if (ownermap
[c
] == 3)
1982 /* Count neighbors. */
1984 foreach_neighbor(board
, c
, {
1987 /* If we have neighbors of both colors, or dame,
1988 * we are dame too. */
1989 if ((nei
[1] && nei
[2]) || nei
[3]) {
1991 /* Speed up the propagation. */
1992 foreach_neighbor(board
, c
, {
1993 if (board_at(board
, c
) == S_NONE
)
1996 needs_update
= true;
1999 /* If we have neighbors of one color, we are owned
2000 * by that color, too. */
2001 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
2002 int newowner
= nei
[1] ? 1 : 2;
2003 ownermap
[c
] = newowner
;
2004 /* Speed up the propagation. */
2005 foreach_neighbor(board
, c
, {
2006 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
2007 ownermap
[c
] = newowner
;
2009 needs_update
= true;
2012 } foreach_free_point_end
;
2013 return needs_update
;
2016 /* Tromp-Taylor Counting */
2018 board_official_score(struct board
*board
, struct move_queue
*q
)
2021 /* A point P, not colored C, is said to reach C, if there is a path of
2022 * (vertically or horizontally) adjacent points of P's color from P to
2023 * a point of color C.
2025 * A player's score is the number of points of her color, plus the
2026 * number of empty points that reach only her color. */
2028 int ownermap
[board_size2(board
)];
2030 const int o
[4] = {0, 1, 2, 0};
2031 foreach_point(board
) {
2032 ownermap
[c
] = o
[board_at(board
, c
)];
2033 s
[board_at(board
, c
)]++;
2034 } foreach_point_end
;
2037 /* Process dead groups. */
2038 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
2039 foreach_in_group(board
, q
->move
[i
]) {
2040 enum stone color
= board_at(board
, c
);
2041 ownermap
[c
] = o
[stone_other(color
)];
2042 s
[color
]--; s
[stone_other(color
)]++;
2043 } foreach_in_group_end
;
2047 /* We need to special-case empty board. */
2048 if (!s
[S_BLACK
] && !s
[S_WHITE
])
2051 while (board_tromp_taylor_iter(board
, ownermap
))
2052 /* Flood-fill... */;
2055 memset(scores
, 0, sizeof(scores
));
2057 foreach_point(board
) {
2058 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
2059 if (ownermap
[c
] == 3)
2061 scores
[ownermap
[c
]]++;
2062 } foreach_point_end
;
2064 return board
->komi
+ (board
->rules
!= RULES_SIMING
? board
->handicap
: 0) + scores
[S_WHITE
] - scores
[S_BLACK
];
2068 board_set_rules(struct board
*board
, char *name
)
2070 if (!strcasecmp(name
, "japanese")) {
2071 board
->rules
= RULES_JAPANESE
;
2072 } else if (!strcasecmp(name
, "chinese")) {
2073 board
->rules
= RULES_CHINESE
;
2074 } else if (!strcasecmp(name
, "aga")) {
2075 board
->rules
= RULES_AGA
;
2076 } else if (!strcasecmp(name
, "new_zealand")) {
2077 board
->rules
= RULES_NEW_ZEALAND
;
2078 } else if (!strcasecmp(name
, "siming") || !strcasecmp(name
, "simplified_ing")) {
2079 board
->rules
= RULES_SIMING
;