13 /* The board implementation has bunch of optional features.
14 * Turn them on below: */
16 #define WANT_BOARD_C // capturable groups queue
18 //#define BOARD_SIZE 9 // constant board size, allows better optimization
20 //#define BOARD_SPATHASH // incremental patternsp.h hashes
21 #define BOARD_SPATHASH_MAXD 3 // maximal diameter
23 #define BOARD_PAT3 // incremental 3x3 pattern codes
25 //#define BOARD_TRAITS 1 // incremental point traits (see struct btraits)
28 /* Allow board_play_random_move() to return pass even when
29 * there are other moves available. */
30 extern bool random_pass
;
33 /* Some engines might normalize their reading and skip symmetrical
34 * moves. We will tell them how can they do it. */
35 struct board_symmetry
{
36 /* Playground is in this rectangle. */
38 /* d == 0: Full rectangle
39 * d == 1: Top triangle */
41 /* General symmetry type. */
42 /* Note that the above is redundant to this, but just provided
43 * for easier usage. */
55 typedef uint64_t hash_t
;
56 #define PRIhash PRIx64
59 /* Note that "group" is only chain of stones that is solidly
60 * connected for us. */
61 typedef coord_t group_t
;
64 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
66 /* _Combination_ of these two values can make some difference
67 * in performance - fine-tune. */
68 #define GROUP_KEEP_LIBS 10
69 // refill lib[] only when we hit this; this must be at least 2!
70 // Moggy requires at least 3 - see below for semantic impact.
71 #define GROUP_REFILL_LIBS 5
72 coord_t lib
[GROUP_KEEP_LIBS
];
73 /* libs is only LOWER BOUND for the number of real liberties!!!
74 * It denotes only number of items in lib[], thus you can rely
75 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
79 struct neighbor_colors
{
84 /* Point traits bitmap; we update this information incrementally,
85 * it can be used e.g. for fast pattern.h features matching. */
87 /* Number of neighbors we can capture. 0=this move is
88 * not capturing, 1..4=this many neighbors we can capture
89 * (can be multiple neighbors of same group). */
94 /* You should treat this struct as read-only. Always call functions below if
95 * you want to change it. */
98 int size
; /* Including S_OFFBOARD margin - see below. */
99 int size2
; /* size^2 */
104 /* Iterator offsets for foreach_neighbor*() */
105 int nei8
[8], dnei
[4];
108 struct move last_move
;
109 struct move last_move2
; /* second-to-last move */
110 /* Whether we tried to add a hash twice; board_play*() can
111 * set this, but it will still carry out the move as well! */
112 bool superko_violation
;
114 /* The following two structures are goban maps and are indexed by
115 * coord.pos. The map is surrounded by a one-point margin from
116 * S_OFFBOARD stones in order to speed up some internal loops.
117 * Some of the foreach iterators below might include these points;
118 * you need to handle them yourselves, if you need to. */
120 /* Stones played on the board */
121 enum stone
*b
; /* enum stone */
122 /* Group id the stones are part of; 0 == no group */
124 /* Positions of next stones in the stone group; 0 == last stone */
126 /* Neighboring colors; numbers of neighbors of index color */
127 struct neighbor_colors
*n
;
128 /* Zobrist hash for each position */
130 #ifdef BOARD_SPATHASH
131 /* For spatial hashes, we use only 24 bits. */
132 /* [0] is d==1, we don't keep hash for d==0. */
133 /* We keep hashes for black-to-play ([][0]) and white-to-play
134 * ([][1], reversed stone colors since we match all patterns as
136 uint32_t (*spathash
)[BOARD_SPATHASH_MAXD
][2];
139 /* 3x3 pattern code for each position; see pattern3.h for encoding
140 * specification. The information is only valid for empty points. */
144 /* Incrementally matched point traits information. */
145 /* The information is only valid for empty points. */
149 /* Group information - indexed by gid (which is coord of base group stone) */
152 /* Positions of free positions - queue (not map) */
153 /* Note that free position here is any valid move; including single-point eyes! */
154 coord_t
*f
; int flen
;
157 /* Queue of capturable groups */
158 group_t
*c
; int clen
;
161 /* Symmetry information */
162 struct board_symmetry symmetry
;
164 /* Last ko played on the board. */
171 /* Engine-specific state; persistent through board development,
172 * is reset only at clear_board. */
175 /* Playout-specific state; persistent through board development,
176 * but its lifetime is maintained in play_random_game(); it should
177 * not be set outside of it. */
181 /* --- PRIVATE DATA --- */
183 /* For superko check: */
185 /* Board "history" - hashes encountered. Size of the hash should be
186 * >> board_size^2. */
187 #define history_hash_bits 12
188 #define history_hash_mask ((1 << history_hash_bits) - 1)
189 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
190 #define history_hash_next(i) ((i + 1) & history_hash_mask)
191 hash_t history_hash
[1 << history_hash_bits
];
192 /* Hash of current board position. */
197 /* Avoid unused variable warnings */
198 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
199 #define board_size2(b_) (board_size(b_) * board_size(b_))
201 #define board_size(b_) ((b_)->size)
202 #define board_size2(b_) ((b_)->size2)
205 #define board_at(b_, c) ((b_)->b[coord_raw(c)])
206 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
208 #define group_at(b_, c) ((b_)->g[coord_raw(c)])
209 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
211 /* Warning! Neighbor count is kept up-to-date for S_NONE! */
212 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord_raw(coord)].colors[(enum stone) color])
213 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
214 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
215 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
216 #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))
218 #define groupnext_at(b_, c) ((b_)->p[coord_raw(c)])
219 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
221 #define group_base(g_) (g_)
222 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
223 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
224 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
226 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord_raw(coord)])
228 struct board
*board_init(void);
229 struct board
*board_copy(struct board
*board2
, struct board
*board1
);
230 void board_done_noalloc(struct board
*board
);
231 void board_done(struct board
*board
);
232 /* size here is without the S_OFFBOARD margin. */
233 void board_resize(struct board
*board
, int size
);
234 void board_clear(struct board
*board
);
237 typedef void (*board_cprint
)(struct board
*b
, coord_t c
, FILE *f
);
238 void board_print(struct board
*board
, FILE *f
);
239 void board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
);
241 /* Place given handicap on the board; coordinates are printed to f. */
242 void board_handicap(struct board
*board
, int stones
, FILE *f
);
244 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
245 int board_play(struct board
*board
, struct move
*m
);
246 /* Like above, but plays random move; the move coordinate is recorded
247 * to *coord. This method will never fill your own eye. pass is played
248 * when no move can be played. You can impose extra restrictions if you
249 * supply your own permit function. */
250 typedef bool (*ppr_permit
)(void *data
, struct board
*b
, struct move
*m
);
251 void board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
);
253 /* Returns true if given move can be played. */
254 static bool board_is_valid_move(struct board
*b
, struct move
*m
);
255 /* Returns true if ko was just taken. */
256 static bool board_playing_ko_threat(struct board
*b
);
257 /* Returns 0 or ID of neighboring group in atari. */
258 static group_t
board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
);
260 /* Adjust symmetry information as if given coordinate has been played. */
261 void board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
);
263 /* Returns true if given coordinate has all neighbors of given color or the edge. */
264 static bool board_is_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
);
265 /* Returns true if given coordinate could be a false eye; this check makes
266 * sense only if you already know the coordinate is_eyelike(). */
267 bool board_is_false_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
);
268 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
269 * at least tries to). */
270 bool board_is_one_point_eye(struct board
*board
, coord_t
*c
, enum stone eye_color
);
271 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
272 enum stone
board_get_one_point_eye(struct board
*board
, coord_t
*c
);
274 /* board_official_score() is the scoring method for yielding score suitable
275 * for external presentation. For fast scoring of entirely filled boards
276 * (e.g. playouts), use board_fast_score(). */
277 /* Positive: W wins */
278 /* Compare number of stones + 1pt eyes. */
279 float board_fast_score(struct board
*board
);
280 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
282 float board_official_score(struct board
*board
, struct move_queue
*mq
);
286 #define foreach_point(board_) \
288 coord_t c; coord_pos(c, 0, (board_)); \
289 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
290 #define foreach_point_and_pass(board_) \
292 coord_t c; coord_pos(c, -1, (board_)); \
293 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
294 #define foreach_point_end \
297 #define foreach_in_group(board_, group_) \
299 struct board *board__ = board_; \
300 coord_t c = group_base(group_); \
301 coord_t c2 = c; coord_raw(c2) = groupnext_at(board__, c2); \
303 #define foreach_in_group_end \
304 c = c2; coord_raw(c2) = groupnext_at(board__, c2); \
305 } while (coord_raw(c) != 0); \
308 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
309 * on S_OFFBOARD coordinates. */
310 #define foreach_neighbor(board_, coord_, loop_body) \
312 struct board *board__ = board_; \
313 coord_t coord__ = coord_; \
315 coord_pos(c, coord_raw(coord__) - 1, (board__)); do { loop_body } while (0); \
316 coord_pos(c, coord_raw(coord__) - board_size(board__), (board__)); do { loop_body } while (0); \
317 coord_pos(c, coord_raw(coord__) + 1, (board__)); do { loop_body } while (0); \
318 coord_pos(c, coord_raw(coord__) + board_size(board__), (board__)); do { loop_body } while (0); \
321 #define foreach_8neighbor(board_, coord_) \
324 coord_t c = (coord_); \
325 for (fn__i = 0; fn__i < 8; fn__i++) { \
326 c += (board_)->nei8[fn__i];
327 #define foreach_8neighbor_end \
331 #define foreach_diag_neighbor(board_, coord_) \
334 coord_t c = (coord_); \
335 for (fn__i = 0; fn__i < 4; fn__i++) { \
336 c += (board_)->dnei[fn__i];
337 #define foreach_diag_neighbor_end \
343 board_is_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
345 return (neighbor_count_at(board
, *coord
, eye_color
)
346 + neighbor_count_at(board
, *coord
, S_OFFBOARD
)) == 4;
350 board_is_valid_move(struct board
*board
, struct move
*m
)
352 if (board_at(board
, m
->coord
) != S_NONE
)
354 if (!board_is_eyelike(board
, &m
->coord
, stone_other(m
->color
)))
356 /* Play within {true,false} eye-ish formation */
357 if (board
->ko
.coord
== m
->coord
&& board
->ko
.color
== m
->color
)
359 int groups_in_atari
= 0;
360 foreach_neighbor(board
, m
->coord
, {
361 group_t g
= group_at(board
, c
);
362 groups_in_atari
+= (board_group_info(board
, g
).libs
== 1);
364 return !!groups_in_atari
;
368 board_playing_ko_threat(struct board
*b
)
370 return !is_pass(b
->ko
.coord
);
373 static inline group_t
374 board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
)
377 if (!b
->t
[coord
].cap
) return 0;
379 foreach_neighbor(b
, coord
, {
380 group_t g
= group_at(b
, c
);
381 if (g
&& board_at(b
, c
) == group_color
&& board_group_info(b
, g
).libs
== 1)
383 /* We return first match. */