13 /* The board implementation has bunch of optional features.
14 * Turn them on below: */
15 #define WANT_BOARD_C // capturable groups queue
16 //#define BOARD_SIZE 9 // constant board size, allows better optimization
17 //#define BOARD_SPATHASH // incremental patternsp.h hashes
18 #define BOARD_SPATHASH_MAXD 3 // maximal diameter
19 #define BOARD_PAT3 // incremental 3x3 pattern codes
22 /* To avoid running out of time, assume we always have at least 10 more moves to play
23 * if we don't have more precise information from gtp time_left: */
24 #define MIN_MOVES_LEFT 10
26 /* Allow board_play_random_move() to return pass even when
27 * there are other moves available. */
28 extern bool random_pass
;
31 /* Some engines might normalize their reading and skip symmetrical
32 * moves. We will tell them how can they do it. */
33 struct board_symmetry
{
34 /* Playground is in this rectangle. */
36 /* d == 0: Full rectangle
37 * d == 1: Top triangle */
39 /* General symmetry type. */
40 /* Note that the above is redundant to this, but just provided
41 * for easier usage. */
53 typedef uint64_t hash_t
;
54 #define PRIhash PRIx64
57 /* Note that "group" is only chain of stones that is solidly
58 * connected for us. */
59 typedef coord_t group_t
;
62 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
64 /* _Combination_ of these two values can make some difference
65 * in performance - fine-tune. */
66 #define GROUP_KEEP_LIBS 10
67 // refill lib[] only when we hit this; this must be at least 2!
68 // Moggy requires at least 3 - see below for semantic impact.
69 #define GROUP_REFILL_LIBS 5
70 coord_t lib
[GROUP_KEEP_LIBS
];
71 /* libs is only LOWER BOUND for the number of real liberties!!!
72 * It denotes only number of items in lib[], thus you can rely
73 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
77 struct neighbor_colors
{
81 /* You should treat this struct as read-only. Always call functions below if
82 * you want to change it. */
85 int size
; /* Including S_OFFBOARD margin - see below. */
86 int size2
; /* size^2 */
91 /* Iterator offsets for foreach_neighbor*() */
95 struct move last_move
;
96 struct move last_move2
; /* second-to-last move */
97 /* Whether we tried to add a hash twice; board_play*() can
98 * set this, but it will still carry out the move as well! */
99 bool superko_violation
;
101 /* The following two structures are goban maps and are indexed by
102 * coord.pos. The map is surrounded by a one-point margin from
103 * S_OFFBOARD stones in order to speed up some internal loops.
104 * Some of the foreach iterators below might include these points;
105 * you need to handle them yourselves, if you need to. */
107 /* Stones played on the board */
108 enum stone
*b
; /* enum stone */
109 /* Group id the stones are part of; 0 == no group */
111 /* Positions of next stones in the stone group; 0 == last stone */
113 /* Neighboring colors; numbers of neighbors of index color */
114 struct neighbor_colors
*n
;
115 /* Zobrist hash for each position */
117 #ifdef BOARD_SPATHASH
118 /* For spatial hashes, we use only 24 bits. */
119 /* [0] is d==1, we don't keep hash for d==0. */
120 /* We keep hashes for black-to-play ([][0]) and white-to-play
121 * ([][1], reversed stone colors since we match all patterns as
123 uint32_t (*spathash
)[BOARD_SPATHASH_MAXD
][2];
126 /* 3x3 pattern code for each position; see pattern3.h for encoding
127 * specification. The information is only valid for empty points. */
131 /* Group information - indexed by gid (which is coord of base group stone) */
134 /* Positions of free positions - queue (not map) */
135 /* Note that free position here is any valid move; including single-point eyes! */
136 coord_t
*f
; int flen
;
139 /* Queue of capturable groups */
140 group_t
*c
; int clen
;
143 /* Symmetry information */
144 struct board_symmetry symmetry
;
146 /* Last ko played on the board. */
153 /* Engine-specific state; persistent through board development,
154 * is reset only at clear_board. */
157 /* Playout-specific state; persistent through board development,
158 * but its lifetime is maintained in play_random_game(); it should
159 * not be set outside of it. */
163 /* --- PRIVATE DATA --- */
165 /* For superko check: */
167 /* Board "history" - hashes encountered. Size of the hash should be
168 * >> board_size^2. */
169 #define history_hash_bits 12
170 #define history_hash_mask ((1 << history_hash_bits) - 1)
171 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
172 #define history_hash_next(i) ((i + 1) & history_hash_mask)
173 hash_t history_hash
[1 << history_hash_bits
];
174 /* Hash of current board position. */
179 /* Avoid unused variable warnings */
180 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
181 #define board_size2(b_) (board_size(b_) * board_size(b_))
183 #define board_size(b_) ((b_)->size)
184 #define board_size2(b_) ((b_)->size2)
187 #define board_at(b_, c) ((b_)->b[coord_raw(c)])
188 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
190 #define group_at(b_, c) ((b_)->g[coord_raw(c)])
191 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
193 /* Warning! Neighbor count is kept up-to-date for S_NONE! */
194 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord_raw(coord)].colors[(enum stone) color])
195 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
196 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
197 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
198 #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))
200 #define groupnext_at(b_, c) ((b_)->p[coord_raw(c)])
201 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
203 #define group_base(g_) (g_)
204 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
205 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
206 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
208 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord_raw(coord)])
210 struct board
*board_init(void);
211 struct board
*board_copy(struct board
*board2
, struct board
*board1
);
212 void board_done_noalloc(struct board
*board
);
213 void board_done(struct board
*board
);
214 /* size here is without the S_OFFBOARD margin. */
215 void board_resize(struct board
*board
, int size
);
216 void board_clear(struct board
*board
);
219 typedef void (*board_cprint
)(struct board
*b
, coord_t c
, FILE *f
);
220 void board_print(struct board
*board
, FILE *f
);
221 void board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
);
223 /* Place given handicap on the board; coordinates are printed to f. */
224 void board_handicap(struct board
*board
, int stones
, FILE *f
);
226 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
227 int board_play(struct board
*board
, struct move
*m
);
228 /* Like above, but plays random move; the move coordinate is recorded
229 * to *coord. This method will never fill your own eye. pass is played
230 * when no move can be played. You can impose extra restrictions if you
231 * supply your own permit function. */
232 typedef bool (*ppr_permit
)(void *data
, struct board
*b
, struct move
*m
);
233 void board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
);
235 /* Returns true if given move can be played. */
236 static bool board_is_valid_move(struct board
*b
, struct move
*m
);
237 /* Returns true if ko was just taken. */
238 static bool board_playing_ko_threat(struct board
*b
);
240 /* Adjust symmetry information as if given coordinate has been played. */
241 void board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
);
243 /* Returns true if given coordinate has all neighbors of given color or the edge. */
244 static bool board_is_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
);
245 /* Returns true if given coordinate could be a false eye; this check makes
246 * sense only if you already know the coordinate is_eyelike(). */
247 bool board_is_false_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
);
248 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
249 * at least tries to). */
250 bool board_is_one_point_eye(struct board
*board
, coord_t
*c
, enum stone eye_color
);
251 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
252 enum stone
board_get_one_point_eye(struct board
*board
, coord_t
*c
);
254 /* board_official_score() is the scoring method for yielding score suitable
255 * for external presentation. For fast scoring of entirely filled boards
256 * (e.g. playouts), use board_fast_score(). */
257 /* Positive: W wins */
258 /* Compare number of stones + 1pt eyes. */
259 float board_fast_score(struct board
*board
);
260 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
262 float board_official_score(struct board
*board
, struct move_queue
*mq
);
264 /* Returns estimated number of remaining moves for one player until end of game. */
265 int board_estimated_moves_left(struct board
*b
);
269 #define foreach_point(board_) \
271 coord_t c; coord_pos(c, 0, (board_)); \
272 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
273 #define foreach_point_and_pass(board_) \
275 coord_t c; coord_pos(c, -1, (board_)); \
276 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
277 #define foreach_point_end \
280 #define foreach_in_group(board_, group_) \
282 struct board *board__ = board_; \
283 coord_t c = group_base(group_); \
284 coord_t c2 = c; coord_raw(c2) = groupnext_at(board__, c2); \
286 #define foreach_in_group_end \
287 c = c2; coord_raw(c2) = groupnext_at(board__, c2); \
288 } while (coord_raw(c) != 0); \
291 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
292 * on S_OFFBOARD coordinates. */
293 #define foreach_neighbor(board_, coord_, loop_body) \
295 struct board *board__ = board_; \
296 coord_t coord__ = coord_; \
298 coord_pos(c, coord_raw(coord__) - 1, (board__)); do { loop_body } while (0); \
299 coord_pos(c, coord_raw(coord__) - board_size(board__), (board__)); do { loop_body } while (0); \
300 coord_pos(c, coord_raw(coord__) + 1, (board__)); do { loop_body } while (0); \
301 coord_pos(c, coord_raw(coord__) + board_size(board__), (board__)); do { loop_body } while (0); \
304 #define foreach_8neighbor(board_, coord_) \
307 coord_t c = (coord_); \
308 for (fn__i = 0; fn__i < 8; fn__i++) { \
309 c += (board_)->nei8[fn__i];
310 #define foreach_8neighbor_end \
314 #define foreach_diag_neighbor(board_, coord_) \
317 coord_t c = (coord_); \
318 for (fn__i = 0; fn__i < 4; fn__i++) { \
319 c += (board_)->dnei[fn__i];
320 #define foreach_diag_neighbor_end \
326 board_is_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
)
328 return (neighbor_count_at(board
, *coord
, eye_color
)
329 + neighbor_count_at(board
, *coord
, S_OFFBOARD
)) == 4;
333 board_is_valid_move(struct board
*board
, struct move
*m
)
335 if (board_at(board
, m
->coord
) != S_NONE
)
337 if (!board_is_eyelike(board
, &m
->coord
, stone_other(m
->color
)))
339 /* Play within {true,false} eye-ish formation */
340 if (board
->ko
.coord
== m
->coord
&& board
->ko
.color
== m
->color
)
342 int groups_in_atari
= 0;
343 foreach_neighbor(board
, m
->coord
, {
344 group_t g
= group_at(board
, c
);
345 groups_in_atari
+= (board_group_info(board
, g
).libs
== 1);
347 return !!groups_in_atari
;
351 board_playing_ko_threat(struct board
*b
)
353 return !is_pass(b
->ko
.coord
);