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_alloc(struct board
*board
)
62 /* We do not allocate the board structure itself but we allocate
63 * all the arrays with board contents. */
65 int bsize
= board_size2(board
) * sizeof(*board
->b
);
66 int gsize
= board_size2(board
) * sizeof(*board
->g
);
67 int fsize
= board_size2(board
) * sizeof(*board
->f
);
68 int nsize
= board_size2(board
) * sizeof(*board
->n
);
69 int psize
= board_size2(board
) * sizeof(*board
->p
);
70 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
71 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
73 int csize
= board_size2(board
) * sizeof(*board
->c
);
78 int ssize
= board_size2(board
) * sizeof(*board
->spathash
);
83 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
88 int tsize
= board_size2(board
) * sizeof(*board
->t
);
89 int tqsize
= board_size2(board
) * sizeof(*board
->t
);
95 int pbsize
= board_size2(board
) * sizeof(*board
->prob
[0].items
);
96 int rowpbsize
= board_size(board
) * sizeof(*board
->prob
[0].rowtotals
);
101 int cdsize
= board_size2(board
) * sizeof(*board
->coord
);
103 size_t size
= bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ ssize
+ p3size
+ tsize
+ tqsize
+ (pbsize
+ rowpbsize
) * 2 + cdsize
;
104 void *x
= malloc2(size
);
106 /* board->b must come first */
107 board
->b
= x
; x
+= bsize
;
108 board
->g
= x
; x
+= gsize
;
109 board
->f
= x
; x
+= fsize
;
110 board
->p
= x
; x
+= psize
;
111 board
->n
= x
; x
+= nsize
;
112 board
->h
= x
; x
+= hsize
;
113 board
->gi
= x
; x
+= gisize
;
115 board
->c
= x
; x
+= csize
;
117 #ifdef BOARD_SPATHASH
118 board
->spathash
= x
; x
+= ssize
;
121 board
->pat3
= x
; x
+= p3size
;
124 board
->t
= x
; x
+= tsize
;
125 board
->tq
= x
; x
+= tqsize
;
128 board
->prob
[0].items
= x
; x
+= pbsize
;
129 board
->prob
[1].items
= x
; x
+= pbsize
;
130 board
->prob
[0].rowtotals
= x
; x
+= rowpbsize
;
131 board
->prob
[1].rowtotals
= x
; x
+= rowpbsize
;
133 board
->coord
= x
; x
+= cdsize
;
139 board_copy(struct board
*b2
, struct board
*b1
)
141 memcpy(b2
, b1
, sizeof(struct board
));
143 size_t size
= board_alloc(b2
);
144 memcpy(b2
->b
, b1
->b
, size
);
150 board_done_noalloc(struct board
*board
)
152 if (board
->b
) free(board
->b
);
156 board_done(struct board
*board
)
158 board_done_noalloc(board
);
163 board_resize(struct board
*board
, int size
)
166 assert(board_size(board
) == size
+ 2);
168 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
169 board
->size2
= board_size(board
) * board_size(board
);
172 while ((1 << board
->bits2
) < board
->size2
) board
->bits2
++;
177 size_t asize
= board_alloc(board
);
178 memset(board
->b
, 0, asize
);
182 board_clear(struct board
*board
)
184 int size
= board_size(board
);
185 float komi
= board
->komi
;
187 board_done_noalloc(board
);
189 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
193 /* Setup neighborhood iterators */
194 board
->nei8
[0] = -size
- 1; // (-1,-1)
197 board
->nei8
[3] = size
- 2; // (-1,0)
199 board
->nei8
[5] = size
- 2; // (-1,1)
202 board
->dnei
[0] = -size
- 1;
204 board
->dnei
[2] = size
*2 - 2;
207 /* Setup initial symmetry */
208 board
->symmetry
.d
= 1;
209 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
210 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
211 board
->symmetry
.type
= SYM_FULL
;
213 /* Set up coordinate cache */
214 foreach_point(board
) {
215 board
->coord
[c
][0] = c
% board_size(board
);
216 board
->coord
[c
][1] = c
/ board_size(board
);
219 /* Draw the offboard margin */
220 int top_row
= board_size2(board
) - board_size(board
);
222 for (i
= 0; i
< board_size(board
); i
++)
223 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
224 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
225 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
227 foreach_point(board
) {
229 if (board_at(board
, coord
) == S_OFFBOARD
)
231 foreach_neighbor(board
, c
, {
232 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
236 /* All positions are free! Except the margin. */
237 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
238 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
239 board
->f
[board
->flen
++] = i
;
241 /* Initialize zobrist hashtable. */
242 foreach_point(board
) {
243 int max
= (sizeof(hash_t
) << history_hash_bits
);
244 /* fast_random() is 16-bit only */
245 board
->h
[c
* 2] = ((hash_t
) fast_random(max
))
246 | ((hash_t
) fast_random(max
) << 16)
247 | ((hash_t
) fast_random(max
) << 32)
248 | ((hash_t
) fast_random(max
) << 48);
249 if (!board
->h
[c
* 2])
250 /* Would be kinda "oops". */
252 /* And once again for white */
253 board
->h
[c
* 2 + 1] = ((hash_t
) fast_random(max
))
254 | ((hash_t
) fast_random(max
) << 16)
255 | ((hash_t
) fast_random(max
) << 32)
256 | ((hash_t
) fast_random(max
) << 48);
257 if (!board
->h
[c
* 2 + 1])
258 board
->h
[c
* 2 + 1] = 1;
261 #ifdef BOARD_SPATHASH
262 /* Initialize spatial hashes. */
263 foreach_point(board
) {
264 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
265 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
266 ptcoords_at(x
, y
, c
, board
, j
);
267 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
268 pthashes
[0][j
][board_at(board
, c
)];
269 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
270 pthashes
[0][j
][stone_other(board_at(board
, c
))];
276 /* Initialize 3x3 pattern codes. */
277 foreach_point(board
) {
278 if (board_at(board
, c
) == S_NONE
)
279 board
->pat3
[c
] = pattern3_hash(board
, c
);
283 /* Initialize traits. */
284 foreach_point(board
) {
285 trait_at(board
, c
, S_BLACK
).cap
= 0;
286 trait_at(board
, c
, S_BLACK
).safe
= true;
287 trait_at(board
, c
, S_WHITE
).cap
= 0;
288 trait_at(board
, c
, S_WHITE
).safe
= true;
292 board
->prob
[0].b
= board
->prob
[1].b
= board
;
293 foreach_point(board
) {
294 probdist_set(&board
->prob
[0], c
, (board_at(board
, c
) == S_NONE
) * 1.0f
);
295 probdist_set(&board
->prob
[1], c
, (board_at(board
, c
) == S_NONE
) * 1.0f
);
301 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
303 for (int i
= 0; i
< c
; i
++) {
304 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
305 s
+= snprintf(s
, end
- s
, " ");
306 for (int x
= 1; x
< board_size(board
) - 1; x
++)
307 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
308 s
+= snprintf(s
, end
-s
, " ");
310 s
+= snprintf(s
, end
- s
, "\n");
311 for (int i
= 0; i
< c
; i
++) {
312 s
+= snprintf(s
, end
- s
, " +-");
313 for (int x
= 1; x
< board_size(board
) - 1; x
++)
314 s
+= snprintf(s
, end
- s
, "--");
315 s
+= snprintf(s
, end
- s
, "+");
317 s
+= snprintf(s
, end
- s
, "\n");
322 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
324 for (int i
= 0; i
< c
; i
++) {
325 s
+= snprintf(s
, end
- s
, " +-");
326 for (int x
= 1; x
< board_size(board
) - 1; x
++)
327 s
+= snprintf(s
, end
- s
, "--");
328 s
+= snprintf(s
, end
- s
, "+");
330 s
+= snprintf(s
, end
- s
, "\n");
335 board_print_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
)
337 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
338 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
339 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
340 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
342 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
344 s
+= snprintf(s
, end
- s
, "|");
346 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
347 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
348 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
);
350 s
+= snprintf(s
, end
- s
, "|");
352 s
+= snprintf(s
, end
- s
, "\n");
357 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
361 char *end
= buf
+ sizeof(buf
);
362 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
363 board
->moves
, board
->komi
, board
->handicap
,
364 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
365 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
366 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
367 s
= board_print_row(board
, y
, s
, end
, cprint
);
368 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
369 fprintf(f
, "%s\n", buf
);
373 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
)
375 s
+= snprintf(s
, end
- s
, "%d ", group_base(group_at(board
, c
)));
380 board_print(struct board
*board
, FILE *f
)
382 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
386 board_gamma_set(struct board
*b
, struct features_gamma
*gamma
, bool precise_selfatari
)
390 b
->precise_selfatari
= precise_selfatari
;
391 for (int i
= 0; i
< b
->flen
; i
++) {
392 board_trait_recompute(b
, b
->f
[i
]);
398 /* Update the probability distribution we maintain incrementally. */
400 board_gamma_update(struct board
*board
, coord_t coord
, enum stone color
)
406 /* Punch out invalid moves and moves filling our own eyes. */
407 if (board_at(board
, coord
) != S_NONE
408 || (board_is_eyelike(board
, coord
, stone_other(color
))
409 && !trait_at(board
, coord
, color
).cap
)
410 || (board_is_one_point_eye(board
, coord
, color
))) {
411 probdist_set(&board
->prob
[color
- 1], coord
, 0);
415 hash3_t pat
= board
->pat3
[coord
];
416 if (color
== S_WHITE
) {
417 /* We work with the pattern3s as black-to-play. */
418 pat
= pattern3_reverse(pat
);
421 /* We just quickly replicate the general pattern matcher stuff
422 * here in the most bare-bone way. */
423 double value
= board
->gamma
->gamma
[FEAT_PATTERN3
][pat
];
424 if (trait_at(board
, coord
, color
).cap
)
425 value
*= board
->gamma
->gamma
[FEAT_CAPTURE
][0];
426 if (trait_at(board
, coord
, stone_other(color
)).cap
427 && trait_at(board
, coord
, color
).safe
)
428 value
*= board
->gamma
->gamma
[FEAT_AESCAPE
][0];
429 if (!trait_at(board
, coord
, color
).safe
)
430 value
*= board
->gamma
->gamma
[FEAT_SELFATARI
][1 + board
->precise_selfatari
];
431 probdist_set(&board
->prob
[color
- 1], coord
, value
);
437 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
439 if (board
->precise_selfatari
)
440 return !is_bad_selfatari(board
, color
, coord
);
442 return board_safe_to_play(board
, coord
, color
);
446 board_trait_recompute(struct board
*board
, coord_t coord
)
448 trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);;
449 trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
451 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d safe=%d) (white cap=%d safe=%d)\n",
452 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
453 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).safe
,
454 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).safe
);
456 board_gamma_update(board
, coord
, S_BLACK
);
457 board_gamma_update(board
, coord
, S_WHITE
);
461 /* Recompute traits for dirty points that we have previously touched
462 * somehow (libs of their neighbors changed or so). */
464 board_traits_recompute(struct board
*board
)
467 for (int i
= 0; i
< board
->tqlen
; i
++) {
468 coord_t coord
= board
->tq
[i
];
469 trait_at(board
, coord
, S_BLACK
).dirty
= false;
470 if (board_at(board
, coord
) != S_NONE
)
472 board_trait_recompute(board
, coord
);
478 /* Queue traits of given point for recomputing. */
480 board_trait_queue(struct board
*board
, coord_t coord
)
483 if (trait_at(board
, coord
, S_BLACK
).dirty
)
485 board
->tq
[board
->tqlen
++] = coord
;
486 trait_at(board
, coord
, S_BLACK
).dirty
= true;
491 /* Update board hash with given coordinate. */
492 static void profiling_noinline
493 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
495 board
->hash
^= hash_at(board
, coord
, color
);
497 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
);
499 #ifdef BOARD_SPATHASH
500 /* Gridcular metric is reflective, so we update all hashes
501 * of appropriate ditance in OUR circle. */
502 for (int d
= 1; d
<= BOARD_SPATHASH_MAXD
; d
++) {
503 for (int j
= ptind
[d
]; j
< ptind
[d
+ 1]; j
++) {
504 ptcoords_at(x
, y
, coord
, board
, j
);
505 /* We either changed from S_NONE to color
506 * or vice versa; doesn't matter. */
507 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][0] ^=
508 pthashes
[0][j
][color
] ^ pthashes
[0][j
][S_NONE
];
509 board
->spathash
[coord_xy(board
, x
, y
)][d
- 1][1] ^=
510 pthashes
[0][j
][stone_other(color
)] ^ pthashes
[0][j
][S_NONE
];
515 #if defined(BOARD_PAT3)
516 /* @color is not what we need in case of capture. */
517 enum stone new_color
= board_at(board
, coord
);
518 if (new_color
== S_NONE
)
519 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
520 foreach_8neighbor(board
, coord
) { // internally, the loop uses fn__i=[0..7]
521 if (board_at(board
, c
) != S_NONE
)
523 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
524 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
526 if (board_at(board
, c
) != S_OFFBOARD
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
527 board_print(board
, stderr
);
528 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
);
532 board_trait_queue(board
, c
);
533 } foreach_8neighbor_end
;
537 /* Commit current board hash to history. */
538 static void profiling_noinline
539 board_hash_commit(struct board
*board
)
542 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
543 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
544 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
546 hash_t i
= board
->hash
;
547 while (board
->history_hash
[i
& history_hash_mask
]) {
548 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
550 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
551 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
552 board
->superko_violation
= true;
555 i
= history_hash_next(i
);
557 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
563 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
565 if (likely(symmetry
->type
== SYM_NONE
)) {
566 /* Fully degenerated already. We do not support detection
567 * of restoring of symmetry, assuming that this is too rare
568 * a case to handle. */
572 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
573 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
575 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
576 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
577 symmetry
->d
, symmetry
->type
, x
, y
);
580 switch (symmetry
->type
) {
582 if (x
== t
&& y
== t
) {
583 /* Tengen keeps full symmetry. */
586 /* New symmetry now? */
588 symmetry
->type
= SYM_DIAG_UP
;
589 symmetry
->x1
= symmetry
->y1
= 1;
590 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
592 } else if (dx
== y
) {
593 symmetry
->type
= SYM_DIAG_DOWN
;
594 symmetry
->x1
= symmetry
->y1
= 1;
595 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
598 symmetry
->type
= SYM_HORIZ
;
600 symmetry
->y2
= board_size(b
) - 1;
603 symmetry
->type
= SYM_VERT
;
605 symmetry
->x2
= board_size(b
) - 1;
609 symmetry
->type
= SYM_NONE
;
610 symmetry
->x1
= symmetry
->y1
= 1;
611 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
637 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
638 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
639 symmetry
->d
, symmetry
->type
);
646 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
649 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
651 board_play(board
, &m
);
652 /* Simulate white passing; otherwise, UCT search can get confused since
653 * tree depth parity won't match the color to move. */
656 char *str
= coord2str(m
.coord
, board
);
658 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
659 if (f
) fprintf(f
, "%s ", str
);
664 board_handicap(struct board
*board
, int stones
, FILE *f
)
666 int margin
= 3 + (board_size(board
) >= 13);
668 int mid
= board_size(board
) / 2;
669 int max
= board_size(board
) - 1 - margin
;
670 const int places
[][2] = {
671 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
672 { min
, mid
}, { max
, mid
},
673 { mid
, min
}, { mid
, max
},
677 board
->handicap
= stones
;
679 if (stones
== 5 || stones
== 7) {
680 board_handicap_stone(board
, mid
, mid
, f
);
685 for (i
= 0; i
< stones
; i
++)
686 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
690 static void __attribute__((noinline
))
691 check_libs_consistency(struct board
*board
, group_t g
)
695 struct group
*gi
= &board_group_info(board
, g
);
696 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
697 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
698 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
705 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
)
707 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
709 /* Increase capturable count trait of my last lib. */
710 enum stone capturing_color
= stone_other(board_at(board
, group
));
711 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
712 foreach_neighbor(board
, lib
, {
713 if (DEBUGL(8) && group_at(board
, c
) == group
)
714 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
));
715 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
717 board_trait_queue(board
, lib
);
721 /* Update the list of capturable groups. */
723 assert(board
->clen
< board_size2(board
));
724 board
->c
[board
->clen
++] = group
;
728 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
)
730 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
732 /* Decrease capturable count trait of my previously-last lib. */
733 enum stone capturing_color
= stone_other(board_at(board
, group
));
734 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
735 foreach_neighbor(board
, lib
, {
736 if (DEBUGL(8) && group_at(board
, c
) == group
)
737 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
));
738 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
740 board_trait_queue(board
, lib
);
744 /* Update the list of capturable groups. */
745 for (int i
= 0; i
< board
->clen
; i
++) {
746 if (unlikely(board
->c
[i
] == group
)) {
747 board
->c
[i
] = board
->c
[--board
->clen
];
751 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
757 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
760 board_trait_queue(board
, lib1
);
761 board_trait_queue(board
, lib2
);
765 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
768 board_trait_queue(board
, lib1
);
769 board_trait_queue(board
, lib2
);
774 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
777 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
778 group_base(group
), coord2sstr(group_base(group
), board
),
779 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
782 check_libs_consistency(board
, group
);
784 struct group
*gi
= &board_group_info(board
, group
);
785 if (gi
->libs
< GROUP_KEEP_LIBS
) {
786 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
788 /* Seems extra branch just slows it down */
792 if (unlikely(gi
->lib
[i
] == coord
))
796 board_capturable_add(board
, group
, coord
);
797 } else if (gi
->libs
== 1) {
798 board_capturable_rm(board
, group
, gi
->lib
[0]);
799 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
800 } else if (gi
->libs
== 2) {
801 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
803 gi
->lib
[gi
->libs
++] = coord
;
806 check_libs_consistency(board
, group
);
810 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
812 /* Add extra liberty from the board to our liberty list. */
813 unsigned char watermark
[board_size2(board
) / 8];
814 memset(watermark
, 0, sizeof(watermark
));
815 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
816 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
818 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
819 watermark_set(gi
->lib
[i
]);
820 watermark_set(avoid
);
822 foreach_in_group(board
, group
) {
824 foreach_neighbor(board
, coord2
, {
825 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
828 gi
->lib
[gi
->libs
++] = c
;
829 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
832 } foreach_in_group_end
;
838 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
841 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
842 group_base(group
), coord2sstr(group_base(group
), board
),
843 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
846 struct group
*gi
= &board_group_info(board
, group
);
847 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
849 /* Seems extra branch just slows it down */
853 if (likely(gi
->lib
[i
] != coord
))
856 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
857 gi
->lib
[gi
->libs
] = 0;
859 check_libs_consistency(board
, group
);
861 /* Postpone refilling lib[] until we need to. */
862 assert(GROUP_REFILL_LIBS
> 1);
863 if (gi
->libs
> GROUP_REFILL_LIBS
)
865 if (gi
->libs
== GROUP_REFILL_LIBS
)
866 board_group_find_extra_libs(board
, group
, gi
, coord
);
869 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
870 } else if (gi
->libs
== 1) {
871 board_capturable_add(board
, group
, gi
->lib
[0]);
872 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
873 } else if (gi
->libs
== 0)
874 board_capturable_rm(board
, group
, lib
);
878 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
879 * can call this multiple times per coord. */
880 check_libs_consistency(board
, group
);
885 /* This is a low-level routine that doesn't maintain consistency
886 * of all the board data structures. */
888 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
890 enum stone color
= board_at(board
, c
);
891 board_at(board
, c
) = S_NONE
;
892 group_at(board
, c
) = 0;
893 board_hash_update(board
, c
, color
);
895 /* We mark as cannot-capture now. If this is a ko/snapback,
896 * we will get incremented later in board_group_addlib(). */
897 trait_at(board
, c
, S_BLACK
).cap
= 0;
898 trait_at(board
, c
, S_WHITE
).cap
= 0;
899 board_trait_queue(board
, c
);
902 /* Increase liberties of surrounding groups */
904 foreach_neighbor(board
, coord
, {
905 dec_neighbor_count_at(board
, c
, color
);
906 board_trait_queue(board
, c
);
907 group_t g
= group_at(board
, c
);
909 board_group_addlib(board
, g
, coord
);
913 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
914 board
->f
[board
->flen
++] = c
;
917 static int profiling_noinline
918 board_group_capture(struct board
*board
, group_t group
)
922 foreach_in_group(board
, group
) {
923 board
->captures
[stone_other(board_at(board
, c
))]++;
924 board_remove_stone(board
, group
, c
);
926 } foreach_in_group_end
;
928 struct group
*gi
= &board_group_info(board
, group
);
930 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
931 else if (gi
->libs
== 1)
932 board_capturable_rm(board
, group
, gi
->lib
[0]);
933 memset(gi
, 0, sizeof(*gi
));
939 static void profiling_noinline
940 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
942 group_at(board
, coord
) = group
;
943 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
944 groupnext_at(board
, prevstone
) = coord
;
947 if (board_group_info(board
, group
).libs
== 1) {
948 /* Our group is temporarily in atari; make sure the capturable
949 * counts also correspond to the newly added stone before we
950 * start adding liberties again so bump-dump ops match. */
951 enum stone capturing_color
= stone_other(board_at(board
, group
));
952 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
953 coord_t lib
= board_group_info(board
, group
).lib
[0];
954 if (coord_is_adjecent(lib
, coord
, board
)) {
955 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
);
956 trait_at(board
, lib
, capturing_color
).cap
++;
957 board_trait_queue(board
, lib
);
962 foreach_neighbor(board
, coord
, {
963 if (board_at(board
, c
) == S_NONE
)
964 board_group_addlib(board
, group
, c
);
968 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
969 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
970 coord_x(coord
, board
), coord_y(coord
, board
),
971 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
975 static void profiling_noinline
976 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
979 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
980 group_base(group_from
), group_base(group_to
));
981 struct group
*gi_from
= &board_group_info(board
, group_from
);
982 struct group
*gi_to
= &board_group_info(board
, group_to
);
984 /* We do this early before the group info is rewritten. */
985 if (gi_from
->libs
== 2)
986 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
987 else if (gi_from
->libs
== 1)
988 board_capturable_rm(board
, group_from
, gi_from
->lib
[0]);
991 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
993 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
994 for (int i
= 0; i
< gi_from
->libs
; i
++) {
995 for (int j
= 0; j
< gi_to
->libs
; j
++)
996 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
998 if (gi_to
->libs
== 0) {
999 board_capturable_add(board
, group_to
, gi_from
->lib
[i
]);
1000 } else if (gi_to
->libs
== 1) {
1001 board_capturable_rm(board
, group_to
, gi_to
->lib
[0]);
1002 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
1003 } else if (gi_to
->libs
== 2) {
1004 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
1006 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
1007 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
1014 if (board_group_info(board
, group_to
).libs
== 1) {
1015 /* Our group is currently in atari; make sure we properly
1016 * count in even the neighbors from the other group in the
1017 * capturable counter. */
1018 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
1019 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
1020 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
1021 foreach_neighbor(board
, lib
, {
1022 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
);
1023 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
1025 board_trait_queue(board
, lib
);
1029 coord_t last_in_group
;
1030 foreach_in_group(board
, group_from
) {
1032 group_at(board
, c
) = group_to
;
1033 } foreach_in_group_end
;
1034 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1035 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1036 memset(gi_from
, 0, sizeof(struct group
));
1039 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1040 group_base(group_to
));
1043 static group_t profiling_noinline
1044 new_group(struct board
*board
, coord_t coord
)
1046 group_t group
= coord
;
1047 struct group
*gi
= &board_group_info(board
, group
);
1048 foreach_neighbor(board
, coord
, {
1049 if (board_at(board
, c
) == S_NONE
)
1050 /* board_group_addlib is ridiculously expensive for us */
1051 #if GROUP_KEEP_LIBS < 4
1052 if (gi
->libs
< GROUP_KEEP_LIBS
)
1054 gi
->lib
[gi
->libs
++] = c
;
1057 group_at(board
, coord
) = group
;
1058 groupnext_at(board
, coord
) = 0;
1061 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1062 else if (gi
->libs
== 1)
1063 board_capturable_add(board
, group
, gi
->lib
[0]);
1064 check_libs_consistency(board
, group
);
1067 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1068 coord_x(coord
, board
), coord_y(coord
, board
),
1074 static inline group_t
1075 play_one_neighbor(struct board
*board
,
1076 coord_t coord
, enum stone color
, enum stone other_color
,
1077 coord_t c
, group_t group
)
1079 enum stone ncolor
= board_at(board
, c
);
1080 group_t ngroup
= group_at(board
, c
);
1082 inc_neighbor_count_at(board
, c
, color
);
1083 /* We can be S_NONE, in that case we need to update the safety
1084 * trait since we might be left with only one liberty. */
1085 board_trait_queue(board
, c
);
1090 board_group_rmlib(board
, ngroup
, coord
);
1092 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1093 group_base(ngroup
), ncolor
, color
, other_color
);
1095 if (ncolor
== color
&& ngroup
!= group
) {
1098 add_to_group(board
, group
, c
, coord
);
1100 merge_groups(board
, group
, ngroup
);
1102 } else if (ncolor
== other_color
) {
1104 struct group
*gi
= &board_group_info(board
, ngroup
);
1105 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1106 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1107 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1108 fprintf(stderr
, "\n");
1110 if (unlikely(board_group_captured(board
, ngroup
)))
1111 board_group_capture(board
, ngroup
);
1116 /* We played on a place with at least one liberty. We will become a member of
1117 * some group for sure. */
1118 static group_t profiling_noinline
1119 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1121 coord_t coord
= m
->coord
;
1122 enum stone color
= m
->color
;
1123 enum stone other_color
= stone_other(color
);
1126 board
->f
[f
] = board
->f
[--board
->flen
];
1128 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1130 #if defined(BOARD_TRAITS) && defined(DEBUG)
1131 /* Sanity check that cap matches reality. */
1134 foreach_neighbor(board
, coord
, {
1135 group_t g
= group_at(board
, c
);
1136 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1138 assert(a
== trait_at(board
, coord
, color
).cap
);
1139 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1142 foreach_neighbor(board
, coord
, {
1143 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1146 board_at(board
, coord
) = color
;
1147 if (unlikely(!group
))
1148 group
= new_group(board
, coord
);
1149 board_gamma_update(board
, coord
, S_BLACK
);
1150 board_gamma_update(board
, coord
, S_WHITE
);
1152 board
->last_move2
= board
->last_move
;
1153 board
->last_move
= *m
;
1155 board_hash_update(board
, coord
, color
);
1156 board_symmetry_update(board
, &board
->symmetry
, coord
);
1157 struct move ko
= { pass
, S_NONE
};
1163 /* We played in an eye-like shape. Either we capture at least one of the eye
1164 * sides in the process of playing, or return -1. */
1165 static int profiling_noinline
1166 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1168 coord_t coord
= m
->coord
;
1169 enum stone color
= m
->color
;
1170 /* Check ko: Capture at a position of ko capture one move ago */
1171 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1173 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1175 } else if (DEBUGL(6)) {
1176 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1177 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1178 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1181 struct move ko
= { pass
, S_NONE
};
1183 int captured_groups
= 0;
1185 foreach_neighbor(board
, coord
, {
1186 group_t g
= group_at(board
, c
);
1188 fprintf(stderr
, "board_check: group %d has %d libs\n",
1189 g
, board_group_info(board
, g
).libs
);
1190 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1193 if (likely(captured_groups
== 0)) {
1196 board_print(board
, stderr
);
1197 fprintf(stderr
, "board_check: one-stone suicide\n");
1203 /* We _will_ for sure capture something. */
1204 assert(trait_at(board
, coord
, color
).cap
> 0);
1205 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1208 board
->f
[f
] = board
->f
[--board
->flen
];
1210 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1212 foreach_neighbor(board
, coord
, {
1213 inc_neighbor_count_at(board
, c
, color
);
1214 /* Originally, this could not have changed any trait
1215 * since no neighbors were S_NONE, however by now some
1216 * of them might be removed from the board. */
1217 board_trait_queue(board
, c
);
1219 group_t group
= group_at(board
, c
);
1223 board_group_rmlib(board
, group
, coord
);
1225 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1228 if (board_group_captured(board
, group
)) {
1229 if (board_group_capture(board
, group
) == 1) {
1230 /* If we captured multiple groups at once,
1231 * we can't be fighting ko so we don't need
1232 * to check for that. */
1233 ko
.color
= stone_other(color
);
1235 board
->last_ko
= ko
;
1236 board
->last_ko_age
= board
->moves
;
1238 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1243 board_at(board
, coord
) = color
;
1244 group_t group
= new_group(board
, coord
);
1245 board_gamma_update(board
, coord
, S_BLACK
);
1246 board_gamma_update(board
, coord
, S_WHITE
);
1248 board
->last_move2
= board
->last_move
;
1249 board
->last_move
= *m
;
1251 board_hash_update(board
, coord
, color
);
1252 board_hash_commit(board
);
1253 board_traits_recompute(board
);
1254 board_symmetry_update(board
, &board
->symmetry
, coord
);
1260 static int __attribute__((flatten
))
1261 board_play_f(struct board
*board
, struct move
*m
, int f
)
1264 fprintf(stderr
, "board_play(): ---- Playing %d,%d\n", coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1266 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1267 /* NOT playing in an eye. Thus this move has to succeed. (This
1268 * is thanks to New Zealand rules. Otherwise, multi-stone
1269 * suicide might fail.) */
1270 group_t group
= board_play_outside(board
, m
, f
);
1271 if (unlikely(board_group_captured(board
, group
))) {
1272 board_group_capture(board
, group
);
1274 board_hash_commit(board
);
1275 board_traits_recompute(board
);
1278 return board_play_in_eye(board
, m
, f
);
1283 board_play(struct board
*board
, struct move
*m
)
1285 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1286 struct move nomove
= { pass
, S_NONE
};
1288 board
->last_move2
= board
->last_move
;
1289 board
->last_move
= *m
;
1294 for (f
= 0; f
< board
->flen
; f
++)
1295 if (board
->f
[f
] == m
->coord
)
1296 return board_play_f(board
, m
, f
);
1299 fprintf(stderr
, "board_check: stone exists\n");
1305 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1308 struct move m
= { *coord
, color
};
1310 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
1311 if (unlikely(board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1312 || !board_is_valid_move(b
, &m
)
1313 || (permit
&& !permit(permit_data
, b
, &m
)))
1315 *coord
= m
.coord
; // permit might modify it
1316 return likely(board_play_f(b
, &m
, f
) >= 0);
1320 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1322 if (unlikely(b
->flen
== 0))
1325 int base
= fast_random(b
->flen
), f
;
1326 for (f
= base
; f
< b
->flen
; f
++)
1327 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1329 for (f
= 0; f
< base
; f
++)
1330 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1335 struct move m
= { pass
, color
};
1341 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1343 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1345 /* XXX: We attempt false eye detection but we will yield false
1346 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1348 foreach_diag_neighbor(board
, coord
) {
1349 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1350 } foreach_diag_neighbor_end
;
1351 /* For false eye, we need two enemy stones diagonally in the
1352 * middle of the board, or just one enemy stone at the edge
1353 * or in the corner. */
1354 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1355 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1359 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1361 return board_is_eyelike(board
, coord
, eye_color
)
1362 && !board_is_false_eyelike(board
, coord
, eye_color
);
1366 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1368 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1370 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1378 board_fast_score(struct board
*board
)
1381 memset(scores
, 0, sizeof(scores
));
1383 foreach_point(board
) {
1384 enum stone color
= board_at(board
, c
);
1385 if (color
== S_NONE
)
1386 color
= board_get_one_point_eye(board
, c
);
1388 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1389 } foreach_point_end
;
1391 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1394 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1396 /* One flood-fill iteration; returns true if next iteration
1399 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1401 bool needs_update
= false;
1402 foreach_point(board
) {
1403 /* Ignore occupied and already-dame positions. */
1404 if (board_at(board
, c
) != S_NONE
|| ownermap
[c
] == 3)
1406 /* Count neighbors. */
1408 foreach_neighbor(board
, c
, {
1411 /* If we have neighbors of both colors, or dame,
1412 * we are dame too. */
1413 if ((nei
[1] && nei
[2]) || nei
[3]) {
1415 /* Speed up the propagation. */
1416 foreach_neighbor(board
, c
, {
1417 if (board_at(board
, c
) == S_NONE
)
1420 needs_update
= true;
1423 /* If we have neighbors of one color, we are owned
1424 * by that color, too. */
1425 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1426 int newowner
= nei
[1] ? 1 : 2;
1427 ownermap
[c
] = newowner
;
1428 /* Speed up the propagation. */
1429 foreach_neighbor(board
, c
, {
1430 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1431 ownermap
[c
] = newowner
;
1433 needs_update
= true;
1436 } foreach_point_end
;
1437 return needs_update
;
1440 /* Tromp-Taylor Counting */
1442 board_official_score(struct board
*board
, struct move_queue
*q
)
1445 /* A point P, not colored C, is said to reach C, if there is a path of
1446 * (vertically or horizontally) adjacent points of P's color from P to
1447 * a point of color C.
1449 * A player's score is the number of points of her color, plus the
1450 * number of empty points that reach only her color. */
1452 int ownermap
[board_size2(board
)];
1454 const int o
[4] = {0, 1, 2, 0};
1455 foreach_point(board
) {
1456 ownermap
[c
] = o
[board_at(board
, c
)];
1457 s
[board_at(board
, c
)]++;
1458 } foreach_point_end
;
1461 /* Process dead groups. */
1462 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
1463 foreach_in_group(board
, q
->move
[i
]) {
1464 enum stone color
= board_at(board
, c
);
1465 ownermap
[c
] = o
[stone_other(color
)];
1466 s
[color
]--; s
[stone_other(color
)]++;
1467 } foreach_in_group_end
;
1471 /* We need to special-case empty board. */
1472 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1473 return board
->komi
+ board
->handicap
;
1475 while (board_tromp_taylor_iter(board
, ownermap
))
1476 /* Flood-fill... */;
1479 memset(scores
, 0, sizeof(scores
));
1481 foreach_point(board
) {
1482 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1483 if (ownermap
[c
] == 3)
1485 scores
[ownermap
[c
]]++;
1486 } foreach_point_end
;
1488 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];