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 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
171 board
->size2
= board_size(board
) * board_size(board
);
174 while ((1 << board
->bits2
) < board
->size2
) board
->bits2
++;
179 size_t asize
= board_alloc(board
);
180 memset(board
->b
, 0, asize
);
184 board_clear(struct board
*board
)
186 int size
= board_size(board
);
187 float komi
= board
->komi
;
189 board_done_noalloc(board
);
191 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
195 /* Setup neighborhood iterators */
196 board
->nei8
[0] = -size
- 1; // (-1,-1)
199 board
->nei8
[3] = size
- 2; // (-1,0)
201 board
->nei8
[5] = size
- 2; // (-1,1)
204 board
->dnei
[0] = -size
- 1;
206 board
->dnei
[2] = size
*2 - 2;
209 /* Setup initial symmetry */
210 board
->symmetry
.d
= 1;
211 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
212 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
213 board
->symmetry
.type
= SYM_FULL
;
215 /* Set up coordinate cache */
216 foreach_point(board
) {
217 board
->coord
[c
][0] = c
% board_size(board
);
218 board
->coord
[c
][1] = c
/ board_size(board
);
221 /* Draw the offboard margin */
222 int top_row
= board_size2(board
) - board_size(board
);
224 for (i
= 0; i
< board_size(board
); i
++)
225 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
226 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
227 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
229 foreach_point(board
) {
231 if (board_at(board
, coord
) == S_OFFBOARD
)
233 foreach_neighbor(board
, c
, {
234 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
238 /* All positions are free! Except the margin. */
239 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
240 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
241 board
->f
[board
->flen
++] = i
;
243 /* Initialize zobrist hashtable. */
244 foreach_point(board
) {
245 int max
= (sizeof(hash_t
) << history_hash_bits
);
246 /* fast_random() is 16-bit only */
247 board
->h
[c
* 2] = ((hash_t
) fast_random(max
))
248 | ((hash_t
) fast_random(max
) << 16)
249 | ((hash_t
) fast_random(max
) << 32)
250 | ((hash_t
) fast_random(max
) << 48);
251 if (!board
->h
[c
* 2])
252 /* Would be kinda "oops". */
254 /* And once again for white */
255 board
->h
[c
* 2 + 1] = ((hash_t
) fast_random(max
))
256 | ((hash_t
) fast_random(max
) << 16)
257 | ((hash_t
) fast_random(max
) << 32)
258 | ((hash_t
) fast_random(max
) << 48);
259 if (!board
->h
[c
* 2 + 1])
260 board
->h
[c
* 2 + 1] = 1;
263 #ifdef BOARD_SPATHASH
264 /* Initialize spatial hashes. */
265 foreach_point(board
) {
266 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
267 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
268 ptcoords_at(x
, y
, c
, board
, j
);
269 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
270 pthashes
[0][j
][board_at(board
, c
)];
271 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
272 pthashes
[0][j
][stone_other(board_at(board
, c
))];
278 /* Initialize 3x3 pattern codes. */
279 foreach_point(board
) {
280 if (board_at(board
, c
) == S_NONE
)
281 board
->pat3
[c
] = pattern3_hash(board
, c
);
285 /* Initialize traits. */
286 foreach_point(board
) {
287 trait_at(board
, c
, S_BLACK
).cap
= 0;
288 trait_at(board
, c
, S_BLACK
).cap1
= 0;
289 trait_at(board
, c
, S_BLACK
).safe
= true;
290 trait_at(board
, c
, S_WHITE
).cap
= 0;
291 trait_at(board
, c
, S_WHITE
).cap1
= 0;
292 trait_at(board
, c
, S_WHITE
).safe
= true;
296 board
->prob
[0].b
= board
->prob
[1].b
= board
;
297 foreach_point(board
) {
298 probdist_set(&board
->prob
[0], c
, double_to_fixp((board_at(board
, c
) == S_NONE
) * 1.0f
));
299 probdist_set(&board
->prob
[1], c
, double_to_fixp((board_at(board
, c
) == S_NONE
) * 1.0f
));
305 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
307 for (int i
= 0; i
< c
; i
++) {
308 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
309 s
+= snprintf(s
, end
- s
, " ");
310 for (int x
= 1; x
< board_size(board
) - 1; x
++)
311 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
312 s
+= snprintf(s
, end
-s
, " ");
314 s
+= snprintf(s
, end
- s
, "\n");
315 for (int i
= 0; i
< c
; i
++) {
316 s
+= snprintf(s
, end
- s
, " +-");
317 for (int x
= 1; x
< board_size(board
) - 1; x
++)
318 s
+= snprintf(s
, end
- s
, "--");
319 s
+= snprintf(s
, end
- s
, "+");
321 s
+= snprintf(s
, end
- s
, "\n");
326 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
328 for (int i
= 0; i
< c
; i
++) {
329 s
+= snprintf(s
, end
- s
, " +-");
330 for (int x
= 1; x
< board_size(board
) - 1; x
++)
331 s
+= snprintf(s
, end
- s
, "--");
332 s
+= snprintf(s
, end
- s
, "+");
334 s
+= snprintf(s
, end
- s
, "\n");
339 board_print_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
)
341 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
342 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
343 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
344 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
346 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
348 s
+= snprintf(s
, end
- s
, "|");
350 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
351 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
352 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
);
354 s
+= snprintf(s
, end
- s
, "|");
356 s
+= snprintf(s
, end
- s
, "\n");
361 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
365 char *end
= buf
+ sizeof(buf
);
366 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
367 board
->moves
, board
->komi
, board
->handicap
,
368 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
369 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
370 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
371 s
= board_print_row(board
, y
, s
, end
, cprint
);
372 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
373 fprintf(f
, "%s\n", buf
);
377 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
)
379 s
+= snprintf(s
, end
- s
, "%d ", group_base(group_at(board
, c
)));
384 board_print(struct board
*board
, FILE *f
)
386 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
390 board_gamma_set(struct board
*b
, struct features_gamma
*gamma
, bool precise_selfatari
)
394 b
->precise_selfatari
= precise_selfatari
;
395 for (int i
= 0; i
< b
->flen
; i
++) {
396 board_trait_recompute(b
, b
->f
[i
]);
402 /* Update the probability distribution we maintain incrementally. */
404 board_gamma_update(struct board
*board
, coord_t coord
, enum stone color
)
410 /* Punch out invalid moves and moves filling our own eyes. */
411 if (board_at(board
, coord
) != S_NONE
412 || (board_is_eyelike(board
, coord
, stone_other(color
))
413 && !trait_at(board
, coord
, color
).cap
)
414 || (board_is_one_point_eye(board
, coord
, color
))) {
415 probdist_set(&board
->prob
[color
- 1], coord
, 0);
419 hash3_t pat
= board
->pat3
[coord
];
420 if (color
== S_WHITE
) {
421 /* We work with the pattern3s as black-to-play. */
422 pat
= pattern3_reverse(pat
);
425 /* We just quickly replicate the general pattern matcher stuff
426 * here in the most bare-bone way. */
427 double value
= board
->gamma
->gamma
[FEAT_PATTERN3
][pat
];
428 if (trait_at(board
, coord
, color
).cap
) {
430 i
|= (trait_at(board
, coord
, color
).cap1
== trait_at(board
, coord
, color
).cap
) << PF_CAPTURE_1STONE
;
431 i
|= (!trait_at(board
, coord
, color
).safe
) << PF_CAPTURE_TRAPPED
;
432 value
*= board
->gamma
->gamma
[FEAT_CAPTURE
][i
];
434 if (trait_at(board
, coord
, stone_other(color
)).cap
435 && trait_at(board
, coord
, color
).safe
) {
437 i
|= (trait_at(board
, coord
, stone_other(color
)).cap1
== trait_at(board
, coord
, stone_other(color
)).cap
) << PF_AESCAPE_1STONE
;
438 i
|= (!trait_at(board
, coord
, stone_other(color
)).safe
) << PF_AESCAPE_TRAPPED
;
439 value
*= board
->gamma
->gamma
[FEAT_AESCAPE
][i
];
441 if (!trait_at(board
, coord
, color
).safe
)
442 value
*= board
->gamma
->gamma
[FEAT_SELFATARI
][1 + board
->precise_selfatari
];
443 probdist_set(&board
->prob
[color
- 1], coord
, double_to_fixp(value
));
449 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
451 if (board
->precise_selfatari
)
452 return !is_bad_selfatari(board
, color
, coord
);
454 return board_safe_to_play(board
, coord
, color
);
458 board_trait_recompute(struct board
*board
, coord_t coord
)
460 trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);;
461 trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
463 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
464 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
465 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).cap1
, trait_at(board
, coord
, S_BLACK
).safe
,
466 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).cap1
, trait_at(board
, coord
, S_WHITE
).safe
);
468 board_gamma_update(board
, coord
, S_BLACK
);
469 board_gamma_update(board
, coord
, S_WHITE
);
473 /* Recompute traits for dirty points that we have previously touched
474 * somehow (libs of their neighbors changed or so). */
476 board_traits_recompute(struct board
*board
)
479 for (int i
= 0; i
< board
->tqlen
; i
++) {
480 coord_t coord
= board
->tq
[i
];
481 trait_at(board
, coord
, S_BLACK
).dirty
= false;
482 if (board_at(board
, coord
) != S_NONE
)
484 board_trait_recompute(board
, coord
);
490 /* Queue traits of given point for recomputing. */
492 board_trait_queue(struct board
*board
, coord_t coord
)
495 if (trait_at(board
, coord
, S_BLACK
).dirty
)
497 board
->tq
[board
->tqlen
++] = coord
;
498 trait_at(board
, coord
, S_BLACK
).dirty
= true;
503 /* Update board hash with given coordinate. */
504 static void profiling_noinline
505 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
507 board
->hash
^= hash_at(board
, coord
, color
);
509 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
);
511 #ifdef BOARD_SPATHASH
512 /* Gridcular metric is reflective, so we update all hashes
513 * of appropriate ditance in OUR circle. */
514 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
515 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
516 ptcoords_at(x
, y
, coord
, board
, j
);
517 /* We either changed from S_NONE to color
518 * or vice versa; doesn't matter. */
519 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
520 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
521 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
522 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
527 #if defined(BOARD_PAT3)
528 /* @color is not what we need in case of capture. */
529 enum stone new_color
= board_at(board
, coord
);
530 if (new_color
== S_NONE
)
531 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
532 foreach_8neighbor(board
, coord
) { // internally, the loop uses fn__i=[0..7]
533 if (board_at(board
, c
) != S_NONE
)
535 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
536 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
538 if (board_at(board
, c
) != S_OFFBOARD
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
539 board_print(board
, stderr
);
540 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
);
544 board_trait_queue(board
, c
);
545 } foreach_8neighbor_end
;
549 /* Commit current board hash to history. */
550 static void profiling_noinline
551 board_hash_commit(struct board
*board
)
554 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
555 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
556 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
558 hash_t i
= board
->hash
;
559 while (board
->history_hash
[i
& history_hash_mask
]) {
560 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
562 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
563 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
564 board
->superko_violation
= true;
567 i
= history_hash_next(i
);
569 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
575 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
577 if (likely(symmetry
->type
== SYM_NONE
)) {
578 /* Fully degenerated already. We do not support detection
579 * of restoring of symmetry, assuming that this is too rare
580 * a case to handle. */
584 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
585 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
587 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
588 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
589 symmetry
->d
, symmetry
->type
, x
, y
);
592 switch (symmetry
->type
) {
594 if (x
== t
&& y
== t
) {
595 /* Tengen keeps full symmetry. */
598 /* New symmetry now? */
600 symmetry
->type
= SYM_DIAG_UP
;
601 symmetry
->x1
= symmetry
->y1
= 1;
602 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
604 } else if (dx
== y
) {
605 symmetry
->type
= SYM_DIAG_DOWN
;
606 symmetry
->x1
= symmetry
->y1
= 1;
607 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
610 symmetry
->type
= SYM_HORIZ
;
612 symmetry
->y2
= board_size(b
) - 1;
615 symmetry
->type
= SYM_VERT
;
617 symmetry
->x2
= board_size(b
) - 1;
621 symmetry
->type
= SYM_NONE
;
622 symmetry
->x1
= symmetry
->y1
= 1;
623 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
649 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
650 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
651 symmetry
->d
, symmetry
->type
);
658 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
661 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
663 board_play(board
, &m
);
664 /* Simulate white passing; otherwise, UCT search can get confused since
665 * tree depth parity won't match the color to move. */
668 char *str
= coord2str(m
.coord
, board
);
670 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
671 if (f
) fprintf(f
, "%s ", str
);
676 board_handicap(struct board
*board
, int stones
, FILE *f
)
678 int margin
= 3 + (board_size(board
) >= 13);
680 int mid
= board_size(board
) / 2;
681 int max
= board_size(board
) - 1 - margin
;
682 const int places
[][2] = {
683 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
684 { min
, mid
}, { max
, mid
},
685 { mid
, min
}, { mid
, max
},
689 board
->handicap
= stones
;
691 if (stones
== 5 || stones
== 7) {
692 board_handicap_stone(board
, mid
, mid
, f
);
697 for (i
= 0; i
< stones
; i
++)
698 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
702 static void __attribute__((noinline
))
703 check_libs_consistency(struct board
*board
, group_t g
)
707 struct group
*gi
= &board_group_info(board
, g
);
708 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
709 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
710 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
717 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
719 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
721 /* Increase capturable count trait of my last lib. */
722 enum stone capturing_color
= stone_other(board_at(board
, group
));
723 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
724 foreach_neighbor(board
, lib
, {
725 if (DEBUGL(8) && group_at(board
, c
) == group
)
726 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
);
727 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
728 trait_at(board
, lib
, capturing_color
).cap1
+= (group_at(board
, c
) == group
&& onestone
);
730 board_trait_queue(board
, lib
);
734 /* Update the list of capturable groups. */
736 assert(board
->clen
< board_size2(board
));
737 board
->c
[board
->clen
++] = group
;
741 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
743 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
745 /* Decrease capturable count trait of my previously-last lib. */
746 enum stone capturing_color
= stone_other(board_at(board
, group
));
747 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
748 foreach_neighbor(board
, lib
, {
749 if (DEBUGL(8) && group_at(board
, c
) == group
)
750 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
);
751 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
752 trait_at(board
, lib
, capturing_color
).cap1
-= (group_at(board
, c
) == group
&& onestone
);
754 board_trait_queue(board
, lib
);
758 /* Update the list of capturable groups. */
759 for (int i
= 0; i
< board
->clen
; i
++) {
760 if (unlikely(board
->c
[i
] == group
)) {
761 board
->c
[i
] = board
->c
[--board
->clen
];
765 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
771 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
774 board_trait_queue(board
, lib1
);
775 board_trait_queue(board
, lib2
);
779 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
782 board_trait_queue(board
, lib1
);
783 board_trait_queue(board
, lib2
);
788 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
791 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
792 group_base(group
), coord2sstr(group_base(group
), board
),
793 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
796 check_libs_consistency(board
, group
);
798 struct group
*gi
= &board_group_info(board
, group
);
799 bool onestone
= group_is_onestone(board
, group
);
800 if (gi
->libs
< GROUP_KEEP_LIBS
) {
801 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
803 /* Seems extra branch just slows it down */
807 if (unlikely(gi
->lib
[i
] == coord
))
811 board_capturable_add(board
, group
, coord
, onestone
);
812 } else if (gi
->libs
== 1) {
813 board_capturable_rm(board
, group
, gi
->lib
[0], onestone
);
814 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
815 } else if (gi
->libs
== 2) {
816 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
818 gi
->lib
[gi
->libs
++] = coord
;
821 check_libs_consistency(board
, group
);
825 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
827 /* Add extra liberty from the board to our liberty list. */
828 unsigned char watermark
[board_size2(board
) / 8];
829 memset(watermark
, 0, sizeof(watermark
));
830 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
831 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
833 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
834 watermark_set(gi
->lib
[i
]);
835 watermark_set(avoid
);
837 foreach_in_group(board
, group
) {
839 foreach_neighbor(board
, coord2
, {
840 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
843 gi
->lib
[gi
->libs
++] = c
;
844 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
847 } foreach_in_group_end
;
853 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
856 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
857 group_base(group
), coord2sstr(group_base(group
), board
),
858 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
861 struct group
*gi
= &board_group_info(board
, group
);
862 bool onestone
= group_is_onestone(board
, group
);
863 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
865 /* Seems extra branch just slows it down */
869 if (likely(gi
->lib
[i
] != coord
))
872 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
873 gi
->lib
[gi
->libs
] = 0;
875 check_libs_consistency(board
, group
);
877 /* Postpone refilling lib[] until we need to. */
878 assert(GROUP_REFILL_LIBS
> 1);
879 if (gi
->libs
> GROUP_REFILL_LIBS
)
881 if (gi
->libs
== GROUP_REFILL_LIBS
)
882 board_group_find_extra_libs(board
, group
, gi
, coord
);
885 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
886 } else if (gi
->libs
== 1) {
887 board_capturable_add(board
, group
, gi
->lib
[0], onestone
);
888 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
889 } else if (gi
->libs
== 0)
890 board_capturable_rm(board
, group
, lib
, onestone
);
894 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
895 * can call this multiple times per coord. */
896 check_libs_consistency(board
, group
);
901 /* This is a low-level routine that doesn't maintain consistency
902 * of all the board data structures. */
904 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
906 enum stone color
= board_at(board
, c
);
907 board_at(board
, c
) = S_NONE
;
908 group_at(board
, c
) = 0;
909 board_hash_update(board
, c
, color
);
911 /* We mark as cannot-capture now. If this is a ko/snapback,
912 * we will get incremented later in board_group_addlib(). */
913 trait_at(board
, c
, S_BLACK
).cap
= trait_at(board
, c
, S_BLACK
).cap1
= 0;
914 trait_at(board
, c
, S_WHITE
).cap
= trait_at(board
, c
, S_WHITE
).cap1
= 0;
915 board_trait_queue(board
, c
);
918 /* Increase liberties of surrounding groups */
920 foreach_neighbor(board
, coord
, {
921 dec_neighbor_count_at(board
, c
, color
);
922 board_trait_queue(board
, c
);
923 group_t g
= group_at(board
, c
);
925 board_group_addlib(board
, g
, coord
);
929 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
930 board
->f
[board
->flen
++] = c
;
933 static int profiling_noinline
934 board_group_capture(struct board
*board
, group_t group
)
938 foreach_in_group(board
, group
) {
939 board
->captures
[stone_other(board_at(board
, c
))]++;
940 board_remove_stone(board
, group
, c
);
942 } foreach_in_group_end
;
944 struct group
*gi
= &board_group_info(board
, group
);
945 assert(gi
->libs
== 0);
946 memset(gi
, 0, sizeof(*gi
));
952 static void profiling_noinline
953 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
956 struct group
*gi
= &board_group_info(board
, group
);
957 bool onestone
= group_is_onestone(board
, group
);
960 /* Our group is temporarily in atari; make sure the capturable
961 * counts also correspond to the newly added stone before we
962 * start adding liberties again so bump-dump ops match. */
963 enum stone capturing_color
= stone_other(board_at(board
, group
));
964 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
966 coord_t lib
= board_group_info(board
, group
).lib
[0];
967 if (coord_is_adjecent(lib
, coord
, board
)) {
969 fprintf(stderr
, "add_to_group %s: %s[%d] bump\n", coord2sstr(group
, board
), coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
970 trait_at(board
, lib
, capturing_color
).cap
++;
971 /* This is never a 1-stone group, obviously. */
972 board_trait_queue(board
, lib
);
976 /* We are not 1-stone group anymore, update the cap1
977 * counter specifically. */
978 foreach_neighbor(board
, group
, {
979 if (board_at(board
, c
) != S_NONE
) continue;
980 trait_at(board
, c
, capturing_color
).cap1
--;
981 board_trait_queue(board
, c
);
987 group_at(board
, coord
) = group
;
988 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
989 groupnext_at(board
, prevstone
) = coord
;
991 foreach_neighbor(board
, coord
, {
992 if (board_at(board
, c
) == S_NONE
)
993 board_group_addlib(board
, group
, c
);
997 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
998 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
999 coord_x(coord
, board
), coord_y(coord
, board
),
1000 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
1004 static void profiling_noinline
1005 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
1008 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
1009 group_base(group_from
), group_base(group_to
));
1010 struct group
*gi_from
= &board_group_info(board
, group_from
);
1011 struct group
*gi_to
= &board_group_info(board
, group_to
);
1012 bool onestone_from
= group_is_onestone(board
, group_from
);
1013 bool onestone_to
= group_is_onestone(board
, group_to
);
1015 /* We do this early before the group info is rewritten. */
1016 if (gi_from
->libs
== 2)
1017 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
1018 else if (gi_from
->libs
== 1)
1019 board_capturable_rm(board
, group_from
, gi_from
->lib
[0], onestone_from
);
1022 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
1024 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
1025 for (int i
= 0; i
< gi_from
->libs
; i
++) {
1026 for (int j
= 0; j
< gi_to
->libs
; j
++)
1027 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
1029 if (gi_to
->libs
== 0) {
1030 board_capturable_add(board
, group_to
, gi_from
->lib
[i
], onestone_to
);
1031 } else if (gi_to
->libs
== 1) {
1032 board_capturable_rm(board
, group_to
, gi_to
->lib
[0], onestone_to
);
1033 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
1034 } else if (gi_to
->libs
== 2) {
1035 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
1037 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
1038 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
1045 if (gi_to
->libs
== 1) {
1046 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
1047 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1049 /* Our group is currently in atari; make sure we properly
1050 * count in even the neighbors from the other group in the
1051 * capturable counter. */
1052 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
1053 foreach_neighbor(board
, lib
, {
1054 if (DEBUGL(8) && group_at(board
, c
) == group_from
)
1055 fprintf(stderr
, "%s[%d] cap bump\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
1056 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
1057 /* This is never a 1-stone group, obviously. */
1059 board_trait_queue(board
, lib
);
1062 /* We are not 1-stone group anymore, update the cap1
1063 * counter specifically. */
1064 foreach_neighbor(board
, group_to
, {
1065 if (board_at(board
, c
) != S_NONE
) continue;
1066 trait_at(board
, c
, capturing_color
).cap1
--;
1067 board_trait_queue(board
, c
);
1073 coord_t last_in_group
;
1074 foreach_in_group(board
, group_from
) {
1076 group_at(board
, c
) = group_to
;
1077 } foreach_in_group_end
;
1078 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1079 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1080 memset(gi_from
, 0, sizeof(struct group
));
1083 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1084 group_base(group_to
));
1087 static group_t profiling_noinline
1088 new_group(struct board
*board
, coord_t coord
)
1090 group_t group
= coord
;
1091 struct group
*gi
= &board_group_info(board
, group
);
1092 foreach_neighbor(board
, coord
, {
1093 if (board_at(board
, c
) == S_NONE
)
1094 /* board_group_addlib is ridiculously expensive for us */
1095 #if GROUP_KEEP_LIBS < 4
1096 if (gi
->libs
< GROUP_KEEP_LIBS
)
1098 gi
->lib
[gi
->libs
++] = c
;
1101 group_at(board
, coord
) = group
;
1102 groupnext_at(board
, coord
) = 0;
1105 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1106 else if (gi
->libs
== 1)
1107 board_capturable_add(board
, group
, gi
->lib
[0], true);
1108 check_libs_consistency(board
, group
);
1111 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1112 coord_x(coord
, board
), coord_y(coord
, board
),
1118 static inline group_t
1119 play_one_neighbor(struct board
*board
,
1120 coord_t coord
, enum stone color
, enum stone other_color
,
1121 coord_t c
, group_t group
)
1123 enum stone ncolor
= board_at(board
, c
);
1124 group_t ngroup
= group_at(board
, c
);
1126 inc_neighbor_count_at(board
, c
, color
);
1127 /* We can be S_NONE, in that case we need to update the safety
1128 * trait since we might be left with only one liberty. */
1129 board_trait_queue(board
, c
);
1134 board_group_rmlib(board
, ngroup
, coord
);
1136 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1137 group_base(ngroup
), ncolor
, color
, other_color
);
1139 if (ncolor
== color
&& ngroup
!= group
) {
1142 add_to_group(board
, group
, c
, coord
);
1144 merge_groups(board
, group
, ngroup
);
1146 } else if (ncolor
== other_color
) {
1148 struct group
*gi
= &board_group_info(board
, ngroup
);
1149 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1150 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1151 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1152 fprintf(stderr
, "\n");
1154 if (unlikely(board_group_captured(board
, ngroup
)))
1155 board_group_capture(board
, ngroup
);
1160 /* We played on a place with at least one liberty. We will become a member of
1161 * some group for sure. */
1162 static group_t profiling_noinline
1163 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1165 coord_t coord
= m
->coord
;
1166 enum stone color
= m
->color
;
1167 enum stone other_color
= stone_other(color
);
1170 board
->f
[f
] = board
->f
[--board
->flen
];
1172 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1174 #if defined(BOARD_TRAITS) && defined(DEBUG)
1175 /* Sanity check that cap matches reality. */
1178 foreach_neighbor(board
, coord
, {
1179 group_t g
= group_at(board
, c
);
1180 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1181 b
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1) && group_is_onestone(board
, g
);
1183 assert(a
== trait_at(board
, coord
, color
).cap
);
1184 assert(b
== trait_at(board
, coord
, color
).cap1
);
1185 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1188 foreach_neighbor(board
, coord
, {
1189 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1192 board_at(board
, coord
) = color
;
1193 if (unlikely(!group
))
1194 group
= new_group(board
, coord
);
1195 board_gamma_update(board
, coord
, S_BLACK
);
1196 board_gamma_update(board
, coord
, S_WHITE
);
1198 board
->last_move2
= board
->last_move
;
1199 board
->last_move
= *m
;
1201 board_hash_update(board
, coord
, color
);
1202 board_symmetry_update(board
, &board
->symmetry
, coord
);
1203 struct move ko
= { pass
, S_NONE
};
1209 /* We played in an eye-like shape. Either we capture at least one of the eye
1210 * sides in the process of playing, or return -1. */
1211 static int profiling_noinline
1212 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1214 coord_t coord
= m
->coord
;
1215 enum stone color
= m
->color
;
1216 /* Check ko: Capture at a position of ko capture one move ago */
1217 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1219 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1221 } else if (DEBUGL(6)) {
1222 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1223 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1224 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1227 struct move ko
= { pass
, S_NONE
};
1229 int captured_groups
= 0;
1231 foreach_neighbor(board
, coord
, {
1232 group_t g
= group_at(board
, c
);
1234 fprintf(stderr
, "board_check: group %d has %d libs\n",
1235 g
, board_group_info(board
, g
).libs
);
1236 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1239 if (likely(captured_groups
== 0)) {
1242 board_print(board
, stderr
);
1243 fprintf(stderr
, "board_check: one-stone suicide\n");
1249 /* We _will_ for sure capture something. */
1250 assert(trait_at(board
, coord
, color
).cap
> 0);
1251 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1254 board
->f
[f
] = board
->f
[--board
->flen
];
1256 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1258 foreach_neighbor(board
, coord
, {
1259 inc_neighbor_count_at(board
, c
, color
);
1260 /* Originally, this could not have changed any trait
1261 * since no neighbors were S_NONE, however by now some
1262 * of them might be removed from the board. */
1263 board_trait_queue(board
, c
);
1265 group_t group
= group_at(board
, c
);
1269 board_group_rmlib(board
, group
, coord
);
1271 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1274 if (board_group_captured(board
, group
)) {
1275 if (board_group_capture(board
, group
) == 1) {
1276 /* If we captured multiple groups at once,
1277 * we can't be fighting ko so we don't need
1278 * to check for that. */
1279 ko
.color
= stone_other(color
);
1281 board
->last_ko
= ko
;
1282 board
->last_ko_age
= board
->moves
;
1284 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1289 board_at(board
, coord
) = color
;
1290 group_t group
= new_group(board
, coord
);
1291 board_gamma_update(board
, coord
, S_BLACK
);
1292 board_gamma_update(board
, coord
, S_WHITE
);
1294 board
->last_move2
= board
->last_move
;
1295 board
->last_move
= *m
;
1297 board_hash_update(board
, coord
, color
);
1298 board_hash_commit(board
);
1299 board_traits_recompute(board
);
1300 board_symmetry_update(board
, &board
->symmetry
, coord
);
1306 static int __attribute__((flatten
))
1307 board_play_f(struct board
*board
, struct move
*m
, int f
)
1310 fprintf(stderr
, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m
->coord
, board
), coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1312 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1313 /* NOT playing in an eye. Thus this move has to succeed. (This
1314 * is thanks to New Zealand rules. Otherwise, multi-stone
1315 * suicide might fail.) */
1316 group_t group
= board_play_outside(board
, m
, f
);
1317 if (unlikely(board_group_captured(board
, group
))) {
1318 board_group_capture(board
, group
);
1320 board_hash_commit(board
);
1321 board_traits_recompute(board
);
1324 return board_play_in_eye(board
, m
, f
);
1329 board_play(struct board
*board
, struct move
*m
)
1331 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1332 struct move nomove
= { pass
, S_NONE
};
1334 board
->last_move2
= board
->last_move
;
1335 board
->last_move
= *m
;
1340 for (f
= 0; f
< board
->flen
; f
++)
1341 if (board
->f
[f
] == m
->coord
)
1342 return board_play_f(board
, m
, f
);
1345 fprintf(stderr
, "board_check: stone exists\n");
1351 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1354 struct move m
= { *coord
, color
};
1356 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
1357 if (unlikely(board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1358 || !board_is_valid_move(b
, &m
)
1359 || (permit
&& !permit(permit_data
, b
, &m
)))
1361 *coord
= m
.coord
; // permit might modify it
1362 return likely(board_play_f(b
, &m
, f
) >= 0);
1366 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1368 if (unlikely(b
->flen
== 0))
1371 int base
= fast_random(b
->flen
), f
;
1372 for (f
= base
; f
< b
->flen
; f
++)
1373 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1375 for (f
= 0; f
< base
; f
++)
1376 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1381 struct move m
= { pass
, color
};
1387 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1389 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1391 /* XXX: We attempt false eye detection but we will yield false
1392 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1394 foreach_diag_neighbor(board
, coord
) {
1395 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1396 } foreach_diag_neighbor_end
;
1397 /* For false eye, we need two enemy stones diagonally in the
1398 * middle of the board, or just one enemy stone at the edge
1399 * or in the corner. */
1400 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1401 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1405 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1407 return board_is_eyelike(board
, coord
, eye_color
)
1408 && !board_is_false_eyelike(board
, coord
, eye_color
);
1412 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1414 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1416 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1424 board_fast_score(struct board
*board
)
1427 memset(scores
, 0, sizeof(scores
));
1429 foreach_point(board
) {
1430 enum stone color
= board_at(board
, c
);
1431 if (color
== S_NONE
)
1432 color
= board_get_one_point_eye(board
, c
);
1434 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1435 } foreach_point_end
;
1437 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1440 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1442 /* One flood-fill iteration; returns true if next iteration
1445 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1447 bool needs_update
= false;
1448 foreach_free_point(board
) {
1449 /* Ignore occupied and already-dame positions. */
1450 assert(board_at(board
, c
) == S_NONE
);
1451 if (ownermap
[c
] == 3)
1453 /* Count neighbors. */
1455 foreach_neighbor(board
, c
, {
1458 /* If we have neighbors of both colors, or dame,
1459 * we are dame too. */
1460 if ((nei
[1] && nei
[2]) || nei
[3]) {
1462 /* Speed up the propagation. */
1463 foreach_neighbor(board
, c
, {
1464 if (board_at(board
, c
) == S_NONE
)
1467 needs_update
= true;
1470 /* If we have neighbors of one color, we are owned
1471 * by that color, too. */
1472 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1473 int newowner
= nei
[1] ? 1 : 2;
1474 ownermap
[c
] = newowner
;
1475 /* Speed up the propagation. */
1476 foreach_neighbor(board
, c
, {
1477 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1478 ownermap
[c
] = newowner
;
1480 needs_update
= true;
1483 } foreach_free_point_end
;
1484 return needs_update
;
1487 /* Tromp-Taylor Counting */
1489 board_official_score(struct board
*board
, struct move_queue
*q
)
1492 /* A point P, not colored C, is said to reach C, if there is a path of
1493 * (vertically or horizontally) adjacent points of P's color from P to
1494 * a point of color C.
1496 * A player's score is the number of points of her color, plus the
1497 * number of empty points that reach only her color. */
1499 int ownermap
[board_size2(board
)];
1501 const int o
[4] = {0, 1, 2, 0};
1502 foreach_point(board
) {
1503 ownermap
[c
] = o
[board_at(board
, c
)];
1504 s
[board_at(board
, c
)]++;
1505 } foreach_point_end
;
1508 /* Process dead groups. */
1509 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
1510 foreach_in_group(board
, q
->move
[i
]) {
1511 enum stone color
= board_at(board
, c
);
1512 ownermap
[c
] = o
[stone_other(color
)];
1513 s
[color
]--; s
[stone_other(color
)]++;
1514 } foreach_in_group_end
;
1518 /* We need to special-case empty board. */
1519 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1520 return board
->komi
+ board
->handicap
;
1522 while (board_tromp_taylor_iter(board
, ownermap
))
1523 /* Flood-fill... */;
1526 memset(scores
, 0, sizeof(scores
));
1528 foreach_point(board
) {
1529 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1530 if (ownermap
[c
] == 3)
1532 scores
[ownermap
[c
]]++;
1533 } foreach_point_end
;
1535 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];