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