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_PAT3 // incremental 3x3 pattern codes
32 //#define BOARD_TRAITS 1 // incremental point traits (see struct btraits)
35 #define BOARD_MAX_MOVES (BOARD_MAX_SIZE * BOARD_MAX_SIZE)
36 #define BOARD_MAX_GROUPS (BOARD_MAX_SIZE * BOARD_MAX_SIZE / 2)
39 /* Some engines might normalize their reading and skip symmetrical
40 * moves. We will tell them how can they do it. */
41 struct board_symmetry
{
42 /* Playground is in this rectangle. */
44 /* d == 0: Full rectangle
45 * d == 1: Top triangle */
47 /* General symmetry type. */
48 /* Note that the above is redundant to this, but just provided
49 * for easier usage. */
61 typedef uint64_t hash_t
;
62 #define PRIhash PRIx64
64 /* XXX: This really belongs in pattern3.h, unfortunately that would mean
65 * a dependency hell. */
66 typedef uint32_t hash3_t
; // 3x3 pattern hash
69 /* Note that "group" is only chain of stones that is solidly
70 * connected for us. */
71 typedef coord_t group_t
;
74 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
76 /* _Combination_ of these two values can make some difference
77 * in performance - fine-tune. */
78 #define GROUP_KEEP_LIBS 10
79 // refill lib[] only when we hit this; this must be at least 2!
80 // Moggy requires at least 3 - see below for semantic impact.
81 #define GROUP_REFILL_LIBS 5
82 coord_t lib
[GROUP_KEEP_LIBS
];
83 /* libs is only LOWER BOUND for the number of real liberties!!!
84 * It denotes only number of items in lib[], thus you can rely
85 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
89 struct neighbor_colors
{
94 /* Point traits bitmap; we update this information incrementally,
95 * it can be used e.g. for fast pattern features matching. */
97 /* Number of neighbors we can capture. 0=this move is
98 * not capturing, 1..4=this many neighbors we can capture
99 * (can be multiple neighbors of same group). */
101 /* Number of 1-stone neighbors we can capture. */
103 /* Whether it is SAFE to play here. This is essentially just
104 * cached result of board_safe_to_play(). (Of course the concept
105 * of "safety" is not perfect here, but it's the cheapest
106 * reasonable thing we can do.) */
108 /* Whether we need to re-compute this coordinate; used to
109 * weed out duplicates. Maintained only for S_BLACK. */
114 /* You should treat this struct as read-only. Always call functions below if
115 * you want to change it. */
118 int size
; /* Including S_OFFBOARD margin - see below. */
119 int size2
; /* size^2 */
120 int bits2
; /* ceiling(log2(size2)) */
124 /* The ruleset is currently almost never taken into account;
125 * the board implementation is basically Chinese rules (handicap
126 * stones compensation) w/ suicide (or you can look at it as
127 * New Zealand w/o handi stones compensation), while the engine
128 * enforces no-suicide, making for real Chinese rules. */
130 RULES_CHINESE
, /* default value */
139 /* Iterator offsets for foreach_neighbor*() */
140 int nei8
[8], dnei
[4];
143 struct move last_move
;
144 struct move last_move2
; /* second-to-last move */
145 /* Whether we tried to add a hash twice; board_play*() can
146 * set this, but it will still carry out the move as well! */
147 bool superko_violation
;
149 /* The following two structures are goban maps and are indexed by
150 * coord.pos. The map is surrounded by a one-point margin from
151 * S_OFFBOARD stones in order to speed up some internal loops.
152 * Some of the foreach iterators below might include these points;
153 * you need to handle them yourselves, if you need to. */
155 /* Stones played on the board */
156 enum stone
*b
; /* enum stone */
157 /* Group id the stones are part of; 0 == no group */
159 /* Positions of next stones in the stone group; 0 == last stone */
161 /* Neighboring colors; numbers of neighbors of index color */
162 struct neighbor_colors
*n
;
163 /* Zobrist hash for each position */
166 /* 3x3 pattern code for each position; see pattern3.h for encoding
167 * specification. The information is only valid for empty points. */
171 /* Incrementally matched point traits information, black-to-play
172 * ([][0]) and white-to-play ([][1]). */
173 /* The information is only valid for empty points. */
174 struct btraits (*t
)[2];
176 /* Cached information on x-y coordinates so that we avoid division. */
179 /* Group information - indexed by gid (which is coord of base group stone) */
182 /* Positions of free positions - queue (not map) */
183 /* Note that free position here is any valid move; including single-point eyes!
184 * However, pass is not included. */
185 coord_t
*f
; int flen
;
188 /* Queue of capturable groups */
189 group_t
*c
; int clen
;
193 /* Queue of positions that need their traits updated */
194 coord_t
*tq
; int tqlen
;
197 /* Symmetry information */
198 struct board_symmetry symmetry
;
200 /* Last ko played on the board. */
207 /* Engine-specific state; persistent through board development,
208 * is reset only at clear_board. */
211 /* Playout-specific state; persistent through board development,
212 * but its lifetime is maintained in play_random_game(); it should
213 * not be set outside of it. */
217 /* --- PRIVATE DATA --- */
219 /* For superko check: */
221 /* Board "history" - hashes encountered. Size of the hash should be
222 * >> board_size^2. */
223 #define history_hash_bits 12
224 #define history_hash_mask ((1 << history_hash_bits) - 1)
225 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
226 #define history_hash_next(i) ((i + 1) & history_hash_mask)
227 hash_t history_hash
[1 << history_hash_bits
];
228 /* Hash of current board position. */
230 /* Hash of current board position quadrants. */
235 /* Avoid unused variable warnings */
236 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
237 #define board_size2(b_) (board_size(b_) * board_size(b_))
239 #define board_size(b_) ((b_)->size)
240 #define board_size2(b_) ((b_)->size2)
243 /* This is a shortcut for taking different action on smaller
244 * and large boards (e.g. picking different variable defaults).
245 * This is of course less optimal than fine-tuning dependency
246 * function of values on board size, but that is difficult and
247 * possibly not very rewarding if you are interested just in
249 #define board_large(b_) (board_size(b_)-2 >= 15)
252 # define board_bits2(b_) 9
253 #elif BOARD_SIZE == 13
254 # define board_bits2(b_) 8
255 #elif BOARD_SIZE == 9
256 # define board_bits2(b_) 7
258 # define board_bits2(b_) ((b_)->bits2)
261 #define board_at(b_, c) ((b_)->b[c])
262 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
264 #define group_at(b_, c) ((b_)->g[c])
265 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
267 /* Warning! Neighbor count is kept up-to-date for S_NONE! */
268 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord].colors[(enum stone) color])
269 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
270 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
271 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
272 #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))
274 #define trait_at(b_, coord, color) (b_)->t[coord][(color) - 1]
276 #define groupnext_at(b_, c) ((b_)->p[c])
277 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
279 #define group_base(g_) (g_)
280 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
281 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
282 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
283 /* board_group_other_lib() makes sense only for groups with two liberties. */
284 #define board_group_other_lib(b_, g_, l_) (board_group_info(b_, g_).lib[board_group_info(b_, g_).lib[0] != (l_) ? 0 : 1])
286 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord])
288 struct board
*board_init(char *fbookfile
);
289 struct board
*board_copy(struct board
*board2
, struct board
*board1
);
290 void board_done_noalloc(struct board
*board
);
291 void board_done(struct board
*board
);
292 /* size here is without the S_OFFBOARD margin. */
293 void board_resize(struct board
*board
, int size
);
294 void board_clear(struct board
*board
);
297 typedef char *(*board_cprint
)(struct board
*b
, coord_t c
, char *s
, char *end
);
298 void board_print(struct board
*board
, FILE *f
);
299 void board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
);
301 /* Place given handicap on the board; coordinates are printed to f. */
302 void board_handicap(struct board
*board
, int stones
, FILE *f
);
304 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
305 int board_play(struct board
*board
, struct move
*m
);
306 /* Like above, but plays random move; the move coordinate is recorded
307 * to *coord. This method will never fill your own eye. pass is played
308 * when no move can be played. You can impose extra restrictions if you
309 * supply your own permit function; the permit function can also modify
310 * the move coordinate to redirect the move elsewhere. */
311 typedef bool (*ppr_permit
)(void *data
, struct board
*b
, struct move
*m
);
312 void board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
);
314 /* Returns true if given move can be played. */
315 static bool board_is_valid_play(struct board
*b
, enum stone color
, coord_t coord
);
316 static bool board_is_valid_move(struct board
*b
, struct move
*m
);
317 /* Returns true if ko was just taken. */
318 static bool board_playing_ko_threat(struct board
*b
);
319 /* Returns 0 or ID of neighboring group in atari. */
320 static group_t
board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
);
321 /* Returns true if the move is not obvious self-atari. */
322 static bool board_safe_to_play(struct board
*b
, coord_t coord
, enum stone color
);
324 /* Determine number of stones in a group, up to @max stones. */
325 static int group_stone_count(struct board
*b
, group_t group
, int max
);
327 /* Adjust symmetry information as if given coordinate has been played. */
328 void board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
);
330 /* Returns true if given coordinate has all neighbors of given color or the edge. */
331 static bool board_is_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
);
332 /* Returns true if given coordinate could be a false eye; this check makes
333 * sense only if you already know the coordinate is_eyelike(). */
334 bool board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
);
335 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
336 * at least tries to). */
337 bool board_is_one_point_eye(struct board
*board
, coord_t c
, enum stone eye_color
);
338 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
339 enum stone
board_get_one_point_eye(struct board
*board
, coord_t c
);
341 /* board_official_score() is the scoring method for yielding score suitable
342 * for external presentation. For fast scoring of entirely filled boards
343 * (e.g. playouts), use board_fast_score(). */
344 /* Positive: W wins */
345 /* Compare number of stones + 1pt eyes. */
346 floating_t
board_fast_score(struct board
*board
);
347 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
349 floating_t
board_official_score(struct board
*board
, struct move_queue
*mq
);
353 #define foreach_point(board_) \
356 for (; c < board_size(board_) * board_size(board_); c++)
357 #define foreach_point_and_pass(board_) \
360 for (; c < board_size(board_) * board_size(board_); c++)
361 #define foreach_point_end \
364 #define foreach_free_point(board_) \
366 int fmax__ = (board_)->flen; \
367 for (int f__ = 0; f__ < fmax__; f__++) { \
368 coord_t c = (board_)->f[f__];
369 #define foreach_free_point_end \
373 #define foreach_in_group(board_, group_) \
375 struct board *board__ = board_; \
376 coord_t c = group_base(group_); \
377 coord_t c2 = c; c2 = groupnext_at(board__, c2); \
379 #define foreach_in_group_end \
380 c = c2; c2 = groupnext_at(board__, c2); \
384 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
385 * on S_OFFBOARD coordinates. */
386 #define foreach_neighbor(board_, coord_, loop_body) \
388 struct board *board__ = board_; \
389 coord_t coord__ = coord_; \
391 c = coord__ - board_size(board__); do { loop_body } while (0); \
392 c = coord__ - 1; do { loop_body } while (0); \
393 c = coord__ + 1; do { loop_body } while (0); \
394 c = coord__ + board_size(board__); do { loop_body } while (0); \
397 #define foreach_8neighbor(board_, coord_) \
400 coord_t c = (coord_); \
401 for (fn__i = 0; fn__i < 8; fn__i++) { \
402 c += (board_)->nei8[fn__i];
403 #define foreach_8neighbor_end \
407 #define foreach_diag_neighbor(board_, coord_) \
410 coord_t c = (coord_); \
411 for (fn__i = 0; fn__i < 4; fn__i++) { \
412 c += (board_)->dnei[fn__i];
413 #define foreach_diag_neighbor_end \
419 board_is_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
421 return (neighbor_count_at(board
, coord
, eye_color
)
422 + neighbor_count_at(board
, coord
, S_OFFBOARD
)) == 4;
426 board_is_valid_play(struct board
*board
, enum stone color
, coord_t coord
)
428 if (board_at(board
, coord
) != S_NONE
)
430 if (!board_is_eyelike(board
, coord
, stone_other(color
)))
432 /* Play within {true,false} eye-ish formation */
433 if (board
->ko
.coord
== coord
&& board
->ko
.color
== color
)
436 /* XXX: Disallows suicide. */
437 return trait_at(board
, coord
, color
).cap
> 0;
439 int groups_in_atari
= 0;
440 foreach_neighbor(board
, coord
, {
441 group_t g
= group_at(board
, c
);
442 groups_in_atari
+= (board_group_info(board
, g
).libs
== 1);
444 return !!groups_in_atari
;
449 board_is_valid_move(struct board
*board
, struct move
*m
)
451 return board_is_valid_play(board
, m
->color
, m
->coord
);
455 board_playing_ko_threat(struct board
*b
)
457 return !is_pass(b
->ko
.coord
);
460 static inline group_t
461 board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
)
464 if (!trait_at(b
, coord
, stone_other(group_color
)).cap
) return 0;
466 foreach_neighbor(b
, coord
, {
467 group_t g
= group_at(b
, c
);
468 if (g
&& board_at(b
, c
) == group_color
&& board_group_info(b
, g
).libs
== 1)
470 /* We return first match. */
476 board_safe_to_play(struct board
*b
, coord_t coord
, enum stone color
)
478 /* number of free neighbors */
479 int libs
= immediate_liberty_count(b
, coord
);
484 /* number of capturable enemy groups */
485 if (trait_at(b
, coord
, color
).cap
> 0)
486 return true; // XXX: We don't account for snapback.
487 /* number of non-capturable friendly groups */
488 int noncap_ours
= neighbor_count_at(b
, coord
, color
) - trait_at(b
, coord
, stone_other(color
)).cap
;
494 /* ok, but we need to check if they don't have just two libs. */
496 foreach_neighbor(b
, coord
, {
498 if (board_at(b
, c
) == stone_other(color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1)
499 return true; // can capture; no snapback check
501 if (board_at(b
, c
) != color
) continue;
502 group_t g
= group_at(b
, c
);
503 if (board_group_info(b
, g
).libs
== 1) continue; // in atari
504 if (board_group_info(b
, g
).libs
== 2) { // two liberties
505 if (libs
> 0) return true; // we already have one real liberty
506 /* we might be connecting two 2-lib groups, which is ok;
507 * so remember the other liberty and just make sure it's
508 * not the same one */
509 if (onelib
>= 0 && c
!= onelib
) return true;
510 onelib
= board_group_other_lib(b
, g
, c
);
516 // no good support group
521 group_stone_count(struct board
*b
, group_t group
, int max
)
524 foreach_in_group(b
, group
) {
526 if (n
>= max
) return max
;
527 } foreach_in_group_end
;