patterns.gammaf: Regenerate with correct patternset
[pachi/json.git] / board.h
blob292185b989832d675f54e50948b22e2e32034764
1 #ifndef ZZGO_BOARD_H
2 #define ZZGO_BOARD_H
4 #include <inttypes.h>
5 #include <stdbool.h>
6 #include <stdint.h>
8 #include "stone.h"
9 #include "move.h"
10 #include "util.h"
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
21 /* Allow board_play_random_move() to return pass even when
22 * there are other moves available. */
23 extern bool random_pass;
26 /* Some engines might normalize their reading and skip symmetrical
27 * moves. We will tell them how can they do it. */
28 struct board_symmetry {
29 /* Playground is in this rectangle. */
30 int x1, x2, y1, y2;
31 /* d == 0: Full rectangle
32 * d == 1: Top triangle */
33 int d;
34 /* General symmetry type. */
35 /* Note that the above is redundant to this, but just provided
36 * for easier usage. */
37 enum {
38 SYM_FULL,
39 SYM_DIAG_UP,
40 SYM_DIAG_DOWN,
41 SYM_HORIZ,
42 SYM_VERT,
43 SYM_NONE
44 } type;
48 typedef uint64_t hash_t;
49 #define PRIhash PRIx64
52 /* Note that "group" is only chain of stones that is solidly
53 * connected for us. */
54 typedef coord_t group_t;
56 struct group {
57 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
58 * don't care. */
59 /* _Combination_ of these two values can make some difference
60 * in performance - fine-tune. */
61 #define GROUP_KEEP_LIBS 10
62 // refill lib[] only when we hit this; this must be at least 2!
63 // Moggy requires at least 3 - see below for semantic impact.
64 #define GROUP_REFILL_LIBS 5
65 coord_t lib[GROUP_KEEP_LIBS];
66 /* libs is only LOWER BOUND for the number of real liberties!!!
67 * It denotes only number of items in lib[], thus you can rely
68 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
69 int libs;
72 struct neighbor_colors {
73 char colors[S_MAX];
76 /* You should treat this struct as read-only. Always call functions below if
77 * you want to change it. */
79 struct board {
80 int size; /* Including S_OFFBOARD margin - see below. */
81 int size2; /* size^2 */
82 int captures[S_MAX];
83 float komi;
84 int handicap;
86 int moves;
87 struct move last_move;
88 struct move last_move2; /* second-to-last move */
89 /* Whether we tried to add a hash twice; board_play*() can
90 * set this, but it will still carry out the move as well! */
91 bool superko_violation;
93 /* The following two structures are goban maps and are indexed by
94 * coord.pos. The map is surrounded by a one-point margin from
95 * S_OFFBOARD stones in order to speed up some internal loops.
96 * Some of the foreach iterators below might include these points;
97 * you need to handle them yourselves, if you need to. */
99 /* Stones played on the board */
100 enum stone *b; /* enum stone */
101 /* Group id the stones are part of; 0 == no group */
102 group_t *g;
103 /* Positions of next stones in the stone group; 0 == last stone */
104 coord_t *p;
105 /* Neighboring colors; numbers of neighbors of index color */
106 struct neighbor_colors *n;
107 /* Zobrist hash for each position */
108 hash_t *h;
109 #ifdef BOARD_SPATHASH
110 /* For spatial hashes, we use only 24 bits. */
111 /* [0] is d==1, we don't keep hash for d==0. */
112 /* We keep hashes for black-to-play ([][0]) and white-to-play
113 * ([][1], reversed stone colors since we match all patterns as
114 * black-to-play). */
115 uint32_t (*spathash)[BOARD_SPATHASH_MAXD][2];
116 #endif
118 /* Group information - indexed by gid (which is coord of base group stone) */
119 struct group *gi;
121 /* Positions of free positions - queue (not map) */
122 /* Note that free position here is any valid move; including single-point eyes! */
123 coord_t *f; int flen;
125 #ifdef WANT_BOARD_C
126 /* Queue of capturable groups */
127 group_t *c; int clen;
128 #endif
130 /* Symmetry information */
131 struct board_symmetry symmetry;
133 /* Last ko played on the board. */
134 struct move last_ko;
135 int last_ko_age;
137 /* Basic ko check */
138 struct move ko;
140 /* Engine-specific state; persistent through board development,
141 * is reset only at clear_board. */
142 void *es;
144 /* Playout-specific state; persistent through board development,
145 * but its lifetime is maintained in play_random_game(); it should
146 * not be set outside of it. */
147 void *ps;
150 /* --- PRIVATE DATA --- */
152 /* For superko check: */
154 /* Board "history" - hashes encountered. Size of the hash should be
155 * >> board_size^2. */
156 #define history_hash_bits 12
157 #define history_hash_mask ((1 << history_hash_bits) - 1)
158 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
159 #define history_hash_next(i) ((i + 1) & history_hash_mask)
160 hash_t history_hash[1 << history_hash_bits];
161 /* Hash of current board position. */
162 hash_t hash;
165 #ifdef BOARD_SIZE
166 /* Avoid unused variable warnings */
167 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
168 #define board_size2(b_) (board_size(b_) * board_size(b_))
169 #else
170 #define board_size(b_) ((b_)->size)
171 #define board_size2(b_) ((b_)->size2)
172 #endif
174 #define board_at(b_, c) ((b_)->b[coord_raw(c)])
175 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
177 #define group_at(b_, c) ((b_)->g[coord_raw(c)])
178 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
180 /* Warning! Neighbor count is kept up-to-date for S_NONE! */
181 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord_raw(coord)].colors[(enum stone) color])
182 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
183 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
184 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
185 #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))
187 #define groupnext_at(b_, c) ((b_)->p[coord_raw(c)])
188 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
190 #define group_base(g_) (g_)
191 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
192 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
193 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
195 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord_raw(coord)])
197 struct board *board_init(void);
198 struct board *board_copy(struct board *board2, struct board *board1);
199 void board_done_noalloc(struct board *board);
200 void board_done(struct board *board);
201 /* size here is without the S_OFFBOARD margin. */
202 void board_resize(struct board *board, int size);
203 /* The caller must take care of releasing board.es first. */
204 void board_clear(struct board *board);
206 struct FILE;
207 typedef void (*board_cprint)(struct board *b, coord_t c, FILE *f);
208 void board_print(struct board *board, FILE *f);
209 void board_print_custom(struct board *board, FILE *f, board_cprint cprint);
211 /* Place given handicap on the board; coordinates are printed to f. */
212 void board_handicap(struct board *board, int stones, FILE *f);
214 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
215 int board_play(struct board *board, struct move *m);
216 /* Like above, but plays random move; the move coordinate is recorded
217 * to *coord. This method will never fill your own eye. pass is played
218 * when no move can be played. You can impose extra restrictions if you
219 * supply your own permit function. */
220 typedef bool (*ppr_permit)(void *data, struct board *b, struct move *m);
221 void board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data);
223 /* Returns true if given move can be played. */
224 static bool board_is_valid_move(struct board *b, struct move *m);
225 /* Returns true if ko was just taken. */
226 static bool board_playing_ko_threat(struct board *b);
228 /* Adjust symmetry information as if given coordinate has been played. */
229 void board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c);
231 /* Returns true if given coordinate has all neighbors of given color or the edge. */
232 static bool board_is_eyelike(struct board *board, coord_t *coord, enum stone eye_color);
233 /* Returns true if given coordinate could be a false eye; this check makes
234 * sense only if you already know the coordinate is_eyelike(). */
235 bool board_is_false_eyelike(struct board *board, coord_t *coord, enum stone eye_color);
236 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
237 * at least tries to). */
238 bool board_is_one_point_eye(struct board *board, coord_t *c, enum stone eye_color);
239 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
240 enum stone board_get_one_point_eye(struct board *board, coord_t *c);
242 /* board_official_score() is the scoring method for yielding score suitable
243 * for external presentation. For fast scoring of entirely filled boards
244 * (e.g. playouts), use board_fast_score(). */
245 /* Positive: W wins */
246 /* Compare number of stones + 1pt eyes. */
247 float board_fast_score(struct board *board);
248 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
249 struct move_queue;
250 float board_official_score(struct board *board, struct move_queue *mq);
253 /** Iterators */
255 #define foreach_point(board_) \
256 do { \
257 coord_t c; coord_pos(c, 0, (board_)); \
258 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
259 #define foreach_point_and_pass(board_) \
260 do { \
261 coord_t c; coord_pos(c, -1, (board_)); \
262 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
263 #define foreach_point_end \
264 } while (0)
266 #define foreach_in_group(board_, group_) \
267 do { \
268 struct board *board__ = board_; \
269 coord_t c = group_base(group_); \
270 coord_t c2 = c; coord_raw(c2) = groupnext_at(board__, c2); \
271 do {
272 #define foreach_in_group_end \
273 c = c2; coord_raw(c2) = groupnext_at(board__, c2); \
274 } while (coord_raw(c) != 0); \
275 } while (0)
277 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
278 * on S_OFFBOARD coordinates. */
279 #define foreach_neighbor(board_, coord_, loop_body) \
280 do { \
281 struct board *board__ = board_; \
282 coord_t coord__ = coord_; \
283 coord_t c; \
284 coord_pos(c, coord_raw(coord__) - 1, (board__)); do { loop_body } while (0); \
285 coord_pos(c, coord_raw(coord__) - board_size(board__), (board__)); do { loop_body } while (0); \
286 coord_pos(c, coord_raw(coord__) + 1, (board__)); do { loop_body } while (0); \
287 coord_pos(c, coord_raw(coord__) + board_size(board__), (board__)); do { loop_body } while (0); \
288 } while (0)
290 #define foreach_8neighbor(board_, coord_) \
291 do { \
292 coord_t q__[8]; int q__i = 0; \
293 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) - 1, (board_)); \
294 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_), (board_)); \
295 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) + 1, (board_)); \
296 coord_pos(q__[q__i++], coord_raw(coord_) - 1, (board_)); \
297 coord_pos(q__[q__i++], coord_raw(coord_) + 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_), (board_)); \
300 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_) + 1, (board_)); \
301 int fn__i; \
302 for (fn__i = 0; fn__i < q__i; fn__i++) { \
303 coord_t c = q__[fn__i];
304 #define foreach_8neighbor_end \
306 } while (0)
308 #define foreach_diag_neighbor(board_, coord_) \
309 do { \
310 coord_t q__[4]; int q__i = 0; \
311 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) - 1, (board_)); \
312 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) + 1, (board_)); \
313 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_) - 1, (board_)); \
314 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_) + 1, (board_)); \
315 int fn__i; \
316 for (fn__i = 0; fn__i < q__i; fn__i++) { \
317 coord_t c = q__[fn__i];
318 #define foreach_diag_neighbor_end \
320 } while (0)
323 static inline bool
324 board_is_eyelike(struct board *board, coord_t *coord, enum stone eye_color)
326 return (neighbor_count_at(board, *coord, eye_color)
327 + neighbor_count_at(board, *coord, S_OFFBOARD)) == 4;
330 static inline bool
331 board_is_valid_move(struct board *board, struct move *m)
333 if (board_at(board, m->coord) != S_NONE)
334 return false;
335 if (!board_is_eyelike(board, &m->coord, stone_other(m->color)))
336 return true;
337 /* Play within {true,false} eye-ish formation */
338 if (board->ko.coord == m->coord && board->ko.color == m->color)
339 return false;
340 int groups_in_atari = 0;
341 foreach_neighbor(board, m->coord, {
342 group_t g = group_at(board, c);
343 groups_in_atari += (board_group_info(board, g).libs == 1);
345 return !!groups_in_atari;
348 static inline bool
349 board_playing_ko_threat(struct board *b)
351 return !is_pass(b->ko.coord);
354 #endif