10 #define likely(x) __builtin_expect(!!(x), 1)
11 #define unlikely(x) __builtin_expect((x), 0)
14 /* Allow board_play_random_move() to return pass even when
15 * there are other moves available. */
16 extern bool random_pass
;
19 typedef uint64_t hash_t
;
22 /* Note that "group" is only chain of stones that is solidly
23 * connected for us. */
25 typedef coord_t group_t
;
28 /* Number of group pseudo-liberties */
29 /* Pseudo-liberties count empty-stone edges, not empty positions.
30 * Thus, a single stone will have 4 pseudo-liberties, but so will
31 * an one-eyed group in atari. The advantage is that we can update
32 * the liberties lightning-fast. */
34 coord_t base_stone
; /* First stone in group */
37 struct neighbor_colors
{
41 /* You should treat this struct as read-only. Always call functions below if
42 * you want to change it. */
45 int size
; /* Including S_OFFBOARD margin - see below. */
46 int size2
; /* size^2 */
52 struct move last_move
;
53 /* Whether we tried to add a hash twice; board_play*() can
54 * set this, but it will still carry out the move as well! */
55 bool superko_violation
;
57 /* The following two structures are goban maps and are indexed by
58 * coord.pos. The map is surrounded by a one-point margin from
59 * S_OFFBOARD stones in order to speed up some internal loops.
60 * Some of the foreach iterators below might include these points;
61 * you need to handle them yourselves, if you need to. */
63 /* Stones played on the board */
64 enum stone
*b
; /* enum stone */
65 /* Group id the stones are part of; 0 == no group */
67 /* Positions of next stones in the stone group; 0 == last stone */
69 /* Neighboring colors; numbers of neighbors of index color */
70 struct neighbor_colors
*n
;
71 /* Zobrist hash for each position */
74 /* Group liberties - indexed by gid (which is coord of base group stone) */
77 /* Positions of free positions - queue (not map) */
78 /* Note that free position here is any valid move; including single-point eyes! */
82 /* --- PRIVATE DATA --- */
87 /* For superko check: */
89 /* Board "history" - hashes encountered. Size of the hash should be
91 #define history_hash_bits 12
92 #define history_hash_mask ((1 << history_hash_bits) - 1)
93 hash_t history_hash
[1 << history_hash_bits
];
94 /* Hash of current board position. */
98 #define board_at(b_, c) ((b_)->b[coord_raw(c)])
99 #define board_atxy(b_, x, y) ((b_)->b[(x) + (b_)->size * (y)])
101 #define group_at(b_, c) ((b_)->g[coord_raw(c)])
102 #define group_atxy(b_, x, y) ((b_)->g[(x) + (b_)->size * (y)])
104 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord_raw(coord)].colors[(enum stone) color])
105 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
106 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
107 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
108 #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))
110 #define groupnext_at(b_, c) ((b_)->p[coord_raw(c)])
111 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + (b_)->size * (y)])
113 #define board_group_libs(b_, g_) ((b_)->l[(g_)])
114 #define board_group_captured(b_, g_) (board_group_libs(b_, g_) == 0)
116 struct board
*board_init(void);
117 struct board
*board_copy(struct board
*board2
, struct board
*board1
);
118 void board_done_noalloc(struct board
*board
);
119 void board_done(struct board
*board
);
120 /* size here is without the S_OFFBOARD margin. */
121 void board_resize(struct board
*board
, int size
);
122 void board_clear(struct board
*board
);
125 void board_print(struct board
*board
, FILE *f
);
127 /* Place given handicap on the board; coordinates are printed to f. */
128 void board_handicap(struct board
*board
, int stones
, FILE *f
);
130 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
131 int board_play(struct board
*board
, struct move
*m
);
132 /* Like above, but plays random move; the move coordinate is recorded
133 * to *coord. This method will never fill your own eye. pass is played
134 * when no move can be played. */
135 void board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
);
137 /* Returns true if given coordinate has all neighbors of given color or the edge. */
138 bool board_is_eyelike(struct board
*board
, coord_t
*coord
, enum stone eye_color
);
139 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
140 * at least tries to). */
141 bool board_is_one_point_eye(struct board
*board
, coord_t
*c
, enum stone eye_color
);
142 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
143 enum stone
board_get_one_point_eye(struct board
*board
, coord_t
*c
);
145 /* Check if group is in atari; this check can be potentially quite expensive!
146 * The last liberty is recorded to lastlib (content is undefined if group
147 * is not in atari). */
148 bool board_group_in_atari(struct board
*board
, int group
, coord_t
*lastlib
);
150 /* Positive: W wins */
151 /* board_official_score() is the scoring method for yielding score suitable
152 * for external presentation. For fast scoring of two Pachis playing,
153 * use board_fast_score(). */
154 float board_official_score(struct board
*board
);
155 float board_fast_score(struct board
*board
);
160 #define foreach_point(board_) \
162 coord_t c; coord_pos(c, 0, (board_)); \
163 for (; coord_raw(c) < (board_)->size * (board_)->size; coord_raw(c)++)
164 #define foreach_point_end \
167 #define foreach_in_group(board_, group_) \
169 struct board *board__ = board_; \
170 coord_t c = (group_); \
171 coord_t c2 = c; coord_raw(c2) = groupnext_at(board__, c2); \
173 #define foreach_in_group_end \
174 c = c2; coord_raw(c2) = groupnext_at(board__, c2); \
175 } while (coord_raw(c) != 0); \
178 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
179 * on S_OFFBOARD coordinates. */
180 #define foreach_neighbor(board_, coord_, loop_body) \
182 struct board *board__ = board_; \
183 coord_t coord__ = coord_; \
185 coord_pos(c, coord_raw(coord__) - 1, (board__)); do { loop_body } while (0); \
186 coord_pos(c, coord_raw(coord__) - (board__)->size, (board__)); do { loop_body } while (0); \
187 coord_pos(c, coord_raw(coord__) + 1, (board__)); do { loop_body } while (0); \
188 coord_pos(c, coord_raw(coord__) + (board__)->size, (board__)); do { loop_body } while (0); \
191 #define foreach_diag_neighbor(board_, coord_) \
193 coord_t q__[4]; int q__i = 0; \
194 coord_pos(q__[q__i++], coord_raw(coord_) - (board_)->size - 1, (board_)); \
195 coord_pos(q__[q__i++], coord_raw(coord_) - (board_)->size + 1, (board_)); \
196 coord_pos(q__[q__i++], coord_raw(coord_) + (board_)->size - 1, (board_)); \
197 coord_pos(q__[q__i++], coord_raw(coord_) + (board_)->size + 1, (board_)); \
199 for (fn__i = 0; fn__i < q__i; fn__i++) { \
200 coord_t c = q__[fn__i];
201 #define foreach_diag_neighbor_end \