1 /* probdist.h must be included before the include goard since we require
2 * proper including order. */
19 /* Maximum supported board size. (Without the S_OFFBOARD edges.) */
20 #define BOARD_MAX_SIZE 19
23 /* The board implementation has bunch of optional features.
24 * Turn them on below: */
26 #define WANT_BOARD_C // capturable groups queue
28 //#define BOARD_SIZE 9 // constant board size, allows better optimization
30 #define BOARD_PAT3 // incremental 3x3 pattern codes
32 //#define BOARD_TRAITS 1 // incremental point traits (see struct btraits)
33 //#define BOARD_TRAIT_SAFE 1 // include btraits.safe (rather expensive, unused)
34 //#define BOARD_TRAIT_SAFE 2 // include btraits.safe based on full is_bad_selfatari()
37 #define BOARD_MAX_MOVES (BOARD_MAX_SIZE * BOARD_MAX_SIZE)
38 #define BOARD_MAX_GROUPS (BOARD_MAX_SIZE * BOARD_MAX_SIZE / 2)
41 /* Some engines might normalize their reading and skip symmetrical
42 * moves. We will tell them how can they do it. */
43 struct board_symmetry
{
44 /* Playground is in this rectangle. */
46 /* d == 0: Full rectangle
47 * d == 1: Top triangle */
49 /* General symmetry type. */
50 /* Note that the above is redundant to this, but just provided
51 * for easier usage. */
63 typedef uint64_t hash_t
;
64 #define PRIhash PRIx64
66 /* XXX: This really belongs in pattern3.h, unfortunately that would mean
67 * a dependency hell. */
68 typedef uint32_t hash3_t
; // 3x3 pattern hash
71 /* Note that "group" is only chain of stones that is solidly
72 * connected for us. */
73 typedef coord_t group_t
;
76 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
78 /* _Combination_ of these two values can make some difference
79 * in performance - fine-tune. */
80 #define GROUP_KEEP_LIBS 10
81 // refill lib[] only when we hit this; this must be at least 2!
82 // Moggy requires at least 3 - see below for semantic impact.
83 #define GROUP_REFILL_LIBS 5
84 coord_t lib
[GROUP_KEEP_LIBS
];
85 /* libs is only LOWER BOUND for the number of real liberties!!!
86 * It denotes only number of items in lib[], thus you can rely
87 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
91 struct neighbor_colors
{
96 /* Point traits bitmap; we update this information incrementally,
97 * it can be used e.g. for fast pattern features matching. */
99 /* Number of neighbors we can capture. 0=this move is
100 * not capturing, 1..4=this many neighbors we can capture
101 * (can be multiple neighbors of same group). */
103 /* Number of 1-stone neighbors we can capture. */
105 #ifdef BOARD_TRAIT_SAFE
106 /* Whether it is SAFE to play here. This is essentially just
107 * cached result of board_safe_to_play(). (Of course the concept
108 * of "safety" is not perfect here, but it's the cheapest
109 * reasonable thing we can do.) */
112 /* Whether we need to re-compute this coordinate; used to
113 * weed out duplicates. Maintained only for S_BLACK. */
118 /* You should treat this struct as read-only. Always call functions below if
119 * you want to change it. */
122 int size
; /* Including S_OFFBOARD margin - see below. */
123 int size2
; /* size^2 */
124 int bits2
; /* ceiling(log2(size2)) */
128 /* The ruleset is currently almost never taken into account;
129 * the board implementation is basically Chinese rules (handicap
130 * stones compensation) w/ suicide (or you can look at it as
131 * New Zealand w/o handi stones compensation), while the engine
132 * enforces no-suicide, making for real Chinese rules. */
134 RULES_CHINESE
, /* default value */
143 /* Iterator offsets for foreach_neighbor*() */
144 int nei8
[8], dnei
[4];
147 struct move last_move
;
148 struct move last_move2
; /* second-to-last move */
149 struct move last_move3
; /* just before last_move2, only set if last_move is pass */
150 struct move last_move4
; /* just before last_move3, only set if last_move & last_move2 are pass */
151 /* Whether we tried to add a hash twice; board_play*() can
152 * set this, but it will still carry out the move as well! */
153 bool superko_violation
;
155 /* The following two structures are goban maps and are indexed by
156 * coord.pos. The map is surrounded by a one-point margin from
157 * S_OFFBOARD stones in order to speed up some internal loops.
158 * Some of the foreach iterators below might include these points;
159 * you need to handle them yourselves, if you need to. */
161 /* Stones played on the board */
162 enum stone
*b
; /* enum stone */
163 /* Group id the stones are part of; 0 == no group */
165 /* Positions of next stones in the stone group; 0 == last stone */
167 /* Neighboring colors; numbers of neighbors of index color */
168 struct neighbor_colors
*n
;
169 /* Zobrist hash for each position */
172 /* 3x3 pattern code for each position; see pattern3.h for encoding
173 * specification. The information is only valid for empty points. */
177 /* Incrementally matched point traits information, black-to-play
178 * ([][0]) and white-to-play ([][1]). */
179 /* The information is only valid for empty points. */
180 struct btraits (*t
)[2];
182 /* Cached information on x-y coordinates so that we avoid division. */
185 /* Group information - indexed by gid (which is coord of base group stone) */
188 /* Positions of free positions - queue (not map) */
189 /* Note that free position here is any valid move; including single-point eyes!
190 * However, pass is not included. */
191 coord_t
*f
; int flen
;
194 /* Queue of capturable groups */
195 group_t
*c
; int clen
;
199 /* Queue of positions that need their traits updated */
200 coord_t
*tq
; int tqlen
;
203 /* Symmetry information */
204 struct board_symmetry symmetry
;
206 /* Last ko played on the board. */
213 /* Engine-specific state; persistent through board development,
214 * is reset only at clear_board. */
217 /* Playout-specific state; persistent through board development,
218 * but its lifetime is maintained in play_random_game(); it should
219 * not be set outside of it. */
223 /* --- PRIVATE DATA --- */
225 /* For superko check: */
227 /* Board "history" - hashes encountered. Size of the hash should be
228 * >> board_size^2. */
229 #define history_hash_bits 12
230 #define history_hash_mask ((1 << history_hash_bits) - 1)
231 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
232 #define history_hash_next(i) ((i + 1) & history_hash_mask)
233 hash_t history_hash
[1 << history_hash_bits
];
234 /* Hash of current board position. */
236 /* Hash of current board position quadrants. */
241 /* Avoid unused variable warnings */
242 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
243 #define board_size2(b_) (board_size(b_) * board_size(b_))
245 #define board_size(b_) ((b_)->size)
246 #define board_size2(b_) ((b_)->size2)
249 /* This is a shortcut for taking different action on smaller
250 * and large boards (e.g. picking different variable defaults).
251 * This is of course less optimal than fine-tuning dependency
252 * function of values on board size, but that is difficult and
253 * possibly not very rewarding if you are interested just in
255 #define board_large(b_) (board_size(b_)-2 >= 15)
258 # define board_bits2(b_) 9
259 #elif BOARD_SIZE == 13
260 # define board_bits2(b_) 8
261 #elif BOARD_SIZE == 9
262 # define board_bits2(b_) 7
264 # define board_bits2(b_) ((b_)->bits2)
267 #define board_at(b_, c) ((b_)->b[c])
268 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
270 #define group_at(b_, c) ((b_)->g[c])
271 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
273 /* Warning! Neighbor count is kept up-to-date for S_NONE! */
274 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord].colors[(enum stone) color])
275 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
276 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
277 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
278 #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))
280 #define trait_at(b_, coord, color) (b_)->t[coord][(color) - 1]
282 #define groupnext_at(b_, c) ((b_)->p[c])
283 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
285 #define group_base(g_) (g_)
286 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
287 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
288 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
289 /* board_group_other_lib() makes sense only for groups with two liberties. */
290 #define board_group_other_lib(b_, g_, l_) (board_group_info(b_, g_).lib[board_group_info(b_, g_).lib[0] != (l_) ? 0 : 1])
292 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord])
294 struct board
*board_init(char *fbookfile
);
295 struct board
*board_copy(struct board
*board2
, struct board
*board1
);
296 void board_done_noalloc(struct board
*board
);
297 void board_done(struct board
*board
);
298 /* size here is without the S_OFFBOARD margin. */
299 void board_resize(struct board
*board
, int size
);
300 void board_clear(struct board
*board
);
303 typedef char *(*board_cprint
)(struct board
*b
, coord_t c
, char *s
, char *end
);
304 void board_print(struct board
*board
, FILE *f
);
305 void board_print_custom(struct board
*board
, FILE *f
, board_cprint cprint
);
307 /* Place given handicap on the board; coordinates are printed to f. */
308 void board_handicap(struct board
*board
, int stones
, FILE *f
);
310 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
311 int board_play(struct board
*board
, struct move
*m
);
312 /* Like above, but plays random move; the move coordinate is recorded
313 * to *coord. This method will never fill your own eye. pass is played
314 * when no move can be played. You can impose extra restrictions if you
315 * supply your own permit function; the permit function can also modify
316 * the move coordinate to redirect the move elsewhere. */
317 typedef bool (*ppr_permit
)(void *data
, struct board
*b
, struct move
*m
);
318 void board_play_random(struct board
*b
, enum stone color
, coord_t
*coord
, ppr_permit permit
, void *permit_data
);
320 /*Undo, supported only for pass moves. Returns -1 on error, 0 otherwise. */
321 int board_undo(struct board
*board
);
323 /* Returns true if given move can be played. */
324 static bool board_is_valid_play(struct board
*b
, enum stone color
, coord_t coord
);
325 static bool board_is_valid_move(struct board
*b
, struct move
*m
);
326 /* Returns true if ko was just taken. */
327 static bool board_playing_ko_threat(struct board
*b
);
328 /* Returns 0 or ID of neighboring group in atari. */
329 static group_t
board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
);
330 /* Returns true if the move is not obvious self-atari. */
331 static bool board_safe_to_play(struct board
*b
, coord_t coord
, enum stone color
);
333 /* Determine number of stones in a group, up to @max stones. */
334 static int group_stone_count(struct board
*b
, group_t group
, int max
);
336 /* Adjust symmetry information as if given coordinate has been played. */
337 void board_symmetry_update(struct board
*b
, struct board_symmetry
*symmetry
, coord_t c
);
338 /* Check if coordinates are within symmetry base. (If false, they can
339 * be derived from the base.) */
340 static bool board_coord_in_symmetry(struct board
*b
, coord_t c
);
342 /* Returns true if given coordinate has all neighbors of given color or the edge. */
343 static bool board_is_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
);
344 /* Returns true if given coordinate could be a false eye; this check makes
345 * sense only if you already know the coordinate is_eyelike(). */
346 bool board_is_false_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
);
347 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
348 * at least tries to). */
349 bool board_is_one_point_eye(struct board
*board
, coord_t c
, enum stone eye_color
);
350 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
351 enum stone
board_get_one_point_eye(struct board
*board
, coord_t c
);
353 /* board_official_score() is the scoring method for yielding score suitable
354 * for external presentation. For fast scoring of entirely filled boards
355 * (e.g. playouts), use board_fast_score(). */
356 /* Positive: W wins */
357 /* Compare number of stones + 1pt eyes. */
358 floating_t
board_fast_score(struct board
*board
);
359 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
361 floating_t
board_official_score(struct board
*board
, struct move_queue
*mq
);
365 #define foreach_point(board_) \
368 for (; c < board_size(board_) * board_size(board_); c++)
369 #define foreach_point_and_pass(board_) \
372 for (; c < board_size(board_) * board_size(board_); c++)
373 #define foreach_point_end \
376 #define foreach_free_point(board_) \
378 int fmax__ = (board_)->flen; \
379 for (int f__ = 0; f__ < fmax__; f__++) { \
380 coord_t c = (board_)->f[f__];
381 #define foreach_free_point_end \
385 #define foreach_in_group(board_, group_) \
387 struct board *board__ = board_; \
388 coord_t c = group_base(group_); \
389 coord_t c2 = c; c2 = groupnext_at(board__, c2); \
391 #define foreach_in_group_end \
392 c = c2; c2 = groupnext_at(board__, c2); \
396 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
397 * on S_OFFBOARD coordinates. */
398 #define foreach_neighbor(board_, coord_, loop_body) \
400 struct board *board__ = board_; \
401 coord_t coord__ = coord_; \
403 c = coord__ - board_size(board__); do { loop_body } while (0); \
404 c = coord__ - 1; do { loop_body } while (0); \
405 c = coord__ + 1; do { loop_body } while (0); \
406 c = coord__ + board_size(board__); do { loop_body } while (0); \
409 #define foreach_8neighbor(board_, coord_) \
412 coord_t c = (coord_); \
413 for (fn__i = 0; fn__i < 8; fn__i++) { \
414 c += (board_)->nei8[fn__i];
415 #define foreach_8neighbor_end \
419 #define foreach_diag_neighbor(board_, coord_) \
422 coord_t c = (coord_); \
423 for (fn__i = 0; fn__i < 4; fn__i++) { \
424 c += (board_)->dnei[fn__i];
425 #define foreach_diag_neighbor_end \
431 board_is_eyelike(struct board
*board
, coord_t coord
, enum stone eye_color
)
433 return (neighbor_count_at(board
, coord
, eye_color
)
434 + neighbor_count_at(board
, coord
, S_OFFBOARD
)) == 4;
438 board_is_valid_play(struct board
*board
, enum stone color
, coord_t coord
)
440 if (board_at(board
, coord
) != S_NONE
)
442 if (!board_is_eyelike(board
, coord
, stone_other(color
)))
444 /* Play within {true,false} eye-ish formation */
445 if (board
->ko
.coord
== coord
&& board
->ko
.color
== color
)
448 /* XXX: Disallows suicide. */
449 return trait_at(board
, coord
, color
).cap
> 0;
451 int groups_in_atari
= 0;
452 foreach_neighbor(board
, coord
, {
453 group_t g
= group_at(board
, c
);
454 groups_in_atari
+= (board_group_info(board
, g
).libs
== 1);
456 return !!groups_in_atari
;
461 board_is_valid_move(struct board
*board
, struct move
*m
)
463 return board_is_valid_play(board
, m
->color
, m
->coord
);
467 board_playing_ko_threat(struct board
*b
)
469 return !is_pass(b
->ko
.coord
);
472 static inline group_t
473 board_get_atari_neighbor(struct board
*b
, coord_t coord
, enum stone group_color
)
476 if (!trait_at(b
, coord
, stone_other(group_color
)).cap
) return 0;
478 foreach_neighbor(b
, coord
, {
479 group_t g
= group_at(b
, c
);
480 if (g
&& board_at(b
, c
) == group_color
&& board_group_info(b
, g
).libs
== 1)
482 /* We return first match. */
488 board_safe_to_play(struct board
*b
, coord_t coord
, enum stone color
)
490 /* number of free neighbors */
491 int libs
= immediate_liberty_count(b
, coord
);
496 /* number of capturable enemy groups */
497 if (trait_at(b
, coord
, color
).cap
> 0)
498 return true; // XXX: We don't account for snapback.
499 /* number of non-capturable friendly groups */
500 int noncap_ours
= neighbor_count_at(b
, coord
, color
) - trait_at(b
, coord
, stone_other(color
)).cap
;
506 /* ok, but we need to check if they don't have just two libs. */
508 foreach_neighbor(b
, coord
, {
510 if (board_at(b
, c
) == stone_other(color
) && board_group_info(b
, group_at(b
, c
)).libs
== 1)
511 return true; // can capture; no snapback check
513 if (board_at(b
, c
) != color
) continue;
514 group_t g
= group_at(b
, c
);
515 if (board_group_info(b
, g
).libs
== 1) continue; // in atari
516 if (board_group_info(b
, g
).libs
== 2) { // two liberties
517 if (libs
> 0) return true; // we already have one real liberty
518 /* we might be connecting two 2-lib groups, which is ok;
519 * so remember the other liberty and just make sure it's
520 * not the same one */
521 if (onelib
>= 0 && c
!= onelib
) return true;
522 onelib
= board_group_other_lib(b
, g
, c
);
528 // no good support group
533 group_stone_count(struct board
*b
, group_t group
, int max
)
536 foreach_in_group(b
, group
) {
538 if (n
>= max
) return max
;
539 } foreach_in_group_end
;
544 board_coord_in_symmetry(struct board
*b
, coord_t c
)
546 if (coord_y(c
, b
) < b
->symmetry
.y1
|| coord_y(c
, b
) > b
->symmetry
.y2
)
548 if (coord_x(c
, b
) < b
->symmetry
.x1
|| coord_x(c
, b
) > b
->symmetry
.x2
)
551 int x
= coord_x(c
, b
);
552 if (b
->symmetry
.type
== SYM_DIAG_DOWN
)
553 x
= board_size(b
) - 1 - x
;
554 if (x
> coord_y(c
, b
))