dumpdebug
[pachi.git] / board.h
blob46899e6ee84e8a0d1304527270b539dec292dd4c
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 /* _Combination_ of these two values can make some difference
36 * in performance - fine-tune. */
37 #define GROUP_KEEP_LIBS 10
38 // refill lib[] only when we hit this; this must be at least 2!
39 // Moggy requires at least 3 - see below for semantic impact.
40 #define GROUP_REFILL_LIBS 5
41 coord_t lib[GROUP_KEEP_LIBS];
42 /* libs is only LOWER BOUND for the number of real liberties!!!
43 * It denotes only number of items in lib[], thus you can rely
44 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
45 int libs;
48 struct neighbor_colors {
49 char colors[S_MAX];
52 /* You should treat this struct as read-only. Always call functions below if
53 * you want to change it. */
55 struct board {
56 int size; /* Including S_OFFBOARD margin - see below. */
57 int size2; /* size^2 */
58 int captures[S_MAX];
59 float komi;
60 int handicap;
62 int moves;
63 struct move last_move;
64 /* Whether we tried to add a hash twice; board_play*() can
65 * set this, but it will still carry out the move as well! */
66 bool superko_violation;
68 /* The following two structures are goban maps and are indexed by
69 * coord.pos. The map is surrounded by a one-point margin from
70 * S_OFFBOARD stones in order to speed up some internal loops.
71 * Some of the foreach iterators below might include these points;
72 * you need to handle them yourselves, if you need to. */
74 /* Stones played on the board */
75 enum stone *b; /* enum stone */
76 /* Group id the stones are part of; 0 == no group */
77 group_t *g;
78 /* Positions of next stones in the stone group; 0 == last stone */
79 coord_t *p;
80 /* Neighboring colors; numbers of neighbors of index color */
81 struct neighbor_colors *n;
82 /* Zobrist hash for each position */
83 hash_t *h;
85 /* Group information - indexed by gid (which is coord of base group stone) */
86 struct group *gi;
88 /* Positions of free positions - queue (not map) */
89 /* Note that free position here is any valid move; including single-point eyes! */
90 coord_t *f; int flen;
92 #ifdef WANT_BOARD_C
93 /* Queue of capturable groups */
94 group_t *c; int clen;
95 #endif
98 /* --- PRIVATE DATA --- */
100 /* Basic ko check */
101 struct move ko;
103 /* For superko check: */
105 /* Board "history" - hashes encountered. Size of the hash should be
106 * >> board_size^2. */
107 #define history_hash_bits 12
108 #define history_hash_mask ((1 << history_hash_bits) - 1)
109 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
110 #define history_hash_next(i) ((i + 1) & history_hash_mask)
111 hash_t history_hash[1 << history_hash_bits];
112 /* Hash of current board position. */
113 hash_t hash;
116 #ifdef BOARD_SIZE
117 /* Avoid unused variable warnings */
118 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
119 #define board_size2(b_) (board_size(b_) * board_size(b_))
120 #else
121 #define board_size(b_) ((b_)->size)
122 #define board_size2(b_) ((b_)->size2)
123 #endif
125 #define board_at(b_, c) ((b_)->b[coord_raw(c)])
126 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
128 #define group_at(b_, c) ((b_)->g[coord_raw(c)])
129 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
131 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord_raw(coord)].colors[(enum stone) color])
132 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
133 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
134 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
135 #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))
137 #define groupnext_at(b_, c) ((b_)->p[coord_raw(c)])
138 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
140 #define group_base(g_) (g_)
141 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
142 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
144 #define hash_at(b_, coord, color) (b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord_raw(coord)]
146 struct board *board_init(void);
147 struct board *board_copy(struct board *board2, struct board *board1);
148 void board_done_noalloc(struct board *board);
149 void board_done(struct board *board);
150 /* size here is without the S_OFFBOARD margin. */
151 void board_resize(struct board *board, int size);
152 void board_clear(struct board *board);
154 struct FILE;
155 void board_print(struct board *board, FILE *f);
157 /* Place given handicap on the board; coordinates are printed to f. */
158 void board_handicap(struct board *board, int stones, FILE *f);
160 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
161 int board_play(struct board *board, struct move *m);
162 /* Like above, but plays random move; the move coordinate is recorded
163 * to *coord. This method will never fill your own eye. pass is played
164 * when no move can be played. */
165 void board_play_random(struct board *b, enum stone color, coord_t *coord);
167 /* Returns true if given coordinate has all neighbors of given color or the edge. */
168 bool board_is_eyelike(struct board *board, coord_t *coord, enum stone eye_color);
169 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
170 * at least tries to). */
171 bool board_is_one_point_eye(struct board *board, coord_t *c, enum stone eye_color);
172 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
173 enum stone board_get_one_point_eye(struct board *board, coord_t *c);
175 /* Check if group is in atari. This is very fast.
176 * The last liberty is recorded to lastlib (content is undefined if group
177 * is not in atari). */
178 bool board_group_in_atari(struct board *board, group_t group, coord_t *lastlib);
180 /* Check if group can be put in atari. This is also very fast.
181 * The last two liberties are recorded to lastlib (content is undefined if group
182 * can't be put in atari). */
183 bool board_group_can_atari(struct board *board, group_t group, coord_t lastlib[2]);
185 /* board_official_score() is the scoring method for yielding score suitable
186 * for external presentation. For fast scoring of entirely filled boards
187 * (e.g. playouts), use board_fast_score(). */
188 /* Positive: W wins */
189 /* Tromp-Taylor scoring. */
190 float board_official_score(struct board *board);
191 /* Compare number of stones + 1pt eyes. */
192 float board_fast_score(struct board *board);
194 /* Assess if it is desirable to pull out from atari
195 * by this move. */
196 bool valid_escape_route(struct board *b, enum stone color, coord_t to);
198 /* Checks if there are any stones in n-vincinity of coord. */
199 bool board_stone_radar(struct board *b, coord_t coord, int distance);
202 /** Iterators */
204 #define foreach_point(board_) \
205 do { \
206 coord_t c; coord_pos(c, 0, (board_)); \
207 for (; coord_raw(c) < board_size(board_) * board_size(board_); coord_raw(c)++)
208 #define foreach_point_end \
209 } while (0)
211 #define foreach_in_group(board_, group_) \
212 do { \
213 struct board *board__ = board_; \
214 coord_t c = group_base(group_); \
215 coord_t c2 = c; coord_raw(c2) = groupnext_at(board__, c2); \
216 do {
217 #define foreach_in_group_end \
218 c = c2; coord_raw(c2) = groupnext_at(board__, c2); \
219 } while (coord_raw(c) != 0); \
220 } while (0)
222 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
223 * on S_OFFBOARD coordinates. */
224 #define foreach_neighbor(board_, coord_, loop_body) \
225 do { \
226 struct board *board__ = board_; \
227 coord_t coord__ = coord_; \
228 coord_t c; \
229 coord_pos(c, coord_raw(coord__) - 1, (board__)); do { loop_body } while (0); \
230 coord_pos(c, coord_raw(coord__) - board_size(board__), (board__)); do { loop_body } while (0); \
231 coord_pos(c, coord_raw(coord__) + 1, (board__)); do { loop_body } while (0); \
232 coord_pos(c, coord_raw(coord__) + board_size(board__), (board__)); do { loop_body } while (0); \
233 } while (0)
235 #define foreach_diag_neighbor(board_, coord_) \
236 do { \
237 coord_t q__[4]; int q__i = 0; \
238 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) - 1, (board_)); \
239 coord_pos(q__[q__i++], coord_raw(coord_) - board_size(board_) + 1, (board_)); \
240 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_) - 1, (board_)); \
241 coord_pos(q__[q__i++], coord_raw(coord_) + board_size(board_) + 1, (board_)); \
242 int fn__i; \
243 for (fn__i = 0; fn__i < q__i; fn__i++) { \
244 coord_t c = q__[fn__i];
245 #define foreach_diag_neighbor_end \
247 } while (0)
250 #endif