13 #include "patternsp.h"
19 static void board_trait_recompute(struct board
*board
, coord_t coord
);
28 #define profiling_noinline __attribute__((noinline))
30 #define profiling_noinline
33 #define gi_granularity 4
34 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
38 board_setup(struct board
*b
)
40 memset(b
, 0, sizeof(*b
));
42 struct move m
= { pass
, S_NONE
};
43 b
->last_move
= b
->last_move2
= b
->last_ko
= b
->ko
= m
;
49 struct board
*b
= malloc2(sizeof(struct board
));
60 board_copy(struct board
*b2
, struct board
*b1
)
62 memcpy(b2
, b1
, sizeof(struct board
));
64 int bsize
= board_size2(b2
) * sizeof(*b2
->b
);
65 int gsize
= board_size2(b2
) * sizeof(*b2
->g
);
66 int fsize
= board_size2(b2
) * sizeof(*b2
->f
);
67 int nsize
= board_size2(b2
) * sizeof(*b2
->n
);
68 int psize
= board_size2(b2
) * sizeof(*b2
->p
);
69 int hsize
= board_size2(b2
) * 2 * sizeof(*b2
->h
);
70 int gisize
= board_size2(b2
) * sizeof(*b2
->gi
);
72 int csize
= board_size2(b2
) * sizeof(*b2
->c
);
77 int ssize
= board_size2(b2
) * sizeof(*b2
->spathash
);
82 int p3size
= board_size2(b2
) * sizeof(*b2
->pat3
);
87 int tsize
= board_size2(b2
) * sizeof(*b2
->t
);
88 int tqsize
= board_size2(b2
) * sizeof(*b2
->t
);
94 int pbsize
= board_size2(b2
) * sizeof(*b2
->prob
[0].items
);
95 int rowpbsize
= board_size(b2
) * sizeof(*b2
->prob
[0].rowtotals
);
100 int cdsize
= board_size2(b2
) * sizeof(*b2
->coord
);
101 void *x
= malloc2(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ (pbsize
+ rowpbsize
) * 2 + cdsize
);
102 memcpy(x
, b1
->b
, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ (pbsize
+ rowpbsize
) * 2 + cdsize
);
103 b2
->b
= x
; x
+= bsize
;
104 b2
->g
= x
; x
+= gsize
;
105 b2
->f
= x
; x
+= fsize
;
106 b2
->p
= x
; x
+= psize
;
107 b2
->n
= x
; x
+= nsize
;
108 b2
->h
= x
; x
+= hsize
;
109 b2
->gi
= x
; x
+= gisize
;
111 b2
->c
= x
; x
+= csize
;
113 #ifdef BOARD_SPATHASH
114 b2
->spathash
= x
; x
+= ssize
;
117 b2
->pat3
= x
; x
+= p3size
;
120 b2
->t
= x
; x
+= tsize
;
121 b2
->tq
= x
; x
+= tqsize
;
124 b2
->prob
[0].items
= x
; x
+= pbsize
;
125 b2
->prob
[1].items
= x
; x
+= pbsize
;
126 b2
->prob
[0].rowtotals
= x
; x
+= rowpbsize
;
127 b2
->prob
[1].rowtotals
= x
; x
+= rowpbsize
;
129 b2
->coord
= x
; x
+= cdsize
;
135 board_done_noalloc(struct board
*board
)
137 if (board
->b
) free(board
->b
);
141 board_done(struct board
*board
)
143 board_done_noalloc(board
);
148 board_resize(struct board
*board
, int size
)
151 assert(board_size(board
) == size
+ 2);
153 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
154 board
->size2
= board_size(board
) * board_size(board
);
157 while ((1 << board
->bits2
) < board
->size2
) board
->bits2
++;
162 int bsize
= board_size2(board
) * sizeof(*board
->b
);
163 int gsize
= board_size2(board
) * sizeof(*board
->g
);
164 int fsize
= board_size2(board
) * sizeof(*board
->f
);
165 int nsize
= board_size2(board
) * sizeof(*board
->n
);
166 int psize
= board_size2(board
) * sizeof(*board
->p
);
167 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
168 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
170 int csize
= board_size2(board
) * sizeof(*board
->c
);
174 #ifdef BOARD_SPATHASH
175 int ssize
= board_size2(board
) * sizeof(*board
->spathash
);
180 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
185 int tsize
= board_size2(board
) * sizeof(*board
->t
);
186 int tqsize
= board_size2(board
) * sizeof(*board
->t
);
192 int pbsize
= board_size2(board
) * sizeof(*board
->prob
[0].items
);
193 int rowpbsize
= board_size(board
) * sizeof(*board
->prob
[0].rowtotals
);
198 int cdsize
= board_size2(board
) * sizeof(*board
->coord
);
199 void *x
= malloc2(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ (pbsize
+ rowpbsize
) * 2 + cdsize
);
200 memset(x
, 0, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ (pbsize
+ rowpbsize
) * 2 + cdsize
);
201 board
->b
= x
; x
+= bsize
;
202 board
->g
= x
; x
+= gsize
;
203 board
->f
= x
; x
+= fsize
;
204 board
->p
= x
; x
+= psize
;
205 board
->n
= x
; x
+= nsize
;
206 board
->h
= x
; x
+= hsize
;
207 board
->gi
= x
; x
+= gisize
;
209 board
->c
= x
; x
+= csize
;
211 #ifdef BOARD_SPATHASH
212 board
->spathash
= x
; x
+= ssize
;
215 board
->pat3
= x
; x
+= p3size
;
218 board
->t
= x
; x
+= tsize
;
219 board
->tq
= x
; x
+= tqsize
;
222 board
->prob
[0].items
= x
; x
+= pbsize
;
223 board
->prob
[1].items
= x
; x
+= pbsize
;
224 board
->prob
[0].rowtotals
= x
; x
+= rowpbsize
;
225 board
->prob
[1].rowtotals
= x
; x
+= rowpbsize
;
227 board
->coord
= x
; x
+= cdsize
;
231 board_clear(struct board
*board
)
233 int size
= board_size(board
);
234 float komi
= board
->komi
;
236 board_done_noalloc(board
);
238 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
242 /* Setup neighborhood iterators */
243 board
->nei8
[0] = -size
- 1; // (-1,-1)
246 board
->nei8
[3] = size
- 2; // (-1,0)
248 board
->nei8
[5] = size
- 2; // (-1,1)
251 board
->dnei
[0] = -size
- 1;
253 board
->dnei
[2] = size
*2 - 2;
256 /* Setup initial symmetry */
257 board
->symmetry
.d
= 1;
258 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
259 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
260 board
->symmetry
.type
= SYM_FULL
;
262 /* Set up coordinate cache */
263 foreach_point(board
) {
264 board
->coord
[c
][0] = c
% board_size(board
);
265 board
->coord
[c
][1] = c
/ board_size(board
);
268 /* Draw the offboard margin */
269 int top_row
= board_size2(board
) - board_size(board
);
271 for (i
= 0; i
< board_size(board
); i
++)
272 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
273 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
274 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
276 foreach_point(board
) {
278 if (board_at(board
, coord
) == S_OFFBOARD
)
280 foreach_neighbor(board
, c
, {
281 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
285 /* All positions are free! Except the margin. */
286 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
287 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
288 board
->f
[board
->flen
++] = i
;
290 /* Initialize zobrist hashtable. */
291 foreach_point(board
) {
292 int max
= (sizeof(hash_t
) << history_hash_bits
);
293 /* fast_random() is 16-bit only */
294 board
->h
[c
* 2] = ((hash_t
) fast_random(max
))
295 | ((hash_t
) fast_random(max
) << 16)
296 | ((hash_t
) fast_random(max
) << 32)
297 | ((hash_t
) fast_random(max
) << 48);
298 if (!board
->h
[c
* 2])
299 /* Would be kinda "oops". */
301 /* And once again for white */
302 board
->h
[c
* 2 + 1] = ((hash_t
) fast_random(max
))
303 | ((hash_t
) fast_random(max
) << 16)
304 | ((hash_t
) fast_random(max
) << 32)
305 | ((hash_t
) fast_random(max
) << 48);
306 if (!board
->h
[c
* 2 + 1])
307 board
->h
[c
* 2 + 1] = 1;
310 #ifdef BOARD_SPATHASH
311 /* Initialize spatial hashes. */
312 foreach_point(board
) {
313 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
314 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
315 ptcoords_at(x
, y
, c
, board
, j
);
316 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
317 pthashes
[0][j
][board_at(board
, c
)];
318 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
319 pthashes
[0][j
][stone_other(board_at(board
, c
))];
325 /* Initialize 3x3 pattern codes. */
326 foreach_point(board
) {
327 if (board_at(board
, c
) == S_NONE
)
328 board
->pat3
[c
] = pattern3_hash(board
, c
);
332 /* Initialize traits. */
333 foreach_point(board
) {
334 trait_at(board
, c
, S_BLACK
).cap
= 0;
335 trait_at(board
, c
, S_BLACK
).safe
= true;
336 trait_at(board
, c
, S_WHITE
).cap
= 0;
337 trait_at(board
, c
, S_WHITE
).safe
= true;
341 board
->prob
[0].n
= board
->prob
[1].n
= board_size2(board
);
342 board
->prob
[0].n1
= board
->prob
[1].n1
= board_size(board
);
343 foreach_point(board
) {
344 probdist_set(&board
->prob
[0], c
, (board_at(board
, c
) == S_NONE
) * 1.0f
);
345 probdist_set(&board
->prob
[1], c
, (board_at(board
, c
) == S_NONE
) * 1.0f
);
351 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
353 for (int i
= 0; i
< c
; i
++) {
354 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
355 s
+= snprintf(s
, end
- s
, " ");
356 for (int x
= 1; x
< board_size(board
) - 1; x
++)
357 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
358 s
+= snprintf(s
, end
-s
, " ");
360 s
+= snprintf(s
, end
- s
, "\n");
361 for (int i
= 0; i
< c
; i
++) {
362 s
+= snprintf(s
, end
- s
, " +-");
363 for (int x
= 1; x
< board_size(board
) - 1; x
++)
364 s
+= snprintf(s
, end
- s
, "--");
365 s
+= snprintf(s
, end
- s
, "+");
367 s
+= snprintf(s
, end
- s
, "\n");
372 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
374 for (int i
= 0; i
< c
; i
++) {
375 s
+= snprintf(s
, end
- s
, " +-");
376 for (int x
= 1; x
< board_size(board
) - 1; x
++)
377 s
+= snprintf(s
, end
- s
, "--");
378 s
+= snprintf(s
, end
- s
, "+");
380 s
+= snprintf(s
, end
- s
, "\n");
385 board_print_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
)
387 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
388 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
389 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
390 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
392 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
394 s
+= snprintf(s
, end
- s
, "|");
396 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
397 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
398 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
);
400 s
+= snprintf(s
, end
- s
, "|");
402 s
+= snprintf(s
, end
- s
, "\n");
407 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
411 char *end
= buf
+ sizeof(buf
);
412 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
413 board
->moves
, board
->komi
, board
->handicap
,
414 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
415 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
416 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
417 s
= board_print_row(board
, y
, s
, end
, cprint
);
418 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
419 fprintf(f
, "%s\n", buf
);
423 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
)
425 s
+= snprintf(s
, end
- s
, "%d ", group_base(group_at(board
, c
)));
430 board_print(struct board
*board
, FILE *f
)
432 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
436 board_gamma_set(struct board
*b
, struct features_gamma
*gamma
, bool precise_selfatari
)
440 b
->precise_selfatari
= precise_selfatari
;
441 for (int i
= 0; i
< b
->flen
; i
++) {
442 board_trait_recompute(b
, b
->f
[i
]);
448 /* Update the probability distribution we maintain incrementally. */
450 board_gamma_update(struct board
*board
, coord_t coord
, enum stone color
)
456 /* Punch out invalid moves and moves filling our own eyes. */
457 if (board_at(board
, coord
) != S_NONE
458 || (board_is_eyelike(board
, coord
, stone_other(color
))
459 && !trait_at(board
, coord
, color
).cap
)
460 || (board_is_one_point_eye(board
, coord
, color
))) {
461 probdist_set(&board
->prob
[color
- 1], coord
, 0);
465 int pat
= board
->pat3
[coord
];
466 if (color
== S_WHITE
) {
467 /* We work with the pattern3s as black-to-play. */
468 pat
= pattern3_reverse(pat
);
471 /* We just quickly replicate the general pattern matcher stuff
472 * here in the most bare-bone way. */
473 double value
= board
->gamma
->gamma
[FEAT_PATTERN3
][pat
];
474 if (trait_at(board
, coord
, color
).cap
)
475 value
*= board
->gamma
->gamma
[FEAT_CAPTURE
][0];
476 if (trait_at(board
, coord
, stone_other(color
)).cap
477 && trait_at(board
, coord
, color
).safe
)
478 value
*= board
->gamma
->gamma
[FEAT_AESCAPE
][0];
479 if (!trait_at(board
, coord
, color
).safe
)
480 value
*= board
->gamma
->gamma
[FEAT_SELFATARI
][1 + board
->precise_selfatari
];
481 probdist_set(&board
->prob
[color
- 1], coord
, value
);
487 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
490 if (board
->precise_selfatari
)
491 return is_bad_selfatari(board
, color
, coord
);
493 return board_safe_to_play(board
, coord
, color
);
497 board_trait_recompute(struct board
*board
, coord_t coord
)
499 trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);;
500 trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
502 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d safe=%d) (white cap=%d safe=%d)\n",
503 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
504 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).safe
,
505 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).safe
);
507 board_gamma_update(board
, coord
, S_BLACK
);
508 board_gamma_update(board
, coord
, S_WHITE
);
512 /* Recompute traits for dirty points that we have previously touched
513 * somehow (libs of their neighbors changed or so). */
515 board_traits_recompute(struct board
*board
)
518 for (int i
= 0; i
< board
->tqlen
; i
++) {
519 coord_t coord
= board
->tq
[i
];
520 if (!trait_at(board
, coord
, S_BLACK
).dirty
) continue;
521 if (board_at(board
, coord
) != S_NONE
) continue;
522 board_trait_recompute(board
, coord
);
523 trait_at(board
, coord
, S_BLACK
).dirty
= false;
529 /* Queue traits of given point for recomputing. */
531 board_trait_queue(struct board
*board
, coord_t coord
)
534 board
->tq
[board
->tqlen
++] = coord
;
535 trait_at(board
, coord
, S_BLACK
).dirty
= true;
540 /* Update board hash with given coordinate. */
541 static void profiling_noinline
542 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
544 board
->hash
^= hash_at(board
, coord
, color
);
546 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
);
548 #ifdef BOARD_SPATHASH
549 /* Gridcular metric is reflective, so we update all hashes
550 * of appropriate ditance in OUR circle. */
551 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
552 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
553 ptcoords_at(x
, y
, coord
, board
, j
);
554 /* We either changed from S_NONE to color
555 * or vice versa; doesn't matter. */
556 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
557 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
558 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
559 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
564 #if defined(BOARD_PAT3)
565 /* @color is not what we need in case of capture. */
566 enum stone new_color
= board_at(board
, coord
);
567 if (new_color
== S_NONE
)
568 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
569 foreach_8neighbor(board
, coord
) { // internally, the loop uses fn__i=[0..7]
570 if (board_at(board
, c
) != S_NONE
)
572 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
573 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
575 if (board_at(board
, c
) != S_OFFBOARD
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
576 board_print(board
, stderr
);
577 fprintf(stderr
, "%s->%s %x != %x (%d-%d:%d)\n", coord2sstr(coord
, board
), coord2sstr(c
, board
), pattern3_hash(board
, c
), board
->pat3
[c
], coord
, c
, fn__i
);
581 board_gamma_update(board
, c
, S_BLACK
);
582 board_gamma_update(board
, c
, S_WHITE
);
583 } foreach_8neighbor_end
;
587 /* Commit current board hash to history. */
588 static void profiling_noinline
589 board_hash_commit(struct board
*board
)
592 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
593 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
594 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
596 hash_t i
= board
->hash
;
597 while (board
->history_hash
[i
& history_hash_mask
]) {
598 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
600 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
601 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
602 board
->superko_violation
= true;
605 i
= history_hash_next(i
);
607 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
613 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
615 if (likely(symmetry
->type
== SYM_NONE
)) {
616 /* Fully degenerated already. We do not support detection
617 * of restoring of symmetry, assuming that this is too rare
618 * a case to handle. */
622 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
623 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
625 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
626 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
627 symmetry
->d
, symmetry
->type
, x
, y
);
630 switch (symmetry
->type
) {
632 if (x
== t
&& y
== t
) {
633 /* Tengen keeps full symmetry. */
636 /* New symmetry now? */
638 symmetry
->type
= SYM_DIAG_UP
;
639 symmetry
->x1
= symmetry
->y1
= 1;
640 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
642 } else if (dx
== y
) {
643 symmetry
->type
= SYM_DIAG_DOWN
;
644 symmetry
->x1
= symmetry
->y1
= 1;
645 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
648 symmetry
->type
= SYM_HORIZ
;
650 symmetry
->y2
= board_size(b
) - 1;
653 symmetry
->type
= SYM_VERT
;
655 symmetry
->x2
= board_size(b
) - 1;
659 symmetry
->type
= SYM_NONE
;
660 symmetry
->x1
= symmetry
->y1
= 1;
661 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
687 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
688 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
689 symmetry
->d
, symmetry
->type
);
696 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
699 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
701 board_play(board
, &m
);
702 /* Simulate white passing; otherwise, UCT search can get confused since
703 * tree depth parity won't match the color to move. */
706 char *str
= coord2str(m
.coord
, board
);
708 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
709 if (f
) fprintf(f
, "%s ", str
);
714 board_handicap(struct board
*board
, int stones
, FILE *f
)
716 int margin
= 3 + (board_size(board
) >= 13);
718 int mid
= board_size(board
) / 2;
719 int max
= board_size(board
) - 1 - margin
;
720 const int places
[][2] = {
721 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
722 { min
, mid
}, { max
, mid
},
723 { mid
, min
}, { mid
, max
},
727 board
->handicap
= stones
;
729 if (stones
== 5 || stones
== 7) {
730 board_handicap_stone(board
, mid
, mid
, f
);
735 for (i
= 0; i
< stones
; i
++)
736 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
740 static void __attribute__((noinline
))
741 check_libs_consistency(struct board
*board
, group_t g
)
745 struct group
*gi
= &board_group_info(board
, g
);
746 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
747 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
748 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
755 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
)
757 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
759 /* Increase capturable count trait of my last lib. */
760 enum stone capturing_color
= stone_other(board_at(board
, group
));
761 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
762 foreach_neighbor(board
, lib
, {
763 if (DEBUGL(8) && group_at(board
, c
) == group
)
764 fprintf(stderr
, "%s[%d] %s cap bump bc of %s(%d) member %s\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
));
765 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
767 board_trait_queue(board
, lib
);
771 /* Update the list of capturable groups. */
773 assert(board
->clen
< board_size2(board
));
774 board
->c
[board
->clen
++] = group
;
778 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
)
780 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
782 /* Decrease capturable count trait of my previously-last lib. */
783 enum stone capturing_color
= stone_other(board_at(board
, group
));
784 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
785 foreach_neighbor(board
, lib
, {
786 if (DEBUGL(8) && group_at(board
, c
) == group
)
787 fprintf(stderr
, "%s[%d] cap dump bc of %s(%d) member %s\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
, coord2sstr(group
, board
), board_group_info(board
, group
).libs
, coord2sstr(c
, board
));
788 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
790 board_trait_queue(board
, lib
);
794 /* Update the list of capturable groups. */
795 for (int i
= 0; i
< board
->clen
; i
++) {
796 if (unlikely(board
->c
[i
] == group
)) {
797 board
->c
[i
] = board
->c
[--board
->clen
];
801 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
807 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
810 board_trait_queue(board
, lib1
);
811 board_trait_queue(board
, lib2
);
815 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
818 board_trait_queue(board
, lib1
);
819 board_trait_queue(board
, lib2
);
824 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
827 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
828 group_base(group
), coord2sstr(group_base(group
), board
),
829 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
832 check_libs_consistency(board
, group
);
834 struct group
*gi
= &board_group_info(board
, group
);
835 if (gi
->libs
< GROUP_KEEP_LIBS
) {
836 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
838 /* Seems extra branch just slows it down */
842 if (unlikely(gi
->lib
[i
] == coord
))
846 board_capturable_add(board
, group
, coord
);
847 } else if (gi
->libs
== 1) {
848 board_capturable_rm(board
, group
, gi
->lib
[0]);
849 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
850 } else if (gi
->libs
== 2) {
851 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
853 gi
->lib
[gi
->libs
++] = coord
;
856 check_libs_consistency(board
, group
);
860 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
862 /* Add extra liberty from the board to our liberty list. */
863 unsigned char watermark
[board_size2(board
) / 8];
864 memset(watermark
, 0, sizeof(watermark
));
865 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
866 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
868 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
869 watermark_set(gi
->lib
[i
]);
870 watermark_set(avoid
);
872 foreach_in_group(board
, group
) {
874 foreach_neighbor(board
, coord2
, {
875 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
878 gi
->lib
[gi
->libs
++] = c
;
879 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
882 } foreach_in_group_end
;
888 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
891 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
892 group_base(group
), coord2sstr(group_base(group
), board
),
893 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
896 struct group
*gi
= &board_group_info(board
, group
);
897 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
899 /* Seems extra branch just slows it down */
903 if (likely(gi
->lib
[i
] != coord
))
906 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
907 gi
->lib
[gi
->libs
] = 0;
909 check_libs_consistency(board
, group
);
911 /* Postpone refilling lib[] until we need to. */
912 assert(GROUP_REFILL_LIBS
> 1);
913 if (gi
->libs
> GROUP_REFILL_LIBS
)
915 if (gi
->libs
== GROUP_REFILL_LIBS
)
916 board_group_find_extra_libs(board
, group
, gi
, coord
);
919 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
920 } else if (gi
->libs
== 1) {
921 board_capturable_add(board
, group
, gi
->lib
[0]);
922 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
923 } else if (gi
->libs
== 0)
924 board_capturable_rm(board
, group
, lib
);
928 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
929 * can call this multiple times per coord. */
930 check_libs_consistency(board
, group
);
935 /* This is a low-level routine that doesn't maintain consistency
936 * of all the board data structures. */
938 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
940 enum stone color
= board_at(board
, c
);
941 board_at(board
, c
) = S_NONE
;
942 group_at(board
, c
) = 0;
943 board_hash_update(board
, c
, color
);
945 /* We mark as cannot-capture now. If this is a ko/snapback,
946 * we will get incremented later in board_group_addlib(). */
947 trait_at(board
, c
, S_BLACK
).cap
= 0;
948 trait_at(board
, c
, S_WHITE
).cap
= 0;
949 board_trait_queue(board
, c
);
952 /* Increase liberties of surrounding groups */
954 foreach_neighbor(board
, coord
, {
955 dec_neighbor_count_at(board
, c
, color
);
956 board_trait_queue(board
, c
);
957 group_t g
= group_at(board
, c
);
959 board_group_addlib(board
, g
, coord
);
963 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
964 board
->f
[board
->flen
++] = c
;
967 static int profiling_noinline
968 board_group_capture(struct board
*board
, group_t group
)
972 foreach_in_group(board
, group
) {
973 board
->captures
[stone_other(board_at(board
, c
))]++;
974 board_remove_stone(board
, group
, c
);
976 } foreach_in_group_end
;
978 struct group
*gi
= &board_group_info(board
, group
);
980 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
981 else if (gi
->libs
== 1)
982 board_capturable_rm(board
, group
, gi
->lib
[0]);
983 memset(gi
, 0, sizeof(*gi
));
989 static void profiling_noinline
990 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
992 group_at(board
, coord
) = group
;
993 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
994 groupnext_at(board
, prevstone
) = coord
;
997 if (board_group_info(board
, group
).libs
== 1) {
998 /* Our group is temporarily in atari; make sure the capturable
999 * counts also correspond to the newly added stone before we
1000 * start adding liberties again so bump-dump ops match. */
1001 enum stone capturing_color
= stone_other(board_at(board
, group
));
1002 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1003 coord_t lib
= board_group_info(board
, group
).lib
[0];
1004 if (coord_is_adjecent(lib
, coord
, board
)) {
1005 if (DEBUGL(8)) fprintf(stderr
, "add_to_group %s: %s[%d] bump\n", coord2sstr(group
, board
), coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
1006 trait_at(board
, lib
, capturing_color
).cap
++;
1007 board_trait_queue(board
, lib
);
1012 foreach_neighbor(board
, coord
, {
1013 if (board_at(board
, c
) == S_NONE
)
1014 board_group_addlib(board
, group
, c
);
1018 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1019 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
1020 coord_x(coord
, board
), coord_y(coord
, board
),
1021 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
1025 static void profiling_noinline
1026 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
1029 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
1030 group_base(group_from
), group_base(group_to
));
1031 struct group
*gi_from
= &board_group_info(board
, group_from
);
1032 struct group
*gi_to
= &board_group_info(board
, group_to
);
1034 /* We do this early before the group info is rewritten. */
1035 if (gi_from
->libs
== 2)
1036 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
1037 else if (gi_from
->libs
== 1)
1038 board_capturable_rm(board
, group_from
, gi_from
->lib
[0]);
1041 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
1043 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
1044 for (int i
= 0; i
< gi_from
->libs
; i
++) {
1045 for (int j
= 0; j
< gi_to
->libs
; j
++)
1046 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
1048 if (gi_to
->libs
== 0) {
1049 board_capturable_add(board
, group_to
, gi_from
->lib
[i
]);
1050 } else if (gi_to
->libs
== 1) {
1051 board_capturable_rm(board
, group_to
, gi_to
->lib
[0]);
1052 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
1053 } else if (gi_to
->libs
== 2) {
1054 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
1056 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
1057 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
1064 if (board_group_info(board
, group_to
).libs
== 1) {
1065 /* Our group is currently in atari; make sure we properly
1066 * count in even the neighbors from the other group in the
1067 * capturable counter. */
1068 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
1069 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1070 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
1071 foreach_neighbor(board
, lib
, {
1072 if (DEBUGL(8) && group_at(board
, c
) == group_from
) 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
);
1075 board_trait_queue(board
, lib
);
1079 coord_t last_in_group
;
1080 foreach_in_group(board
, group_from
) {
1082 group_at(board
, c
) = group_to
;
1083 } foreach_in_group_end
;
1084 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1085 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1086 memset(gi_from
, 0, sizeof(struct group
));
1089 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1090 group_base(group_to
));
1093 static group_t profiling_noinline
1094 new_group(struct board
*board
, coord_t coord
)
1096 group_t group
= coord
;
1097 struct group
*gi
= &board_group_info(board
, group
);
1098 foreach_neighbor(board
, coord
, {
1099 if (board_at(board
, c
) == S_NONE
)
1100 /* board_group_addlib is ridiculously expensive for us */
1101 #if GROUP_KEEP_LIBS < 4
1102 if (gi
->libs
< GROUP_KEEP_LIBS
)
1104 gi
->lib
[gi
->libs
++] = c
;
1107 group_at(board
, coord
) = group
;
1108 groupnext_at(board
, coord
) = 0;
1111 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1112 else if (gi
->libs
== 1)
1113 board_capturable_add(board
, group
, gi
->lib
[0]);
1114 check_libs_consistency(board
, group
);
1117 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1118 coord_x(coord
, board
), coord_y(coord
, board
),
1124 static inline group_t
1125 play_one_neighbor(struct board
*board
,
1126 coord_t coord
, enum stone color
, enum stone other_color
,
1127 coord_t c
, group_t group
)
1129 enum stone ncolor
= board_at(board
, c
);
1130 group_t ngroup
= group_at(board
, c
);
1132 inc_neighbor_count_at(board
, c
, color
);
1133 /* We can be S_NONE, in that case we need to update the safety
1134 * trait since we might be left with only one liberty. */
1135 board_trait_queue(board
, c
);
1140 board_group_rmlib(board
, ngroup
, coord
);
1142 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1143 group_base(ngroup
), ncolor
, color
, other_color
);
1145 if (ncolor
== color
&& ngroup
!= group
) {
1148 add_to_group(board
, group
, c
, coord
);
1150 merge_groups(board
, group
, ngroup
);
1152 } else if (ncolor
== other_color
) {
1154 struct group
*gi
= &board_group_info(board
, ngroup
);
1155 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1156 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1157 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1158 fprintf(stderr
, "\n");
1160 if (unlikely(board_group_captured(board
, ngroup
)))
1161 board_group_capture(board
, ngroup
);
1166 /* We played on a place with at least one liberty. We will become a member of
1167 * some group for sure. */
1168 static group_t profiling_noinline
1169 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1171 coord_t coord
= m
->coord
;
1172 enum stone color
= m
->color
;
1173 enum stone other_color
= stone_other(color
);
1176 board
->f
[f
] = board
->f
[--board
->flen
];
1178 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1180 #if defined(BOARD_TRAITS) && defined(DEBUG)
1181 /* Sanity check that cap matches reality. */
1184 foreach_neighbor(board
, coord
, {
1185 group_t g
= group_at(board
, c
);
1186 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1188 assert(a
== trait_at(board
, coord
, color
).cap
);
1189 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1192 foreach_neighbor(board
, coord
, {
1193 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1196 board_at(board
, coord
) = color
;
1197 if (unlikely(!group
))
1198 group
= new_group(board
, coord
);
1199 board_gamma_update(board
, coord
, S_BLACK
);
1200 board_gamma_update(board
, coord
, S_WHITE
);
1202 board
->last_move2
= board
->last_move
;
1203 board
->last_move
= *m
;
1205 board_hash_update(board
, coord
, color
);
1206 board_symmetry_update(board
, &board
->symmetry
, coord
);
1207 struct move ko
= { pass
, S_NONE
};
1213 /* We played in an eye-like shape. Either we capture at least one of the eye
1214 * sides in the process of playing, or return -1. */
1215 static int profiling_noinline
1216 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1218 coord_t coord
= m
->coord
;
1219 enum stone color
= m
->color
;
1220 /* Check ko: Capture at a position of ko capture one move ago */
1221 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1223 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1225 } else if (DEBUGL(6)) {
1226 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1227 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1228 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1231 struct move ko
= { pass
, S_NONE
};
1233 int captured_groups
= 0;
1235 foreach_neighbor(board
, coord
, {
1236 group_t g
= group_at(board
, c
);
1238 fprintf(stderr
, "board_check: group %d has %d libs\n",
1239 g
, board_group_info(board
, g
).libs
);
1240 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1243 if (likely(captured_groups
== 0)) {
1246 board_print(board
, stderr
);
1247 fprintf(stderr
, "board_check: one-stone suicide\n");
1253 /* We _will_ for sure capture something. */
1254 assert(trait_at(board
, coord
, color
).cap
> 0);
1255 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1258 board
->f
[f
] = board
->f
[--board
->flen
];
1260 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1262 foreach_neighbor(board
, coord
, {
1263 inc_neighbor_count_at(board
, c
, color
);
1264 /* Originally, this could not have changed any trait
1265 * since no neighbors were S_NONE, however by now some
1266 * of them might be removed from the board. */
1267 board_trait_queue(board
, c
);
1269 group_t group
= group_at(board
, c
);
1273 board_group_rmlib(board
, group
, coord
);
1275 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1278 if (board_group_captured(board
, group
)) {
1279 if (board_group_capture(board
, group
) == 1) {
1280 /* If we captured multiple groups at once,
1281 * we can't be fighting ko so we don't need
1282 * to check for that. */
1283 ko
.color
= stone_other(color
);
1285 board
->last_ko
= ko
;
1286 board
->last_ko_age
= board
->moves
;
1288 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1293 board_at(board
, coord
) = color
;
1294 group_t group
= new_group(board
, coord
);
1295 board_gamma_update(board
, coord
, S_BLACK
);
1296 board_gamma_update(board
, coord
, S_WHITE
);
1298 board
->last_move2
= board
->last_move
;
1299 board
->last_move
= *m
;
1301 board_hash_update(board
, coord
, color
);
1302 board_hash_commit(board
);
1303 board_traits_recompute(board
);
1304 board_symmetry_update(board
, &board
->symmetry
, coord
);
1310 static int __attribute__((flatten
))
1311 board_play_f(struct board
*board
, struct move
*m
, int f
)
1314 fprintf(stderr
, "board_play(): ---- Playing %d,%d\n", coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1316 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1317 /* NOT playing in an eye. Thus this move has to succeed. (This
1318 * is thanks to New Zealand rules. Otherwise, multi-stone
1319 * suicide might fail.) */
1320 group_t group
= board_play_outside(board
, m
, f
);
1321 if (unlikely(board_group_captured(board
, group
))) {
1322 board_group_capture(board
, group
);
1324 board_hash_commit(board
);
1325 board_traits_recompute(board
);
1328 return board_play_in_eye(board
, m
, f
);
1333 board_play(struct board
*board
, struct move
*m
)
1335 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1336 struct move nomove
= { pass
, S_NONE
};
1338 board
->last_move2
= board
->last_move
;
1339 board
->last_move
= *m
;
1344 for (f
= 0; f
< board
->flen
; f
++)
1345 if (board
->f
[f
] == m
->coord
)
1346 return board_play_f(board
, m
, f
);
1349 fprintf(stderr
, "board_check: stone exists\n");
1355 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1358 struct move m
= { *coord
, color
};
1360 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
1361 if (unlikely(board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1362 || !board_is_valid_move(b
, &m
)
1363 || (permit
&& !permit(permit_data
, b
, &m
)))
1365 *coord
= m
.coord
; // permit might modify it
1366 return likely(board_play_f(b
, &m
, f
) >= 0);
1370 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1372 if (unlikely(b
->flen
== 0))
1375 int base
= fast_random(b
->flen
), f
;
1376 for (f
= base
; f
< b
->flen
; f
++)
1377 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1379 for (f
= 0; f
< base
; f
++)
1380 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1385 struct move m
= { pass
, color
};
1391 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1393 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1395 /* XXX: We attempt false eye detection but we will yield false
1396 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1398 foreach_diag_neighbor(board
, coord
) {
1399 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1400 } foreach_diag_neighbor_end
;
1401 /* For false eye, we need two enemy stones diagonally in the
1402 * middle of the board, or just one enemy stone at the edge
1403 * or in the corner. */
1404 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1405 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1409 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1411 return board_is_eyelike(board
, coord
, eye_color
)
1412 && !board_is_false_eyelike(board
, coord
, eye_color
);
1416 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1418 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1420 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1428 board_fast_score(struct board
*board
)
1431 memset(scores
, 0, sizeof(scores
));
1433 foreach_point(board
) {
1434 enum stone color
= board_at(board
, c
);
1435 if (color
== S_NONE
)
1436 color
= board_get_one_point_eye(board
, c
);
1438 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1439 } foreach_point_end
;
1441 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1444 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1446 /* One flood-fill iteration; returns true if next iteration
1449 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1451 bool needs_update
= false;
1452 foreach_point(board
) {
1453 /* Ignore occupied and already-dame positions. */
1454 if (board_at(board
, c
) != S_NONE
|| ownermap
[c
] == 3)
1456 /* Count neighbors. */
1458 foreach_neighbor(board
, c
, {
1461 /* If we have neighbors of both colors, or dame,
1462 * we are dame too. */
1463 if ((nei
[1] && nei
[2]) || nei
[3]) {
1465 /* Speed up the propagation. */
1466 foreach_neighbor(board
, c
, {
1467 if (board_at(board
, c
) == S_NONE
)
1470 needs_update
= true;
1473 /* If we have neighbors of one color, we are owned
1474 * by that color, too. */
1475 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1476 int newowner
= nei
[1] ? 1 : 2;
1477 ownermap
[c
] = newowner
;
1478 /* Speed up the propagation. */
1479 foreach_neighbor(board
, c
, {
1480 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1481 ownermap
[c
] = newowner
;
1483 needs_update
= true;
1486 } foreach_point_end
;
1487 return needs_update
;
1490 /* Tromp-Taylor Counting */
1492 board_official_score(struct board
*board
, struct move_queue
*q
)
1495 /* A point P, not colored C, is said to reach C, if there is a path of
1496 * (vertically or horizontally) adjacent points of P's color from P to
1497 * a point of color C.
1499 * A player's score is the number of points of her color, plus the
1500 * number of empty points that reach only her color. */
1502 int ownermap
[board_size2(board
)];
1504 const int o
[4] = {0, 1, 2, 0};
1505 foreach_point(board
) {
1506 ownermap
[c
] = o
[board_at(board
, c
)];
1507 s
[board_at(board
, c
)]++;
1508 } foreach_point_end
;
1511 /* Process dead groups. */
1512 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
1513 foreach_in_group(board
, q
->move
[i
]) {
1514 enum stone color
= board_at(board
, c
);
1515 ownermap
[c
] = o
[stone_other(color
)];
1516 s
[color
]--; s
[stone_other(color
)]++;
1517 } foreach_in_group_end
;
1521 /* We need to special-case empty board. */
1522 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1523 return board
->komi
+ board
->handicap
;
1525 while (board_tromp_taylor_iter(board
, ownermap
))
1526 /* Flood-fill... */;
1529 memset(scores
, 0, sizeof(scores
));
1531 foreach_point(board
) {
1532 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1533 if (ownermap
[c
] == 3)
1535 scores
[ownermap
[c
]]++;
1536 } foreach_point_end
;
1538 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];