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)
244 # define board_bits2(b_) 9
245 #elif BOARD_SIZE == 13
246 # define board_bits2(b_) 8
247 #elif BOARD_SIZE == 9
248 # define board_bits2(b_) 7
250 # define board_bits2(b_) ((b_)->bits2)
253 #define board_at(b_, c) ((b_)->b[c])
254 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
256 #define group_at(b_, c) ((b_)->g[c])
257 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
259 /* Warning! Neighbor count is kept up-to-date for S_NONE! */
260 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord].colors[(enum stone) color])
261 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
262 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
263 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
264 #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))
266 #define trait_at(b_, coord, color) (b_)->t[coord][(color) - 1]
268 #define groupnext_at(b_, c) ((b_)->p[c])
269 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
271 #define group_base(g_) (g_)
272 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
273 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
274 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
275 /* board_group_other_lib() makes sense only for groups with two liberties. */
276 #define board_group_other_lib(b_, g_, l_) (board_group_info(b_, g_).lib[board_group_info(b_, g_).lib[0] != (l_) ? 0 : 1])
278 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord])
280 struct board
*board_init(char *fbookfile
);
281 struct board
*board_copy(struct board
*board2
, struct board
*board1
);
282 void board_done_noalloc(struct board
*board
);
283 void board_done(struct board
*board
);
284 /* size here is without the S_OFFBOARD margin. */
285 void board_resize(struct board
*board
, int size
);
286 void board_clear(struct board
*board
);
289 typedef char *(*board_cprint
)(struct board
*b
, coord_t c
, char *s
, char *end
);
290 void board_print(struct board
*board
, FILE *f
);
291 void board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
);
293 /* Place given handicap on the board; coordinates are printed to f. */
294 void board_handicap(struct board
*board
, int stones
, FILE *f
);
296 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
297 int board_play(struct board
*board
, struct move
*m
);
298 /* Like above, but plays random move; the move coordinate is recorded
299 * to *coord. This method will never fill your own eye. pass is played
300 * when no move can be played. You can impose extra restrictions if you
301 * supply your own permit function; the permit function can also modify
302 * the move coordinate to redirect the move elsewhere. */
303 typedef bool (*ppr_permit
)(void *data
, struct board
*b
, struct move
*m
);
304 void board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
);
306 /* Returns true if given move can be played. */
307 static bool board_is_valid_play(struct board
*b
, enum stone color
, coord_t coord
);
308 static bool board_is_valid_move(struct board
*b
, struct move
*m
);
309 /* Returns true if ko was just taken. */
310 static bool board_playing_ko_threat(struct board
*b
);
311 /* Returns 0 or ID of neighboring group in atari. */
312 static group_t
board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
);
313 /* Returns true if the move is not obvious self-atari. */
314 static bool board_safe_to_play(struct board
*b
, coord_t coord
, enum stone color
);
316 /* Adjust symmetry information as if given coordinate has been played. */
317 void board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
);
319 /* Returns true if given coordinate has all neighbors of given color or the edge. */
320 static bool board_is_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
);
321 /* Returns true if given coordinate could be a false eye; this check makes
322 * sense only if you already know the coordinate is_eyelike(). */
323 bool board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
);
324 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
325 * at least tries to). */
326 bool board_is_one_point_eye(struct board
*board
, coord_t c
, enum stone eye_color
);
327 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
328 enum stone
board_get_one_point_eye(struct board
*board
, coord_t c
);
330 /* board_official_score() is the scoring method for yielding score suitable
331 * for external presentation. For fast scoring of entirely filled boards
332 * (e.g. playouts), use board_fast_score(). */
333 /* Positive: W wins */
334 /* Compare number of stones + 1pt eyes. */
335 floating_t
board_fast_score(struct board
*board
);
336 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
338 floating_t
board_official_score(struct board
*board
, struct move_queue
*mq
);
342 #define foreach_point(board_) \
345 for (; c < board_size(board_) * board_size(board_); c++)
346 #define foreach_point_and_pass(board_) \
349 for (; c < board_size(board_) * board_size(board_); c++)
350 #define foreach_point_end \
353 #define foreach_free_point(board_) \
355 int fmax__ = (board_)->flen; \
356 for (int f__ = 0; f__ < fmax__; f__++) { \
357 coord_t c = (board_)->f[f__];
358 #define foreach_free_point_end \
362 #define foreach_in_group(board_, group_) \
364 struct board *board__ = board_; \
365 coord_t c = group_base(group_); \
366 coord_t c2 = c; c2 = groupnext_at(board__, c2); \
368 #define foreach_in_group_end \
369 c = c2; c2 = groupnext_at(board__, c2); \
373 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
374 * on S_OFFBOARD coordinates. */
375 #define foreach_neighbor(board_, coord_, loop_body) \
377 struct board *board__ = board_; \
378 coord_t coord__ = coord_; \
380 c = coord__ - board_size(board__); do { loop_body } while (0); \
381 c = coord__ - 1; do { loop_body } while (0); \
382 c = coord__ + 1; do { loop_body } while (0); \
383 c = coord__ + board_size(board__); do { loop_body } while (0); \
386 #define foreach_8neighbor(board_, coord_) \
389 coord_t c = (coord_); \
390 for (fn__i = 0; fn__i < 8; fn__i++) { \
391 c += (board_)->nei8[fn__i];
392 #define foreach_8neighbor_end \
396 #define foreach_diag_neighbor(board_, coord_) \
399 coord_t c = (coord_); \
400 for (fn__i = 0; fn__i < 4; fn__i++) { \
401 c += (board_)->dnei[fn__i];
402 #define foreach_diag_neighbor_end \
408 board_is_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
410 return (neighbor_count_at(board
, coord
, eye_color
)
411 + neighbor_count_at(board
, coord
, S_OFFBOARD
)) == 4;
415 board_is_valid_play(struct board
*board
, enum stone color
, coord_t coord
)
417 if (board_at(board
, coord
) != S_NONE
)
419 if (!board_is_eyelike(board
, coord
, stone_other(color
)))
421 /* Play within {true,false} eye-ish formation */
422 if (board
->ko
.coord
== coord
&& board
->ko
.color
== color
)
425 /* XXX: Disallows suicide. */
426 return trait_at(board
, coord
, color
).cap
> 0;
428 int groups_in_atari
= 0;
429 foreach_neighbor(board
, coord
, {
430 group_t g
= group_at(board
, c
);
431 groups_in_atari
+= (board_group_info(board
, g
).libs
== 1);
433 return !!groups_in_atari
;
438 board_is_valid_move(struct board
*board
, struct move
*m
)
440 return board_is_valid_play(board
, m
->color
, m
->coord
);
444 board_playing_ko_threat(struct board
*b
)
446 return !is_pass(b
->ko
.coord
);
449 static inline group_t
450 board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
)
453 if (!trait_at(b
, coord
, stone_other(group_color
)).cap
) return 0;
455 foreach_neighbor(b
, coord
, {
456 group_t g
= group_at(b
, c
);
457 if (g
&& board_at(b
, c
) == group_color
&& board_group_info(b
, g
).libs
== 1)
459 /* We return first match. */
465 board_safe_to_play(struct board
*b
, coord_t coord
, enum stone color
)
467 /* number of free neighbors */
468 int libs
= immediate_liberty_count(b
, coord
);
473 /* number of capturable enemy groups */
474 if (trait_at(b
, coord
, color
).cap
> 0)
475 return true; // XXX: We don't account for snapback.
476 /* number of non-capturable friendly groups */
477 int noncap_ours
= neighbor_count_at(b
, coord
, color
) - trait_at(b
, coord
, stone_other(color
)).cap
;
483 /* ok, but we need to check if they don't have just two libs. */
485 foreach_neighbor(b
, coord
, {
487 if (board_at(b
, c
) == stone_other(color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1)
488 return true; // can capture; no snapback check
490 if (board_at(b
, c
) != color
) continue;
491 group_t g
= group_at(b
, c
);
492 if (board_group_info(b
, g
).libs
== 1) continue; // in atari
493 if (board_group_info(b
, g
).libs
== 2) { // two liberties
494 if (libs
> 0) return true; // we already have one real liberty
495 /* we might be connecting two 2-lib groups, which is ok;
496 * so remember the other liberty and just make sure it's
497 * not the same one */
498 if (onelib
>= 0 && c
!= onelib
) return true;
499 onelib
= board_group_other_lib(b
, g
, c
);
505 // no good support group