Probdist: Change from cummulative probabilities back to individual ones
[pachi/json.git] / board.h
blob9c1bdbc8ccfaedd9090d054daf6c47d47e6377af
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: */
16 #define WANT_BOARD_C // capturable groups queue
18 //#define BOARD_SIZE 9 // constant board size, allows better optimization
20 //#define BOARD_SPATHASH // incremental patternsp.h hashes
21 #define BOARD_SPATHASH_MAXD 3 // maximal diameter
23 #define BOARD_PAT3 // incremental 3x3 pattern codes
25 //#define BOARD_TRAITS 1 // incremental point traits (see struct btraits)
28 /* Allow board_play_random_move() to return pass even when
29 * there are other moves available. */
30 extern bool random_pass;
33 /* Some engines might normalize their reading and skip symmetrical
34 * moves. We will tell them how can they do it. */
35 struct board_symmetry {
36 /* Playground is in this rectangle. */
37 int x1, x2, y1, y2;
38 /* d == 0: Full rectangle
39 * d == 1: Top triangle */
40 int d;
41 /* General symmetry type. */
42 /* Note that the above is redundant to this, but just provided
43 * for easier usage. */
44 enum {
45 SYM_FULL,
46 SYM_DIAG_UP,
47 SYM_DIAG_DOWN,
48 SYM_HORIZ,
49 SYM_VERT,
50 SYM_NONE
51 } type;
55 typedef uint64_t hash_t;
56 #define PRIhash PRIx64
59 /* Note that "group" is only chain of stones that is solidly
60 * connected for us. */
61 typedef coord_t group_t;
63 struct group {
64 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
65 * don't care. */
66 /* _Combination_ of these two values can make some difference
67 * in performance - fine-tune. */
68 #define GROUP_KEEP_LIBS 10
69 // refill lib[] only when we hit this; this must be at least 2!
70 // Moggy requires at least 3 - see below for semantic impact.
71 #define GROUP_REFILL_LIBS 5
72 coord_t lib[GROUP_KEEP_LIBS];
73 /* libs is only LOWER BOUND for the number of real liberties!!!
74 * It denotes only number of items in lib[], thus you can rely
75 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
76 int libs;
79 struct neighbor_colors {
80 char colors[S_MAX];
84 /* Point traits bitmap; we update this information incrementally,
85 * it can be used e.g. for fast pattern.h features matching. */
86 struct btraits {
87 /* Number of neighbors we can capture. 0=this move is
88 * not capturing, 1..4=this many neighbors we can capture
89 * (can be multiple neighbors of same group). */
90 unsigned cap:3;
91 /* Whether it is SAFE to play here. This is essentially just
92 * cached result of the macro below. (Of course the concept
93 * of "safety" is not perfect here, but it's the cheapest
94 * reasonable thing we can do.) */
95 bool safe:1;
96 #define board_safe_to_play(b_, coord_, color_) \
97 (( \
98 /* number of free neighbors, except us */ \
99 immediate_liberty_count(b_, coord_) - 1 \
100 /* number of capturable enemy groups */ \
101 + trait_at(b_, coord_, color_).cap \
102 /* number of non-capturable friendly groups */ \
103 + neighbor_count_at(b_, coord_, color_) - trait_at(b_, coord_, stone_other(color_)).cap \
104 ) > 0)
108 /* You should treat this struct as read-only. Always call functions below if
109 * you want to change it. */
111 struct board {
112 int size; /* Including S_OFFBOARD margin - see below. */
113 int size2; /* size^2 */
114 int captures[S_MAX];
115 float komi;
116 int handicap;
118 /* Iterator offsets for foreach_neighbor*() */
119 int nei8[8], dnei[4];
121 int moves;
122 struct move last_move;
123 struct move last_move2; /* second-to-last move */
124 /* Whether we tried to add a hash twice; board_play*() can
125 * set this, but it will still carry out the move as well! */
126 bool superko_violation;
128 /* The following two structures are goban maps and are indexed by
129 * coord.pos. The map is surrounded by a one-point margin from
130 * S_OFFBOARD stones in order to speed up some internal loops.
131 * Some of the foreach iterators below might include these points;
132 * you need to handle them yourselves, if you need to. */
134 /* Stones played on the board */
135 enum stone *b; /* enum stone */
136 /* Group id the stones are part of; 0 == no group */
137 group_t *g;
138 /* Positions of next stones in the stone group; 0 == last stone */
139 coord_t *p;
140 /* Neighboring colors; numbers of neighbors of index color */
141 struct neighbor_colors *n;
142 /* Zobrist hash for each position */
143 hash_t *h;
144 #ifdef BOARD_SPATHASH
145 /* For spatial hashes, we use only 24 bits. */
146 /* [0] is d==1, we don't keep hash for d==0. */
147 /* We keep hashes for black-to-play ([][0]) and white-to-play
148 * ([][1], reversed stone colors since we match all patterns as
149 * black-to-play). */
150 uint32_t (*spathash)[BOARD_SPATHASH_MAXD][2];
151 #endif
152 #ifdef BOARD_PAT3
153 /* 3x3 pattern code for each position; see pattern3.h for encoding
154 * specification. The information is only valid for empty points. */
155 uint16_t *pat3;
156 #endif
157 #ifdef BOARD_TRAITS
158 /* Incrementally matched point traits information, black-to-play
159 * ([][0]) and white-to-play ([][1]). */
160 /* The information is only valid for empty points. */
161 struct btraits (*t)[2];
162 #endif
164 /* Group information - indexed by gid (which is coord of base group stone) */
165 struct group *gi;
167 /* Positions of free positions - queue (not map) */
168 /* Note that free position here is any valid move; including single-point eyes! */
169 coord_t *f; int flen;
171 #ifdef WANT_BOARD_C
172 /* Queue of capturable groups */
173 group_t *c; int clen;
174 #endif
176 /* Symmetry information */
177 struct board_symmetry symmetry;
179 /* Last ko played on the board. */
180 struct move last_ko;
181 int last_ko_age;
183 /* Basic ko check */
184 struct move ko;
186 /* Engine-specific state; persistent through board development,
187 * is reset only at clear_board. */
188 void *es;
190 /* Playout-specific state; persistent through board development,
191 * but its lifetime is maintained in play_random_game(); it should
192 * not be set outside of it. */
193 void *ps;
196 /* --- PRIVATE DATA --- */
198 /* For superko check: */
200 /* Board "history" - hashes encountered. Size of the hash should be
201 * >> board_size^2. */
202 #define history_hash_bits 12
203 #define history_hash_mask ((1 << history_hash_bits) - 1)
204 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
205 #define history_hash_next(i) ((i + 1) & history_hash_mask)
206 hash_t history_hash[1 << history_hash_bits];
207 /* Hash of current board position. */
208 hash_t hash;
211 #ifdef BOARD_SIZE
212 /* Avoid unused variable warnings */
213 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
214 #define board_size2(b_) (board_size(b_) * board_size(b_))
215 #else
216 #define board_size(b_) ((b_)->size)
217 #define board_size2(b_) ((b_)->size2)
218 #endif
220 #define board_at(b_, c) ((b_)->b[coord_raw(c)])
221 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
223 #define group_at(b_, c) ((b_)->g[coord_raw(c)])
224 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
226 /* Warning! Neighbor count is kept up-to-date for S_NONE! */
227 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord_raw(coord)].colors[(enum stone) color])
228 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
229 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
230 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
231 #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))
233 #define trait_at(b_, coord, color) (b_)->t[coord][(color) - 1]
235 #define groupnext_at(b_, c) ((b_)->p[coord_raw(c)])
236 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
238 #define group_base(g_) (g_)
239 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
240 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
241 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
243 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord_raw(coord)])
245 struct board *board_init(void);
246 struct board *board_copy(struct board *board2, struct board *board1);
247 void board_done_noalloc(struct board *board);
248 void board_done(struct board *board);
249 /* size here is without the S_OFFBOARD margin. */
250 void board_resize(struct board *board, int size);
251 void board_clear(struct board *board);
253 struct FILE;
254 typedef void (*board_cprint)(struct board *b, coord_t c, FILE *f);
255 void board_print(struct board *board, FILE *f);
256 void board_print_custom(struct board *board, FILE *f, board_cprint cprint);
258 /* Place given handicap on the board; coordinates are printed to f. */
259 void board_handicap(struct board *board, int stones, FILE *f);
261 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
262 int board_play(struct board *board, struct move *m);
263 /* Like above, but plays random move; the move coordinate is recorded
264 * to *coord. This method will never fill your own eye. pass is played
265 * when no move can be played. You can impose extra restrictions if you
266 * supply your own permit function. */
267 typedef bool (*ppr_permit)(void *data, struct board *b, struct move *m);
268 void board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data);
270 /* Returns true if given move can be played. */
271 static bool board_is_valid_move(struct board *b, struct move *m);
272 /* Returns true if ko was just taken. */
273 static bool board_playing_ko_threat(struct board *b);
274 /* Returns 0 or ID of neighboring group in atari. */
275 static group_t board_get_atari_neighbor(struct board *b, coord_t coord, enum stone group_color);
277 /* Adjust symmetry information as if given coordinate has been played. */
278 void board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c);
280 /* Returns true if given coordinate has all neighbors of given color or the edge. */
281 static bool board_is_eyelike(struct board *board, coord_t *coord, enum stone eye_color);
282 /* Returns true if given coordinate could be a false eye; this check makes
283 * sense only if you already know the coordinate is_eyelike(). */
284 bool board_is_false_eyelike(struct board *board, coord_t *coord, enum stone eye_color);
285 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
286 * at least tries to). */
287 bool board_is_one_point_eye(struct board *board, coord_t *c, enum stone eye_color);
288 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
289 enum stone board_get_one_point_eye(struct board *board, coord_t *c);
291 /* board_official_score() is the scoring method for yielding score suitable
292 * for external presentation. For fast scoring of entirely filled boards
293 * (e.g. playouts), use board_fast_score(). */
294 /* Positive: W wins */
295 /* Compare number of stones + 1pt eyes. */
296 float board_fast_score(struct board *board);
297 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
298 struct move_queue;
299 float board_official_score(struct board *board, struct move_queue *mq);
301 /** Iterators */
303 #define foreach_point(board_) \
304 do { \
305 coord_t c; coord_pos(c, 0, (board_)); \
306 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
307 #define foreach_point_and_pass(board_) \
308 do { \
309 coord_t c; coord_pos(c, -1, (board_)); \
310 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
311 #define foreach_point_end \
312 } while (0)
314 #define foreach_in_group(board_, group_) \
315 do { \
316 struct board *board__ = board_; \
317 coord_t c = group_base(group_); \
318 coord_t c2 = c; coord_raw(c2) = groupnext_at(board__, c2); \
319 do {
320 #define foreach_in_group_end \
321 c = c2; coord_raw(c2) = groupnext_at(board__, c2); \
322 } while (coord_raw(c) != 0); \
323 } while (0)
325 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
326 * on S_OFFBOARD coordinates. */
327 #define foreach_neighbor(board_, coord_, loop_body) \
328 do { \
329 struct board *board__ = board_; \
330 coord_t coord__ = coord_; \
331 coord_t c; \
332 coord_pos(c, coord_raw(coord__) - 1, (board__)); do { loop_body } while (0); \
333 coord_pos(c, coord_raw(coord__) - board_size(board__), (board__)); do { loop_body } while (0); \
334 coord_pos(c, coord_raw(coord__) + 1, (board__)); do { loop_body } while (0); \
335 coord_pos(c, coord_raw(coord__) + board_size(board__), (board__)); do { loop_body } while (0); \
336 } while (0)
338 #define foreach_8neighbor(board_, coord_) \
339 do { \
340 int fn__i; \
341 coord_t c = (coord_); \
342 for (fn__i = 0; fn__i < 8; fn__i++) { \
343 c += (board_)->nei8[fn__i];
344 #define foreach_8neighbor_end \
346 } while (0)
348 #define foreach_diag_neighbor(board_, coord_) \
349 do { \
350 int fn__i; \
351 coord_t c = (coord_); \
352 for (fn__i = 0; fn__i < 4; fn__i++) { \
353 c += (board_)->dnei[fn__i];
354 #define foreach_diag_neighbor_end \
356 } while (0)
359 static inline bool
360 board_is_eyelike(struct board *board, coord_t *coord, enum stone eye_color)
362 return (neighbor_count_at(board, *coord, eye_color)
363 + neighbor_count_at(board, *coord, S_OFFBOARD)) == 4;
366 static inline bool
367 board_is_valid_move(struct board *board, struct move *m)
369 if (board_at(board, m->coord) != S_NONE)
370 return false;
371 if (!board_is_eyelike(board, &m->coord, stone_other(m->color)))
372 return true;
373 /* Play within {true,false} eye-ish formation */
374 if (board->ko.coord == m->coord && board->ko.color == m->color)
375 return false;
376 int groups_in_atari = 0;
377 foreach_neighbor(board, m->coord, {
378 group_t g = group_at(board, c);
379 groups_in_atari += (board_group_info(board, g).libs == 1);
381 return !!groups_in_atari;
384 static inline bool
385 board_playing_ko_threat(struct board *b)
387 return !is_pass(b->ko.coord);
390 static inline group_t
391 board_get_atari_neighbor(struct board *b, coord_t coord, enum stone group_color)
393 #ifdef BOARD_TRAITS
394 if (!trait_at(b, coord, stone_other(group_color)).cap) return 0;
395 #endif
396 foreach_neighbor(b, coord, {
397 group_t g = group_at(b, c);
398 if (g && board_at(b, c) == group_color && board_group_info(b, g).libs == 1)
399 return g;
400 /* We return first match. */
402 return 0;
405 #endif