10 int board_group_capture(struct board
*board
, int group
);
12 bool random_pass
= false;
16 #define profiling_noinline __attribute__((noinline))
18 #define profiling_noinline
21 #define gi_granularity 4
22 #define gi_allocsize(gids) ((1 << gi_granularity) + ((gids) >> gi_granularity) * (1 << gi_granularity))
26 board_setup(struct board
*b
)
28 memset(b
, 0, sizeof(*b
));
30 struct move m
= { pass
, S_NONE
};
31 b
->last_move
= b
->ko
= m
;
37 struct board
*b
= malloc(sizeof(struct board
));
43 board_copy(struct board
*b2
, struct board
*b1
)
45 memcpy(b2
, b1
, sizeof(struct board
));
47 int bsize
= b2
->size2
* sizeof(*b2
->b
);
48 int gsize
= b2
->size2
* sizeof(*b2
->g
);
49 int fsize
= b2
->size2
* sizeof(*b2
->f
);
50 int nsize
= b2
->size2
* sizeof(*b2
->n
);
51 int psize
= b2
->size2
* sizeof(*b2
->p
);
52 int hsize
= b2
->size2
* 2 * sizeof(*b2
->h
);
53 int lsize
= b2
->size2
* sizeof(*b2
->l
);
54 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ lsize
);
55 memcpy(x
, b1
->b
, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ lsize
);
56 b2
->b
= x
; x
+= bsize
;
57 b2
->g
= x
; x
+= gsize
;
58 b2
->f
= x
; x
+= fsize
;
59 b2
->p
= x
; x
+= psize
;
60 b2
->n
= x
; x
+= nsize
;
61 b2
->h
= x
; x
+= hsize
;
62 b2
->l
= x
; x
+= lsize
;
68 board_done_noalloc(struct board
*board
)
70 if (board
->b
) free(board
->b
);
74 board_done(struct board
*board
)
76 board_done_noalloc(board
);
81 board_resize(struct board
*board
, int size
)
83 board
->size
= size
+ 2 /* S_OFFBOARD margin */;
84 board
->size2
= board
->size
* board
->size
;
88 int bsize
= board
->size2
* sizeof(*board
->b
);
89 int gsize
= board
->size2
* sizeof(*board
->g
);
90 int fsize
= board
->size2
* sizeof(*board
->f
);
91 int nsize
= board
->size2
* sizeof(*board
->n
);
92 int psize
= board
->size2
* sizeof(*board
->p
);
93 int hsize
= board
->size2
* 2 * sizeof(*board
->h
);
94 int lsize
= board
->size2
* sizeof(*board
->l
);
95 void *x
= malloc(bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ lsize
);
96 memset(x
, 0, bsize
+ gsize
+ fsize
+ psize
+ nsize
+ hsize
+ lsize
);
97 board
->b
= x
; x
+= bsize
;
98 board
->g
= x
; x
+= gsize
;
99 board
->f
= x
; x
+= fsize
;
100 board
->p
= x
; x
+= psize
;
101 board
->n
= x
; x
+= nsize
;
102 board
->h
= x
; x
+= hsize
;
103 board
->l
= x
; x
+= lsize
;
107 board_clear(struct board
*board
)
109 int size
= board
->size
;
111 board_done_noalloc(board
);
113 board_resize(board
, size
- 2 /* S_OFFBOARD margin */);
115 /* Draw the offboard margin */
116 int top_row
= board
->size2
- board
->size
;
118 for (i
= 0; i
< board
->size
; i
++)
119 board
->b
[i
] = board
->b
[top_row
+ i
] = S_OFFBOARD
;
120 for (i
= 0; i
<= top_row
; i
+= board
->size
)
121 board
->b
[i
] = board
->b
[board
->size
- 1 + i
] = S_OFFBOARD
;
123 foreach_point(board
) {
125 if (board_at(board
, coord
) == S_OFFBOARD
)
127 foreach_neighbor(board
, c
, {
128 inc_neighbor_count_at(board
, coord
, board_at(board
, c
));
132 /* First, pass is always a free position. */
133 board
->f
[board
->flen
++] = coord_raw(pass
);
134 /* All positions are free! Except the margin. */
135 for (i
= board
->size
; i
< (board
->size
- 1) * board
->size
; i
++)
136 if (i
% board
->size
!= 0 && i
% board
->size
!= board
->size
- 1)
137 board
->f
[board
->flen
++] = i
;
139 /* Initialize zobrist hashtable. */
140 foreach_point(board
) {
141 int max
= (sizeof(hash_t
) << history_hash_bits
);
142 /* fast_random() is 16-bit only */
143 board
->h
[coord_raw(c
) * 2] = ((hash_t
) fast_random(max
))
144 | ((hash_t
) fast_random(max
) << 16)
145 | ((hash_t
) fast_random(max
) << 32)
146 | ((hash_t
) fast_random(max
) << 48);
147 if (!board
->h
[coord_raw(c
) * 2])
148 /* Would be kinda "oops". */
149 board
->h
[coord_raw(c
) * 2] = 1;
150 /* And once again for white */
151 board
->h
[coord_raw(c
) * 2 + 1] = ((hash_t
) fast_random(max
))
152 | ((hash_t
) fast_random(max
) << 16)
153 | ((hash_t
) fast_random(max
) << 32)
154 | ((hash_t
) fast_random(max
) << 48);
155 if (!board
->h
[coord_raw(c
) * 2 + 1])
156 board
->h
[coord_raw(c
) * 2 + 1] = 1;
162 board_print(struct board
*board
, FILE *f
)
164 fprintf(f
, "Move: % 3d Komi: %2.1f Captures B: %d W: %d\n ",
165 board
->moves
, board
->komi
,
166 board
->captures
[S_BLACK
], board
->captures
[S_WHITE
]);
168 char asdf
[] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
169 for (x
= 1; x
< board
->size
- 1; x
++)
170 fprintf(f
, "%c ", asdf
[x
- 1]);
172 for (x
= 1; x
< board
->size
- 1; x
++)
175 for (y
= board
->size
- 2; y
>= 1; y
--) {
176 fprintf(f
, "%2d | ", y
);
177 for (x
= 1; x
< board
->size
- 1; x
++) {
178 if (coord_x(board
->last_move
.coord
, board
) == x
&& coord_y(board
->last_move
.coord
, board
) == y
)
179 fprintf(f
, "%c)", stone2char(board_atxy(board
, x
, y
)));
181 fprintf(f
, "%c ", stone2char(board_atxy(board
, x
, y
)));
185 for (x
= 1; x
< board
->size
- 1; x
++) {
186 fprintf(f
, "%d ", group_atxy(board
, x
, y
));
192 for (x
= 1; x
< board
->size
- 1; x
++)
198 /* Update board hash with given coordinate. */
199 static void profiling_noinline
200 board_hash_update(struct board
*board
, coord_t coord
, enum stone color
)
202 board
->hash
^= board
->h
[(color
== S_BLACK
? board
->size2
: 0) + coord_raw(coord
)];
204 fprintf(stderr
, "board_hash_update(%d,%d,%d) ^ %llx -> %llx\n", color
, coord_x(coord
, board
), coord_y(coord
, board
), board
->h
[color
* coord_raw(coord
)], board
->hash
);
207 /* Commit current board hash to history. */
208 static void profiling_noinline
209 board_hash_commit(struct board
*board
)
212 fprintf(stderr
, "board_hash_commit %llx\n", board
->hash
);
213 if (likely(board
->history_hash
[board
->hash
& history_hash_mask
]) == 0) {
214 board
->history_hash
[board
->hash
& history_hash_mask
] = board
->hash
;
216 hash_t i
= board
->hash
;
217 while (board
->history_hash
[i
& history_hash_mask
]) {
218 if (board
->history_hash
[i
& history_hash_mask
] == board
->hash
) {
220 fprintf(stderr
, "SUPERKO VIOLATION noted at %d,%d\n",
221 coord_x(board
->last_move
.coord
, board
), coord_y(board
->last_move
.coord
, board
));
222 board
->superko_violation
= true;
227 board
->history_hash
[i
& history_hash_mask
] = board
->hash
;
233 board_handicap_stone(struct board
*board
, int x
, int y
, FILE *f
)
237 coord_xy(m
.coord
, x
, y
, board
);
239 board_play(board
, &m
);
241 char *str
= coord2str(m
.coord
, board
);
243 fprintf(stderr
, "choosing handicap %s (%d,%d)\n", str
, x
, y
);
244 fprintf(f
, "%s ", str
);
249 board_handicap(struct board
*board
, int stones
, FILE *f
)
251 int margin
= 3 + (board
->size
>= 13);
253 int mid
= board
->size
/ 2;
254 int max
= board
->size
- 1 - margin
;
255 const int places
[][2] = {
256 { min
, min
}, { max
, max
}, { max
, min
}, { min
, max
},
257 { min
, mid
}, { max
, mid
},
258 { mid
, min
}, { mid
, max
},
262 board
->handicap
= stones
;
264 if (stones
== 5 || stones
== 7) {
265 board_handicap_stone(board
, mid
, mid
, f
);
270 for (i
= 0; i
< stones
; i
++)
271 board_handicap_stone(board
, places
[i
][0], places
[i
][1], f
);
275 /* This is a low-level routine that doesn't maintain consistency
276 * of all the board data structures. Use board_group_capture() from
279 board_remove_stone(struct board
*board
, coord_t c
)
281 enum stone color
= board_at(board
, c
);
282 board_at(board
, c
) = S_NONE
;
283 group_at(board
, c
) = 0;
284 board_hash_update(board
, c
, color
);
286 /* Increase liberties of surrounding groups */
288 foreach_neighbor(board
, coord
, {
289 dec_neighbor_count_at(board
, c
, color
);
290 board_group_libs(board
, group_at(board
, c
))++;
294 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
295 board
->f
[board
->flen
++] = coord_raw(c
);
299 static void profiling_noinline
300 add_to_group(struct board
*board
, int gid
, coord_t prevstone
, coord_t coord
)
302 board_group_libs(board
, gid
) += immediate_liberty_count(board
, coord
);
304 group_at(board
, coord
) = gid
;
305 groupnext_at(board
, coord
) = groupnext_at(board
, prevstone
);
306 groupnext_at(board
, prevstone
) = coord_raw(coord
);
309 fprintf(stderr
, "add_to_group: added (%d,%d ->) %d,%d (-> %d,%d) to group %d - libs %d\n",
310 coord_x(prevstone
, board
), coord_y(prevstone
, board
),
311 coord_x(coord
, board
), coord_y(coord
, board
),
312 groupnext_at(board
, coord
) % board
->size
, groupnext_at(board
, coord
) / board
->size
,
313 gid
, board_group_libs(board
, gid
));
316 static void profiling_noinline
317 merge_groups(struct board
*board
, group_t group_to
, group_t group_from
)
320 fprintf(stderr
, "board_play_raw: merging groups %d(%d) -> %d(%d)\n",
321 group_from
, board_group_libs(board
, group_from
),
322 group_to
, board_group_libs(board
, group_to
));
324 coord_t last_in_group
;
325 foreach_in_group(board
, group_from
) {
327 group_at(board
, c
) = group_to
;
328 } foreach_in_group_end
;
329 groupnext_at(board
, last_in_group
) = groupnext_at(board
, group_to
);
330 groupnext_at(board
, group_to
) = group_from
;
332 board_group_libs(board
, group_to
) += board_group_libs(board
, group_from
);
335 fprintf(stderr
, "board_play_raw: merged group: %d(%d)\n",
336 group_to
, board_group_libs(board
, group_to
));
339 static group_t profiling_noinline
340 new_group(struct board
*board
, coord_t coord
)
342 group_t gid
= coord_raw(coord
);
343 board_group_libs(board
, gid
) = immediate_liberty_count(board
, coord
);
345 group_at(board
, coord
) = gid
;
346 groupnext_at(board
, coord
) = 0;
349 fprintf(stderr
, "new_group: added %d,%d to group %d - libs %d\n",
350 coord_x(coord
, board
), coord_y(coord
, board
),
351 gid
, board_group_libs(board
, gid
));
356 /* We played on a place with at least one liberty. We will become a member of
357 * some group for sure. */
358 static int profiling_noinline
359 board_play_outside(struct board
*board
, struct move
*m
, int f
)
361 coord_t coord
= m
->coord
;
362 enum stone color
= m
->color
;
363 enum stone other_color
= stone_other(color
);
366 board
->f
[f
] = board
->f
[--board
->flen
];
368 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
369 board_at(board
, coord
) = color
;
371 foreach_neighbor(board
, coord
, {
372 enum stone ncolor
= board_at(board
, c
);
373 group_t ngroup
= group_at(board
, c
);
375 inc_neighbor_count_at(board
, c
, color
);
380 board_group_libs(board
, ngroup
)--;
382 fprintf(stderr
, "board_play_raw: reducing libs for group %d: libs %d\n",
383 ngroup
, board_group_libs(board
, ngroup
));
385 if (ncolor
== color
&& ngroup
!= gid
) {
388 add_to_group(board
, gid
, c
, coord
);
390 merge_groups(board
, gid
, ngroup
);
392 } else if (ncolor
== other_color
) {
393 if (unlikely(board_group_captured(board
, ngroup
)))
394 board_group_capture(board
, ngroup
);
398 if (unlikely(gid
<= 0))
399 gid
= new_group(board
, coord
);
401 board
->last_move
= *m
;
403 board_hash_update(board
, coord
, color
);
404 struct move ko
= { pass
, S_NONE
};
410 /* We played in an eye-like shape. Either we capture at least one of the eye
411 * sides in the process of playing, or return -1. */
412 static int profiling_noinline
413 board_play_in_eye(struct board
*board
, struct move
*m
, int f
)
415 coord_t coord
= m
->coord
;
416 enum stone color
= m
->color
;
417 /* Check ko: Capture at a position of ko capture one move ago */
418 if (unlikely(color
== board
->ko
.color
&& coord_eq(coord
, board
->ko
.coord
))) {
420 fprintf(stderr
, "board_check: ko at %d,%d color %d\n", coord_x(coord
, board
), coord_y(coord
, board
), color
);
422 } else if (DEBUGL(6)) {
423 fprintf(stderr
, "board_check: no ko at %d,%d,%d - ko is %d,%d,%d\n",
424 color
, coord_x(coord
, board
), coord_y(coord
, board
),
425 board
->ko
.color
, coord_x(board
->ko
.coord
, board
), coord_y(board
->ko
.coord
, board
));
428 struct move ko
= { pass
, S_NONE
};
430 board
->f
[f
] = board
->f
[--board
->flen
];
432 fprintf(stderr
, "popping free move [%d->%d]: %d\n", board
->flen
, f
, board
->f
[f
]);
434 int captured_groups
= 0;
436 foreach_neighbor(board
, coord
, {
437 group_t group
= group_at(board
, c
);
441 board_group_libs(board
, group
)--;
443 fprintf(stderr
, "board_play_raw: reducing libs for group %d: libs %d\n",
444 group
, board_group_libs(board
, group
));
446 if (unlikely(board_group_captured(board
, group
))) {
448 if (board_group_capture(board
, group
) == 1) {
449 /* If we captured multiple groups at once,
450 * we can't be fighting ko so we don't need
451 * to check for that. */
452 ko
.color
= stone_other(color
);
455 fprintf(stderr
, "guarding ko at %d,%d,%d\n", ko
.color
, coord_x(ko
.coord
, board
), coord_y(ko
.coord
, board
));
460 if (likely(captured_groups
== 0)) {
463 board_print(board
, stderr
);
464 fprintf(stderr
, "board_check: one-stone suicide\n");
467 foreach_neighbor(board
, coord
, {
468 board_group_libs(board
, group_at(board
, c
))++;
470 fprintf(stderr
, "board_play_raw: restoring libs for group %d: libs %d\n",
471 group_at(board
, c
), board_group_libs(board
, group_at(board
, c
)));
476 fprintf(stderr
, "pushing free move [%d]: %d,%d\n", board
->flen
, coord_x(c
, board
), coord_y(c
, board
));
477 board
->f
[board
->flen
++] = coord_raw(c
);
481 foreach_neighbor(board
, coord
, {
482 inc_neighbor_count_at(board
, c
, color
);
485 board_at(board
, coord
) = color
;
487 board
->last_move
= *m
;
489 board_hash_update(board
, coord
, color
);
490 board_hash_commit(board
);
493 return new_group(board
, coord
);
496 static int __attribute__((flatten
))
497 board_play_f(struct board
*board
, struct move
*m
, int f
)
500 fprintf(stderr
, "board_play(): ---- Playing %d,%d\n", coord_x(m
->coord
, board
), coord_y(m
->coord
, board
));
502 if (likely(!board_is_eyelike(board
, &m
->coord
, stone_other(m
->color
)))) {
503 /* NOT playing in an eye. Thus this move has to succeed. (This
504 * is thanks to New Zealand rules. Otherwise, multi-stone
505 * suicide might fail.) */
506 int gid
= board_play_outside(board
, m
, f
);
507 if (unlikely(board_group_captured(board
, gid
))) {
508 board_group_capture(board
, gid
);
510 board_hash_commit(board
);
513 return board_play_in_eye(board
, m
, f
);
518 board_play(struct board
*board
, struct move
*m
)
520 if (unlikely(is_pass(m
->coord
) || is_resign(m
->coord
)))
524 for (f
= 0; f
< board
->flen
; f
++)
525 if (board
->f
[f
] == coord_raw(m
->coord
))
526 return board_play_f(board
, m
, f
);
529 fprintf(stderr
, "board_check: stone exists\n");
535 board_try_random_move(struct board
*b
, enum stone color
, coord_t
*coord
, int f
)
537 coord_raw(*coord
) = b
->f
[f
];
540 struct move m
= { *coord
, color
};
542 fprintf(stderr
, "trying random move %d: %d,%d\n", f
, coord_x(*coord
, b
), coord_y(*coord
, b
));
543 return (!board_is_one_point_eye(b
, coord
, color
) /* bad idea to play into one, usually */
544 && board_play_f(b
, &m
, f
) >= 0);
548 board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
)
550 int base
= fast_random(b
->flen
);
551 coord_pos(*coord
, base
, b
);
552 if (likely(board_try_random_move(b
, color
, coord
, base
)))
556 for (f
= base
+ 1; f
< b
->flen
; f
++)
557 if (board_try_random_move(b
, color
, coord
, f
))
559 for (f
= 0; f
< base
; f
++)
560 if (board_try_random_move(b
, color
, coord
, f
))
568 board_is_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
570 return (neighbor_count_at(board
, *coord
, eye_color
) + neighbor_count_at(board
, *coord
, S_OFFBOARD
)) == 4;
574 board_is_one_point_eye(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
576 enum stone color_diag_libs
[S_MAX
] = {0, 0, 0, 0};
578 if (likely(neighbor_count_at(board
, *coord
, eye_color
) + neighbor_count_at(board
, *coord
, S_OFFBOARD
) < 4)) {
582 /* XXX: We attempt false eye detection but we will yield false
583 * positives in case of http://senseis.xmp.net/?TwoHeadedDragon :-( */
585 foreach_diag_neighbor(board
, *coord
) {
586 color_diag_libs
[(enum stone
) board_at(board
, c
)]++;
587 } foreach_diag_neighbor_end
;
588 color_diag_libs
[stone_other(eye_color
)] += !!color_diag_libs
[S_OFFBOARD
];
589 return likely(color_diag_libs
[stone_other(eye_color
)] < 2);
593 board_get_one_point_eye(struct board
*board
, coord_t
*coord
)
595 if (board_is_one_point_eye(board
, coord
, S_WHITE
))
597 else if (board_is_one_point_eye(board
, coord
, S_BLACK
))
604 int profiling_noinline
605 board_group_capture(struct board
*board
, int group
)
609 foreach_in_group(board
, group
) {
610 board
->captures
[stone_other(board_at(board
, c
))]++;
611 board_remove_stone(board
, c
);
613 } foreach_in_group_end
;
619 board_group_in_atari(struct board
*board
, group_t group
, coord_t
*lastlib
)
621 /* First rule out obvious fakes. */
622 if (!group
|| board_group_libs(board
, group
) > 4)
624 coord_t base_stone
= group
;
625 if (immediate_liberty_count(board
, base_stone
) > 1)
630 bool watermark
[board
->size2
];
631 memset(watermark
, 0, sizeof(watermark
));
633 foreach_in_group(board
, group
) {
635 foreach_neighbor(board
, coord
, {
636 if (likely(watermark
[coord_raw(c
)]))
638 watermark
[coord_raw(c
)] = true;
639 if (unlikely(board_at(board
, c
) == S_NONE
)) {
641 if (unlikely(libs
> 1))
646 } foreach_in_group_end
;
652 /* Chinese counting */
654 board_official_score(struct board
*board
)
657 memset(scores
, 0, sizeof(scores
));
659 enum { GC_DUNNO
, GC_ALIVE
, GC_DEAD
} gcache
[board
->size
* board
->size
+ 1];
660 memset(gcache
, 0, sizeof(gcache
));
662 foreach_point(board
) {
663 enum stone color
= board_at(board
, c
);
664 group_t g
= group_at(board
, c
);
666 /* There is a complication: There can be some dead
667 * stones that could not have been removed because
668 * they are in enemy territory and we can't suicide.
669 * At least we know they are in atari. */
670 if (gcache
[g
] == GC_DUNNO
) {
672 gcache
[g
] = board_group_in_atari(board
, g
, &x
) == 1 ? GC_DEAD
: GC_ALIVE
;
674 if (gcache
[g
] == GC_ALIVE
)
677 scores
[stone_other(color
)]++;
678 /* XXX: But we still miss the one empty opponent's point. */
680 } else if (color
== S_NONE
) {
681 /* TODO: Count multi-point eyes */
682 color
= board_get_one_point_eye(board
, &c
);
687 return board
->komi
+ scores
[S_WHITE
] - scores
[S_BLACK
];
691 board_fast_score(struct board
*board
)
694 memset(scores
, 0, sizeof(scores
));
696 foreach_point(board
) {
697 enum stone color
= board_at(board
, c
);
699 color
= board_get_one_point_eye(board
, &c
);
701 // fprintf(stderr, "%d, %d ++%d = %d\n", coord_x(c, board), coord_y(c, board), color, scores[color]);
704 return board
->komi
+ board
->handicap
+ scores
[S_WHITE
] - scores
[S_BLACK
];