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