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