13 struct features_gamma
;
16 /* The board implementation has bunch of optional features.
17 * Turn them on below: */
19 #define WANT_BOARD_C // capturable groups queue
21 //#define BOARD_SIZE 9 // constant board size, allows better optimization
23 //#define BOARD_SPATHASH // incremental patternsp.h hashes
24 #define BOARD_SPATHASH_MAXD 3 // maximal diameter
26 #define BOARD_PAT3 // incremental 3x3 pattern codes
28 //#define BOARD_TRAITS 1 // incremental point traits (see struct btraits)
29 //#define BOARD_GAMMA 1 // incremental probability distribution (requires BOARD_TRAITS, BOARD_PAT3)
32 /* Some engines might normalize their reading and skip symmetrical
33 * moves. We will tell them how can they do it. */
34 struct board_symmetry
{
35 /* Playground is in this rectangle. */
37 /* d == 0: Full rectangle
38 * d == 1: Top triangle */
40 /* General symmetry type. */
41 /* Note that the above is redundant to this, but just provided
42 * for easier usage. */
54 typedef uint64_t hash_t
;
55 #define PRIhash PRIx64
58 /* Note that "group" is only chain of stones that is solidly
59 * connected for us. */
60 typedef coord_t group_t
;
63 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
65 /* _Combination_ of these two values can make some difference
66 * in performance - fine-tune. */
67 #define GROUP_KEEP_LIBS 10
68 // refill lib[] only when we hit this; this must be at least 2!
69 // Moggy requires at least 3 - see below for semantic impact.
70 #define GROUP_REFILL_LIBS 5
71 coord_t lib
[GROUP_KEEP_LIBS
];
72 /* libs is only LOWER BOUND for the number of real liberties!!!
73 * It denotes only number of items in lib[], thus you can rely
74 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
78 struct neighbor_colors
{
83 /* Point traits bitmap; we update this information incrementally,
84 * it can be used e.g. for fast pattern.h features matching. */
86 /* Number of neighbors we can capture. 0=this move is
87 * not capturing, 1..4=this many neighbors we can capture
88 * (can be multiple neighbors of same group). */
90 /* Whether it is SAFE to play here. This is essentially just
91 * cached result of board_safe_to_play(). (Of course the concept
92 * of "safety" is not perfect here, but it's the cheapest
93 * reasonable thing we can do.) */
95 /* Whether we need to re-compute this coordinate; used to
96 * weed out duplicates. Maintained only for S_BLACK. */
101 /* You should treat this struct as read-only. Always call functions below if
102 * you want to change it. */
105 int size
; /* Including S_OFFBOARD margin - see below. */
106 int size2
; /* size^2 */
110 /* The ruleset is currently almost never taken into account;
111 * the board implementation is basically Chinese rules (handicap
112 * stones compensation) w/ suicide (or you can look at it as
113 * New Zealand w/o handi stones compensation), while the engine
114 * enforces no-suicide, making for real Chinese rules. */
116 RULES_CHINESE
, /* default value */
122 /* Iterator offsets for foreach_neighbor*() */
123 int nei8
[8], dnei
[4];
126 struct move last_move
;
127 struct move last_move2
; /* second-to-last move */
128 /* Whether we tried to add a hash twice; board_play*() can
129 * set this, but it will still carry out the move as well! */
130 bool superko_violation
;
132 /* The following two structures are goban maps and are indexed by
133 * coord.pos. The map is surrounded by a one-point margin from
134 * S_OFFBOARD stones in order to speed up some internal loops.
135 * Some of the foreach iterators below might include these points;
136 * you need to handle them yourselves, if you need to. */
138 /* Stones played on the board */
139 enum stone
*b
; /* enum stone */
140 /* Group id the stones are part of; 0 == no group */
142 /* Positions of next stones in the stone group; 0 == last stone */
144 /* Neighboring colors; numbers of neighbors of index color */
145 struct neighbor_colors
*n
;
146 /* Zobrist hash for each position */
148 #ifdef BOARD_SPATHASH
149 /* For spatial hashes, we use only 24 bits. */
150 /* [0] is d==1, we don't keep hash for d==0. */
151 /* We keep hashes for black-to-play ([][0]) and white-to-play
152 * ([][1], reversed stone colors since we match all patterns as
154 uint32_t (*spathash
)[BOARD_SPATHASH_MAXD
][2];
157 /* 3x3 pattern code for each position; see pattern3.h for encoding
158 * specification. The information is only valid for empty points. */
162 /* Incrementally matched point traits information, black-to-play
163 * ([][0]) and white-to-play ([][1]). */
164 /* The information is only valid for empty points. */
165 struct btraits (*t
)[2];
168 /* Relative probabilities of moves being played next, computed by
169 * multiplying gammas of the appropriate pattern features based on
170 * pat3 and traits (see pattern.h). The probability distribution
171 * is maintained over the full board grid. */
172 /* - Always invalid moves are guaranteed to have zero probability.
173 * - Self-eye-filling moves will always have zero probability.
174 * - Ko-prohibited moves might have non-zero probability.
175 * - FEAT_CONTIGUITY is not accounted for in the probability. */
176 struct probdist prob
[2];
178 /* Cached information on x-y coordinates so that we avoid division. */
181 /* Group information - indexed by gid (which is coord of base group stone) */
184 /* Positions of free positions - queue (not map) */
185 /* Note that free position here is any valid move; including single-point eyes!
186 * However, pass is not included. */
187 coord_t
*f
; int flen
;
190 /* Queue of capturable groups */
191 group_t
*c
; int clen
;
195 /* Queue of positions that need their traits updated */
196 coord_t
*tq
; int tqlen
;
199 /* Symmetry information */
200 struct board_symmetry symmetry
;
202 /* Last ko played on the board. */
209 /* Engine-specific state; persistent through board development,
210 * is reset only at clear_board. */
213 /* Playout-specific state; persistent through board development,
214 * but its lifetime is maintained in play_random_game(); it should
215 * not be set outside of it. */
219 /* Gamma values for probability distribution; user must setup
220 * this pointer before any move is played, using board_gamma_set(). */
221 struct features_gamma
*gamma
;
222 /* Whether to compute the 'safe' trait using board_safe_to_play()
223 * (false) or is_bad_selfatari() (true, much slower). */
224 bool precise_selfatari
;
228 /* --- PRIVATE DATA --- */
230 /* For superko check: */
232 /* Board "history" - hashes encountered. Size of the hash should be
233 * >> board_size^2. */
234 #define history_hash_bits 12
235 #define history_hash_mask ((1 << history_hash_bits) - 1)
236 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
237 #define history_hash_next(i) ((i + 1) & history_hash_mask)
238 hash_t history_hash
[1 << history_hash_bits
];
239 /* Hash of current board position. */
244 /* Avoid unused variable warnings */
245 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
246 #define board_size2(b_) (board_size(b_) * board_size(b_))
248 #define board_size(b_) ((b_)->size)
249 #define board_size2(b_) ((b_)->size2)
252 #define board_at(b_, c) ((b_)->b[c])
253 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
255 #define group_at(b_, c) ((b_)->g[c])
256 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
258 /* Warning! Neighbor count is kept up-to-date for S_NONE! */
259 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord].colors[(enum stone) color])
260 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
261 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
262 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
263 #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))
265 #define trait_at(b_, coord, color) (b_)->t[coord][(color) - 1]
267 #define groupnext_at(b_, c) ((b_)->p[c])
268 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
270 #define group_base(g_) (g_)
271 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
272 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
273 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
275 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord])
277 struct board
*board_init(void);
278 struct board
*board_copy(struct board
*board2
, struct board
*board1
);
279 void board_done_noalloc(struct board
*board
);
280 void board_done(struct board
*board
);
281 /* size here is without the S_OFFBOARD margin. */
282 void board_resize(struct board
*board
, int size
);
283 void board_clear(struct board
*board
);
286 typedef char *(*board_cprint
)(struct board
*b
, coord_t c
, char *s
, char *end
);
287 void board_print(struct board
*board
, FILE *f
);
288 void board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
);
290 /* Place given handicap on the board; coordinates are printed to f. */
291 void board_handicap(struct board
*board
, int stones
, FILE *f
);
293 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
294 int board_play(struct board
*board
, struct move
*m
);
295 /* Like above, but plays random move; the move coordinate is recorded
296 * to *coord. This method will never fill your own eye. pass is played
297 * when no move can be played. You can impose extra restrictions if you
298 * supply your own permit function. */
299 typedef bool (*ppr_permit
)(void *data
, struct board
*b
, struct move
*m
);
300 void board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
);
302 /* Returns true if given move can be played. */
303 static bool board_is_valid_play(struct board
*b
, enum stone color
, coord_t coord
);
304 static bool board_is_valid_move(struct board
*b
, struct move
*m
);
305 /* Returns true if ko was just taken. */
306 static bool board_playing_ko_threat(struct board
*b
);
307 /* Returns 0 or ID of neighboring group in atari. */
308 static group_t
board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
);
309 /* Returns true if the move is not obvious self-atari. */
310 static bool board_safe_to_play(struct board
*b
, coord_t coord
, enum stone color
);
312 /* Adjust symmetry information as if given coordinate has been played. */
313 void board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
);
314 /* Associate a set of feature gamma values (for pd building) with the board. */
315 void board_gamma_set(struct board
*b
, struct features_gamma
*gamma
, bool precise_selfatari
);
316 /* Force re-compute of a probability distribution item. */
317 void board_gamma_update(struct board
*b
, coord_t coord
, enum stone color
);
319 /* Returns true if given coordinate has all neighbors of given color or the edge. */
320 static bool board_is_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
);
321 /* Returns true if given coordinate could be a false eye; this check makes
322 * sense only if you already know the coordinate is_eyelike(). */
323 bool board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
);
324 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
325 * at least tries to). */
326 bool board_is_one_point_eye(struct board
*board
, coord_t c
, enum stone eye_color
);
327 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
328 enum stone
board_get_one_point_eye(struct board
*board
, coord_t c
);
330 /* board_official_score() is the scoring method for yielding score suitable
331 * for external presentation. For fast scoring of entirely filled boards
332 * (e.g. playouts), use board_fast_score(). */
333 /* Positive: W wins */
334 /* Compare number of stones + 1pt eyes. */
335 float board_fast_score(struct board
*board
);
336 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
338 float board_official_score(struct board
*board
, struct move_queue
*mq
);
342 #define foreach_point(board_) \
345 for (; c < board_size(board_) * board_size(board_); c++)
346 #define foreach_point_and_pass(board_) \
349 for (; c < board_size(board_) * board_size(board_); c++)
350 #define foreach_point_end \
353 #define foreach_in_group(board_, group_) \
355 struct board *board__ = board_; \
356 coord_t c = group_base(group_); \
357 coord_t c2 = c; c2 = groupnext_at(board__, c2); \
359 #define foreach_in_group_end \
360 c = c2; c2 = groupnext_at(board__, c2); \
364 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
365 * on S_OFFBOARD coordinates. */
366 #define foreach_neighbor(board_, coord_, loop_body) \
368 struct board *board__ = board_; \
369 coord_t coord__ = coord_; \
371 c = coord__ - 1; do { loop_body } while (0); \
372 c = coord__ - board_size(board__); do { loop_body } while (0); \
373 c = coord__ + 1; do { loop_body } while (0); \
374 c = coord__ + board_size(board__); do { loop_body } while (0); \
377 #define foreach_8neighbor(board_, coord_) \
380 coord_t c = (coord_); \
381 for (fn__i = 0; fn__i < 8; fn__i++) { \
382 c += (board_)->nei8[fn__i];
383 #define foreach_8neighbor_end \
387 #define foreach_diag_neighbor(board_, coord_) \
390 coord_t c = (coord_); \
391 for (fn__i = 0; fn__i < 4; fn__i++) { \
392 c += (board_)->dnei[fn__i];
393 #define foreach_diag_neighbor_end \
399 board_is_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
401 return (neighbor_count_at(board
, coord
, eye_color
)
402 + neighbor_count_at(board
, coord
, S_OFFBOARD
)) == 4;
406 board_is_valid_play(struct board
*board
, enum stone color
, coord_t coord
)
408 if (board_at(board
, coord
) != S_NONE
)
410 if (!board_is_eyelike(board
, coord
, stone_other(color
)))
412 /* Play within {true,false} eye-ish formation */
413 if (board
->ko
.coord
== coord
&& board
->ko
.color
== color
)
416 /* XXX: Disallows suicide. */
417 return trait_at(board
, coord
, color
).cap
> 0;
419 int groups_in_atari
= 0;
420 foreach_neighbor(board
, coord
, {
421 group_t g
= group_at(board
, c
);
422 groups_in_atari
+= (board_group_info(board
, g
).libs
== 1);
424 return !!groups_in_atari
;
429 board_is_valid_move(struct board
*board
, struct move
*m
)
431 return board_is_valid_play(board
, m
->color
, m
->coord
);
435 board_playing_ko_threat(struct board
*b
)
437 return !is_pass(b
->ko
.coord
);
440 static inline group_t
441 board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
)
444 if (!trait_at(b
, coord
, stone_other(group_color
)).cap
) return 0;
446 foreach_neighbor(b
, coord
, {
447 group_t g
= group_at(b
, c
);
448 if (g
&& board_at(b
, c
) == group_color
&& board_group_info(b
, g
).libs
== 1)
450 /* We return first match. */
456 board_safe_to_play(struct board
*b
, coord_t coord
, enum stone color
)
458 /* number of free neighbors */
459 int libs
= immediate_liberty_count(b
, coord
);
464 /* number of capturable enemy groups */
465 if (trait_at(b
, coord
, color
).cap
> 0)
466 return true; // XXX: We don't account for snapback.
467 /* number of non-capturable friendly groups */
468 int noncap_ours
= neighbor_count_at(b
, coord
, color
) - trait_at(b
, coord
, stone_other(color
)).cap
;
474 /* ok, but we need to check if they don't have just two libs. */
476 foreach_neighbor(b
, coord
, {
478 if (board_at(b
, c
) == stone_other(color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1)
479 return true; // can capture; no snapback check
481 if (board_at(b
, c
) != color
) continue;
482 group_t g
= group_at(b
, c
);
483 if (board_group_info(b
, g
).libs
== 1) continue; // in atari
484 if (board_group_info(b
, g
).libs
== 2) { // two liberties
485 if (libs
> 0) return true; // we already have one real liberty
486 // get the other liberty
487 coord_t lib
= board_group_info(b
, g
).lib
[0];
488 if (lib
== coord
) lib
= board_group_info(b
, g
).lib
[0];
489 /* we might be connecting two 2-lib groups, which is ok;
490 * so remember the other liberty and just make sure it's
491 * not the same one */
492 if (onelib
>= 0 && lib
!= onelib
) return true;
499 // no good support group