19 static void board_trait_recompute(struct board
*board
, coord_t coord
);
20 #include "tactics/selfatari.h"
25 #define profiling_noinline __attribute__((noinline))
27 #define profiling_noinline
30 #define gi_granularity 4
31 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
35 board_setup(struct board
*b
)
37 char *fbookfile
= b
->fbookfile
;
39 memset(b
, 0, sizeof(*b
));
41 b
->fbookfile
= fbookfile
;
43 struct move m
= { pass
, S_NONE
};
44 b
->last_move
= b
->last_move2
= b
->last_ko
= b
->ko
= m
;
48 board_init(char *fbookfile
)
50 struct board
*b
= malloc2(sizeof(struct board
));
53 b
->fbookfile
= fbookfile
;
63 board_alloc(struct board
*board
)
65 /* We do not allocate the board structure itself but we allocate
66 * all the arrays with board contents. */
68 int bsize
= board_size2(board
) * sizeof(*board
->b
);
69 int gsize
= board_size2(board
) * sizeof(*board
->g
);
70 int fsize
= board_size2(board
) * sizeof(*board
->f
);
71 int nsize
= board_size2(board
) * sizeof(*board
->n
);
72 int psize
= board_size2(board
) * sizeof(*board
->p
);
73 int hsize
= board_size2(board
) * 2 * sizeof(*board
->h
);
74 int gisize
= board_size2(board
) * sizeof(*board
->gi
);
76 int csize
= board_size2(board
) * sizeof(*board
->c
);
81 int p3size
= board_size2(board
) * sizeof(*board
->pat3
);
86 int tsize
= board_size2(board
) * sizeof(*board
->t
);
87 int tqsize
= board_size2(board
) * sizeof(*board
->t
);
92 int cdsize
= board_size2(board
) * sizeof(*board
->coord
);
94 size_t size
= bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
+ p3size
+ tsize
+ tqsize
+ cdsize
;
95 void *x
= malloc2(size
);
97 /* board->b must come first */
98 board
->b
= x
; x
+= bsize
;
99 board
->g
= x
; x
+= gsize
;
100 board
->f
= x
; x
+= fsize
;
101 board
->p
= x
; x
+= psize
;
102 board
->n
= x
; x
+= nsize
;
103 board
->h
= x
; x
+= hsize
;
104 board
->gi
= x
; x
+= gisize
;
106 board
->c
= x
; x
+= csize
;
109 board
->pat3
= x
; x
+= p3size
;
112 board
->t
= x
; x
+= tsize
;
113 board
->tq
= x
; x
+= tqsize
;
115 board
->coord
= x
; x
+= cdsize
;
121 board_copy(struct board
*b2
, struct board
*b1
)
123 memcpy(b2
, b1
, sizeof(struct board
));
125 size_t size
= board_alloc(b2
);
126 memcpy(b2
->b
, b1
->b
, size
);
128 // XXX: Special semantics.
135 board_done_noalloc(struct board
*board
)
137 if (board
->b
) free(board
->b
);
138 if (board
->fbook
) fbook_done(board
->fbook
);
142 board_done(struct board
*board
)
144 board_done_noalloc(board
);
149 board_resize(struct board
*board
, int size
)
152 assert(board_size(board
) == size
+ 2);
154 assert(size
<= BOARD_MAX_SIZE
);
155 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
156 board
->size2
= board_size(board
) * board_size(board
);
159 while ((1 << board
->bits2
) < board
->size2
) board
->bits2
++;
164 size_t asize
= board_alloc(board
);
165 memset(board
->b
, 0, asize
);
169 board_clear(struct board
*board
)
171 int size
= board_size(board
);
172 floating_t komi
= board
->komi
;
174 board_done_noalloc(board
);
176 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
180 /* Setup neighborhood iterators */
181 board
->nei8
[0] = -size
- 1; // (-1,-1)
184 board
->nei8
[3] = size
- 2; // (-1,0)
186 board
->nei8
[5] = size
- 2; // (-1,1)
189 board
->dnei
[0] = -size
- 1;
191 board
->dnei
[2] = size
*2 - 2;
194 /* Setup initial symmetry */
195 board
->symmetry
.d
= 1;
196 board
->symmetry
.x1
= board
->symmetry
.y1
= board_size(board
) / 2;
197 board
->symmetry
.x2
= board
->symmetry
.y2
= board_size(board
) - 1;
198 board
->symmetry
.type
= SYM_FULL
;
200 /* Set up coordinate cache */
201 foreach_point(board
) {
202 board
->coord
[c
][0] = c
% board_size(board
);
203 board
->coord
[c
][1] = c
/ board_size(board
);
206 /* Draw the offboard margin */
207 int top_row
= board_size2(board
) - board_size(board
);
209 for (i
= 0; i
< board_size(board
); i
++)
210 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
211 for (i
= 0; i
<= top_row
; i
+= board_size(board
))
212 board
->b
[i
] = board
->b
[board_size(board
) - 1 + i
] = S_OFFBOARD
;
214 foreach_point(board
) {
216 if (board_at(board
, coord
) == S_OFFBOARD
)
218 foreach_neighbor(board
, c
, {
219 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
223 /* All positions are free! Except the margin. */
224 for (i
= board_size(board
); i
< (board_size(board
) - 1) * board_size(board
); i
++)
225 if (i
% board_size(board
) != 0 && i
% board_size(board
) != board_size(board
) - 1)
226 board
->f
[board
->flen
++] = i
;
228 /* Initialize zobrist hashtable. */
229 /* We will need these to be stable across Pachi runs for
230 * certain kinds of pattern matching, thus we do not use
231 * fast_random() for this. */
232 hash_t hseed
= 0x3121110101112131;
233 foreach_point(board
) {
234 board
->h
[c
* 2] = (hseed
*= 16807);
235 if (!board
->h
[c
* 2])
237 /* And once again for white */
238 board
->h
[c
* 2 + 1] = (hseed
*= 16807);
239 if (!board
->h
[c
* 2 + 1])
240 board
->h
[c
* 2 + 1] = 1;
244 /* Initialize 3x3 pattern codes. */
245 foreach_point(board
) {
246 if (board_at(board
, c
) == S_NONE
)
247 board
->pat3
[c
] = pattern3_hash(board
, c
);
251 /* Initialize traits. */
252 foreach_point(board
) {
253 trait_at(board
, c
, S_BLACK
).cap
= 0;
254 trait_at(board
, c
, S_BLACK
).cap1
= 0;
255 trait_at(board
, c
, S_BLACK
).safe
= true;
256 trait_at(board
, c
, S_WHITE
).cap
= 0;
257 trait_at(board
, c
, S_WHITE
).cap1
= 0;
258 trait_at(board
, c
, S_WHITE
).safe
= true;
262 if (board
->fbookfile
) {
263 board
->fbook
= fbook_init(board
->fbookfile
, board
);
268 board_print_top(struct board
*board
, char *s
, char *end
, int c
)
270 for (int i
= 0; i
< c
; i
++) {
271 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
272 s
+= snprintf(s
, end
- s
, " ");
273 for (int x
= 1; x
< board_size(board
) - 1; x
++)
274 s
+= snprintf(s
, end
- s
, "%c ", asdf
[x
- 1]);
275 s
+= snprintf(s
, end
-s
, " ");
277 s
+= snprintf(s
, end
- s
, "\n");
278 for (int i
= 0; i
< c
; i
++) {
279 s
+= snprintf(s
, end
- s
, " +-");
280 for (int x
= 1; x
< board_size(board
) - 1; x
++)
281 s
+= snprintf(s
, end
- s
, "--");
282 s
+= snprintf(s
, end
- s
, "+");
284 s
+= snprintf(s
, end
- s
, "\n");
289 board_print_bottom(struct board
*board
, char *s
, char *end
, int c
)
291 for (int i
= 0; i
< c
; i
++) {
292 s
+= snprintf(s
, end
- s
, " +-");
293 for (int x
= 1; x
< board_size(board
) - 1; x
++)
294 s
+= snprintf(s
, end
- s
, "--");
295 s
+= snprintf(s
, end
- s
, "+");
297 s
+= snprintf(s
, end
- s
, "\n");
302 board_print_row(struct board
*board
, int y
, char *s
, char *end
, board_cprint cprint
)
304 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
305 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
306 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
307 s
+= snprintf(s
, end
- s
, "%c)", stone2char(board_atxy(board
, x
, y
)));
309 s
+= snprintf(s
, end
- s
, "%c ", stone2char(board_atxy(board
, x
, y
)));
311 s
+= snprintf(s
, end
- s
, "|");
313 s
+= snprintf(s
, end
- s
, " %2d | ", y
);
314 for (int x
= 1; x
< board_size(board
) - 1; x
++) {
315 s
= cprint(board
, coord_xy(board
, x
, y
), s
, end
);
317 s
+= snprintf(s
, end
- s
, "|");
319 s
+= snprintf(s
, end
- s
, "\n");
324 board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
)
328 char *end
= buf
+ sizeof(buf
);
329 s
+= snprintf(s
, end
- s
, "Move: % 3d Komi: %2.1f Handicap: %d Captures B: %d W: %d\n",
330 board
->moves
, board
->komi
, board
->handicap
,
331 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
332 s
= board_print_top(board
, s
, end
, 1 + !!cprint
);
333 for (int y
= board_size(board
) - 2; y
>= 1; y
--)
334 s
= board_print_row(board
, y
, s
, end
, cprint
);
335 board_print_bottom(board
, s
, end
, 1 + !!cprint
);
336 fprintf(f
, "%s\n", buf
);
340 cprint_group(struct board
*board
, coord_t c
, char *s
, char *end
)
342 s
+= snprintf(s
, end
- s
, "%d ", group_base(group_at(board
, c
)));
347 board_print(struct board
*board
, FILE *f
)
349 board_print_custom(board
, f
, DEBUGL(6) ? cprint_group
: NULL
);
355 board_trait_safe(struct board
*board
, coord_t coord
, enum stone color
)
357 if (board
->precise_selfatari
)
358 return !is_bad_selfatari(board
, color
, coord
);
360 return board_safe_to_play(board
, coord
, color
);
364 board_trait_recompute(struct board
*board
, coord_t coord
)
366 trait_at(board
, coord
, S_BLACK
).safe
= board_trait_safe(board
, coord
, S_BLACK
);;
367 trait_at(board
, coord
, S_WHITE
).safe
= board_trait_safe(board
, coord
, S_WHITE
);
369 fprintf(stderr
, "traits[%s:%s lib=%d] (black cap=%d cap1=%d safe=%d) (white cap=%d cap1=%d safe=%d)\n",
370 coord2sstr(coord
, board
), stone2str(board_at(board
, coord
)), immediate_liberty_count(board
, coord
),
371 trait_at(board
, coord
, S_BLACK
).cap
, trait_at(board
, coord
, S_BLACK
).cap1
, trait_at(board
, coord
, S_BLACK
).safe
,
372 trait_at(board
, coord
, S_WHITE
).cap
, trait_at(board
, coord
, S_WHITE
).cap1
, trait_at(board
, coord
, S_WHITE
).safe
);
377 /* Recompute traits for dirty points that we have previously touched
378 * somehow (libs of their neighbors changed or so). */
380 board_traits_recompute(struct board
*board
)
383 for (int i
= 0; i
< board
->tqlen
; i
++) {
384 coord_t coord
= board
->tq
[i
];
385 trait_at(board
, coord
, S_BLACK
).dirty
= false;
386 if (board_at(board
, coord
) != S_NONE
)
388 board_trait_recompute(board
, coord
);
394 /* Queue traits of given point for recomputing. */
396 board_trait_queue(struct board
*board
, coord_t coord
)
399 if (trait_at(board
, coord
, S_BLACK
).dirty
)
401 board
->tq
[board
->tqlen
++] = coord
;
402 trait_at(board
, coord
, S_BLACK
).dirty
= true;
407 /* Update board hash with given coordinate. */
408 static void profiling_noinline
409 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
411 board
->hash
^= hash_at(board
, coord
, color
);
412 board
->qhash
[coord_quadrant(coord
, board
)] ^= hash_at(board
, coord
, color
);
414 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
);
416 #if defined(BOARD_PAT3)
417 /* @color is not what we need in case of capture. */
418 static const int ataribits
[8] = { -1, 0, -1, 1, 2, -1, 3, -1 };
419 enum stone new_color
= board_at(board
, coord
);
420 bool in_atari
= false;
421 if (new_color
== S_NONE
) {
422 board
->pat3
[coord
] = pattern3_hash(board
, coord
);
424 in_atari
= (board_group_info(board
, group_at(board
, coord
)).libs
== 1);
426 foreach_8neighbor(board
, coord
) {
427 /* Internally, the loop uses fn__i=[0..7]. We can use
428 * it directly to address bits within the bitmap of the
429 * neighbors since the bitmap order is reverse to the
431 if (board_at(board
, c
) != S_NONE
)
433 board
->pat3
[c
] &= ~(3 << (fn__i
*2));
434 board
->pat3
[c
] |= new_color
<< (fn__i
*2);
435 if (ataribits
[fn__i
] >= 0) {
436 board
->pat3
[c
] &= ~(1 << (16 + ataribits
[fn__i
]));
437 board
->pat3
[c
] |= in_atari
<< (16 + ataribits
[fn__i
]);
439 #if defined(BOARD_TRAITS)
440 board_trait_queue(board
, c
);
442 } foreach_8neighbor_end
;
446 /* Commit current board hash to history. */
447 static void profiling_noinline
448 board_hash_commit(struct board
*board
)
451 fprintf(stderr
, "board_hash_commit %"PRIhash
"\n", board
->hash
);
452 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
453 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
455 hash_t i
= board
->hash
;
456 while (board
->history_hash
[i
& history_hash_mask
]) {
457 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
459 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
460 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
461 board
->superko_violation
= true;
464 i
= history_hash_next(i
);
466 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
472 board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
)
474 if (likely(symmetry
->type
== SYM_NONE
)) {
475 /* Fully degenerated already. We do not support detection
476 * of restoring of symmetry, assuming that this is too rare
477 * a case to handle. */
481 int x
= coord_x(c
, b
), y
= coord_y(c
, b
), t
= board_size(b
) / 2;
482 int dx
= board_size(b
) - 1 - x
; /* for SYM_DOWN */
484 fprintf(stderr
, "SYMMETRY [%d,%d,%d,%d|%d=%d] update for %d,%d\n",
485 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
486 symmetry
->d
, symmetry
->type
, x
, y
);
489 switch (symmetry
->type
) {
491 if (x
== t
&& y
== t
) {
492 /* Tengen keeps full symmetry. */
495 /* New symmetry now? */
497 symmetry
->type
= SYM_DIAG_UP
;
498 symmetry
->x1
= symmetry
->y1
= 1;
499 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
501 } else if (dx
== y
) {
502 symmetry
->type
= SYM_DIAG_DOWN
;
503 symmetry
->x1
= symmetry
->y1
= 1;
504 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
507 symmetry
->type
= SYM_HORIZ
;
509 symmetry
->y2
= board_size(b
) - 1;
512 symmetry
->type
= SYM_VERT
;
514 symmetry
->x2
= board_size(b
) - 1;
518 symmetry
->type
= SYM_NONE
;
519 symmetry
->x1
= symmetry
->y1
= 1;
520 symmetry
->x2
= symmetry
->y2
= board_size(b
) - 1;
546 fprintf(stderr
, "NEW SYMMETRY [%d,%d,%d,%d|%d=%d]\n",
547 symmetry
->x1
, symmetry
->y1
, symmetry
->x2
, symmetry
->y2
,
548 symmetry
->d
, symmetry
->type
);
555 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
558 m
.color
= S_BLACK
; m
.coord
= coord_xy(board
, x
, y
);
560 board_play(board
, &m
);
561 /* Simulate white passing; otherwise, UCT search can get confused since
562 * tree depth parity won't match the color to move. */
565 char *str
= coord2str(m
.coord
, board
);
567 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
568 if (f
) fprintf(f
, "%s ", str
);
573 board_handicap(struct board
*board
, int stones
, FILE *f
)
575 int margin
= 3 + (board_size(board
) >= 13);
577 int mid
= board_size(board
) / 2;
578 int max
= board_size(board
) - 1 - margin
;
579 const int places
[][2] = {
580 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
581 { min
, mid
}, { max
, mid
},
582 { mid
, min
}, { mid
, max
},
586 board
->handicap
= stones
;
588 if (stones
== 5 || stones
== 7) {
589 board_handicap_stone(board
, mid
, mid
, f
);
594 for (i
= 0; i
< stones
; i
++)
595 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
599 static void __attribute__((noinline
))
600 check_libs_consistency(struct board
*board
, group_t g
)
604 struct group
*gi
= &board_group_info(board
, g
);
605 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
606 if (gi
->lib
[i
] && board_at(board
, gi
->lib
[i
]) != S_NONE
) {
607 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(group_base(g
), board
));
614 check_pat3_consistency(struct board
*board
, coord_t coord
)
617 foreach_8neighbor(board
, coord
) {
618 if (board_at(board
, c
) == S_NONE
&& pattern3_hash(board
, c
) != board
->pat3
[c
]) {
619 board_print(board
, stderr
);
620 fprintf(stderr
, "%s(%d)->%s(%d) computed %x != stored %x (%d)\n", coord2sstr(coord
, board
), coord
, coord2sstr(c
, board
), c
, pattern3_hash(board
, c
), board
->pat3
[c
], fn__i
);
623 } foreach_8neighbor_end
;
628 board_capturable_add(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
630 //fprintf(stderr, "group %s cap %s\n", coord2sstr(group, board), coord2sstr(lib, boarD));
632 /* Increase capturable count trait of my last lib. */
633 enum stone capturing_color
= stone_other(board_at(board
, group
));
634 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
635 foreach_neighbor(board
, lib
, {
636 if (DEBUGL(8) && group_at(board
, c
) == group
)
637 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
);
638 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group
);
639 trait_at(board
, lib
, capturing_color
).cap1
+= (group_at(board
, c
) == group
&& onestone
);
641 board_trait_queue(board
, lib
);
646 foreach_neighbor(board
, lib
, {
647 board
->pat3
[lib
] |= (group_at(board
, c
) == group
) << (16 + 3 - fn__i
);
653 /* Update the list of capturable groups. */
655 assert(board
->clen
< board_size2(board
));
656 board
->c
[board
->clen
++] = group
;
660 board_capturable_rm(struct board
*board
, group_t group
, coord_t lib
, bool onestone
)
662 //fprintf(stderr, "group %s nocap %s\n", coord2sstr(group, board), coord2sstr(lib, board));
664 /* Decrease capturable count trait of my previously-last lib. */
665 enum stone capturing_color
= stone_other(board_at(board
, group
));
666 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
667 foreach_neighbor(board
, lib
, {
668 if (DEBUGL(8) && group_at(board
, c
) == group
)
669 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
);
670 trait_at(board
, lib
, capturing_color
).cap
-= (group_at(board
, c
) == group
);
671 trait_at(board
, lib
, capturing_color
).cap1
-= (group_at(board
, c
) == group
&& onestone
);
673 board_trait_queue(board
, lib
);
678 foreach_neighbor(board
, lib
, {
679 board
->pat3
[lib
] &= ~((group_at(board
, c
) == group
) << (16 + 3 - fn__i
));
685 /* Update the list of capturable groups. */
686 for (int i
= 0; i
< board
->clen
; i
++) {
687 if (unlikely(board
->c
[i
] == group
)) {
688 board
->c
[i
] = board
->c
[--board
->clen
];
692 fprintf(stderr
, "rm of bad group %d\n", group_base(group
));
698 board_atariable_add(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
701 board_trait_queue(board
, lib1
);
702 board_trait_queue(board
, lib2
);
706 board_atariable_rm(struct board
*board
, group_t group
, coord_t lib1
, coord_t lib2
)
709 board_trait_queue(board
, lib1
);
710 board_trait_queue(board
, lib2
);
715 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
718 fprintf(stderr
, "Group %d[%s] %d: Adding liberty %s\n",
719 group_base(group
), coord2sstr(group_base(group
), board
),
720 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
723 check_libs_consistency(board
, group
);
725 struct group
*gi
= &board_group_info(board
, group
);
726 bool onestone
= group_is_onestone(board
, group
);
727 if (gi
->libs
< GROUP_KEEP_LIBS
) {
728 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
730 /* Seems extra branch just slows it down */
734 if (unlikely(gi
->lib
[i
] == coord
))
738 board_capturable_add(board
, group
, coord
, onestone
);
739 } else if (gi
->libs
== 1) {
740 board_capturable_rm(board
, group
, gi
->lib
[0], onestone
);
741 board_atariable_add(board
, group
, gi
->lib
[0], coord
);
742 } else if (gi
->libs
== 2) {
743 board_atariable_rm(board
, group
, gi
->lib
[0], gi
->lib
[1]);
745 gi
->lib
[gi
->libs
++] = coord
;
748 check_libs_consistency(board
, group
);
752 board_group_find_extra_libs(struct board
*board
, group_t group
, struct group
*gi
, coord_t avoid
)
754 /* Add extra liberty from the board to our liberty list. */
755 unsigned char watermark
[board_size2(board
) / 8];
756 memset(watermark
, 0, sizeof(watermark
));
757 #define watermark_get(c) (watermark[c >> 3] & (1 << (c & 7)))
758 #define watermark_set(c) watermark[c >> 3] |= (1 << (c & 7))
760 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++)
761 watermark_set(gi
->lib
[i
]);
762 watermark_set(avoid
);
764 foreach_in_group(board
, group
) {
766 foreach_neighbor(board
, coord2
, {
767 if (board_at(board
, c
) + watermark_get(c
) != S_NONE
)
770 gi
->lib
[gi
->libs
++] = c
;
771 if (unlikely(gi
->libs
>= GROUP_KEEP_LIBS
))
774 } foreach_in_group_end
;
780 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
783 fprintf(stderr
, "Group %d[%s] %d: Removing liberty %s\n",
784 group_base(group
), coord2sstr(group_base(group
), board
),
785 board_group_info(board
, group
).libs
, coord2sstr(coord
, board
));
788 struct group
*gi
= &board_group_info(board
, group
);
789 bool onestone
= group_is_onestone(board
, group
);
790 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++) {
792 /* Seems extra branch just slows it down */
796 if (likely(gi
->lib
[i
] != coord
))
799 coord_t lib
= gi
->lib
[i
] = gi
->lib
[--gi
->libs
];
800 gi
->lib
[gi
->libs
] = 0;
802 check_libs_consistency(board
, group
);
804 /* Postpone refilling lib[] until we need to. */
805 assert(GROUP_REFILL_LIBS
> 1);
806 if (gi
->libs
> GROUP_REFILL_LIBS
)
808 if (gi
->libs
== GROUP_REFILL_LIBS
)
809 board_group_find_extra_libs(board
, group
, gi
, coord
);
812 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
813 } else if (gi
->libs
== 1) {
814 board_capturable_add(board
, group
, gi
->lib
[0], onestone
);
815 board_atariable_rm(board
, group
, gi
->lib
[0], lib
);
816 } else if (gi
->libs
== 0)
817 board_capturable_rm(board
, group
, lib
, onestone
);
821 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
822 * can call this multiple times per coord. */
823 check_libs_consistency(board
, group
);
828 /* This is a low-level routine that doesn't maintain consistency
829 * of all the board data structures. */
831 board_remove_stone(struct board
*board
, group_t group
, coord_t c
)
833 enum stone color
= board_at(board
, c
);
834 board_at(board
, c
) = S_NONE
;
835 group_at(board
, c
) = 0;
836 board_hash_update(board
, c
, color
);
838 /* We mark as cannot-capture now. If this is a ko/snapback,
839 * we will get incremented later in board_group_addlib(). */
840 trait_at(board
, c
, S_BLACK
).cap
= trait_at(board
, c
, S_BLACK
).cap1
= 0;
841 trait_at(board
, c
, S_WHITE
).cap
= trait_at(board
, c
, S_WHITE
).cap1
= 0;
842 board_trait_queue(board
, c
);
845 /* Increase liberties of surrounding groups */
847 foreach_neighbor(board
, coord
, {
848 dec_neighbor_count_at(board
, c
, color
);
849 board_trait_queue(board
, c
);
850 group_t g
= group_at(board
, c
);
852 board_group_addlib(board
, g
, coord
);
856 /* board_hash_update() might have seen the freed up point as able
857 * to capture another group in atari that only after the loop
858 * above gained enough liberties. Reset pat3 again. */
859 board
->pat3
[c
] = pattern3_hash(board
, c
);
863 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
864 board
->f
[board
->flen
++] = c
;
867 static int profiling_noinline
868 board_group_capture(struct board
*board
, group_t group
)
872 foreach_in_group(board
, group
) {
873 board
->captures
[stone_other(board_at(board
, c
))]++;
874 board_remove_stone(board
, group
, c
);
876 } foreach_in_group_end
;
878 struct group
*gi
= &board_group_info(board
, group
);
879 assert(gi
->libs
== 0);
880 memset(gi
, 0, sizeof(*gi
));
886 static void profiling_noinline
887 add_to_group(struct board
*board
, group_t group
, coord_t prevstone
, coord_t coord
)
890 struct group
*gi
= &board_group_info(board
, group
);
891 bool onestone
= group_is_onestone(board
, group
);
894 /* Our group is temporarily in atari; make sure the capturable
895 * counts also correspond to the newly added stone before we
896 * start adding liberties again so bump-dump ops match. */
897 enum stone capturing_color
= stone_other(board_at(board
, group
));
898 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
900 coord_t lib
= board_group_info(board
, group
).lib
[0];
901 if (coord_is_adjecent(lib
, coord
, board
)) {
903 fprintf(stderr
, "add_to_group %s: %s[%d] bump\n", coord2sstr(group
, board
), coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
904 trait_at(board
, lib
, capturing_color
).cap
++;
905 /* This is never a 1-stone group, obviously. */
906 board_trait_queue(board
, lib
);
910 /* We are not 1-stone group anymore, update the cap1
911 * counter specifically. */
912 foreach_neighbor(board
, group
, {
913 if (board_at(board
, c
) != S_NONE
) continue;
914 trait_at(board
, c
, capturing_color
).cap1
--;
915 board_trait_queue(board
, c
);
921 group_at(board
, coord
) = group
;
922 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
923 groupnext_at(board
, prevstone
) = coord
;
925 foreach_neighbor(board
, coord
, {
926 if (board_at(board
, c
) == S_NONE
)
927 board_group_addlib(board
, group
, c
);
931 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
932 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
933 coord_x(coord
, board
), coord_y(coord
, board
),
934 groupnext_at(board
, coord
) % board_size(board
), groupnext_at(board
, coord
) / board_size(board
),
938 static void profiling_noinline
939 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
942 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
943 group_base(group_from
), group_base(group_to
));
944 struct group
*gi_from
= &board_group_info(board
, group_from
);
945 struct group
*gi_to
= &board_group_info(board
, group_to
);
946 bool onestone_from
= group_is_onestone(board
, group_from
);
947 bool onestone_to
= group_is_onestone(board
, group_to
);
949 /* We do this early before the group info is rewritten. */
950 if (gi_from
->libs
== 2)
951 board_atariable_rm(board
, group_from
, gi_from
->lib
[0], gi_from
->lib
[1]);
952 else if (gi_from
->libs
== 1)
953 board_capturable_rm(board
, group_from
, gi_from
->lib
[0], onestone_from
);
956 fprintf(stderr
,"---- (froml %d, tol %d)\n", gi_from
->libs
, gi_to
->libs
);
958 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
959 for (int i
= 0; i
< gi_from
->libs
; i
++) {
960 for (int j
= 0; j
< gi_to
->libs
; j
++)
961 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
963 if (gi_to
->libs
== 0) {
964 board_capturable_add(board
, group_to
, gi_from
->lib
[i
], onestone_to
);
965 } else if (gi_to
->libs
== 1) {
966 board_capturable_rm(board
, group_to
, gi_to
->lib
[0], onestone_to
);
967 board_atariable_add(board
, group_to
, gi_to
->lib
[0], gi_from
->lib
[i
]);
968 } else if (gi_to
->libs
== 2) {
969 board_atariable_rm(board
, group_to
, gi_to
->lib
[0], gi_to
->lib
[1]);
971 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
972 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
978 if (gi_to
->libs
== 1) {
979 coord_t lib
= board_group_info(board
, group_to
).lib
[0];
981 enum stone capturing_color
= stone_other(board_at(board
, group_to
));
982 assert(capturing_color
== S_BLACK
|| capturing_color
== S_WHITE
);
984 /* Our group is currently in atari; make sure we properly
985 * count in even the neighbors from the other group in the
986 * capturable counter. */
987 foreach_neighbor(board
, lib
, {
988 if (DEBUGL(8) && group_at(board
, c
) == group_from
)
989 fprintf(stderr
, "%s[%d] cap bump\n", coord2sstr(lib
, board
), trait_at(board
, lib
, capturing_color
).cap
);
990 trait_at(board
, lib
, capturing_color
).cap
+= (group_at(board
, c
) == group_from
);
991 /* This is never a 1-stone group, obviously. */
993 board_trait_queue(board
, lib
);
996 /* We are not 1-stone group anymore, update the cap1
997 * counter specifically. */
998 foreach_neighbor(board
, group_to
, {
999 if (board_at(board
, c
) != S_NONE
) continue;
1000 trait_at(board
, c
, capturing_color
).cap1
--;
1001 board_trait_queue(board
, c
);
1006 if (gi_from
->libs
== 1) {
1007 /* We removed group_from from capturable groups,
1008 * therefore switching the atari flag off.
1009 * We need to set it again since group_to is also
1012 foreach_neighbor(board
, lib
, {
1013 board
->pat3
[lib
] |= (group_at(board
, c
) == group_from
) << (16 + 3 - fn__i
);
1020 coord_t last_in_group
;
1021 foreach_in_group(board
, group_from
) {
1023 group_at(board
, c
) = group_to
;
1024 } foreach_in_group_end
;
1025 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_base(group_to
));
1026 groupnext_at(board
, group_base(group_to
)) = group_base(group_from
);
1027 memset(gi_from
, 0, sizeof(struct group
));
1030 fprintf(stderr
, "board_play_raw: merged group: %d\n",
1031 group_base(group_to
));
1034 static group_t profiling_noinline
1035 new_group(struct board
*board
, coord_t coord
)
1037 group_t group
= coord
;
1038 struct group
*gi
= &board_group_info(board
, group
);
1039 foreach_neighbor(board
, coord
, {
1040 if (board_at(board
, c
) == S_NONE
)
1041 /* board_group_addlib is ridiculously expensive for us */
1042 #if GROUP_KEEP_LIBS < 4
1043 if (gi
->libs
< GROUP_KEEP_LIBS
)
1045 gi
->lib
[gi
->libs
++] = c
;
1048 group_at(board
, coord
) = group
;
1049 groupnext_at(board
, coord
) = 0;
1052 board_atariable_add(board
, group
, gi
->lib
[0], gi
->lib
[1]);
1053 else if (gi
->libs
== 1)
1054 board_capturable_add(board
, group
, gi
->lib
[0], true);
1055 check_libs_consistency(board
, group
);
1058 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
1059 coord_x(coord
, board
), coord_y(coord
, board
),
1065 static inline group_t
1066 play_one_neighbor(struct board
*board
,
1067 coord_t coord
, enum stone color
, enum stone other_color
,
1068 coord_t c
, group_t group
)
1070 enum stone ncolor
= board_at(board
, c
);
1071 group_t ngroup
= group_at(board
, c
);
1073 inc_neighbor_count_at(board
, c
, color
);
1074 /* We can be S_NONE, in that case we need to update the safety
1075 * trait since we might be left with only one liberty. */
1076 board_trait_queue(board
, c
);
1081 board_group_rmlib(board
, ngroup
, coord
);
1083 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
1084 group_base(ngroup
), ncolor
, color
, other_color
);
1086 if (ncolor
== color
&& ngroup
!= group
) {
1089 add_to_group(board
, group
, c
, coord
);
1091 merge_groups(board
, group
, ngroup
);
1093 } else if (ncolor
== other_color
) {
1095 struct group
*gi
= &board_group_info(board
, ngroup
);
1096 fprintf(stderr
, "testing captured group %d[%s]: ", group_base(ngroup
), coord2sstr(group_base(ngroup
), board
));
1097 for (int i
= 0; i
< GROUP_KEEP_LIBS
; i
++)
1098 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
1099 fprintf(stderr
, "\n");
1101 if (unlikely(board_group_captured(board
, ngroup
)))
1102 board_group_capture(board
, ngroup
);
1107 /* We played on a place with at least one liberty. We will become a member of
1108 * some group for sure. */
1109 static group_t profiling_noinline
1110 board_play_outside(struct board
*board
, struct move
*m
, int f
)
1112 coord_t coord
= m
->coord
;
1113 enum stone color
= m
->color
;
1114 enum stone other_color
= stone_other(color
);
1117 board
->f
[f
] = board
->f
[--board
->flen
];
1119 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1121 #if defined(BOARD_TRAITS) && defined(DEBUG)
1122 /* Sanity check that cap matches reality. */
1125 foreach_neighbor(board
, coord
, {
1126 group_t g
= group_at(board
, c
);
1127 a
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1);
1128 b
+= g
&& (board_at(board
, c
) == other_color
&& board_group_info(board
, g
).libs
== 1) && group_is_onestone(board
, g
);
1130 assert(a
== trait_at(board
, coord
, color
).cap
);
1131 assert(b
== trait_at(board
, coord
, color
).cap1
);
1132 assert(board_trait_safe(board
, coord
, color
) == trait_at(board
, coord
, color
).safe
);
1135 foreach_neighbor(board
, coord
, {
1136 group
= play_one_neighbor(board
, coord
, color
, other_color
, c
, group
);
1139 board_at(board
, coord
) = color
;
1140 if (unlikely(!group
))
1141 group
= new_group(board
, coord
);
1143 board
->last_move2
= board
->last_move
;
1144 board
->last_move
= *m
;
1146 board_hash_update(board
, coord
, color
);
1147 board_symmetry_update(board
, &board
->symmetry
, coord
);
1148 struct move ko
= { pass
, S_NONE
};
1151 check_pat3_consistency(board
, coord
);
1156 /* We played in an eye-like shape. Either we capture at least one of the eye
1157 * sides in the process of playing, or return -1. */
1158 static int profiling_noinline
1159 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
1161 coord_t coord
= m
->coord
;
1162 enum stone color
= m
->color
;
1163 /* Check ko: Capture at a position of ko capture one move ago */
1164 if (unlikely(color
== board
->ko
.color
&& coord
== board
->ko
.coord
)) {
1166 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
1168 } else if (DEBUGL(6)) {
1169 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
1170 color
, coord_x(coord
, board
), coord_y(coord
, board
),
1171 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
1174 struct move ko
= { pass
, S_NONE
};
1176 int captured_groups
= 0;
1178 foreach_neighbor(board
, coord
, {
1179 group_t g
= group_at(board
, c
);
1181 fprintf(stderr
, "board_check: group %d has %d libs\n",
1182 g
, board_group_info(board
, g
).libs
);
1183 captured_groups
+= (board_group_info(board
, g
).libs
== 1);
1186 if (likely(captured_groups
== 0)) {
1189 board_print(board
, stderr
);
1190 fprintf(stderr
, "board_check: one-stone suicide\n");
1196 /* We _will_ for sure capture something. */
1197 assert(trait_at(board
, coord
, color
).cap
> 0);
1198 assert(trait_at(board
, coord
, color
).safe
== board_trait_safe(board
, coord
, color
));
1201 board
->f
[f
] = board
->f
[--board
->flen
];
1203 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
1205 foreach_neighbor(board
, coord
, {
1206 inc_neighbor_count_at(board
, c
, color
);
1207 /* Originally, this could not have changed any trait
1208 * since no neighbors were S_NONE, however by now some
1209 * of them might be removed from the board. */
1210 board_trait_queue(board
, c
);
1212 group_t group
= group_at(board
, c
);
1216 board_group_rmlib(board
, group
, coord
);
1218 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
1221 if (board_group_captured(board
, group
)) {
1222 if (board_group_capture(board
, group
) == 1) {
1223 /* If we captured multiple groups at once,
1224 * we can't be fighting ko so we don't need
1225 * to check for that. */
1226 ko
.color
= stone_other(color
);
1228 board
->last_ko
= ko
;
1229 board
->last_ko_age
= board
->moves
;
1231 fprintf(stderr
, "guarding ko at %d,%s\n", ko
.color
, coord2sstr(ko
.coord
, board
));
1236 board_at(board
, coord
) = color
;
1237 group_t group
= new_group(board
, coord
);
1239 board
->last_move2
= board
->last_move
;
1240 board
->last_move
= *m
;
1242 board_hash_update(board
, coord
, color
);
1243 board_hash_commit(board
);
1244 board_traits_recompute(board
);
1245 board_symmetry_update(board
, &board
->symmetry
, coord
);
1248 check_pat3_consistency(board
, coord
);
1253 static int __attribute__((flatten
))
1254 board_play_f(struct board
*board
, struct move
*m
, int f
)
1257 fprintf(stderr
, "board_play(%s): ---- Playing %d,%d\n", coord2sstr(m
->coord
, board
), coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
1259 if (likely(!board_is_eyelike(board
, m
->coord
, stone_other(m
->color
)))) {
1260 /* NOT playing in an eye. Thus this move has to succeed. (This
1261 * is thanks to New Zealand rules. Otherwise, multi-stone
1262 * suicide might fail.) */
1263 group_t group
= board_play_outside(board
, m
, f
);
1264 if (unlikely(board_group_captured(board
, group
))) {
1265 board_group_capture(board
, group
);
1267 board_hash_commit(board
);
1268 board_traits_recompute(board
);
1271 return board_play_in_eye(board
, m
, f
);
1276 board_play(struct board
*board
, struct move
*m
)
1278 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
1279 struct move nomove
= { pass
, S_NONE
};
1281 board
->last_move2
= board
->last_move
;
1282 board
->last_move
= *m
;
1287 for (f
= 0; f
< board
->flen
; f
++)
1288 if (board
->f
[f
] == m
->coord
)
1289 return board_play_f(board
, m
, f
);
1292 fprintf(stderr
, "board_check: stone exists\n");
1298 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
, ppr_permit permit
, void *permit_data
)
1301 struct move m
= { *coord
, color
};
1303 fprintf(stderr
, "trying random move %d: %d,%d %s %d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
), coord2sstr(*coord
, b
), board_is_valid_move(b
, &m
));
1304 if (unlikely(board_is_one_point_eye(b
, *coord
, color
)) /* bad idea to play into one, usually */
1305 || !board_is_valid_move(b
, &m
)
1306 || (permit
&& !permit(permit_data
, b
, &m
)))
1308 if (m
.coord
== *coord
) {
1309 return likely(board_play_f(b
, &m
, f
) >= 0);
1311 *coord
= m
.coord
; // permit modified the coordinate
1312 return likely(board_play(b
, &m
) >= 0);
1317 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
)
1319 if (unlikely(b
->flen
== 0))
1322 int base
= fast_random(b
->flen
), f
;
1323 for (f
= base
; f
< b
->flen
; f
++)
1324 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1326 for (f
= 0; f
< base
; f
++)
1327 if (board_try_random_move(b
, color
, coord
, f
, permit
, permit_data
))
1332 struct move m
= { pass
, color
};
1338 board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
1340 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
1342 /* XXX: We attempt false eye detection but we will yield false
1343 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
1345 foreach_diag_neighbor(board
, coord
) {
1346 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
1347 } foreach_diag_neighbor_end
;
1348 /* For false eye, we need two enemy stones diagonally in the
1349 * middle of the board, or just one enemy stone at the edge
1350 * or in the corner. */
1351 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
1352 return color_diag_libs
[stone_other(eye_color
)] >= 2;
1356 board_is_one_point_eye(struct board
*board
, coord_t coord
, enum stone eye_color
)
1358 return board_is_eyelike(board
, coord
, eye_color
)
1359 && !board_is_false_eyelike(board
, coord
, eye_color
);
1363 board_get_one_point_eye(struct board
*board
, coord_t coord
)
1365 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
1367 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
1375 board_fast_score(struct board
*board
)
1378 memset(scores
, 0, sizeof(scores
));
1380 foreach_point(board
) {
1381 enum stone color
= board_at(board
, c
);
1382 if (color
== S_NONE
)
1383 color
= board_get_one_point_eye(board
, c
);
1385 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
1386 } foreach_point_end
;
1388 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
1391 /* Owner map: 0: undecided; 1: black; 2: white; 3: dame */
1393 /* One flood-fill iteration; returns true if next iteration
1396 board_tromp_taylor_iter(struct board
*board
, int *ownermap
)
1398 bool needs_update
= false;
1399 foreach_free_point(board
) {
1400 /* Ignore occupied and already-dame positions. */
1401 assert(board_at(board
, c
) == S_NONE
);
1402 if (ownermap
[c
] == 3)
1404 /* Count neighbors. */
1406 foreach_neighbor(board
, c
, {
1409 /* If we have neighbors of both colors, or dame,
1410 * we are dame too. */
1411 if ((nei
[1] && nei
[2]) || nei
[3]) {
1413 /* Speed up the propagation. */
1414 foreach_neighbor(board
, c
, {
1415 if (board_at(board
, c
) == S_NONE
)
1418 needs_update
= true;
1421 /* If we have neighbors of one color, we are owned
1422 * by that color, too. */
1423 if (!ownermap
[c
] && (nei
[1] || nei
[2])) {
1424 int newowner
= nei
[1] ? 1 : 2;
1425 ownermap
[c
] = newowner
;
1426 /* Speed up the propagation. */
1427 foreach_neighbor(board
, c
, {
1428 if (board_at(board
, c
) == S_NONE
&& !ownermap
[c
])
1429 ownermap
[c
] = newowner
;
1431 needs_update
= true;
1434 } foreach_free_point_end
;
1435 return needs_update
;
1438 /* Tromp-Taylor Counting */
1440 board_official_score(struct board
*board
, struct move_queue
*q
)
1443 /* A point P, not colored C, is said to reach C, if there is a path of
1444 * (vertically or horizontally) adjacent points of P's color from P to
1445 * a point of color C.
1447 * A player's score is the number of points of her color, plus the
1448 * number of empty points that reach only her color. */
1450 int ownermap
[board_size2(board
)];
1452 const int o
[4] = {0, 1, 2, 0};
1453 foreach_point(board
) {
1454 ownermap
[c
] = o
[board_at(board
, c
)];
1455 s
[board_at(board
, c
)]++;
1456 } foreach_point_end
;
1459 /* Process dead groups. */
1460 for (unsigned int i
= 0; i
< q
->moves
; i
++) {
1461 foreach_in_group(board
, q
->move
[i
]) {
1462 enum stone color
= board_at(board
, c
);
1463 ownermap
[c
] = o
[stone_other(color
)];
1464 s
[color
]--; s
[stone_other(color
)]++;
1465 } foreach_in_group_end
;
1469 /* We need to special-case empty board. */
1470 if (!s
[S_BLACK
] && !s
[S_WHITE
])
1471 return board
->komi
+ board
->handicap
;
1473 while (board_tromp_taylor_iter(board
, ownermap
))
1474 /* Flood-fill... */;
1477 memset(scores
, 0, sizeof(scores
));
1479 foreach_point(board
) {
1480 assert(board_at(board
, c
) == S_OFFBOARD
|| ownermap
[c
] != 0);
1481 if (ownermap
[c
] == 3)
1483 scores
[ownermap
[c
]]++;
1484 } foreach_point_end
;
1486 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];