10 #define likely(x) __builtin_expect(!!(x), 1)
11 #define unlikely(x) __builtin_expect((x), 0)
14 /* The board implementation has bunch of optional features.
15 * Turn them on below: */
16 #define WANT_BOARD_C // required by playout_moggy
17 //#define BOARD_SIZE 9 // constant board size, allows better optimization
20 /* Allow board_play_random_move() to return pass even when
21 * there are other moves available. */
22 extern bool random_pass
;
25 /* Some engines might normalize their reading and skip symmetrical
26 * moves. We will tell them how can they do it. */
27 struct board_symmetry
{
28 /* Playground is in this rectangle. */
30 /* d == 0: Full rectangle
31 * d == 1: Top triangle */
33 /* General symmetry type. */
34 /* Note that the above is redundant to this, but just provided
35 * for easier usage. */
47 typedef uint64_t hash_t
;
50 /* Note that "group" is only chain of stones that is solidly
51 * connected for us. */
52 typedef coord_t group_t
;
55 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
57 /* _Combination_ of these two values can make some difference
58 * in performance - fine-tune. */
59 #define GROUP_KEEP_LIBS 10
60 // refill lib[] only when we hit this; this must be at least 2!
61 // Moggy requires at least 3 - see below for semantic impact.
62 #define GROUP_REFILL_LIBS 5
63 coord_t lib
[GROUP_KEEP_LIBS
];
64 /* libs is only LOWER BOUND for the number of real liberties!!!
65 * It denotes only number of items in lib[], thus you can rely
66 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
70 struct neighbor_colors
{
74 /* You should treat this struct as read-only. Always call functions below if
75 * you want to change it. */
78 int size
; /* Including S_OFFBOARD margin - see below. */
79 int size2
; /* size^2 */
85 struct move last_move
;
86 /* Whether we tried to add a hash twice; board_play*() can
87 * set this, but it will still carry out the move as well! */
88 bool superko_violation
;
90 /* The following two structures are goban maps and are indexed by
91 * coord.pos. The map is surrounded by a one-point margin from
92 * S_OFFBOARD stones in order to speed up some internal loops.
93 * Some of the foreach iterators below might include these points;
94 * you need to handle them yourselves, if you need to. */
96 /* Stones played on the board */
97 enum stone
*b
; /* enum stone */
98 /* Group id the stones are part of; 0 == no group */
100 /* Positions of next stones in the stone group; 0 == last stone */
102 /* Neighboring colors; numbers of neighbors of index color */
103 struct neighbor_colors
*n
;
104 /* Zobrist hash for each position */
107 /* Group information - indexed by gid (which is coord of base group stone) */
110 /* Positions of free positions - queue (not map) */
111 /* Note that free position here is any valid move; including single-point eyes! */
112 coord_t
*f
; int flen
;
115 /* Queue of capturable groups */
116 group_t
*c
; int clen
;
119 /* Symmetry information */
120 struct board_symmetry symmetry
;
123 /* --- PRIVATE DATA --- */
128 /* For superko check: */
130 /* Board "history" - hashes encountered. Size of the hash should be
131 * >> board_size^2. */
132 #define history_hash_bits 12
133 #define history_hash_mask ((1 << history_hash_bits) - 1)
134 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
135 #define history_hash_next(i) ((i + 1) & history_hash_mask)
136 hash_t history_hash
[1 << history_hash_bits
];
137 /* Hash of current board position. */
142 /* Avoid unused variable warnings */
143 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
144 #define board_size2(b_) (board_size(b_) * board_size(b_))
146 #define board_size(b_) ((b_)->size)
147 #define board_size2(b_) ((b_)->size2)
150 #define board_at(b_, c) ((b_)->b[coord_raw(c)])
151 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
153 #define group_at(b_, c) ((b_)->g[coord_raw(c)])
154 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
156 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord_raw(coord)].colors[(enum stone) color])
157 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
158 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
159 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
160 #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))
162 #define groupnext_at(b_, c) ((b_)->p[coord_raw(c)])
163 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
165 #define group_base(g_) (g_)
166 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
167 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
168 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
170 #define hash_at(b_, coord, color) (b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord_raw(coord)]
172 struct board
*board_init(void);
173 struct board
*board_copy(struct board
*board2
, struct board
*board1
);
174 void board_done_noalloc(struct board
*board
);
175 void board_done(struct board
*board
);
176 /* size here is without the S_OFFBOARD margin. */
177 void board_resize(struct board
*board
, int size
);
178 void board_clear(struct board
*board
);
181 void board_print(struct board
*board
, FILE *f
);
183 /* Place given handicap on the board; coordinates are printed to f. */
184 void board_handicap(struct board
*board
, int stones
, FILE *f
);
186 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
187 int board_play(struct board
*board
, struct move
*m
);
188 /* Like above, but plays random move; the move coordinate is recorded
189 * to *coord. This method will never fill your own eye. pass is played
190 * when no move can be played. You can impose extra restrictions if you
191 * supply your own permit function. */
192 typedef bool (*ppr_permit
)(void *data
, struct board
*b
, struct move
*m
);
193 void board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
);
195 /* Adjust symmetry information as if given coordinate has been played. */
196 void board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
);
198 /* Returns true if given coordinate has all neighbors of given color or the edge. */
199 bool board_is_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
);
200 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
201 * at least tries to). */
202 bool board_is_one_point_eye(struct board
*board
, coord_t
*c
, enum stone eye_color
);
203 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
204 enum stone
board_get_one_point_eye(struct board
*board
, coord_t
*c
);
206 /* Check if group is in atari. This is very fast.
207 * The last liberty is recorded to lastlib (content is undefined if group
208 * is not in atari). */
209 bool board_group_in_atari(struct board
*board
, group_t group
, coord_t
*lastlib
);
211 /* Check if group can be put in atari. This is also very fast.
212 * The last two liberties are recorded to lastlib (content is undefined if group
213 * can't be put in atari). */
214 bool board_group_can_atari(struct board
*board
, group_t group
, coord_t lastlib
[2]);
216 /* board_official_score() is the scoring method for yielding score suitable
217 * for external presentation. For fast scoring of entirely filled boards
218 * (e.g. playouts), use board_fast_score(). */
219 /* Positive: W wins */
220 /* Tromp-Taylor scoring. */
221 float board_official_score(struct board
*board
);
222 /* Compare number of stones + 1pt eyes. */
223 float board_fast_score(struct board
*board
);
225 /* Check if this move is self-atari. This also in fact assesses if it is
226 * desirable to pull out from atari by this move. */
227 bool is_selfatari(struct board
*b
, enum stone color
, coord_t to
);
229 /* Checks if there are any stones in n-vincinity of coord. */
230 bool board_stone_radar(struct board
*b
, coord_t coord
, int distance
);
235 #define foreach_point(board_) \
237 coord_t c; coord_pos(c, 0, (board_)); \
238 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
239 #define foreach_point_end \
242 #define foreach_in_group(board_, group_) \
244 struct board *board__ = board_; \
245 coord_t c = group_base(group_); \
246 coord_t c2 = c; coord_raw(c2) = groupnext_at(board__, c2); \
248 #define foreach_in_group_end \
249 c = c2; coord_raw(c2) = groupnext_at(board__, c2); \
250 } while (coord_raw(c) != 0); \
253 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
254 * on S_OFFBOARD coordinates. */
255 #define foreach_neighbor(board_, coord_, loop_body) \
257 struct board *board__ = board_; \
258 coord_t coord__ = coord_; \
260 coord_pos(c, coord_raw(coord__) - 1, (board__)); do { loop_body } while (0); \
261 coord_pos(c, coord_raw(coord__) - board_size(board__), (board__)); do { loop_body } while (0); \
262 coord_pos(c, coord_raw(coord__) + 1, (board__)); do { loop_body } while (0); \
263 coord_pos(c, coord_raw(coord__) + board_size(board__), (board__)); do { loop_body } while (0); \
266 #define foreach_diag_neighbor(board_, coord_) \
268 coord_t q__[4]; int q__i = 0; \
269 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) - 1, (board_)); \
270 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) + 1, (board_)); \
271 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_) - 1, (board_)); \
272 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_) + 1, (board_)); \
274 for (fn__i = 0; fn__i < q__i; fn__i++) { \
275 coord_t c = q__[fn__i];
276 #define foreach_diag_neighbor_end \