15 #include "patternsp.h"
21 static void board_trait_recompute(struct board
*board
, coord_t coord
);
30 #define profiling_noinline __attribute__((noinline))
32 #define profiling_noinline
35 #define gi_granularity 4
36 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
40 board_setup(struct board
*b
)
42 memset(b
, 0, sizeof(*b
));
44 struct move m
= { pass
, S_NONE
};
45 b
->last_move
= b
->last_move2
= b
->last_ko
= b
->ko
= m
;
51 struct board
*b
= malloc2(sizeof(struct board
));
62 board_alloc(struct board
*board
)
64 /* We do not allocate the board structure itself but we allocate
65 * all the arrays with board contents. */
67 int bsize
= board_size2(board
) * sizeof(*board
->b
);
68 int gsize
= board_size2(board
) * sizeof(*board
->g
);
69 int fsize
= board_size2(board
) * sizeof(*board
->f
);
70 int nsize
= board_size2(board
) * sizeof(*board
->n
);
71 int psize
= board_size2(board
) * sizeof(*board
->p
);
72 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
73 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
75 int csize
= board_size2(board
) * sizeof(*board
->c
);
80 int ssize
= board_size2(board
) * sizeof(*board
->spathash
);
85 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
90 int tsize
= board_size2(board
) * sizeof(*board
->t
);
91 int tqsize
= board_size2(board
) * sizeof(*board
->t
);
97 int pbsize
= board_size2(board
) * sizeof(*board
->prob
[0].items
);
98 int rowpbsize
= board_size(board
) * sizeof(*board
->prob
[0].rowtotals
);
103 int cdsize
= board_size2(board
) * sizeof(*board
->coord
);
105 size_t size
= bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ (pbsize
+ rowpbsize
) * 2 + cdsize
;
106 void *x
= malloc2(size
);
108 /* board->b must come first */
109 board
->b
= x
; x
+= bsize
;
110 board
->g
= x
; x
+= gsize
;
111 board
->f
= x
; x
+= fsize
;
112 board
->p
= x
; x
+= psize
;
113 board
->n
= x
; x
+= nsize
;
114 board
->h
= x
; x
+= hsize
;
115 board
->gi
= x
; x
+= gisize
;
117 board
->c
= x
; x
+= csize
;
119 #ifdef BOARD_SPATHASH
120 board
->spathash
= x
; x
+= ssize
;
123 board
->pat3
= x
; x
+= p3size
;
126 board
->t
= x
; x
+= tsize
;
127 board
->tq
= x
; x
+= tqsize
;
130 board
->prob
[0].items
= x
; x
+= pbsize
;
131 board
->prob
[1].items
= x
; x
+= pbsize
;
132 board
->prob
[0].rowtotals
= x
; x
+= rowpbsize
;
133 board
->prob
[1].rowtotals
= x
; x
+= rowpbsize
;
135 board
->coord
= x
; x
+= cdsize
;
141 board_copy(struct board
*b2
, struct board
*b1
)
143 memcpy(b2
, b1
, sizeof(struct board
));
145 size_t size
= board_alloc(b2
);
146 memcpy(b2
->b
, b1
->b
, size
);
152 board_done_noalloc(struct board
*board
)
154 if (board
->b
) free(board
->b
);
158 board_done(struct board
*board
)
160 board_done_noalloc(board
);
165 board_resize(struct board
*board
, int size
)
168 assert(board_size(board
) == size
+ 2);
170 assert(size
<= BOARD_MAX_SIZE
);
171 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
172 board
->size2
= board_size(board
) * board_size(board
);
175 while ((1 << board
->bits2
) < board
->size2
) board
->bits2
++;
180 size_t asize
= board_alloc(board
);
181 memset(board
->b
, 0, asize
);
185 board_clear(struct board
*board
)
187 int size
= board_size(board
);
188 float komi
= board
->komi
;
190 board_done_noalloc(board
);
192 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
196 /* Setup neighborhood iterators */
197 board
->nei8
[0] = -size
- 1; // (-1,-1)
200 board
->nei8
[3] = size
- 2; // (-1,0)
202 board
->nei8
[5] = size
- 2; // (-1,1)
205 board
->dnei
[0] = -size
- 1;
207 board
->dnei
[2] = size
*2 - 2;
210 /* Setup initial symmetry */
211 board
->symmetry
.d
= 1;
212 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
213 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
214 board
->symmetry
.type
= SYM_FULL
;
216 /* Set up coordinate cache */
217 foreach_point(board
) {
218 board
->coord
[c
][0] = c
% board_size(board
);
219 board
->coord
[c
][1] = c
/ board_size(board
);
222 /* Draw the offboard margin */
223 int top_row
= board_size2(board
) - board_size(board
);
225 for (i
= 0; i
< board_size(board
); i
++)
226 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
227 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
228 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
230 foreach_point(board
) {
232 if (board_at(board
, coord
) == S_OFFBOARD
)
234 foreach_neighbor(board
, c
, {
235 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
239 /* All positions are free! Except the margin. */
240 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
241 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
242 board
->f
[board
->flen
++] = i
;
244 /* Initialize zobrist hashtable. */
245 foreach_point(board
) {
246 int max
= (sizeof(hash_t
) << history_hash_bits
);
247 /* fast_random() is 16-bit only */
248 board
->h
[c
* 2] = ((hash_t
) fast_random(max
))
249 | ((hash_t
) fast_random(max
) << 16)
250 | ((hash_t
) fast_random(max
) << 32)
251 | ((hash_t
) fast_random(max
) << 48);
252 if (!board
->h
[c
* 2])
253 /* Would be kinda "oops". */
255 /* And once again for white */
256 board
->h
[c
* 2 + 1] = ((hash_t
) fast_random(max
))
257 | ((hash_t
) fast_random(max
) << 16)
258 | ((hash_t
) fast_random(max
) << 32)
259 | ((hash_t
) fast_random(max
) << 48);
260 if (!board
->h
[c
* 2 + 1])
261 board
->h
[c
* 2 + 1] = 1;
264 #ifdef BOARD_SPATHASH
265 /* Initialize spatial hashes. */
266 foreach_point(board
) {
267 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
268 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
269 ptcoords_at(x
, y
, c
, board
, j
);
270 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
271 pthashes
[0][j
][board_at(board
, c
)];
272 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
273 pthashes
[0][j
][stone_other(board_at(board
, c
))];
279 /* Initialize 3x3 pattern codes. */
280 foreach_point(board
) {
281 if (board_at(board
, c
) == S_NONE
)
282 board
->pat3
[c
] = pattern3_hash(board
, c
);
286 /* Initialize traits. */
287 foreach_point(board
) {
288 trait_at(board
, c
, S_BLACK
).cap
= 0;
289 trait_at(board
, c
, S_BLACK
).cap1
= 0;
290 trait_at(board
, c
, S_BLACK
).safe
= true;
291 trait_at(board
, c
, S_WHITE
).cap
= 0;
292 trait_at(board
, c
, S_WHITE
).cap1
= 0;
293 trait_at(board
, c
, S_WHITE
).safe
= true;
297 board
->prob
[0].b
= board
->prob
[1].b
= board
;
298 foreach_point(board
) {
299 probdist_set(&board
->prob
[0], c
, double_to_fixp((board_at(board
, c
) == S_NONE
) * 1.0f
));
300 probdist_set(&board
->prob
[1], c
, double_to_fixp((board_at(board
, c
) == S_NONE
) * 1.0f
));
306 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
308 for (int i
= 0; i
< c
; i
++) {
309 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
310 s
+= snprintf(s
, end
- s
, " ");
311 for (int x
= 1; x
< board_size(board
) - 1; x
++)
312 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
313 s
+= snprintf(s
, end
-s
, " ");
315 s
+= snprintf(s
, end
- s
, "\n");
316 for (int i
= 0; i
< c
; i
++) {
317 s
+= snprintf(s
, end
- s
, " +-");
318 for (int x
= 1; x
< board_size(board
) - 1; x
++)
319 s
+= snprintf(s
, end
- s
, "--");
320 s
+= snprintf(s
, end
- s
, "+");
322 s
+= snprintf(s
, end
- s
, "\n");
327 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
329 for (int i
= 0; i
< c
; i
++) {
330 s
+= snprintf(s
, end
- s
, " +-");
331 for (int x
= 1; x
< board_size(board
) - 1; x
++)
332 s
+= snprintf(s
, end
- s
, "--");
333 s
+= snprintf(s
, end
- s
, "+");
335 s
+= snprintf(s
, end
- s
, "\n");
340 board_print_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
)
342 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
343 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
344 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
345 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
347 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
349 s
+= snprintf(s
, end
- s
, "|");
351 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
352 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
353 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
);
355 s
+= snprintf(s
, end
- s
, "|");
357 s
+= snprintf(s
, end
- s
, "\n");
362 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
366 char *end
= buf
+ sizeof(buf
);
367 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
368 board
->moves
, board
->komi
, board
->handicap
,
369 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
370 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
371 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
372 s
= board_print_row(board
, y
, s
, end
, cprint
);
373 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
374 fprintf(f
, "%s\n", buf
);
378 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
)
380 s
+= snprintf(s
, end
- s
, "%d ", group_base(group_at(board
, c
)));
385 board_print(struct board
*board
, FILE *f
)
387 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
391 board_gamma_set(struct board
*b
, struct features_gamma
*gamma
, bool precise_selfatari
)
395 b
->precise_selfatari
= precise_selfatari
;
396 for (int i
= 0; i
< b
->flen
; i
++) {
397 board_trait_recompute(b
, b
->f
[i
]);
403 /* Update the probability distribution we maintain incrementally. */
405 board_gamma_update(struct board
*board
, coord_t coord
, enum stone color
)
411 /* Punch out invalid moves and moves filling our own eyes. */
412 if (board_at(board
, coord
) != S_NONE
413 || (board_is_eyelike(board
, coord
, stone_other(color
))
414 && !trait_at(board
, coord
, color
).cap
)
415 || (board_is_one_point_eye(board
, coord
, color
))) {
416 probdist_set(&board
->prob
[color
- 1], coord
, 0);
420 hash3_t pat
= board
->pat3
[coord
];
421 if (color
== S_WHITE
) {
422 /* We work with the pattern3s as black-to-play. */
423 pat
= pattern3_reverse(pat
);
426 /* We just quickly replicate the general pattern matcher stuff
427 * here in the most bare-bone way. */
428 double value
= board
->gamma
->gamma
[FEAT_PATTERN3
][pat
];
429 if (trait_at(board
, coord
, color
).cap
) {
431 i
|= (trait_at(board
, coord
, color
).cap1
== trait_at(board
, coord
, color
).cap
) << PF_CAPTURE_1STONE
;
432 i
|= (!trait_at(board
, coord
, stone_other(color
)).safe
) << PF_CAPTURE_TRAPPED
;
433 i
|= (trait_at(board
, coord
, color
).cap
< neighbor_count_at(board
, coord
, stone_other(color
))) << PF_CAPTURE_CONNECTION
;
434 value
*= board
->gamma
->gamma
[FEAT_CAPTURE
][i
];
436 if (trait_at(board
, coord
, stone_other(color
)).cap
) {
438 i
|= (trait_at(board
, coord
, stone_other(color
)).cap1
== trait_at(board
, coord
, stone_other(color
)).cap
) << PF_AESCAPE_1STONE
;
439 i
|= (!trait_at(board
, coord
, color
).safe
) << PF_AESCAPE_TRAPPED
;
440 i
|= (trait_at(board
, coord
, stone_other(color
)).cap
< neighbor_count_at(board
, coord
, color
)) << PF_AESCAPE_CONNECTION
;
441 value
*= board
->gamma
->gamma
[FEAT_AESCAPE
][i
];
443 if (!trait_at(board
, coord
, color
).safe
)
444 value
*= board
->gamma
->gamma
[FEAT_SELFATARI
][1 + board
->precise_selfatari
];
445 probdist_set(&board
->prob
[color
- 1], coord
, double_to_fixp(value
));
451 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
453 if (board
->precise_selfatari
)
454 return !is_bad_selfatari(board
, color
, coord
);
456 return board_safe_to_play(board
, coord
, color
);
460 board_trait_recompute(struct board
*board
, coord_t coord
)
462 trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);;
463 trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
465 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
466 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
467 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).cap1
, trait_at(board
, coord
, S_BLACK
).safe
,
468 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).cap1
, trait_at(board
, coord
, S_WHITE
).safe
);
470 board_gamma_update(board
, coord
, S_BLACK
);
471 board_gamma_update(board
, coord
, S_WHITE
);
475 /* Recompute traits for dirty points that we have previously touched
476 * somehow (libs of their neighbors changed or so). */
478 board_traits_recompute(struct board
*board
)
481 for (int i
= 0; i
< board
->tqlen
; i
++) {
482 coord_t coord
= board
->tq
[i
];
483 trait_at(board
, coord
, S_BLACK
).dirty
= false;
484 if (board_at(board
, coord
) != S_NONE
)
486 board_trait_recompute(board
, coord
);
492 /* Queue traits of given point for recomputing. */
494 board_trait_queue(struct board
*board
, coord_t coord
)
497 if (trait_at(board
, coord
, S_BLACK
).dirty
)
499 board
->tq
[board
->tqlen
++] = coord
;
500 trait_at(board
, coord
, S_BLACK
).dirty
= true;
505 /* Update board hash with given coordinate. */
506 static void profiling_noinline
507 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
509 board
->hash
^= hash_at(board
, coord
, color
);
511 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
);
513 #ifdef BOARD_SPATHASH
514 /* Gridcular metric is reflective, so we update all hashes
515 * of appropriate ditance in OUR circle. */
516 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
517 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
518 ptcoords_at(x
, y
, coord
, board
, j
);
519 /* We either changed from S_NONE to color
520 * or vice versa; doesn't matter. */
521 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
522 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
523 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
524 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
529 #if defined(BOARD_PAT3)
530 /* @color is not what we need in case of capture. */
531 enum stone new_color
= board_at(board
, coord
);
532 if (new_color
== S_NONE
)
533 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
534 foreach_8neighbor(board
, coord
) { // internally, the loop uses fn__i=[0..7]
535 if (board_at(board
, c
) != S_NONE
)
537 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
538 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
540 if (board_at(board
, c
) != S_OFFBOARD
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
541 board_print(board
, stderr
);
542 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
);
546 board_trait_queue(board
, c
);
547 } foreach_8neighbor_end
;
551 /* Commit current board hash to history. */
552 static void profiling_noinline
553 board_hash_commit(struct board
*board
)
556 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
557 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
558 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
560 hash_t i
= board
->hash
;
561 while (board
->history_hash
[i
& history_hash_mask
]) {
562 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
564 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
565 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
566 board
->superko_violation
= true;
569 i
= history_hash_next(i
);
571 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
577 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
579 if (likely(symmetry
->type
== SYM_NONE
)) {
580 /* Fully degenerated already. We do not support detection
581 * of restoring of symmetry, assuming that this is too rare
582 * a case to handle. */
586 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
587 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
589 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
590 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
591 symmetry
->d
, symmetry
->type
, x
, y
);
594 switch (symmetry
->type
) {
596 if (x
== t
&& y
== t
) {
597 /* Tengen keeps full symmetry. */
600 /* New symmetry now? */
602 symmetry
->type
= SYM_DIAG_UP
;
603 symmetry
->x1
= symmetry
->y1
= 1;
604 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
606 } else if (dx
== y
) {
607 symmetry
->type
= SYM_DIAG_DOWN
;
608 symmetry
->x1
= symmetry
->y1
= 1;
609 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
612 symmetry
->type
= SYM_HORIZ
;
614 symmetry
->y2
= board_size(b
) - 1;
617 symmetry
->type
= SYM_VERT
;
619 symmetry
->x2
= board_size(b
) - 1;
623 symmetry
->type
= SYM_NONE
;
624 symmetry
->x1
= symmetry
->y1
= 1;
625 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
651 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
652 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
653 symmetry
->d
, symmetry
->type
);
660 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
663 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
665 board_play(board
, &m
);
666 /* Simulate white passing; otherwise, UCT search can get confused since
667 * tree depth parity won't match the color to move. */
670 char *str
= coord2str(m
.coord
, board
);
672 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
673 if (f
) fprintf(f
, "%s ", str
);
678 board_handicap(struct board
*board
, int stones
, FILE *f
)
680 int margin
= 3 + (board_size(board
) >= 13);
682 int mid
= board_size(board
) / 2;
683 int max
= board_size(board
) - 1 - margin
;
684 const int places
[][2] = {
685 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
686 { min
, mid
}, { max
, mid
},
687 { mid
, min
}, { mid
, max
},
691 board
->handicap
= stones
;
693 if (stones
== 5 || stones
== 7) {
694 board_handicap_stone(board
, mid
, mid
, f
);
699 for (i
= 0; i
< stones
; i
++)
700 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
704 static void __attribute__((noinline
))
705 check_libs_consistency(struct board
*board
, group_t g
)
709 struct group
*gi
= &board_group_info(board
, g
);
710 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
711 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
712 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
719 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
721 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
723 /* Increase capturable count trait of my last lib. */
724 enum stone capturing_color
= stone_other(board_at(board
, group
));
725 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
726 foreach_neighbor(board
, lib
, {
727 if (DEBUGL(8) && group_at(board
, c
) == group
)
728 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
);
729 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
730 trait_at(board
, lib
, capturing_color
).cap1
+= (group_at(board
, c
) == group
&& onestone
);
732 board_trait_queue(board
, lib
);
736 /* Update the list of capturable groups. */
738 assert(board
->clen
< board_size2(board
));
739 board
->c
[board
->clen
++] = group
;
743 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
745 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
747 /* Decrease capturable count trait of my previously-last lib. */
748 enum stone capturing_color
= stone_other(board_at(board
, group
));
749 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
750 foreach_neighbor(board
, lib
, {
751 if (DEBUGL(8) && group_at(board
, c
) == group
)
752 fprintf(stderr
, "%s[%d] cap dump bc of %s(%d) member %s onestone %d\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
, coord2sstr(group
, board
), board_group_info(board
, group
).libs
, coord2sstr(c
, board
), onestone
);
753 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
754 trait_at(board
, lib
, capturing_color
).cap1
-= (group_at(board
, c
) == group
&& onestone
);
756 board_trait_queue(board
, lib
);
760 /* Update the list of capturable groups. */
761 for (int i
= 0; i
< board
->clen
; i
++) {
762 if (unlikely(board
->c
[i
] == group
)) {
763 board
->c
[i
] = board
->c
[--board
->clen
];
767 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
773 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
776 board_trait_queue(board
, lib1
);
777 board_trait_queue(board
, lib2
);
781 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
784 board_trait_queue(board
, lib1
);
785 board_trait_queue(board
, lib2
);
790 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
793 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
794 group_base(group
), coord2sstr(group_base(group
), board
),
795 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
798 check_libs_consistency(board
, group
);
800 struct group
*gi
= &board_group_info(board
, group
);
801 bool onestone
= group_is_onestone(board
, group
);
802 if (gi
->libs
< GROUP_KEEP_LIBS
) {
803 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
805 /* Seems extra branch just slows it down */
809 if (unlikely(gi
->lib
[i
] == coord
))
813 board_capturable_add(board
, group
, coord
, onestone
);
814 } else if (gi
->libs
== 1) {
815 board_capturable_rm(board
, group
, gi
->lib
[0], onestone
);
816 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
817 } else if (gi
->libs
== 2) {
818 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
820 gi
->lib
[gi
->libs
++] = coord
;
823 check_libs_consistency(board
, group
);
827 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
829 /* Add extra liberty from the board to our liberty list. */
830 unsigned char watermark
[board_size2(board
) / 8];
831 memset(watermark
, 0, sizeof(watermark
));
832 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
833 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
835 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
836 watermark_set(gi
->lib
[i
]);
837 watermark_set(avoid
);
839 foreach_in_group(board
, group
) {
841 foreach_neighbor(board
, coord2
, {
842 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
845 gi
->lib
[gi
->libs
++] = c
;
846 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
849 } foreach_in_group_end
;
855 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
858 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
859 group_base(group
), coord2sstr(group_base(group
), board
),
860 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
863 struct group
*gi
= &board_group_info(board
, group
);
864 bool onestone
= group_is_onestone(board
, group
);
865 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
867 /* Seems extra branch just slows it down */
871 if (likely(gi
->lib
[i
] != coord
))
874 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
875 gi
->lib
[gi
->libs
] = 0;
877 check_libs_consistency(board
, group
);
879 /* Postpone refilling lib[] until we need to. */
880 assert(GROUP_REFILL_LIBS
> 1);
881 if (gi
->libs
> GROUP_REFILL_LIBS
)
883 if (gi
->libs
== GROUP_REFILL_LIBS
)
884 board_group_find_extra_libs(board
, group
, gi
, coord
);
887 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
888 } else if (gi
->libs
== 1) {
889 board_capturable_add(board
, group
, gi
->lib
[0], onestone
);
890 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
891 } else if (gi
->libs
== 0)
892 board_capturable_rm(board
, group
, lib
, onestone
);
896 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
897 * can call this multiple times per coord. */
898 check_libs_consistency(board
, group
);
903 /* This is a low-level routine that doesn't maintain consistency
904 * of all the board data structures. */
906 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
908 enum stone color
= board_at(board
, c
);
909 board_at(board
, c
) = S_NONE
;
910 group_at(board
, c
) = 0;
911 board_hash_update(board
, c
, color
);
913 /* We mark as cannot-capture now. If this is a ko/snapback,
914 * we will get incremented later in board_group_addlib(). */
915 trait_at(board
, c
, S_BLACK
).cap
= trait_at(board
, c
, S_BLACK
).cap1
= 0;
916 trait_at(board
, c
, S_WHITE
).cap
= trait_at(board
, c
, S_WHITE
).cap1
= 0;
917 board_trait_queue(board
, c
);
920 /* Increase liberties of surrounding groups */
922 foreach_neighbor(board
, coord
, {
923 dec_neighbor_count_at(board
, c
, color
);
924 board_trait_queue(board
, c
);
925 group_t g
= group_at(board
, c
);
927 board_group_addlib(board
, g
, coord
);
931 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
932 board
->f
[board
->flen
++] = c
;
935 static int profiling_noinline
936 board_group_capture(struct board
*board
, group_t group
)
940 foreach_in_group(board
, group
) {
941 board
->captures
[stone_other(board_at(board
, c
))]++;
942 board_remove_stone(board
, group
, c
);
944 } foreach_in_group_end
;
946 struct group
*gi
= &board_group_info(board
, group
);
947 assert(gi
->libs
== 0);
948 memset(gi
, 0, sizeof(*gi
));
954 static void profiling_noinline
955 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
958 struct group
*gi
= &board_group_info(board
, group
);
959 bool onestone
= group_is_onestone(board
, group
);
962 /* Our group is temporarily in atari; make sure the capturable
963 * counts also correspond to the newly added stone before we
964 * start adding liberties again so bump-dump ops match. */
965 enum stone capturing_color
= stone_other(board_at(board
, group
));
966 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
968 coord_t lib
= board_group_info(board
, group
).lib
[0];
969 if (coord_is_adjecent(lib
, coord
, board
)) {
971 fprintf(stderr
, "add_to_group %s: %s[%d] bump\n", coord2sstr(group
, board
), coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
972 trait_at(board
, lib
, capturing_color
).cap
++;
973 /* This is never a 1-stone group, obviously. */
974 board_trait_queue(board
, lib
);
978 /* We are not 1-stone group anymore, update the cap1
979 * counter specifically. */
980 foreach_neighbor(board
, group
, {
981 if (board_at(board
, c
) != S_NONE
) continue;
982 trait_at(board
, c
, capturing_color
).cap1
--;
983 board_trait_queue(board
, c
);
989 group_at(board
, coord
) = group
;
990 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
991 groupnext_at(board
, prevstone
) = coord
;
993 foreach_neighbor(board
, coord
, {
994 if (board_at(board
, c
) == S_NONE
)
995 board_group_addlib(board
, group
, c
);
999 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1000 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
1001 coord_x(coord
, board
), coord_y(coord
, board
),
1002 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
1006 static void profiling_noinline
1007 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
1010 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
1011 group_base(group_from
), group_base(group_to
));
1012 struct group
*gi_from
= &board_group_info(board
, group_from
);
1013 struct group
*gi_to
= &board_group_info(board
, group_to
);
1014 bool onestone_from
= group_is_onestone(board
, group_from
);
1015 bool onestone_to
= group_is_onestone(board
, group_to
);
1017 /* We do this early before the group info is rewritten. */
1018 if (gi_from
->libs
== 2)
1019 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
1020 else if (gi_from
->libs
== 1)
1021 board_capturable_rm(board
, group_from
, gi_from
->lib
[0], onestone_from
);
1024 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
1026 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
1027 for (int i
= 0; i
< gi_from
->libs
; i
++) {
1028 for (int j
= 0; j
< gi_to
->libs
; j
++)
1029 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
1031 if (gi_to
->libs
== 0) {
1032 board_capturable_add(board
, group_to
, gi_from
->lib
[i
], onestone_to
);
1033 } else if (gi_to
->libs
== 1) {
1034 board_capturable_rm(board
, group_to
, gi_to
->lib
[0], onestone_to
);
1035 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
1036 } else if (gi_to
->libs
== 2) {
1037 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
1039 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
1040 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
1047 if (gi_to
->libs
== 1) {
1048 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
1049 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1051 /* Our group is currently in atari; make sure we properly
1052 * count in even the neighbors from the other group in the
1053 * capturable counter. */
1054 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
1055 foreach_neighbor(board
, lib
, {
1056 if (DEBUGL(8) && group_at(board
, c
) == group_from
)
1057 fprintf(stderr
, "%s[%d] cap bump\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
1058 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
1059 /* This is never a 1-stone group, obviously. */
1061 board_trait_queue(board
, lib
);
1064 /* We are not 1-stone group anymore, update the cap1
1065 * counter specifically. */
1066 foreach_neighbor(board
, group_to
, {
1067 if (board_at(board
, c
) != S_NONE
) continue;
1068 trait_at(board
, c
, capturing_color
).cap1
--;
1069 board_trait_queue(board
, c
);
1075 coord_t last_in_group
;
1076 foreach_in_group(board
, group_from
) {
1078 group_at(board
, c
) = group_to
;
1079 } foreach_in_group_end
;
1080 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1081 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1082 memset(gi_from
, 0, sizeof(struct group
));
1085 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1086 group_base(group_to
));
1089 static group_t profiling_noinline
1090 new_group(struct board
*board
, coord_t coord
)
1092 group_t group
= coord
;
1093 struct group
*gi
= &board_group_info(board
, group
);
1094 foreach_neighbor(board
, coord
, {
1095 if (board_at(board
, c
) == S_NONE
)
1096 /* board_group_addlib is ridiculously expensive for us */
1097 #if GROUP_KEEP_LIBS < 4
1098 if (gi
->libs
< GROUP_KEEP_LIBS
)
1100 gi
->lib
[gi
->libs
++] = c
;
1103 group_at(board
, coord
) = group
;
1104 groupnext_at(board
, coord
) = 0;
1107 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1108 else if (gi
->libs
== 1)
1109 board_capturable_add(board
, group
, gi
->lib
[0], true);
1110 check_libs_consistency(board
, group
);
1113 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1114 coord_x(coord
, board
), coord_y(coord
, board
),
1120 static inline group_t
1121 play_one_neighbor(struct board
*board
,
1122 coord_t coord
, enum stone color
, enum stone other_color
,
1123 coord_t c
, group_t group
)
1125 enum stone ncolor
= board_at(board
, c
);
1126 group_t ngroup
= group_at(board
, c
);
1128 inc_neighbor_count_at(board
, c
, color
);
1129 /* We can be S_NONE, in that case we need to update the safety
1130 * trait since we might be left with only one liberty. */
1131 board_trait_queue(board
, c
);
1136 board_group_rmlib(board
, ngroup
, coord
);
1138 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1139 group_base(ngroup
), ncolor
, color
, other_color
);
1141 if (ncolor
== color
&& ngroup
!= group
) {
1144 add_to_group(board
, group
, c
, coord
);
1146 merge_groups(board
, group
, ngroup
);
1148 } else if (ncolor
== other_color
) {
1150 struct group
*gi
= &board_group_info(board
, ngroup
);
1151 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1152 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1153 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1154 fprintf(stderr
, "\n");
1156 if (unlikely(board_group_captured(board
, ngroup
)))
1157 board_group_capture(board
, ngroup
);
1162 /* We played on a place with at least one liberty. We will become a member of
1163 * some group for sure. */
1164 static group_t profiling_noinline
1165 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1167 coord_t coord
= m
->coord
;
1168 enum stone color
= m
->color
;
1169 enum stone other_color
= stone_other(color
);
1172 board
->f
[f
] = board
->f
[--board
->flen
];
1174 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1176 #if defined(BOARD_TRAITS) && defined(DEBUG)
1177 /* Sanity check that cap matches reality. */
1180 foreach_neighbor(board
, coord
, {
1181 group_t g
= group_at(board
, c
);
1182 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1183 b
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1) && group_is_onestone(board
, g
);
1185 assert(a
== trait_at(board
, coord
, color
).cap
);
1186 assert(b
== trait_at(board
, coord
, color
).cap1
);
1187 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1190 foreach_neighbor(board
, coord
, {
1191 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1194 board_at(board
, coord
) = color
;
1195 if (unlikely(!group
))
1196 group
= new_group(board
, coord
);
1197 board_gamma_update(board
, coord
, S_BLACK
);
1198 board_gamma_update(board
, coord
, S_WHITE
);
1200 board
->last_move2
= board
->last_move
;
1201 board
->last_move
= *m
;
1203 board_hash_update(board
, coord
, color
);
1204 board_symmetry_update(board
, &board
->symmetry
, coord
);
1205 struct move ko
= { pass
, S_NONE
};
1211 /* We played in an eye-like shape. Either we capture at least one of the eye
1212 * sides in the process of playing, or return -1. */
1213 static int profiling_noinline
1214 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1216 coord_t coord
= m
->coord
;
1217 enum stone color
= m
->color
;
1218 /* Check ko: Capture at a position of ko capture one move ago */
1219 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1221 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1223 } else if (DEBUGL(6)) {
1224 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1225 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1226 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1229 struct move ko
= { pass
, S_NONE
};
1231 int captured_groups
= 0;
1233 foreach_neighbor(board
, coord
, {
1234 group_t g
= group_at(board
, c
);
1236 fprintf(stderr
, "board_check: group %d has %d libs\n",
1237 g
, board_group_info(board
, g
).libs
);
1238 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1241 if (likely(captured_groups
== 0)) {
1244 board_print(board
, stderr
);
1245 fprintf(stderr
, "board_check: one-stone suicide\n");
1251 /* We _will_ for sure capture something. */
1252 assert(trait_at(board
, coord
, color
).cap
> 0);
1253 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1256 board
->f
[f
] = board
->f
[--board
->flen
];
1258 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1260 foreach_neighbor(board
, coord
, {
1261 inc_neighbor_count_at(board
, c
, color
);
1262 /* Originally, this could not have changed any trait
1263 * since no neighbors were S_NONE, however by now some
1264 * of them might be removed from the board. */
1265 board_trait_queue(board
, c
);
1267 group_t group
= group_at(board
, c
);
1271 board_group_rmlib(board
, group
, coord
);
1273 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1276 if (board_group_captured(board
, group
)) {
1277 if (board_group_capture(board
, group
) == 1) {
1278 /* If we captured multiple groups at once,
1279 * we can't be fighting ko so we don't need
1280 * to check for that. */
1281 ko
.color
= stone_other(color
);
1283 board
->last_ko
= ko
;
1284 board
->last_ko_age
= board
->moves
;
1286 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1291 board_at(board
, coord
) = color
;
1292 group_t group
= new_group(board
, coord
);
1293 board_gamma_update(board
, coord
, S_BLACK
);
1294 board_gamma_update(board
, coord
, S_WHITE
);
1296 board
->last_move2
= board
->last_move
;
1297 board
->last_move
= *m
;
1299 board_hash_update(board
, coord
, color
);
1300 board_hash_commit(board
);
1301 board_traits_recompute(board
);
1302 board_symmetry_update(board
, &board
->symmetry
, coord
);
1308 static int __attribute__((flatten
))
1309 board_play_f(struct board
*board
, struct move
*m
, int f
)
1312 fprintf(stderr
, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m
->coord
, board
), coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1314 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1315 /* NOT playing in an eye. Thus this move has to succeed. (This
1316 * is thanks to New Zealand rules. Otherwise, multi-stone
1317 * suicide might fail.) */
1318 group_t group
= board_play_outside(board
, m
, f
);
1319 if (unlikely(board_group_captured(board
, group
))) {
1320 board_group_capture(board
, group
);
1322 board_hash_commit(board
);
1323 board_traits_recompute(board
);
1326 return board_play_in_eye(board
, m
, f
);
1331 board_play(struct board
*board
, struct move
*m
)
1333 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1334 struct move nomove
= { pass
, S_NONE
};
1336 board
->last_move2
= board
->last_move
;
1337 board
->last_move
= *m
;
1342 for (f
= 0; f
< board
->flen
; f
++)
1343 if (board
->f
[f
] == m
->coord
)
1344 return board_play_f(board
, m
, f
);
1347 fprintf(stderr
, "board_check: stone exists\n");
1353 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1356 struct move m
= { *coord
, color
};
1358 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
1359 if (unlikely(board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1360 || !board_is_valid_move(b
, &m
)
1361 || (permit
&& !permit(permit_data
, b
, &m
)))
1363 *coord
= m
.coord
; // permit might modify it
1364 return likely(board_play_f(b
, &m
, f
) >= 0);
1368 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1370 if (unlikely(b
->flen
== 0))
1373 int base
= fast_random(b
->flen
), f
;
1374 for (f
= base
; f
< b
->flen
; f
++)
1375 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1377 for (f
= 0; f
< base
; f
++)
1378 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1383 struct move m
= { pass
, color
};
1389 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1391 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1393 /* XXX: We attempt false eye detection but we will yield false
1394 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1396 foreach_diag_neighbor(board
, coord
) {
1397 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1398 } foreach_diag_neighbor_end
;
1399 /* For false eye, we need two enemy stones diagonally in the
1400 * middle of the board, or just one enemy stone at the edge
1401 * or in the corner. */
1402 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1403 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1407 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1409 return board_is_eyelike(board
, coord
, eye_color
)
1410 && !board_is_false_eyelike(board
, coord
, eye_color
);
1414 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1416 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1418 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1426 board_fast_score(struct board
*board
)
1429 memset(scores
, 0, sizeof(scores
));
1431 foreach_point(board
) {
1432 enum stone color
= board_at(board
, c
);
1433 if (color
== S_NONE
)
1434 color
= board_get_one_point_eye(board
, c
);
1436 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1437 } foreach_point_end
;
1439 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1442 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1444 /* One flood-fill iteration; returns true if next iteration
1447 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1449 bool needs_update
= false;
1450 foreach_free_point(board
) {
1451 /* Ignore occupied and already-dame positions. */
1452 assert(board_at(board
, c
) == S_NONE
);
1453 if (ownermap
[c
] == 3)
1455 /* Count neighbors. */
1457 foreach_neighbor(board
, c
, {
1460 /* If we have neighbors of both colors, or dame,
1461 * we are dame too. */
1462 if ((nei
[1] && nei
[2]) || nei
[3]) {
1464 /* Speed up the propagation. */
1465 foreach_neighbor(board
, c
, {
1466 if (board_at(board
, c
) == S_NONE
)
1469 needs_update
= true;
1472 /* If we have neighbors of one color, we are owned
1473 * by that color, too. */
1474 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1475 int newowner
= nei
[1] ? 1 : 2;
1476 ownermap
[c
] = newowner
;
1477 /* Speed up the propagation. */
1478 foreach_neighbor(board
, c
, {
1479 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1480 ownermap
[c
] = newowner
;
1482 needs_update
= true;
1485 } foreach_free_point_end
;
1486 return needs_update
;
1489 /* Tromp-Taylor Counting */
1491 board_official_score(struct board
*board
, struct move_queue
*q
)
1494 /* A point P, not colored C, is said to reach C, if there is a path of
1495 * (vertically or horizontally) adjacent points of P's color from P to
1496 * a point of color C.
1498 * A player's score is the number of points of her color, plus the
1499 * number of empty points that reach only her color. */
1501 int ownermap
[board_size2(board
)];
1503 const int o
[4] = {0, 1, 2, 0};
1504 foreach_point(board
) {
1505 ownermap
[c
] = o
[board_at(board
, c
)];
1506 s
[board_at(board
, c
)]++;
1507 } foreach_point_end
;
1510 /* Process dead groups. */
1511 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
1512 foreach_in_group(board
, q
->move
[i
]) {
1513 enum stone color
= board_at(board
, c
);
1514 ownermap
[c
] = o
[stone_other(color
)];
1515 s
[color
]--; s
[stone_other(color
)]++;
1516 } foreach_in_group_end
;
1520 /* We need to special-case empty board. */
1521 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1522 return board
->komi
+ board
->handicap
;
1524 while (board_tromp_taylor_iter(board
, ownermap
))
1525 /* Flood-fill... */;
1528 memset(scores
, 0, sizeof(scores
));
1530 foreach_point(board
) {
1531 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1532 if (ownermap
[c
] == 3)
1534 scores
[ownermap
[c
]]++;
1535 } foreach_point_end
;
1537 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];