11 int board_group_capture(struct board
*board
, int group
);
13 bool random_pass
= false;
17 #define profiling_noinline __attribute__((noinline))
19 #define profiling_noinline
22 #define gi_granularity 4
23 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
27 board_setup(struct board
*b
)
29 memset(b
, 0, sizeof(*b
));
31 struct move m
= { pass
, S_NONE
};
32 b
->last_move
= b
->ko
= m
;
38 struct board
*b
= malloc(sizeof(struct board
));
44 board_copy(struct board
*b2
, struct board
*b1
)
46 memcpy(b2
, b1
, sizeof(struct board
));
48 int bsize
= b2
->size2
* sizeof(*b2
->b
);
49 int gsize
= b2
->size2
* sizeof(*b2
->g
);
50 int fsize
= b2
->size2
* sizeof(*b2
->f
);
51 int nsize
= b2
->size2
* sizeof(*b2
->n
);
52 int psize
= b2
->size2
* sizeof(*b2
->p
);
53 int hsize
= b2
->size2
* 2 * sizeof(*b2
->h
);
54 int gisize
= b2
->size2
* sizeof(*b2
->gi
);
56 int csize
= b2
->size2
* sizeof(*b2
->c
);
60 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
);
61 memcpy(x
, b1
->b
, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
);
62 b2
->b
= x
; x
+= bsize
;
63 b2
->g
= x
; x
+= gsize
;
64 b2
->f
= x
; x
+= fsize
;
65 b2
->p
= x
; x
+= psize
;
66 b2
->n
= x
; x
+= nsize
;
67 b2
->h
= x
; x
+= hsize
;
68 b2
->gi
= x
; x
+= gisize
;
70 b2
->c
= x
; x
+= csize
;
77 board_done_noalloc(struct board
*board
)
79 if (board
->b
) free(board
->b
);
83 board_done(struct board
*board
)
85 board_done_noalloc(board
);
90 board_resize(struct board
*board
, int size
)
92 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
93 board
->size2
= board
->size
* board
->size
;
97 int bsize
= board
->size2
* sizeof(*board
->b
);
98 int gsize
= board
->size2
* sizeof(*board
->g
);
99 int fsize
= board
->size2
* sizeof(*board
->f
);
100 int nsize
= board
->size2
* sizeof(*board
->n
);
101 int psize
= board
->size2
* sizeof(*board
->p
);
102 int hsize
= board
->size2
* 2 * sizeof(*board
->h
);
103 int gisize
= board
->size2
* sizeof(*board
->gi
);
105 int csize
= board
->size2
* sizeof(*board
->c
);
109 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
);
110 memset(x
, 0, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ gisize
+ csize
);
111 board
->b
= x
; x
+= bsize
;
112 board
->g
= x
; x
+= gsize
;
113 board
->f
= x
; x
+= fsize
;
114 board
->p
= x
; x
+= psize
;
115 board
->n
= x
; x
+= nsize
;
116 board
->h
= x
; x
+= hsize
;
117 board
->gi
= x
; x
+= gisize
;
119 board
->c
= x
; x
+= csize
;
124 board_clear(struct board
*board
)
126 int size
= board
->size
;
128 board_done_noalloc(board
);
130 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
132 /* Draw the offboard margin */
133 int top_row
= board
->size2
- board
->size
;
135 for (i
= 0; i
< board
->size
; i
++)
136 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
137 for (i
= 0; i
<= top_row
; i
+= board
->size
)
138 board
->b
[i
] = board
->b
[board
->size
- 1 + i
] = S_OFFBOARD
;
140 foreach_point(board
) {
142 if (board_at(board
, coord
) == S_OFFBOARD
)
144 foreach_neighbor(board
, c
, {
145 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
149 /* First, pass is always a free position. */
150 board
->f
[board
->flen
++] = coord_raw(pass
);
151 /* All positions are free! Except the margin. */
152 for (i
= board
->size
; i
< (board
->size
- 1) * board
->size
; i
++)
153 if (i
% board
->size
!= 0 && i
% board
->size
!= board
->size
- 1)
154 board
->f
[board
->flen
++] = i
;
156 /* Initialize zobrist hashtable. */
157 foreach_point(board
) {
158 int max
= (sizeof(hash_t
) << history_hash_bits
);
159 /* fast_random() is 16-bit only */
160 board
->h
[coord_raw(c
) * 2] = ((hash_t
) fast_random(max
))
161 | ((hash_t
) fast_random(max
) << 16)
162 | ((hash_t
) fast_random(max
) << 32)
163 | ((hash_t
) fast_random(max
) << 48);
164 if (!board
->h
[coord_raw(c
) * 2])
165 /* Would be kinda "oops". */
166 board
->h
[coord_raw(c
) * 2] = 1;
167 /* And once again for white */
168 board
->h
[coord_raw(c
) * 2 + 1] = ((hash_t
) fast_random(max
))
169 | ((hash_t
) fast_random(max
) << 16)
170 | ((hash_t
) fast_random(max
) << 32)
171 | ((hash_t
) fast_random(max
) << 48);
172 if (!board
->h
[coord_raw(c
) * 2 + 1])
173 board
->h
[coord_raw(c
) * 2 + 1] = 1;
179 board_print(struct board
*board
, FILE *f
)
181 fprintf(f
, "Move: % 3d Komi: %2.1f Captures B: %d W: %d\n ",
182 board
->moves
, board
->komi
,
183 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
185 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
186 for (x
= 1; x
< board
->size
- 1; x
++)
187 fprintf(f
, "%c ", asdf
[x
- 1]);
189 for (x
= 1; x
< board
->size
- 1; x
++)
192 for (y
= board
->size
- 2; y
>= 1; y
--) {
193 fprintf(f
, "%2d | ", y
);
194 for (x
= 1; x
< board
->size
- 1; x
++) {
195 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
196 fprintf(f
, "%c)", stone2char(board_atxy(board
, x
, y
)));
198 fprintf(f
, "%c ", stone2char(board_atxy(board
, x
, y
)));
202 for (x
= 1; x
< board
->size
- 1; x
++) {
203 fprintf(f
, "%d ", group_atxy(board
, x
, y
));
209 for (x
= 1; x
< board
->size
- 1; x
++)
215 /* Update board hash with given coordinate. */
216 static void profiling_noinline
217 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
219 board
->hash
^= hash_at(board
, coord
, color
);
221 fprintf(stderr
, "board_hash_update(%d,%d,%d) ^ %llx -> %llx\n", color
, coord_x(coord
, board
), coord_y(coord
, board
), hash_at(board
, coord
, color
), board
->hash
);
224 /* Commit current board hash to history. */
225 static void profiling_noinline
226 board_hash_commit(struct board
*board
)
229 fprintf(stderr
, "board_hash_commit %llx\n", board
->hash
);
230 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
231 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
233 hash_t i
= board
->hash
;
234 while (board
->history_hash
[i
& history_hash_mask
]) {
235 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
237 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
238 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
239 board
->superko_violation
= true;
242 i
= history_hash_next(i
);
244 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
250 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
254 coord_xy(m
.coord
, x
, y
, board
);
256 board_play(board
, &m
);
258 char *str
= coord2str(m
.coord
, board
);
260 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
261 fprintf(f
, "%s ", str
);
266 board_handicap(struct board
*board
, int stones
, FILE *f
)
268 int margin
= 3 + (board
->size
>= 13);
270 int mid
= board
->size
/ 2;
271 int max
= board
->size
- 1 - margin
;
272 const int places
[][2] = {
273 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
274 { min
, mid
}, { max
, mid
},
275 { mid
, min
}, { mid
, max
},
279 board
->handicap
= stones
;
281 if (stones
== 5 || stones
== 7) {
282 board_handicap_stone(board
, mid
, mid
, f
);
287 for (i
= 0; i
< stones
; i
++)
288 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
292 static void __attribute__((noinline
))
293 check_libs_consistency(struct board
*board
, group_t g
)
297 struct group
*gi
= &board_group_info(board
, g
);
298 for (int i
= 0; i
< gi
->libs
; i
++)
299 if (board_at(board
, gi
->lib
[i
]) != S_NONE
) {
300 fprintf(stderr
, "BOGUS LIBERTY %s of group %d[%s]\n", coord2sstr(gi
->lib
[i
], board
), g
, coord2sstr(g
, board
));
307 board_capturable_add(struct board
*board
, group_t group
)
310 //fprintf(stderr, "add of group %d (%d)\n", group, board->clen);
311 assert(board
->clen
< board
->size2
);
312 board
->c
[board
->clen
++] = group
;
316 board_capturable_rm(struct board
*board
, group_t group
)
319 //fprintf(stderr, "rm of group %d\n", group);
320 for (int i
= 0; i
< board
->clen
; i
++) {
321 if (unlikely(board
->c
[i
] == group
)) {
322 board
->c
[i
] = board
->c
[--board
->clen
];
326 fprintf(stderr
, "rm of bad group %d\n", group
);
332 board_group_addlib(struct board
*board
, group_t group
, coord_t coord
)
335 fprintf(stderr
, "Group %d[%s]: Adding liberty %s\n",
336 group
, coord2sstr(group
, board
), coord2sstr(coord
, board
));
339 check_libs_consistency(board
, group
);
341 struct group
*gi
= &board_group_info(board
, group
);
342 if (gi
->libs
< GROUP_KEEP_LIBS
) {
343 for (int i
= 0; i
< gi
->libs
; i
++)
344 if (gi
->lib
[i
] == coord
)
347 board_capturable_add(board
, group
);
348 else if (gi
->libs
== 1)
349 board_capturable_rm(board
, group
);
350 gi
->lib
[gi
->libs
++] = coord
;
355 board_group_rmlib(struct board
*board
, group_t group
, coord_t coord
)
358 fprintf(stderr
, "Group %d[%s]: Removing liberty %s\n",
359 group
, coord2sstr(group
, board
), coord2sstr(coord
, board
));
362 struct group
*gi
= &board_group_info(board
, group
);
363 for (int i
= 0; i
< gi
->libs
; i
++) {
364 if (gi
->lib
[i
] == coord
) {
365 for (i
++; i
< gi
->libs
; i
++)
366 gi
->lib
[i
- 1] = gi
->lib
[i
];
369 check_libs_consistency(board
, group
);
370 if (gi
->libs
< GROUP_KEEP_LIBS
- 1) {
372 board_capturable_add(board
, group
);
373 else if (gi
->libs
== 0)
374 board_capturable_rm(board
, group
);
381 /* This is ok even if gi->libs < GROUP_KEEP_LIBS since we
382 * can call this multiple times per coord. */
383 check_libs_consistency(board
, group
);
386 /* Add extra liberty from the board to our liberty list. */
388 bool watermark
[board
->size2
];
389 memset(watermark
, 0, sizeof(watermark
));
391 foreach_in_group(board
, group
) {
393 foreach_neighbor(board
, coord2
, {
394 if (likely(watermark
[coord_raw(c
)]))
396 watermark
[coord_raw(c
)] = true;
397 if (c
!= coord
&& board_at(board
, c
) == S_NONE
) {
399 for (int i
= 0; i
< GROUP_KEEP_LIBS
- 1; i
++) {
400 if (gi
->lib
[i
] == c
) {
406 gi
->lib
[gi
->libs
++] = c
;
411 } foreach_in_group_end
;
415 /* This is a low-level routine that doesn't maintain consistency
416 * of all the board data structures. Use board_group_capture() from
419 board_remove_stone(struct board
*board
, coord_t c
)
421 enum stone color
= board_at(board
, c
);
422 board_at(board
, c
) = S_NONE
;
423 group_at(board
, c
) = 0;
424 board_hash_update(board
, c
, color
);
426 /* Increase liberties of surrounding groups */
428 foreach_neighbor(board
, coord
, {
429 dec_neighbor_count_at(board
, c
, color
);
430 board_group_addlib(board
, group_at(board
, c
), coord
);
434 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
435 board
->f
[board
->flen
++] = coord_raw(c
);
439 static void profiling_noinline
440 add_to_group(struct board
*board
, int gid
, coord_t prevstone
, coord_t coord
)
442 foreach_neighbor(board
, coord
, {
443 if (board_at(board
, c
) == S_NONE
)
444 board_group_addlib(board
, gid
, c
);
447 group_at(board
, coord
) = gid
;
448 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
449 groupnext_at(board
, prevstone
) = coord_raw(coord
);
452 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d\n",
453 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
454 coord_x(coord
, board
), coord_y(coord
, board
),
455 groupnext_at(board
, coord
) % board
->size
, groupnext_at(board
, coord
) / board
->size
,
459 static void profiling_noinline
460 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
463 fprintf(stderr
, "board_play_raw: merging groups %d -> %d\n",
464 group_from
, group_to
);
466 coord_t last_in_group
;
467 foreach_in_group(board
, group_from
) {
469 group_at(board
, c
) = group_to
;
470 } foreach_in_group_end
;
471 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_to
);
472 groupnext_at(board
, group_to
) = group_from
;
474 struct group
*gi_from
= &board_group_info(board
, group_from
);
475 struct group
*gi_to
= &board_group_info(board
, group_to
);
476 if (gi_to
->libs
< GROUP_KEEP_LIBS
) {
477 for (int i
= 0; i
< gi_from
->libs
; i
++) {
478 for (int j
= 0; j
< gi_to
->libs
; j
++)
479 if (gi_to
->lib
[j
] == gi_from
->lib
[i
])
481 if (gi_to
->libs
== 0)
482 board_capturable_add(board
, group_to
);
483 else if (gi_to
->libs
== 1)
484 board_capturable_rm(board
, group_to
);
485 gi_to
->lib
[gi_to
->libs
++] = gi_from
->lib
[i
];
486 if (gi_to
->libs
>= GROUP_KEEP_LIBS
)
492 if (gi_from
->libs
== 1)
493 board_capturable_rm(board
, group_from
);
494 memset(gi_from
, 0, sizeof(struct group
));
497 fprintf(stderr
, "board_play_raw: merged group: %d\n",
501 static group_t profiling_noinline
502 new_group(struct board
*board
, coord_t coord
)
504 group_t gid
= coord_raw(coord
);
505 foreach_neighbor(board
, coord
, {
506 if (board_at(board
, c
) == S_NONE
)
507 board_group_addlib(board
, gid
, c
);
510 group_at(board
, coord
) = gid
;
511 groupnext_at(board
, coord
) = 0;
514 fprintf(stderr
, "new_group: added %d,%d to group %d\n",
515 coord_x(coord
, board
), coord_y(coord
, board
),
521 /* We played on a place with at least one liberty. We will become a member of
522 * some group for sure. */
523 static int profiling_noinline
524 board_play_outside(struct board
*board
, struct move
*m
, int f
)
526 coord_t coord
= m
->coord
;
527 enum stone color
= m
->color
;
528 enum stone other_color
= stone_other(color
);
531 board
->f
[f
] = board
->f
[--board
->flen
];
533 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
535 foreach_neighbor(board
, coord
, {
536 enum stone ncolor
= board_at(board
, c
);
537 group_t ngroup
= group_at(board
, c
);
539 inc_neighbor_count_at(board
, c
, color
);
544 board_group_rmlib(board
, ngroup
, coord
);
546 fprintf(stderr
, "board_play_raw: reducing libs for group %d (%d:%d,%d)\n",
547 ngroup
, ncolor
, color
, other_color
);
549 if (ncolor
== color
&& ngroup
!= gid
) {
552 add_to_group(board
, gid
, c
, coord
);
554 merge_groups(board
, gid
, ngroup
);
556 } else if (ncolor
== other_color
) {
558 struct group
*gi
= &board_group_info(board
, ngroup
);
559 fprintf(stderr
, "testing captured group %d[%s]: ", ngroup
, coord2sstr(ngroup
, board
));
560 for (int i
= 0; i
< gi
->libs
; i
++)
561 fprintf(stderr
, "%s ", coord2sstr(gi
->lib
[i
], board
));
562 fprintf(stderr
, "\n");
564 if (unlikely(board_group_captured(board
, ngroup
)))
565 board_group_capture(board
, ngroup
);
569 if (unlikely(gid
<= 0))
570 gid
= new_group(board
, coord
);
572 board_at(board
, coord
) = color
;
573 board
->last_move
= *m
;
575 board_hash_update(board
, coord
, color
);
576 struct move ko
= { pass
, S_NONE
};
582 /* We played in an eye-like shape. Either we capture at least one of the eye
583 * sides in the process of playing, or return -1. */
584 static int profiling_noinline
585 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
587 coord_t coord
= m
->coord
;
588 enum stone color
= m
->color
;
589 /* Check ko: Capture at a position of ko capture one move ago */
590 if (unlikely(color
== board
->ko
.color
&& coord_eq(coord
, board
->ko
.coord
))) {
592 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
594 } else if (DEBUGL(6)) {
595 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
596 color
, coord_x(coord
, board
), coord_y(coord
, board
),
597 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
600 struct move ko
= { pass
, S_NONE
};
602 board
->f
[f
] = board
->f
[--board
->flen
];
604 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
606 int captured_groups
= 0;
608 foreach_neighbor(board
, coord
, {
609 group_t group
= group_at(board
, c
);
613 board_group_rmlib(board
, group
, coord
);
615 fprintf(stderr
, "board_play_raw: reducing libs for group %d\n",
618 if (unlikely(board_group_captured(board
, group
))) {
620 if (board_group_capture(board
, group
) == 1) {
621 /* If we captured multiple groups at once,
622 * we can't be fighting ko so we don't need
623 * to check for that. */
624 ko
.color
= stone_other(color
);
627 fprintf(stderr
, "guarding ko at %d,%d,%d\n", ko
.color
, coord_x(ko
.coord
, board
), coord_y(ko
.coord
, board
));
632 if (likely(captured_groups
== 0)) {
635 board_print(board
, stderr
);
636 fprintf(stderr
, "board_check: one-stone suicide\n");
639 foreach_neighbor(board
, coord
, {
640 board_group_addlib(board
, group_at(board
, c
), coord
);
642 fprintf(stderr
, "board_play_raw: restoring libs for group %d\n",
648 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
649 board
->f
[board
->flen
++] = coord_raw(c
);
653 foreach_neighbor(board
, coord
, {
654 inc_neighbor_count_at(board
, c
, color
);
657 board_at(board
, coord
) = color
;
659 board
->last_move
= *m
;
661 board_hash_update(board
, coord
, color
);
662 board_hash_commit(board
);
665 return new_group(board
, coord
);
668 static int __attribute__((flatten
))
669 board_play_f(struct board
*board
, struct move
*m
, int f
)
672 fprintf(stderr
, "board_play(): ---- Playing %d,%d\n", coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
674 if (likely(!board_is_eyelike(board
, &m
->coord
, stone_other(m
->color
)))) {
675 /* NOT playing in an eye. Thus this move has to succeed. (This
676 * is thanks to New Zealand rules. Otherwise, multi-stone
677 * suicide might fail.) */
678 int gid
= board_play_outside(board
, m
, f
);
679 if (unlikely(board_group_captured(board
, gid
))) {
680 board_group_capture(board
, gid
);
682 board_hash_commit(board
);
685 return board_play_in_eye(board
, m
, f
);
690 board_play(struct board
*board
, struct move
*m
)
692 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
))) {
693 board
->last_move
= *m
;
698 for (f
= 0; f
< board
->flen
; f
++)
699 if (board
->f
[f
] == coord_raw(m
->coord
))
700 return board_play_f(board
, m
, f
);
703 fprintf(stderr
, "board_check: stone exists\n");
709 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
)
711 coord_raw(*coord
) = b
->f
[f
];
714 struct move m
= { *coord
, color
};
716 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
717 return (!board_is_one_point_eye(b
, coord
, color
) /* bad idea to play into one, usually */
718 && board_play_f(b
, &m
, f
) >= 0);
722 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
)
724 int base
= fast_random(b
->flen
);
725 coord_pos(*coord
, base
, b
);
726 if (likely(board_try_random_move(b
, color
, coord
, base
)))
730 for (f
= base
+ 1; f
< b
->flen
; f
++)
731 if (board_try_random_move(b
, color
, coord
, f
))
733 for (f
= 0; f
< base
; f
++)
734 if (board_try_random_move(b
, color
, coord
, f
))
742 board_is_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
744 return (neighbor_count_at(board
, *coord
, eye_color
) + neighbor_count_at(board
, *coord
, S_OFFBOARD
)) == 4;
748 board_is_one_point_eye(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
750 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
752 if (likely(neighbor_count_at(board
, *coord
, eye_color
) + neighbor_count_at(board
, *coord
, S_OFFBOARD
) < 4)) {
756 /* XXX: We attempt false eye detection but we will yield false
757 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
759 foreach_diag_neighbor(board
, *coord
) {
760 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
761 } foreach_diag_neighbor_end
;
762 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
763 return likely(color_diag_libs
[stone_other(eye_color
)] < 2);
767 board_get_one_point_eye(struct board
*board
, coord_t
*coord
)
769 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
771 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
778 int profiling_noinline
779 board_group_capture(struct board
*board
, int group
)
783 foreach_in_group(board
, group
) {
784 board
->captures
[stone_other(board_at(board
, c
))]++;
785 board_remove_stone(board
, c
);
787 } foreach_in_group_end
;
789 if (board_group_info(board
, group
).libs
== 1)
790 board_capturable_rm(board
, group
);
791 memset(&board_group_info(board
, group
), 0, sizeof(struct group
));
797 board_group_in_atari(struct board
*board
, group_t group
, coord_t
*lastlib
)
799 if (board_group_info(board
, group
).libs
!= 1)
801 *lastlib
= board_group_info(board
, group
).lib
[0];
806 board_group_can_atari(struct board
*board
, group_t group
, coord_t lastlib
[2])
808 if (board_group_info(board
, group
).libs
!= 2)
810 lastlib
[0] = board_group_info(board
, group
).lib
[0];
811 lastlib
[1] = board_group_info(board
, group
).lib
[1];
817 board_tromp_taylor_owner(struct board
*board
, coord_t c
)
819 int x
= coord_x(c
, board
), y
= coord_y(c
, board
);
820 enum stone color
= S_NONE
;
821 #define TEST_REACH(xx, yy) \
823 enum stone c2 = board_atxy(board, xx, yy); \
824 if (c2 != S_NONE) { \
825 if (color != S_NONE && color != c2) \
831 for (int i
= x
; i
> 0; i
--)
833 for (int i
= x
; i
< board
->size
- 1; i
++)
835 for (int i
= y
; i
> 0; i
--)
837 for (int i
= y
; i
< board
->size
- 1; i
++)
842 /* Tromp-Taylor Counting */
844 board_official_score(struct board
*board
)
847 /* A point P, not colored C, is said to reach C, if there is a path of
848 * (vertically or horizontally) adjacent points of P's color from P to
849 * a point of color C.
851 * A player's score is the number of points of her color, plus the
852 * number of empty points that reach only her color. */
855 memset(scores
, 0, sizeof(scores
));
857 foreach_point(board
) {
858 enum stone color
= board_at(board
, c
);
860 color
= board_tromp_taylor_owner(board
, c
);
864 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
868 board_fast_score(struct board
*board
)
871 memset(scores
, 0, sizeof(scores
));
873 foreach_point(board
) {
874 enum stone color
= board_at(board
, c
);
876 color
= board_get_one_point_eye(board
, &c
);
878 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
881 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];
886 valid_escape_route(struct board
*b
, enum stone color
, coord_t to
)
888 /* Assess if we actually gain any liberties by this escape route.
889 * Note that this is not 100% as we cannot check whether we are
890 * connecting out or just to ourselves. */
891 /* Also, we prohibit 1-1 here:
898 int friends
= neighbor_count_at(b
, to
, color
);
899 int borders
= neighbor_count_at(b
, to
, S_OFFBOARD
);
900 int libs
= immediate_liberty_count(b
, to
);
901 return (friends
> 1 && friends
+ borders
< 4) || (libs
> 1);
906 board_stone_radar(struct board
*b
, coord_t coord
, int distance
)
909 coord_x(coord
, b
) - distance
,
910 coord_y(coord
, b
) - distance
,
911 coord_x(coord
, b
) + distance
,
912 coord_y(coord
, b
) + distance
914 for (int i
= 0; i
< 4; i
++)
917 else if (bounds
[i
] > b
->size
- 2)
918 bounds
[i
] = b
->size
- 2;
919 for (int x
= bounds
[0]; x
<= bounds
[2]; x
++)
920 for (int y
= bounds
[1]; y
<= bounds
[3]; y
++)
921 if (board_atxy(b
, x
, y
) != S_NONE
) {
922 //fprintf(stderr, "radar %d,%d,%d: %d,%d (%d)\n", coord_x(coord, b), coord_y(coord, b), distance, x, y, board_atxy(b, x, y));