1 /* probdist.h must be included before the include goard since we require
2 * proper including order. */
19 /* Maximum supported board size. (Without the S_OFFBOARD edges.) */
20 #define BOARD_MAX_SIZE 19
23 /* The board implementation has bunch of optional features.
24 * Turn them on below: */
26 #define WANT_BOARD_C // capturable groups queue
28 //#define BOARD_SIZE 9 // constant board size, allows better optimization
30 //#define BOARD_SPATHASH // incremental patternsp.h hashes
31 #define BOARD_SPATHASH_MAXD 3 // maximal diameter
33 #define BOARD_PAT3 // incremental 3x3 pattern codes
35 //#define BOARD_TRAITS 1 // incremental point traits (see struct btraits)
36 //#define BOARD_TRAIT_SAFE 1 // include btraits.safe (rather expensive, unused)
37 //#define BOARD_TRAIT_SAFE 2 // include btraits.safe based on full is_bad_selfatari()
40 #define BOARD_MAX_MOVES (BOARD_MAX_SIZE * BOARD_MAX_SIZE)
41 #define BOARD_MAX_GROUPS (BOARD_MAX_SIZE * BOARD_MAX_SIZE / 2)
44 /* Some engines might normalize their reading and skip symmetrical
45 * moves. We will tell them how can they do it. */
46 struct board_symmetry
{
47 /* Playground is in this rectangle. */
49 /* d == 0: Full rectangle
50 * d == 1: Top triangle */
52 /* General symmetry type. */
53 /* Note that the above is redundant to this, but just provided
54 * for easier usage. */
66 typedef uint64_t hash_t
;
67 #define PRIhash PRIx64
69 /* XXX: This really belongs in pattern3.h, unfortunately that would mean
70 * a dependency hell. */
71 typedef uint32_t hash3_t
; // 3x3 pattern hash
74 /* Note that "group" is only chain of stones that is solidly
75 * connected for us. */
76 typedef coord_t group_t
;
79 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
81 /* _Combination_ of these two values can make some difference
82 * in performance - fine-tune. */
83 #define GROUP_KEEP_LIBS 10
84 // refill lib[] only when we hit this; this must be at least 2!
85 // Moggy requires at least 3 - see below for semantic impact.
86 #define GROUP_REFILL_LIBS 5
87 coord_t lib
[GROUP_KEEP_LIBS
];
88 /* libs is only LOWER BOUND for the number of real liberties!!!
89 * It denotes only number of items in lib[], thus you can rely
90 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
94 struct neighbor_colors
{
99 /* Point traits bitmap; we update this information incrementally,
100 * it can be used e.g. for fast pattern features matching. */
102 /* Number of neighbors we can capture. 0=this move is
103 * not capturing, 1..4=this many neighbors we can capture
104 * (can be multiple neighbors of same group). */
106 /* Number of 1-stone neighbors we can capture. */
108 #ifdef BOARD_TRAIT_SAFE
109 /* Whether it is SAFE to play here. This is essentially just
110 * cached result of board_safe_to_play(). (Of course the concept
111 * of "safety" is not perfect here, but it's the cheapest
112 * reasonable thing we can do.) */
115 /* Whether we need to re-compute this coordinate; used to
116 * weed out duplicates. Maintained only for S_BLACK. */
121 /* You should treat this struct as read-only. Always call functions below if
122 * you want to change it. */
125 int size
; /* Including S_OFFBOARD margin - see below. */
126 int size2
; /* size^2 */
127 int bits2
; /* ceiling(log2(size2)) */
131 /* The ruleset is currently almost never taken into account;
132 * the board implementation is basically Chinese rules (handicap
133 * stones compensation) w/ suicide (or you can look at it as
134 * New Zealand w/o handi stones compensation), while the engine
135 * enforces no-suicide, making for real Chinese rules. */
137 RULES_CHINESE
, /* default value */
141 RULES_STONES_ONLY
, /* do not count eyes */
147 /* Iterator offsets for foreach_neighbor*() */
148 int nei8
[8], dnei
[4];
151 struct move last_move
;
152 struct move last_move2
; /* second-to-last move */
153 struct move last_move3
; /* just before last_move2, only set if last_move is pass */
154 struct move last_move4
; /* just before last_move3, only set if last_move & last_move2 are pass */
155 /* Whether we tried to add a hash twice; board_play*() can
156 * set this, but it will still carry out the move as well! */
157 bool superko_violation
;
159 /* The following two structures are goban maps and are indexed by
160 * coord.pos. The map is surrounded by a one-point margin from
161 * S_OFFBOARD stones in order to speed up some internal loops.
162 * Some of the foreach iterators below might include these points;
163 * you need to handle them yourselves, if you need to. */
165 /* Stones played on the board */
166 enum stone
*b
; /* enum stone */
167 /* Group id the stones are part of; 0 == no group */
169 /* Positions of next stones in the stone group; 0 == last stone */
171 /* Neighboring colors; numbers of neighbors of index color */
172 struct neighbor_colors
*n
;
173 /* Zobrist hash for each position */
175 #ifdef BOARD_SPATHASH
176 /* For spatial hashes, we use only 24 bits. */
177 /* [0] is d==1, we don't keep hash for d==0. */
178 /* We keep hashes for black-to-play ([][0]) and white-to-play
179 * ([][1], reversed stone colors since we match all patterns as
181 uint32_t (*spathash
)[BOARD_SPATHASH_MAXD
][2];
184 /* 3x3 pattern code for each position; see pattern3.h for encoding
185 * specification. The information is only valid for empty points. */
189 /* Incrementally matched point traits information, black-to-play
190 * ([][0]) and white-to-play ([][1]). */
191 /* The information is only valid for empty points. */
192 struct btraits (*t
)[2];
194 /* Cached information on x-y coordinates so that we avoid division. */
197 /* Group information - indexed by gid (which is coord of base group stone) */
200 /* Positions of free positions - queue (not map) */
201 /* Note that free position here is any valid move; including single-point eyes!
202 * However, pass is not included. */
203 coord_t
*f
; int flen
;
206 /* Queue of capturable groups */
207 group_t
*c
; int clen
;
211 /* Queue of positions that need their traits updated */
212 coord_t
*tq
; int tqlen
;
215 /* Symmetry information */
216 struct board_symmetry symmetry
;
218 /* Last ko played on the board. */
225 /* Engine-specific state; persistent through board development,
226 * is reset only at clear_board. */
229 /* Playout-specific state; persistent through board development,
230 * but its lifetime is maintained in play_random_game(); it should
231 * not be set outside of it. */
235 /* --- PRIVATE DATA --- */
237 /* For superko check: */
239 /* Board "history" - hashes encountered. Size of the hash should be
240 * >> board_size^2. */
241 #define history_hash_bits 12
242 #define history_hash_mask ((1 << history_hash_bits) - 1)
243 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
244 #define history_hash_next(i) ((i + 1) & history_hash_mask)
245 hash_t history_hash
[1 << history_hash_bits
];
246 /* Hash of current board position. */
248 /* Hash of current board position quadrants. */
253 /* Avoid unused variable warnings */
254 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
255 #define board_size2(b_) (board_size(b_) * board_size(b_))
257 #define board_size(b_) ((b_)->size)
258 #define board_size2(b_) ((b_)->size2)
261 /* This is a shortcut for taking different action on smaller
262 * and large boards (e.g. picking different variable defaults).
263 * This is of course less optimal than fine-tuning dependency
264 * function of values on board size, but that is difficult and
265 * possibly not very rewarding if you are interested just in
267 #define board_large(b_) (board_size(b_)-2 >= 15)
270 # define board_bits2(b_) 9
271 #elif BOARD_SIZE == 13
272 # define board_bits2(b_) 8
273 #elif BOARD_SIZE == 9
274 # define board_bits2(b_) 7
276 # define board_bits2(b_) ((b_)->bits2)
279 #define board_at(b_, c) ((b_)->b[c])
280 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
282 #define group_at(b_, c) ((b_)->g[c])
283 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
285 /* Warning! Neighbor count is kept up-to-date for S_NONE! */
286 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord].colors[(enum stone) color])
287 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
288 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
289 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
290 #define immediate_liberty_count(b_, coord) (4 - neighbor_count_at(b_, coord, S_BLACK) - neighbor_count_at(b_, coord, S_WHITE) - neighbor_count_at(b_, coord, S_OFFBOARD))
292 #define trait_at(b_, coord, color) (b_)->t[coord][(color) - 1]
294 #define groupnext_at(b_, c) ((b_)->p[c])
295 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
297 #define group_base(g_) (g_)
298 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
299 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
300 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
301 /* board_group_other_lib() makes sense only for groups with two liberties. */
302 #define board_group_other_lib(b_, g_, l_) (board_group_info(b_, g_).lib[board_group_info(b_, g_).lib[0] != (l_) ? 0 : 1])
304 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord])
306 struct board
*board_init(char *fbookfile
);
307 struct board
*board_copy(struct board
*board2
, struct board
*board1
);
308 void board_done_noalloc(struct board
*board
);
309 void board_done(struct board
*board
);
310 /* size here is without the S_OFFBOARD margin. */
311 void board_resize(struct board
*board
, int size
);
312 void board_clear(struct board
*board
);
315 typedef char *(*board_cprint
)(struct board
*b
, coord_t c
, char *s
, char *end
);
316 void board_print(struct board
*board
, FILE *f
);
317 void board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
);
319 /* Place given handicap on the board; coordinates are printed to f. */
320 void board_handicap(struct board
*board
, int stones
, FILE *f
);
322 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
323 int board_play(struct board
*board
, struct move
*m
);
324 /* Like above, but plays random move; the move coordinate is recorded
325 * to *coord. This method will never fill your own eye. pass is played
326 * when no move can be played. You can impose extra restrictions if you
327 * supply your own permit function; the permit function can also modify
328 * the move coordinate to redirect the move elsewhere. */
329 typedef bool (*ppr_permit
)(void *data
, struct board
*b
, struct move
*m
);
330 void board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
);
332 /*Undo, supported only for pass moves. Returns -1 on error, 0 otherwise. */
333 int board_undo(struct board
*board
);
335 /* Returns true if given move can be played. */
336 static bool board_is_valid_play(struct board
*b
, enum stone color
, coord_t coord
);
337 static bool board_is_valid_move(struct board
*b
, struct move
*m
);
338 /* Returns true if ko was just taken. */
339 static bool board_playing_ko_threat(struct board
*b
);
340 /* Returns 0 or ID of neighboring group in atari. */
341 static group_t
board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
);
342 /* Returns true if the move is not obvious self-atari. */
343 static bool board_safe_to_play(struct board
*b
, coord_t coord
, enum stone color
);
345 /* Determine number of stones in a group, up to @max stones. */
346 static int group_stone_count(struct board
*b
, group_t group
, int max
);
348 /* Adjust symmetry information as if given coordinate has been played. */
349 void board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
);
350 /* Check if coordinates are within symmetry base. (If false, they can
351 * be derived from the base.) */
352 static bool board_coord_in_symmetry(struct board
*b
, coord_t c
);
354 /* Returns true if given coordinate has all neighbors of given color or the edge. */
355 static bool board_is_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
);
356 /* Returns true if given coordinate could be a false eye; this check makes
357 * sense only if you already know the coordinate is_eyelike(). */
358 bool board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
);
359 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
360 * at least tries to). */
361 bool board_is_one_point_eye(struct board
*board
, coord_t c
, enum stone eye_color
);
362 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
363 enum stone
board_get_one_point_eye(struct board
*board
, coord_t c
);
365 /* board_official_score() is the scoring method for yielding score suitable
366 * for external presentation. For fast scoring of entirely filled boards
367 * (e.g. playouts), use board_fast_score(). */
368 /* Positive: W wins */
369 /* Compare number of stones + 1pt eyes. */
370 floating_t
board_fast_score(struct board
*board
);
371 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
373 floating_t
board_official_score(struct board
*board
, struct move_queue
*mq
);
377 #define foreach_point(board_) \
380 for (; c < board_size(board_) * board_size(board_); c++)
381 #define foreach_point_and_pass(board_) \
384 for (; c < board_size(board_) * board_size(board_); c++)
385 #define foreach_point_end \
388 #define foreach_free_point(board_) \
390 int fmax__ = (board_)->flen; \
391 for (int f__ = 0; f__ < fmax__; f__++) { \
392 coord_t c = (board_)->f[f__];
393 #define foreach_free_point_end \
397 #define foreach_in_group(board_, group_) \
399 struct board *board__ = board_; \
400 coord_t c = group_base(group_); \
401 coord_t c2 = c; c2 = groupnext_at(board__, c2); \
403 #define foreach_in_group_end \
404 c = c2; c2 = groupnext_at(board__, c2); \
408 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
409 * on S_OFFBOARD coordinates. */
410 #define foreach_neighbor(board_, coord_, loop_body) \
412 struct board *board__ = board_; \
413 coord_t coord__ = coord_; \
415 c = coord__ - board_size(board__); do { loop_body } while (0); \
416 c = coord__ - 1; do { loop_body } while (0); \
417 c = coord__ + 1; do { loop_body } while (0); \
418 c = coord__ + board_size(board__); do { loop_body } while (0); \
421 #define foreach_8neighbor(board_, coord_) \
424 coord_t c = (coord_); \
425 for (fn__i = 0; fn__i < 8; fn__i++) { \
426 c += (board_)->nei8[fn__i];
427 #define foreach_8neighbor_end \
431 #define foreach_diag_neighbor(board_, coord_) \
434 coord_t c = (coord_); \
435 for (fn__i = 0; fn__i < 4; fn__i++) { \
436 c += (board_)->dnei[fn__i];
437 #define foreach_diag_neighbor_end \
443 board_is_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
445 return (neighbor_count_at(board
, coord
, eye_color
)
446 + neighbor_count_at(board
, coord
, S_OFFBOARD
)) == 4;
450 board_is_valid_play(struct board
*board
, enum stone color
, coord_t coord
)
452 if (board_at(board
, coord
) != S_NONE
)
454 if (!board_is_eyelike(board
, coord
, stone_other(color
)))
456 /* Play within {true,false} eye-ish formation */
457 if (board
->ko
.coord
== coord
&& board
->ko
.color
== color
)
460 /* XXX: Disallows suicide. */
461 return trait_at(board
, coord
, color
).cap
> 0;
463 int groups_in_atari
= 0;
464 foreach_neighbor(board
, coord
, {
465 group_t g
= group_at(board
, c
);
466 groups_in_atari
+= (board_group_info(board
, g
).libs
== 1);
468 return !!groups_in_atari
;
473 board_is_valid_move(struct board
*board
, struct move
*m
)
475 return board_is_valid_play(board
, m
->color
, m
->coord
);
479 board_playing_ko_threat(struct board
*b
)
481 return !is_pass(b
->ko
.coord
);
484 static inline group_t
485 board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
)
488 if (!trait_at(b
, coord
, stone_other(group_color
)).cap
) return 0;
490 foreach_neighbor(b
, coord
, {
491 group_t g
= group_at(b
, c
);
492 if (g
&& board_at(b
, c
) == group_color
&& board_group_info(b
, g
).libs
== 1)
494 /* We return first match. */
500 board_safe_to_play(struct board
*b
, coord_t coord
, enum stone color
)
502 /* number of free neighbors */
503 int libs
= immediate_liberty_count(b
, coord
);
508 /* number of capturable enemy groups */
509 if (trait_at(b
, coord
, color
).cap
> 0)
510 return true; // XXX: We don't account for snapback.
511 /* number of non-capturable friendly groups */
512 int noncap_ours
= neighbor_count_at(b
, coord
, color
) - trait_at(b
, coord
, stone_other(color
)).cap
;
518 /* ok, but we need to check if they don't have just two libs. */
520 foreach_neighbor(b
, coord
, {
522 if (board_at(b
, c
) == stone_other(color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1)
523 return true; // can capture; no snapback check
525 if (board_at(b
, c
) != color
) continue;
526 group_t g
= group_at(b
, c
);
527 if (board_group_info(b
, g
).libs
== 1) continue; // in atari
528 if (board_group_info(b
, g
).libs
== 2) { // two liberties
529 if (libs
> 0) return true; // we already have one real liberty
530 /* we might be connecting two 2-lib groups, which is ok;
531 * so remember the other liberty and just make sure it's
532 * not the same one */
533 if (onelib
>= 0 && c
!= onelib
) return true;
534 onelib
= board_group_other_lib(b
, g
, c
);
540 // no good support group
545 group_stone_count(struct board
*b
, group_t group
, int max
)
548 foreach_in_group(b
, group
) {
550 if (n
>= max
) return max
;
551 } foreach_in_group_end
;
556 board_coord_in_symmetry(struct board
*b
, coord_t c
)
558 if (coord_y(c
, b
) < b
->symmetry
.y1
|| coord_y(c
, b
) > b
->symmetry
.y2
)
560 if (coord_x(c
, b
) < b
->symmetry
.x1
|| coord_x(c
, b
) > b
->symmetry
.x2
)
563 int x
= coord_x(c
, b
);
564 if (b
->symmetry
.type
== SYM_DIAG_DOWN
)
565 x
= board_size(b
) - 1 - x
;
566 if (x
> coord_y(c
, b
))