board_undo: define BOARD_UNDO_CHECKS to guard against invalid quick_play() / quick_un...
[pachi.git] / board.h
blob62a5a3941e86013705dab35b23b1f9b042da4612
1 /* probdist.h must be included before the include goard since we require
2 * proper including order. */
3 #include "probdist.h"
5 #ifndef PACHI_BOARD_H
6 #define PACHI_BOARD_H
8 #include <inttypes.h>
9 #include <stdbool.h>
10 #include <stdint.h>
12 #include "util.h"
13 #include "stone.h"
14 #include "move.h"
16 struct fbook;
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_SPATHASH // incremental patternsp.h hashes
31 #define BOARD_SPATHASH_MAXD 3 // maximal diameter
33 #define BOARD_PAT3 // incremental 3x3 pattern codes
35 //#define BOARD_TRAITS 1 // incremental point traits (see struct btraits)
36 //#define BOARD_TRAIT_SAFE 1 // include btraits.safe (rather expensive, unused)
37 //#define BOARD_TRAIT_SAFE 2 // include btraits.safe based on full is_bad_selfatari()
39 //#define BOARD_UNDO_CHECKS 1 // Guard against invalid quick_play() / quick_undo() uses
41 #define BOARD_MAX_COORDS ((BOARD_MAX_SIZE+2) * (BOARD_MAX_SIZE+2) )
42 #define BOARD_MAX_MOVES (BOARD_MAX_SIZE * BOARD_MAX_SIZE)
43 #define BOARD_MAX_GROUPS (BOARD_MAX_SIZE * BOARD_MAX_SIZE / 2)
45 enum e_sym {
46 SYM_FULL,
47 SYM_DIAG_UP,
48 SYM_DIAG_DOWN,
49 SYM_HORIZ,
50 SYM_VERT,
51 SYM_NONE
55 /* Some engines might normalize their reading and skip symmetrical
56 * moves. We will tell them how can they do it. */
57 struct board_symmetry {
58 /* Playground is in this rectangle. */
59 int x1, x2, y1, y2;
60 /* d == 0: Full rectangle
61 * d == 1: Top triangle */
62 int d;
63 /* General symmetry type. */
64 /* Note that the above is redundant to this, but just provided
65 * for easier usage. */
66 enum e_sym type;
70 typedef uint64_t hash_t;
71 #define PRIhash PRIx64
73 /* XXX: This really belongs in pattern3.h, unfortunately that would mean
74 * a dependency hell. */
75 typedef uint32_t hash3_t; // 3x3 pattern hash
78 /* Note that "group" is only chain of stones that is solidly
79 * connected for us. */
80 typedef coord_t group_t;
82 struct group {
83 /* We keep track of only up to GROUP_KEEP_LIBS; over that, we
84 * don't care. */
85 /* _Combination_ of these two values can make some difference
86 * in performance - fine-tune. */
87 #define GROUP_KEEP_LIBS 10
88 // refill lib[] only when we hit this; this must be at least 2!
89 // Moggy requires at least 3 - see below for semantic impact.
90 #define GROUP_REFILL_LIBS 5
91 coord_t lib[GROUP_KEEP_LIBS];
92 /* libs is only LOWER BOUND for the number of real liberties!!!
93 * It denotes only number of items in lib[], thus you can rely
94 * on it to store real liberties only up to <= GROUP_REFILL_LIBS. */
95 int libs;
98 struct neighbor_colors {
99 char colors[S_MAX];
103 /* Point traits bitmap; we update this information incrementally,
104 * it can be used e.g. for fast pattern features matching. */
105 struct btraits {
106 /* Number of neighbors we can capture. 0=this move is
107 * not capturing, 1..4=this many neighbors we can capture
108 * (can be multiple neighbors of same group). */
109 unsigned cap:3;
110 /* Number of 1-stone neighbors we can capture. */
111 unsigned cap1:3;
112 #ifdef BOARD_TRAIT_SAFE
113 /* Whether it is SAFE to play here. This is essentially just
114 * cached result of board_safe_to_play(). (Of course the concept
115 * of "safety" is not perfect here, but it's the cheapest
116 * reasonable thing we can do.) */
117 bool safe:1;
118 #endif
119 /* Whether we need to re-compute this coordinate; used to
120 * weed out duplicates. Maintained only for S_BLACK. */
121 bool dirty:1;
125 /* You should treat this struct as read-only. Always call functions below if
126 * you want to change it. */
128 struct board {
129 int size; /* Including S_OFFBOARD margin - see below. */
130 int size2; /* size^2 */
131 int bits2; /* ceiling(log2(size2)) */
132 int captures[S_MAX];
133 floating_t komi;
134 int handicap;
135 /* The ruleset is currently almost never taken into account;
136 * the board implementation is basically Chinese rules (handicap
137 * stones compensation) w/ suicide (or you can look at it as
138 * New Zealand w/o handi stones compensation), while the engine
139 * enforces no-suicide, making for real Chinese rules.
140 * However, we accept suicide moves by the opponent, so we
141 * should work with rules allowing suicide, just not taking
142 * full advantage of them. */
143 enum go_ruleset {
144 RULES_CHINESE, /* default value */
145 RULES_AGA,
146 RULES_NEW_ZEALAND,
147 RULES_JAPANESE,
148 RULES_STONES_ONLY, /* do not count eyes */
149 /* http://home.snafu.de/jasiek/siming.html */
150 /* Simplified ING rules - RULES_CHINESE with handicaps
151 * counting as points and pass stones. Also should
152 * allow suicide, but Pachi will never suicide
153 * nevertheless. */
154 /* XXX: I couldn't find the point about pass stones
155 * in the rule text, but it is Robert Jasiek's
156 * interpretation of them... These rules were
157 * used e.g. at the EGC2012 13x13 tournament.
158 * They are not supported by KGS. */
159 RULES_SIMING,
160 } rules;
162 char *fbookfile;
163 struct fbook *fbook;
165 /* Iterator offsets for foreach_neighbor*() */
166 int nei8[8], dnei[4];
168 int moves;
169 struct move last_move;
170 struct move last_move2; /* second-to-last move */
171 struct move last_move3; /* just before last_move2, only set if last_move is pass */
172 struct move last_move4; /* just before last_move3, only set if last_move & last_move2 are pass */
173 /* Whether we tried to add a hash twice; board_play*() can
174 * set this, but it will still carry out the move as well! */
175 bool superko_violation;
177 /* The following two structures are goban maps and are indexed by
178 * coord.pos. The map is surrounded by a one-point margin from
179 * S_OFFBOARD stones in order to speed up some internal loops.
180 * Some of the foreach iterators below might include these points;
181 * you need to handle them yourselves, if you need to. */
183 /* Stones played on the board */
184 enum stone *b; /* enum stone */
185 /* Group id the stones are part of; 0 == no group */
186 group_t *g;
187 /* Positions of next stones in the stone group; 0 == last stone */
188 coord_t *p;
189 /* Neighboring colors; numbers of neighbors of index color */
190 struct neighbor_colors *n;
191 /* Zobrist hash for each position */
192 hash_t *h;
193 #ifdef BOARD_SPATHASH
194 /* For spatial hashes, we use only 24 bits. */
195 /* [0] is d==1, we don't keep hash for d==0. */
196 /* We keep hashes for black-to-play ([][0]) and white-to-play
197 * ([][1], reversed stone colors since we match all patterns as
198 * black-to-play). */
199 uint32_t (*spathash)[BOARD_SPATHASH_MAXD][2];
200 #endif
201 #ifdef BOARD_PAT3
202 /* 3x3 pattern code for each position; see pattern3.h for encoding
203 * specification. The information is only valid for empty points. */
204 hash3_t *pat3;
205 #endif
206 #ifdef BOARD_TRAITS
207 /* Incrementally matched point traits information, black-to-play
208 * ([][0]) and white-to-play ([][1]). */
209 /* The information is only valid for empty points. */
210 struct btraits (*t)[2];
211 #endif
212 /* Cached information on x-y coordinates so that we avoid division. */
213 uint8_t (*coord)[2];
215 /* Group information - indexed by gid (which is coord of base group stone) */
216 struct group *gi;
218 /* Positions of free positions - queue (not map) */
219 /* Note that free position here is any valid move; including single-point eyes!
220 * However, pass is not included. */
221 coord_t *f; int flen;
223 #ifdef WANT_BOARD_C
224 /* Queue of capturable groups */
225 group_t *c; int clen;
226 #endif
228 #ifdef BOARD_TRAITS
229 /* Queue of positions that need their traits updated */
230 coord_t *tq; int tqlen;
231 #endif
233 /* Symmetry information */
234 struct board_symmetry symmetry;
236 /* Last ko played on the board. */
237 struct move last_ko;
238 int last_ko_age;
240 /* Basic ko check */
241 struct move ko;
243 #ifdef BOARD_UNDO_CHECKS
244 /* Guard against invalid quick_play() / quick_undo() uses */
245 int quicked;
246 #endif
248 /* Engine-specific state; persistent through board development,
249 * is reset only at clear_board. */
250 void *es;
252 /* Playout-specific state; persistent through board development,
253 * but its lifetime is maintained in play_random_game(); it should
254 * not be set outside of it. */
255 void *ps;
258 /* --- PRIVATE DATA --- */
260 /* For superko check: */
262 /* Board "history" - hashes encountered. Size of the hash should be
263 * >> board_size^2. */
264 #define history_hash_bits 12
265 #define history_hash_mask ((1 << history_hash_bits) - 1)
266 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
267 #define history_hash_next(i) ((i + 1) & history_hash_mask)
268 hash_t history_hash[1 << history_hash_bits];
269 /* Hash of current board position. */
270 hash_t hash;
271 /* Hash of current board position quadrants. */
272 hash_t qhash[4];
275 struct undo_merge {
276 group_t group;
277 coord_t last;
278 struct group info;
281 struct undo_enemy {
282 group_t group;
283 struct group info;
284 coord_t stones[BOARD_MAX_MOVES]; // TODO try small array
287 struct board_undo {
288 struct move last_move2;
289 struct move ko;
290 struct move last_ko;
291 int last_ko_age;
293 coord_t next_at;
295 coord_t inserted;
296 struct undo_merge merged[4];
297 int nmerged;
298 int nmerged_tmp;
300 struct undo_enemy enemies[4];
301 int nenemies;
302 int captures; /* number of stones captured */
306 #ifdef BOARD_SIZE
307 /* Avoid unused variable warnings */
308 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
309 #define board_size2(b_) (board_size(b_) * board_size(b_))
310 #define real_board_size(b_) (((b_) == (b_)) ? BOARD_SIZE : 0)
311 #else
312 #define board_size(b_) ((b_)->size)
313 #define board_size2(b_) ((b_)->size2)
314 #define real_board_size(b_) ((b_)->size - 2)
315 #endif
317 /* This is a shortcut for taking different action on smaller
318 * and large boards (e.g. picking different variable defaults).
319 * This is of course less optimal than fine-tuning dependency
320 * function of values on board size, but that is difficult and
321 * possibly not very rewarding if you are interested just in
322 * 9x9 and 19x19. */
323 #define board_large(b_) (board_size(b_)-2 >= 15)
324 #define board_small(b_) (board_size(b_)-2 <= 9)
326 #if BOARD_SIZE == 19
327 # define board_bits2(b_) 9
328 #elif BOARD_SIZE == 13
329 # define board_bits2(b_) 8
330 #elif BOARD_SIZE == 9
331 # define board_bits2(b_) 7
332 #else
333 # define board_bits2(b_) ((b_)->bits2)
334 #endif
336 #define board_at(b_, c) ((b_)->b[c])
337 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
339 #define group_at(b_, c) ((b_)->g[c])
340 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
342 /* Warning! Neighbor count is not kept up-to-date for S_NONE! */
343 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord].colors[(enum stone) color])
344 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
345 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
346 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
347 #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))
349 #define trait_at(b_, coord, color) (b_)->t[coord][(color) - 1]
351 #define groupnext_at(b_, c) ((b_)->p[c])
352 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
354 #define group_base(g_) (g_)
355 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
356 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
357 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
358 /* board_group_other_lib() makes sense only for groups with two liberties. */
359 #define board_group_other_lib(b_, g_, l_) (board_group_info(b_, g_).lib[board_group_info(b_, g_).lib[0] != (l_) ? 0 : 1])
361 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord])
363 struct board *board_init(char *fbookfile);
364 struct board *board_copy(struct board *board2, struct board *board1);
365 void board_done_noalloc(struct board *board);
366 void board_done(struct board *board);
367 /* size here is without the S_OFFBOARD margin. */
368 void board_resize(struct board *board, int size);
369 void board_clear(struct board *board);
371 typedef char *(*board_cprint)(struct board *b, coord_t c, char *s, char *end, void *data);
372 void board_print(struct board *board, FILE *f);
373 void board_print_custom(struct board *board, FILE *f, board_cprint cprint, void *data);
375 /* Debugging: Compare 2 boards byte by byte. Don't use that for sorting =) */
376 int board_cmp(struct board *b1, struct board *b2);
377 /* Same but only care about fields maintained by quick_play() / quick_undo() */
378 int board_quick_cmp(struct board *b1, struct board *b2);
380 /* Place given handicap on the board; coordinates are printed to f. */
381 void board_handicap(struct board *board, int stones, FILE *f);
383 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
384 int board_play(struct board *board, struct move *m);
385 /* Like above, but plays random move; the move coordinate is recorded
386 * to *coord. This method will never fill your own eye. pass is played
387 * when no move can be played. You can impose extra restrictions if you
388 * supply your own permit function; the permit function can also modify
389 * the move coordinate to redirect the move elsewhere. */
390 typedef bool (*ppr_permit)(void *data, struct board *b, struct move *m);
391 void board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data);
393 /* Undo, supported only for pass moves. Returns -1 on error, 0 otherwise. */
394 int board_undo(struct board *board);
396 /* Returns true if given move can be played. */
397 static bool board_is_valid_play(struct board *b, enum stone color, coord_t coord);
398 static bool board_is_valid_move(struct board *b, struct move *m);
399 /* Returns true if ko was just taken. */
400 static bool board_playing_ko_threat(struct board *b);
401 /* Returns 0 or ID of neighboring group in atari. */
402 static group_t board_get_atari_neighbor(struct board *b, coord_t coord, enum stone group_color);
403 /* Returns true if the move is not obvious self-atari. */
404 static bool board_safe_to_play(struct board *b, coord_t coord, enum stone color);
406 /* Determine number of stones in a group, up to @max stones. */
407 static int group_stone_count(struct board *b, group_t group, int max);
409 /* Adjust symmetry information as if given coordinate has been played. */
410 void board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c);
411 /* Check if coordinates are within symmetry base. (If false, they can
412 * be derived from the base.) */
413 static bool board_coord_in_symmetry(struct board *b, coord_t c);
415 /* Returns true if given coordinate has all neighbors of given color or the edge. */
416 static bool board_is_eyelike(struct board *board, coord_t coord, enum stone eye_color);
417 /* Returns true if given coordinate could be a false eye; this check makes
418 * sense only if you already know the coordinate is_eyelike(). */
419 bool board_is_false_eyelike(struct board *board, coord_t coord, enum stone eye_color);
420 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
421 * at least tries to). */
422 bool board_is_one_point_eye(struct board *board, coord_t c, enum stone eye_color);
423 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
424 enum stone board_get_one_point_eye(struct board *board, coord_t c);
426 /* board_official_score() is the scoring method for yielding score suitable
427 * for external presentation. For fast scoring of entirely filled boards
428 * (e.g. playouts), use board_fast_score(). */
429 /* Positive: W wins */
430 /* Compare number of stones + 1pt eyes. */
431 floating_t board_fast_score(struct board *board);
432 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
433 struct move_queue;
434 floating_t board_official_score(struct board *board, struct move_queue *mq);
436 /* Set board rules according to given string. Returns false in case
437 * of unknown ruleset name. */
438 bool board_set_rules(struct board *board, char *name);
440 /* Quick play/undo to try out a move.
441 * WARNING Only core board structures are maintained !
442 * Code in between can't rely on anything else.
444 * Currently this means these can't be used:
445 * - incremental patterns (pat3)
446 * - hashes, superko_violation (spathash, hash, qhash, history_hash)
447 * - list of free positions (f / flen)
448 * - list of capturable groups (c / clen)
449 * - traits (btraits, t, tq, tqlen)
450 * - last_move3, last_move4, last_ko_age
451 * - symmetry information
453 * Invalid quick_play()/quick_undo() combinations (missing undo for example)
454 * are caught at next board_play() if BOARD_UNDO_CHECKS is defined.
456 int board_quick_play(struct board *board, struct move *m, struct board_undo *u);
457 void board_quick_undo(struct board *b, struct move *m, struct board_undo *u);
459 /* quick_play() + quick_undo() combo.
460 * Body is executed only if move is valid (silently ignored otherwise).
461 * Can break out in body, but definitely *NOT* return / jump around !
462 * (caught at run time if BOARD_UNDO_CHECKS defined). Can use
463 * with_move_return(val) to return value for non-nested with_move()'s
464 * though. */
465 #define with_move(board_, coord_, color_, body_) \
466 do { \
467 struct board *board__ = (board_); /* For with_move_return() */ \
468 struct move m_ = { .coord = (coord_), .color = (color_) }; \
469 struct board_undo u_; \
470 if (board_quick_play(board__, &m_, &u_) >= 0) { \
471 do { body_ } while(0); \
472 board_quick_undo(board__, &m_, &u_); \
474 } while (0)
476 /* Return value from within with_move() statement.
477 * Valid for non-nested with_move() *ONLY* */
478 #define with_move_return(val_) \
479 do { typeof(val_) val__ = (val_); board_quick_undo(board__, &m_, &u_); return val__; } while (0)
481 /* Same as with_move() but assert out in case of invalid move. */
482 #define with_move_strict(board_, coord_, color_, body_) \
483 do { \
484 struct board *board__ = (board_); /* For with_move_return() */ \
485 struct move m_ = { .coord = (coord_), .color = (color_) }; \
486 struct board_undo u_; \
487 assert (board_quick_play(board__, &m_, &u_) >= 0); \
488 do { body_ } while(0); \
489 board_quick_undo(board__, &m_, &u_); \
490 } while (0)
493 /** Iterators */
495 #define foreach_point(board_) \
496 do { \
497 coord_t c = 0; \
498 for (; c < board_size(board_) * board_size(board_); c++)
499 #define foreach_point_and_pass(board_) \
500 do { \
501 coord_t c = pass; \
502 for (; c < board_size(board_) * board_size(board_); c++)
503 #define foreach_point_end \
504 } while (0)
506 #define foreach_free_point(board_) \
507 do { \
508 int fmax__ = (board_)->flen; \
509 for (int f__ = 0; f__ < fmax__; f__++) { \
510 coord_t c = (board_)->f[f__];
511 #define foreach_free_point_end \
513 } while (0)
515 #define foreach_in_group(board_, group_) \
516 do { \
517 struct board *board__ = board_; \
518 coord_t c = group_base(group_); \
519 coord_t c2 = c; c2 = groupnext_at(board__, c2); \
520 do {
521 #define foreach_in_group_end \
522 c = c2; c2 = groupnext_at(board__, c2); \
523 } while (c != 0); \
524 } while (0)
526 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
527 * on S_OFFBOARD coordinates. */
528 #define foreach_neighbor(board_, coord_, loop_body) \
529 do { \
530 struct board *board__ = board_; \
531 coord_t coord__ = coord_; \
532 coord_t c; \
533 c = coord__ - board_size(board__); do { loop_body } while (0); \
534 c = coord__ - 1; do { loop_body } while (0); \
535 c = coord__ + 1; do { loop_body } while (0); \
536 c = coord__ + board_size(board__); do { loop_body } while (0); \
537 } while (0)
539 #define foreach_8neighbor(board_, coord_) \
540 do { \
541 int fn__i; \
542 coord_t c = (coord_); \
543 for (fn__i = 0; fn__i < 8; fn__i++) { \
544 c += (board_)->nei8[fn__i];
545 #define foreach_8neighbor_end \
547 } while (0)
549 #define foreach_diag_neighbor(board_, coord_) \
550 do { \
551 int fn__i; \
552 coord_t c = (coord_); \
553 for (fn__i = 0; fn__i < 4; fn__i++) { \
554 c += (board_)->dnei[fn__i];
555 #define foreach_diag_neighbor_end \
557 } while (0)
560 static inline bool
561 board_is_eyelike(struct board *board, coord_t coord, enum stone eye_color)
563 return (neighbor_count_at(board, coord, eye_color)
564 + neighbor_count_at(board, coord, S_OFFBOARD)) == 4;
567 /* Group suicides allowed */
568 static inline bool
569 board_is_valid_play(struct board *board, enum stone color, coord_t coord)
571 if (board_at(board, coord) != S_NONE)
572 return false;
573 if (!board_is_eyelike(board, coord, stone_other(color)))
574 return true;
575 /* Play within {true,false} eye-ish formation */
576 if (board->ko.coord == coord && board->ko.color == color)
577 return false;
578 #ifdef BOARD_TRAITS
579 /* XXX: Disallows suicide. */
580 return trait_at(board, coord, color).cap > 0;
581 #else
582 int groups_in_atari = 0;
583 foreach_neighbor(board, coord, {
584 group_t g = group_at(board, c);
585 groups_in_atari += (board_group_info(board, g).libs == 1);
587 return !!groups_in_atari;
588 #endif
591 /* Check group suicides, slower than board_is_valid_play() */
592 static inline bool
593 board_is_valid_play_no_suicide(struct board *board, enum stone color, coord_t coord)
595 if (board_at(board, coord) != S_NONE)
596 return false;
597 if (immediate_liberty_count(board, coord) >= 1)
598 return true;
599 if (board_is_eyelike(board, coord, stone_other(color)) &&
600 board->ko.coord == coord && board->ko.color == color)
601 return false;
603 // Capturing something ?
604 #ifdef BOARD_TRAITS
605 /* XXX: Disallows suicide. */
606 if (trait_at(board, coord, color).cap > 0)
607 return true;
608 #else
609 foreach_neighbor(board, coord, {
610 if (board_at(board, c) == stone_other(color) &&
611 board_group_info(board, group_at(board, c)).libs == 1)
612 return true;
614 #endif
616 // Neighbour with 2 libs ?
617 foreach_neighbor(board, coord, {
618 if (board_at(board, c) == color &&
619 board_group_info(board, group_at(board, c)).libs > 1)
620 return true;
623 return false; // Suicide
627 static inline bool
628 board_is_valid_move(struct board *board, struct move *m)
630 return board_is_valid_play(board, m->color, m->coord);
633 static inline bool
634 board_playing_ko_threat(struct board *b)
636 return !is_pass(b->ko.coord);
639 static inline group_t
640 board_get_atari_neighbor(struct board *b, coord_t coord, enum stone group_color)
642 #ifdef BOARD_TRAITS
643 if (!trait_at(b, coord, stone_other(group_color)).cap) return 0;
644 #endif
645 foreach_neighbor(b, coord, {
646 group_t g = group_at(b, c);
647 if (g && board_at(b, c) == group_color && board_group_info(b, g).libs == 1)
648 return g;
649 /* We return first match. */
651 return 0;
654 static inline bool
655 board_safe_to_play(struct board *b, coord_t coord, enum stone color)
657 /* number of free neighbors */
658 int libs = immediate_liberty_count(b, coord);
659 if (libs > 1)
660 return true;
662 #ifdef BOARD_TRAITS
663 /* number of capturable enemy groups */
664 if (trait_at(b, coord, color).cap > 0)
665 return true; // XXX: We don't account for snapback.
666 /* number of non-capturable friendly groups */
667 int noncap_ours = neighbor_count_at(b, coord, color) - trait_at(b, coord, stone_other(color)).cap;
668 if (noncap_ours < 1)
669 return false;
670 /*#else see below */
671 #endif
673 /* ok, but we need to check if they don't have just two libs. */
674 coord_t onelib = -1;
675 foreach_neighbor(b, coord, {
676 #ifndef BOARD_TRAITS
677 if (board_at(b, c) == stone_other(color) && board_group_info(b, group_at(b, c)).libs == 1)
678 return true; // can capture; no snapback check
679 #endif
680 if (board_at(b, c) != color) continue;
681 group_t g = group_at(b, c);
682 if (board_group_info(b, g).libs == 1) continue; // in atari
683 if (board_group_info(b, g).libs == 2) { // two liberties
684 if (libs > 0) return true; // we already have one real liberty
685 /* we might be connecting two 2-lib groups, which is ok;
686 * so remember the other liberty and just make sure it's
687 * not the same one */
688 if (onelib >= 0 && c != onelib) return true;
689 onelib = board_group_other_lib(b, g, c);
690 continue;
692 // many liberties
693 return true;
695 // no good support group
696 return false;
699 static inline int
700 group_stone_count(struct board *b, group_t group, int max)
702 int n = 0;
703 foreach_in_group(b, group) {
704 n++;
705 if (n >= max) return max;
706 } foreach_in_group_end;
707 return n;
710 static inline bool
711 board_coord_in_symmetry(struct board *b, coord_t c)
713 if (coord_y(c, b) < b->symmetry.y1 || coord_y(c, b) > b->symmetry.y2)
714 return false;
715 if (coord_x(c, b) < b->symmetry.x1 || coord_x(c, b) > b->symmetry.x2)
716 return false;
717 if (b->symmetry.d) {
718 int x = coord_x(c, b);
719 if (b->symmetry.type == SYM_DIAG_DOWN)
720 x = board_size(b) - 1 - x;
721 if (x > coord_y(c, b))
722 return false;
724 return true;
728 #endif