13 struct features_gamma
;
16 /* The board implementation has bunch of optional features.
17 * Turn them on below: */
19 #define WANT_BOARD_C // capturable groups queue
21 //#define BOARD_SIZE 9 // constant board size, allows better optimization
23 //#define BOARD_SPATHASH // incremental patternsp.h hashes
24 #define BOARD_SPATHASH_MAXD 3 // maximal diameter
26 #define BOARD_PAT3 // incremental 3x3 pattern codes
28 //#define BOARD_TRAITS 1 // incremental point traits (see struct btraits)
29 //#define BOARD_GAMMA 1 // incremental probability distribution (requires BOARD_TRAITS, BOARD_PAT3)
32 /* Allow board_play_random_move() to return pass even when
33 * there are other moves available. */
34 extern bool random_pass
;
37 /* Some engines might normalize their reading and skip symmetrical
38 * moves. We will tell them how can they do it. */
39 struct board_symmetry
{
40 /* Playground is in this rectangle. */
42 /* d == 0: Full rectangle
43 * d == 1: Top triangle */
45 /* General symmetry type. */
46 /* Note that the above is redundant to this, but just provided
47 * for easier usage. */
59 typedef uint64_t hash_t
;
60 #define PRIhash PRIx64
63 /* Note that "group" is only chain of stones that is solidly
64 * connected for us. */
65 typedef coord_t group_t
;
68 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
70 /* _Combination_ of these two values can make some difference
71 * in performance - fine-tune. */
72 #define GROUP_KEEP_LIBS 10
73 // refill lib[] only when we hit this; this must be at least 2!
74 // Moggy requires at least 3 - see below for semantic impact.
75 #define GROUP_REFILL_LIBS 5
76 coord_t lib
[GROUP_KEEP_LIBS
];
77 /* libs is only LOWER BOUND for the number of real liberties!!!
78 * It denotes only number of items in lib[], thus you can rely
79 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
83 struct neighbor_colors
{
88 /* Point traits bitmap; we update this information incrementally,
89 * it can be used e.g. for fast pattern.h features matching. */
91 /* Number of neighbors we can capture. 0=this move is
92 * not capturing, 1..4=this many neighbors we can capture
93 * (can be multiple neighbors of same group). */
95 /* Whether it is SAFE to play here. This is essentially just
96 * cached result of the macro below. (Of course the concept
97 * of "safety" is not perfect here, but it's the cheapest
98 * reasonable thing we can do.) */
100 #define board_safe_to_play(b_, coord_, color_) \
102 /* number of free neighbors, except us */ \
103 immediate_liberty_count(b_, coord_) - 1 \
104 /* number of capturable enemy groups */ \
105 + trait_at(b_, coord_, color_).cap \
106 /* number of non-capturable friendly groups */ \
107 + neighbor_count_at(b_, coord_, color_) - trait_at(b_, coord_, stone_other(color_)).cap \
112 /* You should treat this struct as read-only. Always call functions below if
113 * you want to change it. */
116 int size
; /* Including S_OFFBOARD margin - see below. */
117 int size2
; /* size^2 */
122 /* Iterator offsets for foreach_neighbor*() */
123 int nei8
[8], dnei
[4];
126 struct move last_move
;
127 struct move last_move2
; /* second-to-last move */
128 /* Whether we tried to add a hash twice; board_play*() can
129 * set this, but it will still carry out the move as well! */
130 bool superko_violation
;
132 /* The following two structures are goban maps and are indexed by
133 * coord.pos. The map is surrounded by a one-point margin from
134 * S_OFFBOARD stones in order to speed up some internal loops.
135 * Some of the foreach iterators below might include these points;
136 * you need to handle them yourselves, if you need to. */
138 /* Stones played on the board */
139 enum stone
*b
; /* enum stone */
140 /* Group id the stones are part of; 0 == no group */
142 /* Positions of next stones in the stone group; 0 == last stone */
144 /* Neighboring colors; numbers of neighbors of index color */
145 struct neighbor_colors
*n
;
146 /* Zobrist hash for each position */
148 #ifdef BOARD_SPATHASH
149 /* For spatial hashes, we use only 24 bits. */
150 /* [0] is d==1, we don't keep hash for d==0. */
151 /* We keep hashes for black-to-play ([][0]) and white-to-play
152 * ([][1], reversed stone colors since we match all patterns as
154 uint32_t (*spathash
)[BOARD_SPATHASH_MAXD
][2];
157 /* 3x3 pattern code for each position; see pattern3.h for encoding
158 * specification. The information is only valid for empty points. */
162 /* Incrementally matched point traits information, black-to-play
163 * ([][0]) and white-to-play ([][1]). */
164 /* The information is only valid for empty points. */
165 struct btraits (*t
)[2];
168 /* Relative probabilities of moves being played next, computed by
169 * multiplying gammas of the appropriate pattern features based on
170 * pat3 and traits (see pattern.h). The probability distribution
171 * is maintained over the full board grid. */
172 /* - Always invalid moves are guaranteed to have zero probability.
173 * - Self-eye-filling moves will always have zero probability.
174 * - Ko-prohibited moves might have non-zero probability.
175 * - FEAT_CONTIGUITY is not accounted for in the probability. */
176 struct probdist prob
[2];
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 coord_t
*f
; int flen
;
187 /* Queue of capturable groups */
188 group_t
*c
; int clen
;
191 /* Symmetry information */
192 struct board_symmetry symmetry
;
194 /* Last ko played on the board. */
201 /* Engine-specific state; persistent through board development,
202 * is reset only at clear_board. */
205 /* Playout-specific state; persistent through board development,
206 * but its lifetime is maintained in play_random_game(); it should
207 * not be set outside of it. */
211 /* Gamma values for probability distribution; user must setup
212 * this pointer before any move is played, using board_gamma_set(). */
213 struct features_gamma
*gamma
;
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. */
233 /* Avoid unused variable warnings */
234 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
235 #define board_size2(b_) (board_size(b_) * board_size(b_))
237 #define board_size(b_) ((b_)->size)
238 #define board_size2(b_) ((b_)->size2)
241 #define board_at(b_, c) ((b_)->b[coord_raw(c)])
242 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
244 #define group_at(b_, c) ((b_)->g[coord_raw(c)])
245 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
247 /* Warning! Neighbor count is kept up-to-date for S_NONE! */
248 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord_raw(coord)].colors[(enum stone) color])
249 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
250 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
251 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
252 #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))
254 #define trait_at(b_, coord, color) (b_)->t[coord][(color) - 1]
256 #define groupnext_at(b_, c) ((b_)->p[coord_raw(c)])
257 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
259 #define group_base(g_) (g_)
260 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
261 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
262 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
264 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord_raw(coord)])
266 struct board
*board_init(void);
267 struct board
*board_copy(struct board
*board2
, struct board
*board1
);
268 void board_done_noalloc(struct board
*board
);
269 void board_done(struct board
*board
);
270 /* size here is without the S_OFFBOARD margin. */
271 void board_resize(struct board
*board
, int size
);
272 void board_clear(struct board
*board
);
275 typedef void (*board_cprint
)(struct board
*b
, coord_t c
, FILE *f
);
276 void board_print(struct board
*board
, FILE *f
);
277 void board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
);
279 /* Place given handicap on the board; coordinates are printed to f. */
280 void board_handicap(struct board
*board
, int stones
, FILE *f
);
282 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
283 int board_play(struct board
*board
, struct move
*m
);
284 /* Like above, but plays random move; the move coordinate is recorded
285 * to *coord. This method will never fill your own eye. pass is played
286 * when no move can be played. You can impose extra restrictions if you
287 * supply your own permit function. */
288 typedef bool (*ppr_permit
)(void *data
, struct board
*b
, struct move
*m
);
289 void board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
);
291 /* Returns true if given move can be played. */
292 static bool board_is_valid_move(struct board
*b
, struct move
*m
);
293 /* Returns true if ko was just taken. */
294 static bool board_playing_ko_threat(struct board
*b
);
295 /* Returns 0 or ID of neighboring group in atari. */
296 static group_t
board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
);
298 /* Adjust symmetry information as if given coordinate has been played. */
299 void board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
);
300 /* Associate a set of feature gamma values (for pd building) with the board. */
301 void board_gamma_set(struct board
*b
, struct features_gamma
*gamma
);
302 /* Force re-compute of a probability distribution item. */
303 void board_gamma_update(struct board
*b
, coord_t coord
, enum stone color
);
305 /* Returns true if given coordinate has all neighbors of given color or the edge. */
306 static bool board_is_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
);
307 /* Returns true if given coordinate could be a false eye; this check makes
308 * sense only if you already know the coordinate is_eyelike(). */
309 bool board_is_false_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
);
310 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
311 * at least tries to). */
312 bool board_is_one_point_eye(struct board
*board
, coord_t
*c
, enum stone eye_color
);
313 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
314 enum stone
board_get_one_point_eye(struct board
*board
, coord_t
*c
);
316 /* board_official_score() is the scoring method for yielding score suitable
317 * for external presentation. For fast scoring of entirely filled boards
318 * (e.g. playouts), use board_fast_score(). */
319 /* Positive: W wins */
320 /* Compare number of stones + 1pt eyes. */
321 float board_fast_score(struct board
*board
);
322 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
324 float board_official_score(struct board
*board
, struct move_queue
*mq
);
328 #define foreach_point(board_) \
330 coord_t c; coord_pos(c, 0, (board_)); \
331 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
332 #define foreach_point_and_pass(board_) \
334 coord_t c; coord_pos(c, -1, (board_)); \
335 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
336 #define foreach_point_end \
339 #define foreach_in_group(board_, group_) \
341 struct board *board__ = board_; \
342 coord_t c = group_base(group_); \
343 coord_t c2 = c; coord_raw(c2) = groupnext_at(board__, c2); \
345 #define foreach_in_group_end \
346 c = c2; coord_raw(c2) = groupnext_at(board__, c2); \
347 } while (coord_raw(c) != 0); \
350 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
351 * on S_OFFBOARD coordinates. */
352 #define foreach_neighbor(board_, coord_, loop_body) \
354 struct board *board__ = board_; \
355 coord_t coord__ = coord_; \
357 coord_pos(c, coord_raw(coord__) - 1, (board__)); do { loop_body } while (0); \
358 coord_pos(c, coord_raw(coord__) - board_size(board__), (board__)); do { loop_body } while (0); \
359 coord_pos(c, coord_raw(coord__) + 1, (board__)); do { loop_body } while (0); \
360 coord_pos(c, coord_raw(coord__) + board_size(board__), (board__)); do { loop_body } while (0); \
363 #define foreach_8neighbor(board_, coord_) \
366 coord_t c = (coord_); \
367 for (fn__i = 0; fn__i < 8; fn__i++) { \
368 c += (board_)->nei8[fn__i];
369 #define foreach_8neighbor_end \
373 #define foreach_diag_neighbor(board_, coord_) \
376 coord_t c = (coord_); \
377 for (fn__i = 0; fn__i < 4; fn__i++) { \
378 c += (board_)->dnei[fn__i];
379 #define foreach_diag_neighbor_end \
385 board_is_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
387 return (neighbor_count_at(board
, *coord
, eye_color
)
388 + neighbor_count_at(board
, *coord
, S_OFFBOARD
)) == 4;
392 board_is_valid_move(struct board
*board
, struct move
*m
)
394 if (board_at(board
, m
->coord
) != S_NONE
)
396 if (!board_is_eyelike(board
, &m
->coord
, stone_other(m
->color
)))
398 /* Play within {true,false} eye-ish formation */
399 if (board
->ko
.coord
== m
->coord
&& board
->ko
.color
== m
->color
)
402 /* XXX: Disallows suicide. */
403 return trait_at(board
, m
->coord
, m
->color
).cap
> 0;
405 int groups_in_atari
= 0;
406 foreach_neighbor(board
, m
->coord
, {
407 group_t g
= group_at(board
, c
);
408 groups_in_atari
+= (board_group_info(board
, g
).libs
== 1);
410 return !!groups_in_atari
;
415 board_playing_ko_threat(struct board
*b
)
417 return !is_pass(b
->ko
.coord
);
420 static inline group_t
421 board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
)
424 if (!trait_at(b
, coord
, stone_other(group_color
)).cap
) return 0;
426 foreach_neighbor(b
, coord
, {
427 group_t g
= group_at(b
, c
);
428 if (g
&& board_at(b
, c
) == group_color
&& board_group_info(b
, g
).libs
== 1)
430 /* We return first match. */