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
);
98 int cdsize
= board_size2(b2
) * sizeof(*b2
->coord
);
99 void *x
= malloc2(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ pbsize
* 2 + cdsize
);
100 memcpy(x
, b1
->b
, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ pbsize
* 2 + cdsize
);
101 b2
->b
= x
; x
+= bsize
;
102 b2
->g
= x
; x
+= gsize
;
103 b2
->f
= x
; x
+= fsize
;
104 b2
->p
= x
; x
+= psize
;
105 b2
->n
= x
; x
+= nsize
;
106 b2
->h
= x
; x
+= hsize
;
107 b2
->gi
= x
; x
+= gisize
;
109 b2
->c
= x
; x
+= csize
;
111 #ifdef BOARD_SPATHASH
112 b2
->spathash
= x
; x
+= ssize
;
115 b2
->pat3
= x
; x
+= p3size
;
118 b2
->t
= x
; x
+= tsize
;
119 b2
->tq
= x
; x
+= tqsize
;
122 b2
->prob
[0].items
= x
; x
+= pbsize
;
123 b2
->prob
[1].items
= x
; x
+= pbsize
;
125 b2
->coord
= x
; x
+= cdsize
;
131 board_done_noalloc(struct board
*board
)
133 if (board
->b
) free(board
->b
);
137 board_done(struct board
*board
)
139 board_done_noalloc(board
);
144 board_resize(struct board
*board
, int size
)
147 assert(board_size(board
) == size
+ 2);
149 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
150 board
->size2
= board_size(board
) * board_size(board
);
154 int bsize
= board_size2(board
) * sizeof(*board
->b
);
155 int gsize
= board_size2(board
) * sizeof(*board
->g
);
156 int fsize
= board_size2(board
) * sizeof(*board
->f
);
157 int nsize
= board_size2(board
) * sizeof(*board
->n
);
158 int psize
= board_size2(board
) * sizeof(*board
->p
);
159 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
160 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
162 int csize
= board_size2(board
) * sizeof(*board
->c
);
166 #ifdef BOARD_SPATHASH
167 int ssize
= board_size2(board
) * sizeof(*board
->spathash
);
172 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
177 int tsize
= board_size2(board
) * sizeof(*board
->t
);
178 int tqsize
= board_size2(board
) * sizeof(*board
->t
);
184 int pbsize
= board_size2(board
) * sizeof(*board
->prob
[0].items
);
188 int cdsize
= board_size2(board
) * sizeof(*board
->coord
);
189 void *x
= malloc2(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ pbsize
* 2 + cdsize
);
190 memset(x
, 0, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ pbsize
* 2 + cdsize
);
191 board
->b
= x
; x
+= bsize
;
192 board
->g
= x
; x
+= gsize
;
193 board
->f
= x
; x
+= fsize
;
194 board
->p
= x
; x
+= psize
;
195 board
->n
= x
; x
+= nsize
;
196 board
->h
= x
; x
+= hsize
;
197 board
->gi
= x
; x
+= gisize
;
199 board
->c
= x
; x
+= csize
;
201 #ifdef BOARD_SPATHASH
202 board
->spathash
= x
; x
+= ssize
;
205 board
->pat3
= x
; x
+= p3size
;
208 board
->t
= x
; x
+= tsize
;
209 board
->tq
= x
; x
+= tqsize
;
212 board
->prob
[0].items
= x
; x
+= pbsize
;
213 board
->prob
[1].items
= x
; x
+= pbsize
;
215 board
->coord
= x
; x
+= cdsize
;
219 board_clear(struct board
*board
)
221 int size
= board_size(board
);
222 float komi
= board
->komi
;
224 board_done_noalloc(board
);
226 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
230 /* Setup neighborhood iterators */
231 board
->nei8
[0] = -size
- 1; // (-1,-1)
234 board
->nei8
[3] = size
- 2; // (-1,0)
236 board
->nei8
[5] = size
- 2; // (-1,1)
239 board
->dnei
[0] = -size
- 1;
241 board
->dnei
[2] = size
*2 - 2;
244 /* Setup initial symmetry */
245 board
->symmetry
.d
= 1;
246 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
247 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
248 board
->symmetry
.type
= SYM_FULL
;
250 /* Set up coordinate cache */
251 foreach_point(board
) {
252 board
->coord
[c
][0] = c
% board_size(board
);
253 board
->coord
[c
][1] = c
/ board_size(board
);
256 /* Draw the offboard margin */
257 int top_row
= board_size2(board
) - board_size(board
);
259 for (i
= 0; i
< board_size(board
); i
++)
260 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
261 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
262 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
264 foreach_point(board
) {
266 if (board_at(board
, coord
) == S_OFFBOARD
)
268 foreach_neighbor(board
, c
, {
269 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
273 /* All positions are free! Except the margin. */
274 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
275 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
276 board
->f
[board
->flen
++] = i
;
278 /* Initialize zobrist hashtable. */
279 foreach_point(board
) {
280 int max
= (sizeof(hash_t
) << history_hash_bits
);
281 /* fast_random() is 16-bit only */
282 board
->h
[c
* 2] = ((hash_t
) fast_random(max
))
283 | ((hash_t
) fast_random(max
) << 16)
284 | ((hash_t
) fast_random(max
) << 32)
285 | ((hash_t
) fast_random(max
) << 48);
286 if (!board
->h
[c
* 2])
287 /* Would be kinda "oops". */
289 /* And once again for white */
290 board
->h
[c
* 2 + 1] = ((hash_t
) fast_random(max
))
291 | ((hash_t
) fast_random(max
) << 16)
292 | ((hash_t
) fast_random(max
) << 32)
293 | ((hash_t
) fast_random(max
) << 48);
294 if (!board
->h
[c
* 2 + 1])
295 board
->h
[c
* 2 + 1] = 1;
298 #ifdef BOARD_SPATHASH
299 /* Initialize spatial hashes. */
300 foreach_point(board
) {
301 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
302 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
303 ptcoords_at(x
, y
, c
, board
, j
);
304 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
305 pthashes
[0][j
][board_at(board
, c
)];
306 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
307 pthashes
[0][j
][stone_other(board_at(board
, c
))];
313 /* Initialize 3x3 pattern codes. */
314 foreach_point(board
) {
315 if (board_at(board
, c
) == S_NONE
)
316 board
->pat3
[c
] = pattern3_hash(board
, c
);
320 /* Initialize traits. */
321 foreach_point(board
) {
322 trait_at(board
, c
, S_BLACK
).cap
= 0;
323 trait_at(board
, c
, S_BLACK
).safe
= true;
324 trait_at(board
, c
, S_WHITE
).cap
= 0;
325 trait_at(board
, c
, S_WHITE
).safe
= true;
329 board
->prob
[0].n
= board
->prob
[1].n
= board_size2(board
);
330 foreach_point(board
) {
331 probdist_set(&board
->prob
[0], c
, (board_at(board
, c
) == S_NONE
) * 1.0f
);
332 probdist_set(&board
->prob
[1], c
, (board_at(board
, c
) == S_NONE
) * 1.0f
);
338 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
340 for (int i
= 0; i
< c
; i
++) {
341 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
342 s
+= snprintf(s
, end
- s
, " ");
343 for (int x
= 1; x
< board_size(board
) - 1; x
++)
344 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
345 s
+= snprintf(s
, end
-s
, " ");
347 s
+= snprintf(s
, end
- s
, "\n");
348 for (int i
= 0; i
< c
; i
++) {
349 s
+= snprintf(s
, end
- s
, " +-");
350 for (int x
= 1; x
< board_size(board
) - 1; x
++)
351 s
+= snprintf(s
, end
- s
, "--");
352 s
+= snprintf(s
, end
- s
, "+");
354 s
+= snprintf(s
, end
- s
, "\n");
359 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
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_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
)
374 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
375 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
376 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
377 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
379 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
381 s
+= snprintf(s
, end
- s
, "|");
383 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
384 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
385 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
);
387 s
+= snprintf(s
, end
- s
, "|");
389 s
+= snprintf(s
, end
- s
, "\n");
394 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
398 char *end
= buf
+ sizeof(buf
);
399 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
400 board
->moves
, board
->komi
, board
->handicap
,
401 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
402 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
403 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
404 s
= board_print_row(board
, y
, s
, end
, cprint
);
405 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
406 fprintf(f
, "%s\n", buf
);
410 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
)
412 s
+= snprintf(s
, end
- s
, "%d ", group_base(group_at(board
, c
)));
417 board_print(struct board
*board
, FILE *f
)
419 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
423 board_gamma_set(struct board
*b
, struct features_gamma
*gamma
, bool precise_selfatari
)
427 b
->precise_selfatari
= precise_selfatari
;
428 for (int i
= 0; i
< b
->flen
; i
++) {
429 board_trait_recompute(b
, b
->f
[i
]);
435 /* Update the probability distribution we maintain incrementally. */
437 board_gamma_update(struct board
*board
, coord_t coord
, enum stone color
)
443 /* Punch out invalid moves and moves filling our own eyes. */
444 if (board_at(board
, coord
) != S_NONE
445 || (board_is_eyelike(board
, coord
, stone_other(color
))
446 && !trait_at(board
, coord
, color
).cap
)
447 || (board_is_one_point_eye(board
, coord
, color
))) {
448 probdist_set(&board
->prob
[color
- 1], coord
, 0);
452 int pat
= board
->pat3
[coord
];
453 if (color
== S_WHITE
) {
454 /* We work with the pattern3s as black-to-play. */
455 pat
= pattern3_reverse(pat
);
458 /* We just quickly replicate the general pattern matcher stuff
459 * here in the most bare-bone way. */
460 double value
= board
->gamma
->gamma
[FEAT_PATTERN3
][pat
];
461 if (trait_at(board
, coord
, color
).cap
)
462 value
*= board
->gamma
->gamma
[FEAT_CAPTURE
][0];
463 if (trait_at(board
, coord
, stone_other(color
)).cap
464 && trait_at(board
, coord
, color
).safe
)
465 value
*= board
->gamma
->gamma
[FEAT_AESCAPE
][0];
466 if (!trait_at(board
, coord
, color
).safe
)
467 value
*= board
->gamma
->gamma
[FEAT_SELFATARI
][1 + board
->precise_selfatari
];
468 probdist_set(&board
->prob
[color
- 1], coord
, value
);
474 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
477 if (board
->precise_selfatari
)
478 return is_bad_selfatari(board
, color
, coord
);
480 return board_safe_to_play(board
, coord
, color
);
484 board_trait_recompute(struct board
*board
, coord_t coord
)
486 trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);;
487 trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
489 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d safe=%d) (white cap=%d safe=%d)\n",
490 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
491 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).safe
,
492 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).safe
);
494 board_gamma_update(board
, coord
, S_BLACK
);
495 board_gamma_update(board
, coord
, S_WHITE
);
499 /* Recompute traits for dirty points that we have previously touched
500 * somehow (libs of their neighbors changed or so). */
502 board_traits_recompute(struct board
*board
)
505 for (int i
= 0; i
< board
->tqlen
; i
++) {
506 coord_t coord
= board
->tq
[i
];
507 if (!trait_at(board
, coord
, S_BLACK
).dirty
) continue;
508 if (board_at(board
, coord
) != S_NONE
) continue;
509 board_trait_recompute(board
, coord
);
510 trait_at(board
, coord
, S_BLACK
).dirty
= false;
516 /* Queue traits of given point for recomputing. */
518 board_trait_queue(struct board
*board
, coord_t coord
)
521 board
->tq
[board
->tqlen
++] = coord
;
522 trait_at(board
, coord
, S_BLACK
).dirty
= true;
527 /* Update board hash with given coordinate. */
528 static void profiling_noinline
529 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
531 board
->hash
^= hash_at(board
, coord
, color
);
533 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
);
535 #ifdef BOARD_SPATHASH
536 /* Gridcular metric is reflective, so we update all hashes
537 * of appropriate ditance in OUR circle. */
538 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
539 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
540 ptcoords_at(x
, y
, coord
, board
, j
);
541 /* We either changed from S_NONE to color
542 * or vice versa; doesn't matter. */
543 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
544 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
545 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
546 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
551 #if defined(BOARD_PAT3)
552 /* @color is not what we need in case of capture. */
553 enum stone new_color
= board_at(board
, coord
);
554 if (new_color
== S_NONE
)
555 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
556 foreach_8neighbor(board
, coord
) { // internally, the loop uses fn__i=[0..7]
557 if (board_at(board
, c
) != S_NONE
)
559 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
560 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
562 if (board_at(board
, c
) != S_OFFBOARD
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
563 board_print(board
, stderr
);
564 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
);
568 board_gamma_update(board
, c
, S_BLACK
);
569 board_gamma_update(board
, c
, S_WHITE
);
570 } foreach_8neighbor_end
;
574 /* Commit current board hash to history. */
575 static void profiling_noinline
576 board_hash_commit(struct board
*board
)
579 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
580 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
581 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
583 hash_t i
= board
->hash
;
584 while (board
->history_hash
[i
& history_hash_mask
]) {
585 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
587 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
588 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
589 board
->superko_violation
= true;
592 i
= history_hash_next(i
);
594 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
600 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
602 if (likely(symmetry
->type
== SYM_NONE
)) {
603 /* Fully degenerated already. We do not support detection
604 * of restoring of symmetry, assuming that this is too rare
605 * a case to handle. */
609 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
610 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
612 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
613 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
614 symmetry
->d
, symmetry
->type
, x
, y
);
617 switch (symmetry
->type
) {
619 if (x
== t
&& y
== t
) {
620 /* Tengen keeps full symmetry. */
623 /* New symmetry now? */
625 symmetry
->type
= SYM_DIAG_UP
;
626 symmetry
->x1
= symmetry
->y1
= 1;
627 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
629 } else if (dx
== y
) {
630 symmetry
->type
= SYM_DIAG_DOWN
;
631 symmetry
->x1
= symmetry
->y1
= 1;
632 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
635 symmetry
->type
= SYM_HORIZ
;
637 symmetry
->y2
= board_size(b
) - 1;
640 symmetry
->type
= SYM_VERT
;
642 symmetry
->x2
= board_size(b
) - 1;
646 symmetry
->type
= SYM_NONE
;
647 symmetry
->x1
= symmetry
->y1
= 1;
648 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
674 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
675 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
676 symmetry
->d
, symmetry
->type
);
683 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
686 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
688 board_play(board
, &m
);
689 /* Simulate white passing; otherwise, UCT search can get confused since
690 * tree depth parity won't match the color to move. */
693 char *str
= coord2str(m
.coord
, board
);
695 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
696 if (f
) fprintf(f
, "%s ", str
);
701 board_handicap(struct board
*board
, int stones
, FILE *f
)
703 int margin
= 3 + (board_size(board
) >= 13);
705 int mid
= board_size(board
) / 2;
706 int max
= board_size(board
) - 1 - margin
;
707 const int places
[][2] = {
708 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
709 { min
, mid
}, { max
, mid
},
710 { mid
, min
}, { mid
, max
},
714 board
->handicap
= stones
;
716 if (stones
== 5 || stones
== 7) {
717 board_handicap_stone(board
, mid
, mid
, f
);
722 for (i
= 0; i
< stones
; i
++)
723 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
727 static void __attribute__((noinline
))
728 check_libs_consistency(struct board
*board
, group_t g
)
732 struct group
*gi
= &board_group_info(board
, g
);
733 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
734 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
735 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
742 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
)
744 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
746 /* Increase capturable count trait of my last lib. */
747 enum stone capturing_color
= stone_other(board_at(board
, group
));
748 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
749 foreach_neighbor(board
, lib
, {
750 if (DEBUGL(8) && group_at(board
, c
) == group
)
751 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
));
752 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
754 board_trait_queue(board
, lib
);
758 /* Update the list of capturable groups. */
760 assert(board
->clen
< board_size2(board
));
761 board
->c
[board
->clen
++] = group
;
765 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
)
767 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
769 /* Decrease capturable count trait of my previously-last lib. */
770 enum stone capturing_color
= stone_other(board_at(board
, group
));
771 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
772 foreach_neighbor(board
, lib
, {
773 if (DEBUGL(8) && group_at(board
, c
) == group
)
774 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
));
775 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
777 board_trait_queue(board
, lib
);
781 /* Update the list of capturable groups. */
782 for (int i
= 0; i
< board
->clen
; i
++) {
783 if (unlikely(board
->c
[i
] == group
)) {
784 board
->c
[i
] = board
->c
[--board
->clen
];
788 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
794 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
797 board_trait_queue(board
, lib1
);
798 board_trait_queue(board
, lib2
);
802 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
805 board_trait_queue(board
, lib1
);
806 board_trait_queue(board
, lib2
);
811 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
814 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
815 group_base(group
), coord2sstr(group_base(group
), board
),
816 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
819 check_libs_consistency(board
, group
);
821 struct group
*gi
= &board_group_info(board
, group
);
822 if (gi
->libs
< GROUP_KEEP_LIBS
) {
823 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
825 /* Seems extra branch just slows it down */
829 if (unlikely(gi
->lib
[i
] == coord
))
833 board_capturable_add(board
, group
, coord
);
834 } else if (gi
->libs
== 1) {
835 board_capturable_rm(board
, group
, gi
->lib
[0]);
836 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
837 } else if (gi
->libs
== 2) {
838 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
840 gi
->lib
[gi
->libs
++] = coord
;
843 check_libs_consistency(board
, group
);
847 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
849 /* Add extra liberty from the board to our liberty list. */
850 unsigned char watermark
[board_size2(board
) / 8];
851 memset(watermark
, 0, sizeof(watermark
));
852 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
853 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
855 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
856 watermark_set(gi
->lib
[i
]);
857 watermark_set(avoid
);
859 foreach_in_group(board
, group
) {
861 foreach_neighbor(board
, coord2
, {
862 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
865 gi
->lib
[gi
->libs
++] = c
;
866 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
869 } foreach_in_group_end
;
875 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
878 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
879 group_base(group
), coord2sstr(group_base(group
), board
),
880 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
883 struct group
*gi
= &board_group_info(board
, group
);
884 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
886 /* Seems extra branch just slows it down */
890 if (likely(gi
->lib
[i
] != coord
))
893 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
894 gi
->lib
[gi
->libs
] = 0;
896 check_libs_consistency(board
, group
);
898 /* Postpone refilling lib[] until we need to. */
899 assert(GROUP_REFILL_LIBS
> 1);
900 if (gi
->libs
> GROUP_REFILL_LIBS
)
902 if (gi
->libs
== GROUP_REFILL_LIBS
)
903 board_group_find_extra_libs(board
, group
, gi
, coord
);
906 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
907 } else if (gi
->libs
== 1) {
908 board_capturable_add(board
, group
, gi
->lib
[0]);
909 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
910 } else if (gi
->libs
== 0)
911 board_capturable_rm(board
, group
, lib
);
915 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
916 * can call this multiple times per coord. */
917 check_libs_consistency(board
, group
);
922 /* This is a low-level routine that doesn't maintain consistency
923 * of all the board data structures. */
925 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
927 enum stone color
= board_at(board
, c
);
928 board_at(board
, c
) = S_NONE
;
929 group_at(board
, c
) = 0;
930 board_hash_update(board
, c
, color
);
932 /* We mark as cannot-capture now. If this is a ko/snapback,
933 * we will get incremented later in board_group_addlib(). */
934 trait_at(board
, c
, S_BLACK
).cap
= 0;
935 trait_at(board
, c
, S_WHITE
).cap
= 0;
936 board_trait_queue(board
, c
);
939 /* Increase liberties of surrounding groups */
941 foreach_neighbor(board
, coord
, {
942 dec_neighbor_count_at(board
, c
, color
);
943 board_trait_queue(board
, c
);
944 group_t g
= group_at(board
, c
);
946 board_group_addlib(board
, g
, coord
);
950 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
951 board
->f
[board
->flen
++] = c
;
954 static int profiling_noinline
955 board_group_capture(struct board
*board
, group_t group
)
959 foreach_in_group(board
, group
) {
960 board
->captures
[stone_other(board_at(board
, c
))]++;
961 board_remove_stone(board
, group
, c
);
963 } foreach_in_group_end
;
965 struct group
*gi
= &board_group_info(board
, group
);
967 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
968 else if (gi
->libs
== 1)
969 board_capturable_rm(board
, group
, gi
->lib
[0]);
970 memset(gi
, 0, sizeof(*gi
));
976 static void profiling_noinline
977 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
979 group_at(board
, coord
) = group
;
980 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
981 groupnext_at(board
, prevstone
) = coord
;
984 if (board_group_info(board
, group
).libs
== 1) {
985 /* Our group is temporarily in atari; make sure the capturable
986 * counts also correspond to the newly added stone before we
987 * start adding liberties again so bump-dump ops match. */
988 enum stone capturing_color
= stone_other(board_at(board
, group
));
989 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
990 coord_t lib
= board_group_info(board
, group
).lib
[0];
991 if (coord_is_adjecent(lib
, coord
, board
)) {
992 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
);
993 trait_at(board
, lib
, capturing_color
).cap
++;
994 board_trait_queue(board
, lib
);
999 foreach_neighbor(board
, coord
, {
1000 if (board_at(board
, c
) == S_NONE
)
1001 board_group_addlib(board
, group
, c
);
1005 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
1006 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
1007 coord_x(coord
, board
), coord_y(coord
, board
),
1008 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
1012 static void profiling_noinline
1013 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
1016 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
1017 group_base(group_from
), group_base(group_to
));
1018 struct group
*gi_from
= &board_group_info(board
, group_from
);
1019 struct group
*gi_to
= &board_group_info(board
, group_to
);
1021 /* We do this early before the group info is rewritten. */
1022 if (gi_from
->libs
== 2)
1023 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
1024 else if (gi_from
->libs
== 1)
1025 board_capturable_rm(board
, group_from
, gi_from
->lib
[0]);
1028 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
1030 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
1031 for (int i
= 0; i
< gi_from
->libs
; i
++) {
1032 for (int j
= 0; j
< gi_to
->libs
; j
++)
1033 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
1035 if (gi_to
->libs
== 0) {
1036 board_capturable_add(board
, group_to
, gi_from
->lib
[i
]);
1037 } else if (gi_to
->libs
== 1) {
1038 board_capturable_rm(board
, group_to
, gi_to
->lib
[0]);
1039 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
1040 } else if (gi_to
->libs
== 2) {
1041 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
1043 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
1044 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
1051 if (board_group_info(board
, group_to
).libs
== 1) {
1052 /* Our group is currently in atari; make sure we properly
1053 * count in even the neighbors from the other group in the
1054 * capturable counter. */
1055 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
1056 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1057 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
1058 foreach_neighbor(board
, lib
, {
1059 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
);
1060 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
1062 board_trait_queue(board
, lib
);
1066 coord_t last_in_group
;
1067 foreach_in_group(board
, group_from
) {
1069 group_at(board
, c
) = group_to
;
1070 } foreach_in_group_end
;
1071 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1072 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1073 memset(gi_from
, 0, sizeof(struct group
));
1076 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1077 group_base(group_to
));
1080 static group_t profiling_noinline
1081 new_group(struct board
*board
, coord_t coord
)
1083 group_t group
= coord
;
1084 struct group
*gi
= &board_group_info(board
, group
);
1085 foreach_neighbor(board
, coord
, {
1086 if (board_at(board
, c
) == S_NONE
)
1087 /* board_group_addlib is ridiculously expensive for us */
1088 #if GROUP_KEEP_LIBS < 4
1089 if (gi
->libs
< GROUP_KEEP_LIBS
)
1091 gi
->lib
[gi
->libs
++] = c
;
1094 group_at(board
, coord
) = group
;
1095 groupnext_at(board
, coord
) = 0;
1098 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1099 else if (gi
->libs
== 1)
1100 board_capturable_add(board
, group
, gi
->lib
[0]);
1101 check_libs_consistency(board
, group
);
1104 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1105 coord_x(coord
, board
), coord_y(coord
, board
),
1111 static inline group_t
1112 play_one_neighbor(struct board
*board
,
1113 coord_t coord
, enum stone color
, enum stone other_color
,
1114 coord_t c
, group_t group
)
1116 enum stone ncolor
= board_at(board
, c
);
1117 group_t ngroup
= group_at(board
, c
);
1119 inc_neighbor_count_at(board
, c
, color
);
1120 /* We can be S_NONE, in that case we need to update the safety
1121 * trait since we might be left with only one liberty. */
1122 board_trait_queue(board
, c
);
1127 board_group_rmlib(board
, ngroup
, coord
);
1129 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1130 group_base(ngroup
), ncolor
, color
, other_color
);
1132 if (ncolor
== color
&& ngroup
!= group
) {
1135 add_to_group(board
, group
, c
, coord
);
1137 merge_groups(board
, group
, ngroup
);
1139 } else if (ncolor
== other_color
) {
1141 struct group
*gi
= &board_group_info(board
, ngroup
);
1142 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1143 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1144 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1145 fprintf(stderr
, "\n");
1147 if (unlikely(board_group_captured(board
, ngroup
)))
1148 board_group_capture(board
, ngroup
);
1153 /* We played on a place with at least one liberty. We will become a member of
1154 * some group for sure. */
1155 static group_t profiling_noinline
1156 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1158 coord_t coord
= m
->coord
;
1159 enum stone color
= m
->color
;
1160 enum stone other_color
= stone_other(color
);
1163 board
->f
[f
] = board
->f
[--board
->flen
];
1165 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1167 #if defined(BOARD_TRAITS) && defined(DEBUG)
1168 /* Sanity check that cap matches reality. */
1171 foreach_neighbor(board
, coord
, {
1172 group_t g
= group_at(board
, c
);
1173 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1175 assert(a
== trait_at(board
, coord
, color
).cap
);
1176 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1179 foreach_neighbor(board
, coord
, {
1180 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1183 board_at(board
, coord
) = color
;
1184 if (unlikely(!group
))
1185 group
= new_group(board
, coord
);
1186 board_gamma_update(board
, coord
, S_BLACK
);
1187 board_gamma_update(board
, coord
, S_WHITE
);
1189 board
->last_move2
= board
->last_move
;
1190 board
->last_move
= *m
;
1192 board_hash_update(board
, coord
, color
);
1193 board_symmetry_update(board
, &board
->symmetry
, coord
);
1194 struct move ko
= { pass
, S_NONE
};
1200 /* We played in an eye-like shape. Either we capture at least one of the eye
1201 * sides in the process of playing, or return -1. */
1202 static int profiling_noinline
1203 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1205 coord_t coord
= m
->coord
;
1206 enum stone color
= m
->color
;
1207 /* Check ko: Capture at a position of ko capture one move ago */
1208 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1210 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1212 } else if (DEBUGL(6)) {
1213 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1214 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1215 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1218 struct move ko
= { pass
, S_NONE
};
1220 int captured_groups
= 0;
1222 foreach_neighbor(board
, coord
, {
1223 group_t g
= group_at(board
, c
);
1225 fprintf(stderr
, "board_check: group %d has %d libs\n",
1226 g
, board_group_info(board
, g
).libs
);
1227 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1230 if (likely(captured_groups
== 0)) {
1233 board_print(board
, stderr
);
1234 fprintf(stderr
, "board_check: one-stone suicide\n");
1240 /* We _will_ for sure capture something. */
1241 assert(trait_at(board
, coord
, color
).cap
> 0);
1242 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1245 board
->f
[f
] = board
->f
[--board
->flen
];
1247 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1249 foreach_neighbor(board
, coord
, {
1250 inc_neighbor_count_at(board
, c
, color
);
1251 /* Originally, this could not have changed any trait
1252 * since no neighbors were S_NONE, however by now some
1253 * of them might be removed from the board. */
1254 board_trait_queue(board
, c
);
1256 group_t group
= group_at(board
, c
);
1260 board_group_rmlib(board
, group
, coord
);
1262 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1265 if (board_group_captured(board
, group
)) {
1266 if (board_group_capture(board
, group
) == 1) {
1267 /* If we captured multiple groups at once,
1268 * we can't be fighting ko so we don't need
1269 * to check for that. */
1270 ko
.color
= stone_other(color
);
1272 board
->last_ko
= ko
;
1273 board
->last_ko_age
= board
->moves
;
1275 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1280 board_at(board
, coord
) = color
;
1281 group_t group
= new_group(board
, coord
);
1282 board_gamma_update(board
, coord
, S_BLACK
);
1283 board_gamma_update(board
, coord
, S_WHITE
);
1285 board
->last_move2
= board
->last_move
;
1286 board
->last_move
= *m
;
1288 board_hash_update(board
, coord
, color
);
1289 board_hash_commit(board
);
1290 board_traits_recompute(board
);
1291 board_symmetry_update(board
, &board
->symmetry
, coord
);
1297 static int __attribute__((flatten
))
1298 board_play_f(struct board
*board
, struct move
*m
, int f
)
1301 fprintf(stderr
, "board_play(): ---- Playing %d,%d\n", coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1303 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1304 /* NOT playing in an eye. Thus this move has to succeed. (This
1305 * is thanks to New Zealand rules. Otherwise, multi-stone
1306 * suicide might fail.) */
1307 group_t group
= board_play_outside(board
, m
, f
);
1308 if (unlikely(board_group_captured(board
, group
))) {
1309 board_group_capture(board
, group
);
1311 board_hash_commit(board
);
1312 board_traits_recompute(board
);
1315 return board_play_in_eye(board
, m
, f
);
1320 board_play(struct board
*board
, struct move
*m
)
1322 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1323 struct move nomove
= { pass
, S_NONE
};
1325 board
->last_move2
= board
->last_move
;
1326 board
->last_move
= *m
;
1331 for (f
= 0; f
< board
->flen
; f
++)
1332 if (board
->f
[f
] == m
->coord
)
1333 return board_play_f(board
, m
, f
);
1336 fprintf(stderr
, "board_check: stone exists\n");
1342 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1345 struct move m
= { *coord
, color
};
1347 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
1348 return (likely(!board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1349 && board_is_valid_move(b
, &m
)
1350 && (!permit
|| permit(permit_data
, b
, &m
))
1351 && likely(board_play_f(b
, &m
, f
) >= 0));
1355 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1357 if (unlikely(b
->flen
== 0))
1360 int base
= fast_random(b
->flen
), f
;
1361 for (f
= base
; f
< b
->flen
; f
++)
1362 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1364 for (f
= 0; f
< base
; f
++)
1365 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1370 struct move m
= { pass
, color
};
1376 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1378 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1380 /* XXX: We attempt false eye detection but we will yield false
1381 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1383 foreach_diag_neighbor(board
, coord
) {
1384 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1385 } foreach_diag_neighbor_end
;
1386 /* For false eye, we need two enemy stones diagonally in the
1387 * middle of the board, or just one enemy stone at the edge
1388 * or in the corner. */
1389 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1390 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1394 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1396 return board_is_eyelike(board
, coord
, eye_color
)
1397 && !board_is_false_eyelike(board
, coord
, eye_color
);
1401 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1403 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1405 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1413 board_fast_score(struct board
*board
)
1416 memset(scores
, 0, sizeof(scores
));
1418 foreach_point(board
) {
1419 enum stone color
= board_at(board
, c
);
1420 if (color
== S_NONE
)
1421 color
= board_get_one_point_eye(board
, c
);
1423 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1424 } foreach_point_end
;
1426 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1429 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1431 /* One flood-fill iteration; returns true if next iteration
1434 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1436 bool needs_update
= false;
1437 foreach_point(board
) {
1438 /* Ignore occupied and already-dame positions. */
1439 if (board_at(board
, c
) != S_NONE
|| ownermap
[c
] == 3)
1441 /* Count neighbors. */
1443 foreach_neighbor(board
, c
, {
1446 /* If we have neighbors of both colors, or dame,
1447 * we are dame too. */
1448 if ((nei
[1] && nei
[2]) || nei
[3]) {
1450 /* Speed up the propagation. */
1451 foreach_neighbor(board
, c
, {
1452 if (board_at(board
, c
) == S_NONE
)
1455 needs_update
= true;
1458 /* If we have neighbors of one color, we are owned
1459 * by that color, too. */
1460 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1461 int newowner
= nei
[1] ? 1 : 2;
1462 ownermap
[c
] = newowner
;
1463 /* Speed up the propagation. */
1464 foreach_neighbor(board
, c
, {
1465 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1466 ownermap
[c
] = newowner
;
1468 needs_update
= true;
1471 } foreach_point_end
;
1472 return needs_update
;
1475 /* Tromp-Taylor Counting */
1477 board_official_score(struct board
*board
, struct move_queue
*q
)
1480 /* A point P, not colored C, is said to reach C, if there is a path of
1481 * (vertically or horizontally) adjacent points of P's color from P to
1482 * a point of color C.
1484 * A player's score is the number of points of her color, plus the
1485 * number of empty points that reach only her color. */
1487 int ownermap
[board_size2(board
)];
1489 const int o
[4] = {0, 1, 2, 0};
1490 foreach_point(board
) {
1491 ownermap
[c
] = o
[board_at(board
, c
)];
1492 s
[board_at(board
, c
)]++;
1493 } foreach_point_end
;
1496 /* Process dead groups. */
1497 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
1498 foreach_in_group(board
, q
->move
[i
]) {
1499 enum stone color
= board_at(board
, c
);
1500 ownermap
[c
] = o
[stone_other(color
)];
1501 s
[color
]--; s
[stone_other(color
)]++;
1502 } foreach_in_group_end
;
1506 /* We need to special-case empty board. */
1507 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1508 return board
->komi
+ board
->handicap
;
1510 while (board_tromp_taylor_iter(board
, ownermap
))
1511 /* Flood-fill... */;
1514 memset(scores
, 0, sizeof(scores
));
1516 foreach_point(board
) {
1517 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1518 if (ownermap
[c
] == 3)
1520 scores
[ownermap
[c
]]++;
1521 } foreach_point_end
;
1523 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];