Moggy: Add support for controlling capturerate, ladders reading
[pachi.git] / board.h
blobe2a4a547768f9bab6d20bd2c130c6e6a8ad26eb1
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
19 /* Allow board_play_random_move() to return pass even when
20 * there are other moves available. */
21 extern bool random_pass;
24 typedef uint64_t hash_t;
27 /* Note that "group" is only chain of stones that is solidly
28 * connected for us. */
29 typedef coord_t group_t;
31 struct group {
32 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
33 * don't care. */
34 #define GROUP_KEEP_LIBS 4 // +-1 can make noticeable speed difference
35 coord_t lib[GROUP_KEEP_LIBS];
36 int libs;
39 struct neighbor_colors {
40 char colors[S_MAX];
43 /* You should treat this struct as read-only. Always call functions below if
44 * you want to change it. */
46 struct board {
47 int size; /* Including S_OFFBOARD margin - see below. */
48 int size2; /* size^2 */
49 int captures[S_MAX];
50 float komi;
51 int handicap;
53 int moves;
54 struct move last_move;
55 /* Whether we tried to add a hash twice; board_play*() can
56 * set this, but it will still carry out the move as well! */
57 bool superko_violation;
59 /* The following two structures are goban maps and are indexed by
60 * coord.pos. The map is surrounded by a one-point margin from
61 * S_OFFBOARD stones in order to speed up some internal loops.
62 * Some of the foreach iterators below might include these points;
63 * you need to handle them yourselves, if you need to. */
65 /* Stones played on the board */
66 enum stone *b; /* enum stone */
67 /* Group id the stones are part of; 0 == no group */
68 group_t *g;
69 /* Positions of next stones in the stone group; 0 == last stone */
70 coord_t *p;
71 /* Neighboring colors; numbers of neighbors of index color */
72 struct neighbor_colors *n;
73 /* Zobrist hash for each position */
74 hash_t *h;
76 /* Group information - indexed by gid (which is coord of base group stone) */
77 struct group *gi;
79 /* Positions of free positions - queue (not map) */
80 /* Note that free position here is any valid move; including single-point eyes! */
81 coord_t *f; int flen;
83 #ifdef WANT_BOARD_C
84 /* Queue of capturable groups */
85 group_t *c; int clen;
86 #endif
89 /* --- PRIVATE DATA --- */
91 /* Basic ko check */
92 struct move ko;
94 /* For superko check: */
96 /* Board "history" - hashes encountered. Size of the hash should be
97 * >> board_size^2. */
98 #define history_hash_bits 12
99 #define history_hash_mask ((1 << history_hash_bits) - 1)
100 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
101 #define history_hash_next(i) ((i + 1) & history_hash_mask)
102 hash_t history_hash[1 << history_hash_bits];
103 /* Hash of current board position. */
104 hash_t hash;
107 #define board_at(b_, c) ((b_)->b[coord_raw(c)])
108 #define board_atxy(b_, x, y) ((b_)->b[(x) + (b_)->size * (y)])
110 #define group_at(b_, c) ((b_)->g[coord_raw(c)])
111 #define group_atxy(b_, x, y) ((b_)->g[(x) + (b_)->size * (y)])
113 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord_raw(coord)].colors[(enum stone) color])
114 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
115 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
116 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
117 #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))
119 #define groupnext_at(b_, c) ((b_)->p[coord_raw(c)])
120 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + (b_)->size * (y)])
122 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
123 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
125 #define hash_at(b_, coord, color) (b_)->h[((color) == S_BLACK ? (b_)->size2 : 0) + coord_raw(coord)]
127 struct board *board_init(void);
128 struct board *board_copy(struct board *board2, struct board *board1);
129 void board_done_noalloc(struct board *board);
130 void board_done(struct board *board);
131 /* size here is without the S_OFFBOARD margin. */
132 void board_resize(struct board *board, int size);
133 void board_clear(struct board *board);
135 struct FILE;
136 void board_print(struct board *board, FILE *f);
138 /* Place given handicap on the board; coordinates are printed to f. */
139 void board_handicap(struct board *board, int stones, FILE *f);
141 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
142 int board_play(struct board *board, struct move *m);
143 /* Like above, but plays random move; the move coordinate is recorded
144 * to *coord. This method will never fill your own eye. pass is played
145 * when no move can be played. */
146 void board_play_random(struct board *b, enum stone color, coord_t *coord);
148 /* Returns true if given coordinate has all neighbors of given color or the edge. */
149 bool board_is_eyelike(struct board *board, coord_t *coord, enum stone eye_color);
150 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
151 * at least tries to). */
152 bool board_is_one_point_eye(struct board *board, coord_t *c, enum stone eye_color);
153 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
154 enum stone board_get_one_point_eye(struct board *board, coord_t *c);
156 /* Check if group is in atari. This is very fast.
157 * The last liberty is recorded to lastlib (content is undefined if group
158 * is not in atari). */
159 bool board_group_in_atari(struct board *board, int group, coord_t *lastlib);
161 /* Check if group can be put in atari. This is also very fast.
162 * The last two liberties are recorded to lastlib (content is undefined if group
163 * can't be put in atari). */
164 bool board_group_can_atari(struct board *board, int group, coord_t lastlib[2]);
166 /* board_official_score() is the scoring method for yielding score suitable
167 * for external presentation. For fast scoring of entirely filled boards
168 * (e.g. playouts), use board_fast_score(). */
169 /* Positive: W wins */
170 /* Tromp-Taylor scoring. */
171 float board_official_score(struct board *board);
172 /* Compare number of stones + 1pt eyes. */
173 float board_fast_score(struct board *board);
175 /* Assess if it is desirable to pull out from atari
176 * by this move. */
177 bool valid_escape_route(struct board *b, enum stone color, coord_t to);
179 /* Checks if there are any stones in n-vincinity of coord. */
180 bool board_stone_radar(struct board *b, coord_t coord, int distance);
183 /** Iterators */
185 #define foreach_point(board_) \
186 do { \
187 coord_t c; coord_pos(c, 0, (board_)); \
188 for (; coord_raw(c) < (board_)->size * (board_)->size; coord_raw(c)++)
189 #define foreach_point_end \
190 } while (0)
192 #define foreach_in_group(board_, group_) \
193 do { \
194 struct board *board__ = board_; \
195 coord_t c = (group_); \
196 coord_t c2 = c; coord_raw(c2) = groupnext_at(board__, c2); \
197 do {
198 #define foreach_in_group_end \
199 c = c2; coord_raw(c2) = groupnext_at(board__, c2); \
200 } while (coord_raw(c) != 0); \
201 } while (0)
203 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
204 * on S_OFFBOARD coordinates. */
205 #define foreach_neighbor(board_, coord_, loop_body) \
206 do { \
207 struct board *board__ = board_; \
208 coord_t coord__ = coord_; \
209 coord_t c; \
210 coord_pos(c, coord_raw(coord__) - 1, (board__)); do { loop_body } while (0); \
211 coord_pos(c, coord_raw(coord__) - (board__)->size, (board__)); do { loop_body } while (0); \
212 coord_pos(c, coord_raw(coord__) + 1, (board__)); do { loop_body } while (0); \
213 coord_pos(c, coord_raw(coord__) + (board__)->size, (board__)); do { loop_body } while (0); \
214 } while (0)
216 #define foreach_diag_neighbor(board_, coord_) \
217 do { \
218 coord_t q__[4]; int q__i = 0; \
219 coord_pos(q__[q__i++], coord_raw(coord_) - (board_)->size - 1, (board_)); \
220 coord_pos(q__[q__i++], coord_raw(coord_) - (board_)->size + 1, (board_)); \
221 coord_pos(q__[q__i++], coord_raw(coord_) + (board_)->size - 1, (board_)); \
222 coord_pos(q__[q__i++], coord_raw(coord_) + (board_)->size + 1, (board_)); \
223 int fn__i; \
224 for (fn__i = 0; fn__i < q__i; fn__i++) { \
225 coord_t c = q__[fn__i];
226 #define foreach_diag_neighbor_end \
228 } while (0)
231 #endif