13 #include "patternsp.h"
16 bool random_pass
= false;
20 #define profiling_noinline __attribute__((noinline))
22 #define profiling_noinline
25 #define gi_granularity 4
26 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
30 board_setup(struct board
*b
)
32 memset(b
, 0, sizeof(*b
));
34 struct move m
= { pass
, S_NONE
};
35 b
->last_move
= b
->last_move2
= b
->last_ko
= b
->ko
= m
;
41 struct board
*b
= malloc(sizeof(struct board
));
52 board_copy(struct board
*b2
, struct board
*b1
)
54 memcpy(b2
, b1
, sizeof(struct board
));
56 int bsize
= board_size2(b2
) * sizeof(*b2
->b
);
57 int gsize
= board_size2(b2
) * sizeof(*b2
->g
);
58 int fsize
= board_size2(b2
) * sizeof(*b2
->f
);
59 int nsize
= board_size2(b2
) * sizeof(*b2
->n
);
60 int psize
= board_size2(b2
) * sizeof(*b2
->p
);
61 int hsize
= board_size2(b2
) * 2 * sizeof(*b2
->h
);
62 int gisize
= board_size2(b2
) * sizeof(*b2
->gi
);
64 int csize
= board_size2(b2
) * sizeof(*b2
->c
);
69 int ssize
= board_size2(b2
) * sizeof(*b2
->spathash
);
73 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
);
74 memcpy(x
, b1
->b
, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
);
75 b2
->b
= x
; x
+= bsize
;
76 b2
->g
= x
; x
+= gsize
;
77 b2
->f
= x
; x
+= fsize
;
78 b2
->p
= x
; x
+= psize
;
79 b2
->n
= x
; x
+= nsize
;
80 b2
->h
= x
; x
+= hsize
;
81 b2
->gi
= x
; x
+= gisize
;
83 b2
->c
= x
; x
+= csize
;
86 b2
->spathash
= x
; x
+= ssize
;
93 board_done_noalloc(struct board
*board
)
95 if (board
->b
) free(board
->b
);
99 board_done(struct board
*board
)
101 board_done_noalloc(board
);
106 board_resize(struct board
*board
, int size
)
109 assert(board_size(board
) == size
+ 2);
111 board_size(board
) = size
+ 2 /* S_OFFBOARD margin */;
112 board_size2(board
) = board_size(board
) * board_size(board
);
117 int bsize
= board_size2(board
) * sizeof(*board
->b
);
118 int gsize
= board_size2(board
) * sizeof(*board
->g
);
119 int fsize
= board_size2(board
) * sizeof(*board
->f
);
120 int nsize
= board_size2(board
) * sizeof(*board
->n
);
121 int psize
= board_size2(board
) * sizeof(*board
->p
);
122 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
123 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
125 int csize
= board_size2(board
) * sizeof(*board
->c
);
129 #ifdef BOARD_SPATHASH
130 int ssize
= board_size2(board
) * sizeof(*board
->spathash
);
134 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
);
135 memset(x
, 0, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
);
136 board
->b
= x
; x
+= bsize
;
137 board
->g
= x
; x
+= gsize
;
138 board
->f
= x
; x
+= fsize
;
139 board
->p
= x
; x
+= psize
;
140 board
->n
= x
; x
+= nsize
;
141 board
->h
= x
; x
+= hsize
;
142 board
->gi
= x
; x
+= gisize
;
144 board
->c
= x
; x
+= csize
;
146 #ifdef BOARD_SPATHASH
147 board
->spathash
= x
; x
+= ssize
;
152 board_clear(struct board
*board
)
154 int size
= board_size(board
);
155 float komi
= board
->komi
;
157 board_done_noalloc(board
);
159 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
163 /* Setup initial symmetry */
164 board
->symmetry
.d
= 1;
165 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
166 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
167 board
->symmetry
.type
= SYM_FULL
;
169 /* Draw the offboard margin */
170 int top_row
= board_size2(board
) - board_size(board
);
172 for (i
= 0; i
< board_size(board
); i
++)
173 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
174 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
175 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
177 foreach_point(board
) {
179 if (board_at(board
, coord
) == S_OFFBOARD
)
181 foreach_neighbor(board
, c
, {
182 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
186 /* First, pass is always a free position. */
187 board
->f
[board
->flen
++] = coord_raw(pass
);
188 /* All positions are free! Except the margin. */
189 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
190 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
191 board
->f
[board
->flen
++] = i
;
193 /* Initialize zobrist hashtable. */
194 foreach_point(board
) {
195 int max
= (sizeof(hash_t
) << history_hash_bits
);
196 /* fast_random() is 16-bit only */
197 board
->h
[coord_raw(c
) * 2] = ((hash_t
) fast_random(max
))
198 | ((hash_t
) fast_random(max
) << 16)
199 | ((hash_t
) fast_random(max
) << 32)
200 | ((hash_t
) fast_random(max
) << 48);
201 if (!board
->h
[coord_raw(c
) * 2])
202 /* Would be kinda "oops". */
203 board
->h
[coord_raw(c
) * 2] = 1;
204 /* And once again for white */
205 board
->h
[coord_raw(c
) * 2 + 1] = ((hash_t
) fast_random(max
))
206 | ((hash_t
) fast_random(max
) << 16)
207 | ((hash_t
) fast_random(max
) << 32)
208 | ((hash_t
) fast_random(max
) << 48);
209 if (!board
->h
[coord_raw(c
) * 2 + 1])
210 board
->h
[coord_raw(c
) * 2 + 1] = 1;
213 #ifdef BOARD_SPATHASH
214 /* Initialize spatial hashes. */
215 foreach_point(board
) {
216 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
217 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
218 ptcoords_at(x
, y
, c
, board
, j
);
219 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1] ^=
220 pthashes
[0][j
][board_at(board
, c
)];
229 board_print_top(struct board
*board
, FILE *f
, int c
)
231 for (int i
= 0; i
< c
; i
++) {
232 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
234 for (int x
= 1; x
< board_size(board
) - 1; x
++)
235 fprintf(f
, "%c ", asdf
[x
- 1]);
239 for (int i
= 0; i
< c
; i
++) {
241 for (int x
= 1; x
< board_size(board
) - 1; x
++)
249 board_print_bottom(struct board
*board
, FILE *f
, int c
)
251 for (int i
= 0; i
< c
; i
++) {
253 for (int x
= 1; x
< board_size(board
) - 1; x
++)
261 board_print_row(struct board
*board
, int y
, FILE *f
, board_cprint cprint
)
263 fprintf(f
, " %2d | ", y
);
264 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
265 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
266 fprintf(f
, "%c)", stone2char(board_atxy(board
, x
, y
)));
268 fprintf(f
, "%c ", stone2char(board_atxy(board
, x
, y
)));
272 fprintf(f
, " %2d | ", y
);
273 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
274 cprint(board
, coord_xy(board
, x
, y
), f
);
282 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
284 fprintf(f
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
285 board
->moves
, board
->komi
, board
->handicap
,
286 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
287 board_print_top(board
, f
, 1 + !!cprint
);
288 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
289 board_print_row(board
, y
, f
, cprint
);
290 board_print_bottom(board
, f
, 1 + !!cprint
);
295 cprint_group(struct board
*board
, coord_t c
, FILE *f
)
297 fprintf(f
, "%d ", group_base(group_at(board
, c
)));
301 board_print(struct board
*board
, FILE *f
)
303 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
307 /* Update board hash with given coordinate. */
308 static void profiling_noinline
309 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
311 board
->hash
^= hash_at(board
, coord
, color
);
313 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
);
315 #ifdef BOARD_SPATHASH
316 /* Gridcular metric is reflective, so we update all hashes
317 * of appropriate ditance in OUR circle. */
318 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
319 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
320 ptcoords_at(x
, y
, coord
, board
, j
);
321 /* We either changed from S_NONE to color
322 * or vice versa; doesn't matter. */
323 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1] ^=
324 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
330 /* Commit current board hash to history. */
331 static void profiling_noinline
332 board_hash_commit(struct board
*board
)
335 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
336 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
337 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
339 hash_t i
= board
->hash
;
340 while (board
->history_hash
[i
& history_hash_mask
]) {
341 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
343 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
344 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
345 board
->superko_violation
= true;
348 i
= history_hash_next(i
);
350 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
356 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
358 if (likely(symmetry
->type
== SYM_NONE
)) {
359 /* Fully degenerated already. We do not support detection
360 * of restoring of symmetry, assuming that this is too rare
361 * a case to handle. */
365 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
366 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
368 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
369 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
370 symmetry
->d
, symmetry
->type
, x
, y
);
373 switch (symmetry
->type
) {
375 if (x
== t
&& y
== t
) {
376 /* Tengen keeps full symmetry. */
379 /* New symmetry now? */
381 symmetry
->type
= SYM_DIAG_UP
;
382 symmetry
->x1
= symmetry
->y1
= 1;
383 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
385 } else if (dx
== y
) {
386 symmetry
->type
= SYM_DIAG_DOWN
;
387 symmetry
->x1
= symmetry
->y1
= 1;
388 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
391 symmetry
->type
= SYM_HORIZ
;
393 symmetry
->y2
= board_size(b
) - 1;
396 symmetry
->type
= SYM_VERT
;
398 symmetry
->x2
= board_size(b
) - 1;
402 symmetry
->type
= SYM_NONE
;
403 symmetry
->x1
= symmetry
->y1
= 1;
404 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
430 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
431 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
432 symmetry
->d
, symmetry
->type
);
439 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
442 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
444 board_play(board
, &m
);
445 /* Simulate white passing; otherwise, UCT search can get confused since
446 * tree depth parity won't match the color to move. */
449 char *str
= coord2str(m
.coord
, board
);
451 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
452 fprintf(f
, "%s ", str
);
457 board_handicap(struct board
*board
, int stones
, FILE *f
)
459 int margin
= 3 + (board_size(board
) >= 13);
461 int mid
= board_size(board
) / 2;
462 int max
= board_size(board
) - 1 - margin
;
463 const int places
[][2] = {
464 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
465 { min
, mid
}, { max
, mid
},
466 { mid
, min
}, { mid
, max
},
470 board
->handicap
= stones
;
472 if (stones
== 5 || stones
== 7) {
473 board_handicap_stone(board
, mid
, mid
, f
);
478 for (i
= 0; i
< stones
; i
++)
479 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
483 static void __attribute__((noinline
))
484 check_libs_consistency(struct board
*board
, group_t g
)
488 struct group
*gi
= &board_group_info(board
, g
);
489 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
490 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
491 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
498 board_capturable_add(struct board
*board
, group_t group
)
501 //fprintf(stderr, "add of group %d (%d)\n", group_base(group), board->clen);
503 assert(board
->clen
< board_size2(board
));
504 board
->c
[board
->clen
++] = group
;
508 board_capturable_rm(struct board
*board
, group_t group
)
511 //fprintf(stderr, "rm of group %d\n", group_base(group));
512 for (int i
= 0; i
< board
->clen
; i
++) {
513 if (unlikely(board
->c
[i
] == group
)) {
514 board
->c
[i
] = board
->c
[--board
->clen
];
518 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
524 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
527 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
528 group_base(group
), coord2sstr(group_base(group
), board
),
529 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
532 check_libs_consistency(board
, group
);
534 struct group
*gi
= &board_group_info(board
, group
);
535 if (gi
->libs
< GROUP_KEEP_LIBS
) {
536 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
538 /* Seems extra branch just slows it down */
542 if (unlikely(gi
->lib
[i
] == coord
))
546 board_capturable_add(board
, group
);
547 else if (gi
->libs
== 1)
548 board_capturable_rm(board
, group
);
549 gi
->lib
[gi
->libs
++] = coord
;
552 check_libs_consistency(board
, group
);
556 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
558 /* Add extra liberty from the board to our liberty list. */
559 unsigned char watermark
[board_size2(board
) / 8];
560 memset(watermark
, 0, sizeof(watermark
));
561 #define watermark_get(c) (watermark[coord_raw(c) >> 3] & (1 << (coord_raw(c) & 7)))
562 #define watermark_set(c) watermark[coord_raw(c) >> 3] |= (1 << (coord_raw(c) & 7))
564 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
565 watermark_set(gi
->lib
[i
]);
566 watermark_set(avoid
);
568 foreach_in_group(board
, group
) {
570 foreach_neighbor(board
, coord2
, {
571 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
574 gi
->lib
[gi
->libs
++] = c
;
575 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
578 } foreach_in_group_end
;
584 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
587 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
588 group_base(group
), coord2sstr(group_base(group
), board
),
589 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
592 struct group
*gi
= &board_group_info(board
, group
);
593 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
595 /* Seems extra branch just slows it down */
599 if (likely(gi
->lib
[i
] != coord
))
602 gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
603 gi
->lib
[gi
->libs
] = 0;
605 check_libs_consistency(board
, group
);
607 /* Postpone refilling lib[] until we need to. */
608 assert(GROUP_REFILL_LIBS
> 1);
609 if (gi
->libs
> GROUP_REFILL_LIBS
)
611 if (gi
->libs
== GROUP_REFILL_LIBS
)
612 board_group_find_extra_libs(board
, group
, gi
, coord
);
615 board_capturable_add(board
, group
);
616 else if (gi
->libs
== 0)
617 board_capturable_rm(board
, group
);
621 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
622 * can call this multiple times per coord. */
623 check_libs_consistency(board
, group
);
628 /* This is a low-level routine that doesn't maintain consistency
629 * of all the board data structures. */
631 board_remove_stone(struct board
*board
, coord_t c
)
633 enum stone color
= board_at(board
, c
);
634 board_at(board
, c
) = S_NONE
;
635 group_at(board
, c
) = 0;
636 board_hash_update(board
, c
, color
);
638 /* Increase liberties of surrounding groups */
640 foreach_neighbor(board
, coord
, {
641 dec_neighbor_count_at(board
, c
, color
);
642 group_t g
= group_at(board
, c
);
644 board_group_addlib(board
, g
, coord
);
648 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
649 board
->f
[board
->flen
++] = coord_raw(c
);
652 static int profiling_noinline
653 board_group_capture(struct board
*board
, group_t group
)
657 foreach_in_group(board
, group
) {
658 board
->captures
[stone_other(board_at(board
, c
))]++;
659 board_remove_stone(board
, c
);
661 } foreach_in_group_end
;
663 if (board_group_info(board
, group
).libs
== 1)
664 board_capturable_rm(board
, group
);
665 memset(&board_group_info(board
, group
), 0, sizeof(struct group
));
671 static void profiling_noinline
672 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
674 foreach_neighbor(board
, coord
, {
675 if (board_at(board
, c
) == S_NONE
)
676 board_group_addlib(board
, group
, c
);
679 group_at(board
, coord
) = group
;
680 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
681 groupnext_at(board
, prevstone
) = coord_raw(coord
);
684 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
685 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
686 coord_x(coord
, board
), coord_y(coord
, board
),
687 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
691 static void profiling_noinline
692 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
695 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
696 group_base(group_from
), group_base(group_to
));
698 coord_t last_in_group
;
699 foreach_in_group(board
, group_from
) {
701 group_at(board
, c
) = group_to
;
702 } foreach_in_group_end
;
703 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
704 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
706 struct group
*gi_from
= &board_group_info(board
, group_from
);
707 struct group
*gi_to
= &board_group_info(board
, group_to
);
708 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
709 for (int i
= 0; i
< gi_from
->libs
; i
++) {
710 for (int j
= 0; j
< gi_to
->libs
; j
++)
711 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
713 if (gi_to
->libs
== 0)
714 board_capturable_add(board
, group_to
);
715 else if (gi_to
->libs
== 1)
716 board_capturable_rm(board
, group_to
);
717 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
718 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
724 if (gi_from
->libs
== 1)
725 board_capturable_rm(board
, group_from
);
726 memset(gi_from
, 0, sizeof(struct group
));
729 fprintf(stderr
, "board_play_raw: merged group: %d\n",
730 group_base(group_to
));
733 static group_t profiling_noinline
734 new_group(struct board
*board
, coord_t coord
)
736 group_t group
= coord_raw(coord
);
737 struct group
*gi
= &board_group_info(board
, group
);
738 foreach_neighbor(board
, coord
, {
739 if (board_at(board
, c
) == S_NONE
)
740 /* board_group_addlib is ridiculously expensive for us */
741 #if GROUP_KEEP_LIBS < 4
742 if (gi
->libs
< GROUP_KEEP_LIBS
)
744 gi
->lib
[gi
->libs
++] = c
;
747 board_capturable_add(board
, group
);
748 check_libs_consistency(board
, group
);
750 group_at(board
, coord
) = group
;
751 groupnext_at(board
, coord
) = 0;
754 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
755 coord_x(coord
, board
), coord_y(coord
, board
),
761 static inline group_t
762 play_one_neighbor(struct board
*board
,
763 coord_t coord
, enum stone color
, enum stone other_color
,
764 coord_t c
, group_t group
)
766 enum stone ncolor
= board_at(board
, c
);
767 group_t ngroup
= group_at(board
, c
);
769 inc_neighbor_count_at(board
, c
, color
);
774 board_group_rmlib(board
, ngroup
, coord
);
776 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
777 group_base(ngroup
), ncolor
, color
, other_color
);
779 if (ncolor
== color
&& ngroup
!= group
) {
782 add_to_group(board
, group
, c
, coord
);
784 merge_groups(board
, group
, ngroup
);
786 } else if (ncolor
== other_color
) {
788 struct group
*gi
= &board_group_info(board
, ngroup
);
789 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
790 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
791 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
792 fprintf(stderr
, "\n");
794 if (unlikely(board_group_captured(board
, ngroup
)))
795 board_group_capture(board
, ngroup
);
800 /* We played on a place with at least one liberty. We will become a member of
801 * some group for sure. */
802 static group_t profiling_noinline
803 board_play_outside(struct board
*board
, struct move
*m
, int f
)
805 coord_t coord
= m
->coord
;
806 enum stone color
= m
->color
;
807 enum stone other_color
= stone_other(color
);
810 board
->f
[f
] = board
->f
[--board
->flen
];
812 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
814 foreach_neighbor(board
, coord
, {
815 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
818 if (unlikely(!group
))
819 group
= new_group(board
, coord
);
821 board_at(board
, coord
) = color
;
822 board
->last_move2
= board
->last_move
;
823 board
->last_move
= *m
;
825 board_hash_update(board
, coord
, color
);
826 board_symmetry_update(board
, &board
->symmetry
, coord
);
827 struct move ko
= { pass
, S_NONE
};
833 /* We played in an eye-like shape. Either we capture at least one of the eye
834 * sides in the process of playing, or return -1. */
835 static int profiling_noinline
836 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
838 coord_t coord
= m
->coord
;
839 enum stone color
= m
->color
;
840 /* Check ko: Capture at a position of ko capture one move ago */
841 if (unlikely(color
== board
->ko
.color
&& coord_eq(coord
, board
->ko
.coord
))) {
843 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
845 } else if (DEBUGL(6)) {
846 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
847 color
, coord_x(coord
, board
), coord_y(coord
, board
),
848 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
851 struct move ko
= { pass
, S_NONE
};
853 int captured_groups
= 0;
855 foreach_neighbor(board
, coord
, {
856 group_t g
= group_at(board
, c
);
858 fprintf(stderr
, "board_check: group %d has %d libs\n",
859 g
, board_group_info(board
, g
).libs
);
860 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
863 if (likely(captured_groups
== 0)) {
866 board_print(board
, stderr
);
867 fprintf(stderr
, "board_check: one-stone suicide\n");
873 board
->f
[f
] = board
->f
[--board
->flen
];
875 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
877 foreach_neighbor(board
, coord
, {
878 inc_neighbor_count_at(board
, c
, color
);
880 group_t group
= group_at(board
, c
);
884 board_group_rmlib(board
, group
, coord
);
886 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
889 if (board_group_captured(board
, group
)) {
890 if (board_group_capture(board
, group
) == 1) {
891 /* If we captured multiple groups at once,
892 * we can't be fighting ko so we don't need
893 * to check for that. */
894 ko
.color
= stone_other(color
);
897 board
->last_ko_age
= board
->moves
;
899 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
904 board_at(board
, coord
) = color
;
906 board
->last_move2
= board
->last_move
;
907 board
->last_move
= *m
;
909 board_hash_update(board
, coord
, color
);
910 board_hash_commit(board
);
911 board_symmetry_update(board
, &board
->symmetry
, coord
);
914 return !!new_group(board
, coord
);
917 static int __attribute__((flatten
))
918 board_play_f(struct board
*board
, struct move
*m
, int f
)
921 fprintf(stderr
, "board_play(): ---- Playing %d,%d\n", coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
923 if (likely(!board_is_eyelike(board
, &m
->coord
, stone_other(m
->color
)))) {
924 /* NOT playing in an eye. Thus this move has to succeed. (This
925 * is thanks to New Zealand rules. Otherwise, multi-stone
926 * suicide might fail.) */
927 group_t group
= board_play_outside(board
, m
, f
);
928 if (unlikely(board_group_captured(board
, group
))) {
929 board_group_capture(board
, group
);
931 board_hash_commit(board
);
934 return board_play_in_eye(board
, m
, f
);
939 board_play(struct board
*board
, struct move
*m
)
941 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
942 board
->last_move2
= board
->last_move
;
943 board
->last_move
= *m
;
948 for (f
= 0; f
< board
->flen
; f
++)
949 if (board
->f
[f
] == coord_raw(m
->coord
))
950 return board_play_f(board
, m
, f
);
953 fprintf(stderr
, "board_check: stone exists\n");
959 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
961 coord_raw(*coord
) = b
->f
[f
];
962 if (unlikely(is_pass(*coord
)))
964 struct move m
= { *coord
, color
};
966 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
967 return (likely(!board_is_one_point_eye(b
, coord
, color
)) /* bad idea to play into one, usually */
968 && board_is_valid_move(b
, &m
)
969 && (!permit
|| permit(permit_data
, b
, &m
))
970 && likely(board_play_f(b
, &m
, f
) >= 0));
974 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
976 int base
= fast_random(b
->flen
);
977 coord_pos(*coord
, base
, b
);
978 if (likely(board_try_random_move(b
, color
, coord
, base
, permit
, permit_data
)))
982 for (f
= base
+ 1; f
< b
->flen
; f
++)
983 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
985 for (f
= 0; f
< base
; f
++)
986 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
994 board_is_false_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
996 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
998 /* XXX: We attempt false eye detection but we will yield false
999 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1001 foreach_diag_neighbor(board
, *coord
) {
1002 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1003 } foreach_diag_neighbor_end
;
1004 /* For false eye, we need two enemy stones diagonally in the
1005 * middle of the board, or just one enemy stone at the edge
1006 * or in the corner. */
1007 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1008 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1012 board_is_one_point_eye(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
1014 return board_is_eyelike(board
, coord
, eye_color
)
1015 && !board_is_false_eyelike(board
, coord
, eye_color
);
1019 board_get_one_point_eye(struct board
*board
, coord_t
*coord
)
1021 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1023 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1031 board_fast_score(struct board
*board
)
1034 memset(scores
, 0, sizeof(scores
));
1036 foreach_point(board
) {
1037 enum stone color
= board_at(board
, c
);
1038 if (color
== S_NONE
)
1039 color
= board_get_one_point_eye(board
, &c
);
1041 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1042 } foreach_point_end
;
1044 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1047 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1049 /* One flood-fill iteration; returns true if next iteration
1052 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1054 bool needs_update
= false;
1055 foreach_point(board
) {
1056 /* Ignore occupied and already-dame positions. */
1057 if (board_at(board
, c
) != S_NONE
|| ownermap
[c
] == 3)
1059 /* Count neighbors. */
1061 foreach_neighbor(board
, c
, {
1064 /* If we have neighbors of both colors, or dame,
1065 * we are dame too. */
1066 if ((nei
[1] && nei
[2]) || nei
[3]) {
1068 /* Speed up the propagation. */
1069 foreach_neighbor(board
, c
, {
1070 if (board_at(board
, c
) == S_NONE
)
1073 needs_update
= true;
1076 /* If we have neighbors of one color, we are owned
1077 * by that color, too. */
1078 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1079 int newowner
= nei
[1] ? 1 : 2;
1080 ownermap
[c
] = newowner
;
1081 /* Speed up the propagation. */
1082 foreach_neighbor(board
, c
, {
1083 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1084 ownermap
[c
] = newowner
;
1086 needs_update
= true;
1089 } foreach_point_end
;
1090 return needs_update
;
1093 /* Tromp-Taylor Counting */
1095 board_official_score(struct board
*board
, struct move_queue
*q
)
1098 /* A point P, not colored C, is said to reach C, if there is a path of
1099 * (vertically or horizontally) adjacent points of P's color from P to
1100 * a point of color C.
1102 * A player's score is the number of points of her color, plus the
1103 * number of empty points that reach only her color. */
1105 int ownermap
[board_size2(board
)];
1107 const int o
[4] = {0, 1, 2, 0};
1108 foreach_point(board
) {
1109 ownermap
[c
] = o
[board_at(board
, c
)];
1110 s
[board_at(board
, c
)]++;
1111 } foreach_point_end
;
1114 /* Process dead groups. */
1115 for (int i
= 0; i
< q
->moves
; i
++) {
1116 foreach_in_group(board
, q
->move
[i
]) {
1117 enum stone color
= board_at(board
, c
);
1118 ownermap
[c
] = o
[stone_other(color
)];
1119 s
[color
]--; s
[stone_other(color
)]++;
1120 } foreach_in_group_end
;
1124 /* We need to special-case empty board. */
1125 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1126 return board
->komi
+ board
->handicap
;
1128 while (board_tromp_taylor_iter(board
, ownermap
))
1129 /* Flood-fill... */;
1132 memset(scores
, 0, sizeof(scores
));
1134 foreach_point(board
) {
1135 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1136 if (ownermap
[c
] == 3)
1138 scores
[ownermap
[c
]]++;
1139 } foreach_point_end
;
1141 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];