Autotest show: Better support declared-but-nonexisting pairings
[pachi.git] / board.h
blob5b192fb9e4f4b4072847c1d9dee7db524bfb9103
1 #ifndef ZZGO_BOARD_H
2 #define ZZGO_BOARD_H
4 #include <stdbool.h>
5 #include <stdint.h>
7 #include "stone.h"
8 #include "move.h"
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. */
29 int x1, x2, y1, y2;
30 /* d == 0: Full rectangle
31 * d == 1: Top triangle */
32 int d;
33 /* General symmetry type. */
34 /* Note that the above is redundant to this, but just provided
35 * for easier usage. */
36 enum {
37 SYM_FULL,
38 SYM_DIAG_UP,
39 SYM_DIAG_DOWN,
40 SYM_HORIZ,
41 SYM_VERT,
42 SYM_NONE
43 } type;
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;
54 struct group {
55 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
56 * don't care. */
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. */
67 int libs;
70 struct neighbor_colors {
71 char colors[S_MAX];
74 /* You should treat this struct as read-only. Always call functions below if
75 * you want to change it. */
77 struct board {
78 int size; /* Including S_OFFBOARD margin - see below. */
79 int size2; /* size^2 */
80 int captures[S_MAX];
81 float komi;
82 int handicap;
84 int moves;
85 struct move last_move;
86 struct move last_move2; /* second-to-last move */
87 /* Whether we tried to add a hash twice; board_play*() can
88 * set this, but it will still carry out the move as well! */
89 bool superko_violation;
91 /* The following two structures are goban maps and are indexed by
92 * coord.pos. The map is surrounded by a one-point margin from
93 * S_OFFBOARD stones in order to speed up some internal loops.
94 * Some of the foreach iterators below might include these points;
95 * you need to handle them yourselves, if you need to. */
97 /* Stones played on the board */
98 enum stone *b; /* enum stone */
99 /* Group id the stones are part of; 0 == no group */
100 group_t *g;
101 /* Positions of next stones in the stone group; 0 == last stone */
102 coord_t *p;
103 /* Neighboring colors; numbers of neighbors of index color */
104 struct neighbor_colors *n;
105 /* Zobrist hash for each position */
106 hash_t *h;
108 /* Group information - indexed by gid (which is coord of base group stone) */
109 struct group *gi;
111 /* Positions of free positions - queue (not map) */
112 /* Note that free position here is any valid move; including single-point eyes! */
113 coord_t *f; int flen;
115 #ifdef WANT_BOARD_C
116 /* Queue of capturable groups */
117 group_t *c; int clen;
118 #endif
120 /* Symmetry information */
121 struct board_symmetry symmetry;
123 /* Last ko played on the board. */
124 struct move last_ko;
125 int last_ko_age;
127 /* Basic ko check */
128 struct move ko;
130 /* Engine-specific state; persistent through board development,
131 * is reset only at clear_board. */
132 void *es;
135 /* --- PRIVATE DATA --- */
137 /* For superko check: */
139 /* Board "history" - hashes encountered. Size of the hash should be
140 * >> board_size^2. */
141 #define history_hash_bits 12
142 #define history_hash_mask ((1 << history_hash_bits) - 1)
143 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
144 #define history_hash_next(i) ((i + 1) & history_hash_mask)
145 hash_t history_hash[1 << history_hash_bits];
146 /* Hash of current board position. */
147 hash_t hash;
150 #ifdef BOARD_SIZE
151 /* Avoid unused variable warnings */
152 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
153 #define board_size2(b_) (board_size(b_) * board_size(b_))
154 #else
155 #define board_size(b_) ((b_)->size)
156 #define board_size2(b_) ((b_)->size2)
157 #endif
159 #define board_at(b_, c) ((b_)->b[coord_raw(c)])
160 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
162 #define group_at(b_, c) ((b_)->g[coord_raw(c)])
163 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
165 /* Warning! Neighbor count is kept up-to-date for S_NONE! */
166 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord_raw(coord)].colors[(enum stone) color])
167 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
168 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
169 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
170 #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))
172 #define groupnext_at(b_, c) ((b_)->p[coord_raw(c)])
173 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
175 #define group_base(g_) (g_)
176 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
177 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
178 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
180 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord_raw(coord)])
182 struct board *board_init(void);
183 struct board *board_copy(struct board *board2, struct board *board1);
184 void board_done_noalloc(struct board *board);
185 void board_done(struct board *board);
186 /* size here is without the S_OFFBOARD margin. */
187 void board_resize(struct board *board, int size);
188 /* The caller must take care of releasing board.es first. */
189 void board_clear(struct board *board);
191 struct FILE;
192 typedef void (*board_cprint)(struct board *b, coord_t c, FILE *f);
193 void board_print(struct board *board, FILE *f);
194 void board_print_custom(struct board *board, FILE *f, board_cprint cprint);
196 /* Place given handicap on the board; coordinates are printed to f. */
197 void board_handicap(struct board *board, int stones, FILE *f);
199 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
200 int board_play(struct board *board, struct move *m);
201 /* Like above, but plays random move; the move coordinate is recorded
202 * to *coord. This method will never fill your own eye. pass is played
203 * when no move can be played. You can impose extra restrictions if you
204 * supply your own permit function. */
205 typedef bool (*ppr_permit)(void *data, struct board *b, struct move *m);
206 void board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data);
208 /* Returns true if given move can be played. */
209 static bool board_is_valid_move(struct board *b, struct move *m);
210 /* Returns true if ko was just taken. */
211 static bool board_playing_ko_threat(struct board *b);
213 /* Adjust symmetry information as if given coordinate has been played. */
214 void board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c);
216 /* Returns true if given coordinate has all neighbors of given color or the edge. */
217 static bool board_is_eyelike(struct board *board, coord_t *coord, enum stone eye_color);
218 /* Returns true if given coordinate could be a false eye; this check makes
219 * sense only if you already know the coordinate is_eyelike(). */
220 bool board_is_false_eyelike(struct board *board, coord_t *coord, enum stone eye_color);
221 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
222 * at least tries to). */
223 bool board_is_one_point_eye(struct board *board, coord_t *c, enum stone eye_color);
224 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
225 enum stone board_get_one_point_eye(struct board *board, coord_t *c);
227 /* board_official_score() is the scoring method for yielding score suitable
228 * for external presentation. For fast scoring of entirely filled boards
229 * (e.g. playouts), use board_fast_score(). */
230 /* Positive: W wins */
231 /* Compare number of stones + 1pt eyes. */
232 float board_fast_score(struct board *board);
233 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
234 struct move_queue;
235 float board_official_score(struct board *board, struct move_queue *mq);
238 /** Iterators */
240 #define foreach_point(board_) \
241 do { \
242 coord_t c; coord_pos(c, 0, (board_)); \
243 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
244 #define foreach_point_and_pass(board_) \
245 do { \
246 coord_t c; coord_pos(c, -1, (board_)); \
247 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
248 #define foreach_point_end \
249 } while (0)
251 #define foreach_in_group(board_, group_) \
252 do { \
253 struct board *board__ = board_; \
254 coord_t c = group_base(group_); \
255 coord_t c2 = c; coord_raw(c2) = groupnext_at(board__, c2); \
256 do {
257 #define foreach_in_group_end \
258 c = c2; coord_raw(c2) = groupnext_at(board__, c2); \
259 } while (coord_raw(c) != 0); \
260 } while (0)
262 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
263 * on S_OFFBOARD coordinates. */
264 #define foreach_neighbor(board_, coord_, loop_body) \
265 do { \
266 struct board *board__ = board_; \
267 coord_t coord__ = coord_; \
268 coord_t c; \
269 coord_pos(c, coord_raw(coord__) - 1, (board__)); do { loop_body } while (0); \
270 coord_pos(c, coord_raw(coord__) - board_size(board__), (board__)); do { loop_body } while (0); \
271 coord_pos(c, coord_raw(coord__) + 1, (board__)); do { loop_body } while (0); \
272 coord_pos(c, coord_raw(coord__) + board_size(board__), (board__)); do { loop_body } while (0); \
273 } while (0)
275 #define foreach_8neighbor(board_, coord_) \
276 do { \
277 coord_t q__[8]; int q__i = 0; \
278 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) - 1, (board_)); \
279 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_), (board_)); \
280 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) + 1, (board_)); \
281 coord_pos(q__[q__i++], coord_raw(coord_) - 1, (board_)); \
282 coord_pos(q__[q__i++], coord_raw(coord_) + 1, (board_)); \
283 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_) - 1, (board_)); \
284 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_), (board_)); \
285 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_) + 1, (board_)); \
286 int fn__i; \
287 for (fn__i = 0; fn__i < q__i; fn__i++) { \
288 coord_t c = q__[fn__i];
289 #define foreach_8neighbor_end \
291 } while (0)
293 #define foreach_diag_neighbor(board_, coord_) \
294 do { \
295 coord_t q__[4]; int q__i = 0; \
296 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) - 1, (board_)); \
297 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) + 1, (board_)); \
298 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_) - 1, (board_)); \
299 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_) + 1, (board_)); \
300 int fn__i; \
301 for (fn__i = 0; fn__i < q__i; fn__i++) { \
302 coord_t c = q__[fn__i];
303 #define foreach_diag_neighbor_end \
305 } while (0)
308 static inline bool
309 board_is_eyelike(struct board *board, coord_t *coord, enum stone eye_color)
311 return (neighbor_count_at(board, *coord, eye_color)
312 + neighbor_count_at(board, *coord, S_OFFBOARD)) == 4;
315 static inline bool
316 board_is_valid_move(struct board *board, struct move *m)
318 if (board_at(board, m->coord) != S_NONE)
319 return false;
320 if (!board_is_eyelike(board, &m->coord, stone_other(m->color)))
321 return true;
322 /* Play within {true,false} eye-ish formation */
323 if (board->ko.coord == m->coord && board->ko.color == m->color)
324 return false;
325 int groups_in_atari = 0;
326 foreach_neighbor(board, m->coord, {
327 group_t g = group_at(board, c);
328 groups_in_atari += (board_group_info(board, g).libs == 1);
330 return !!groups_in_atari;
333 static inline bool
334 board_playing_ko_threat(struct board *b)
336 return !is_pass(b->ko.coord);
339 #endif