t-unit: two_eyes test suite
[pachi.git] / board.h
blob738e0b6044d185b758a3e8e2744345faea4ecbf6
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 /* Quick hack to help ensure tactics code stays within quick board limitations.
126 * Ideally we'd have two different types for boards and quick_boards. The idea
127 * of having casts / duplicate api all over the place isn't so appealing though... */
128 #ifndef QUICK_BOARD_CODE
129 #define FB_ONLY(field) field
130 #else
131 #define FB_ONLY(field) field ## _disabled
132 // Try to make error messages more helpful ...
133 #define clen clen_field_not_supported_for_quick_boards
134 #define flen flen_field_not_supported_for_quick_boards
135 #endif
137 /* You should treat this struct as read-only. Always call functions below if
138 * you want to change it. */
140 struct board {
141 int size; /* Including S_OFFBOARD margin - see below. */
142 int size2; /* size^2 */
143 int bits2; /* ceiling(log2(size2)) */
144 int captures[S_MAX];
145 floating_t komi;
146 int handicap;
147 /* The ruleset is currently almost never taken into account;
148 * the board implementation is basically Chinese rules (handicap
149 * stones compensation) w/ suicide (or you can look at it as
150 * New Zealand w/o handi stones compensation), while the engine
151 * enforces no-suicide, making for real Chinese rules.
152 * However, we accept suicide moves by the opponent, so we
153 * should work with rules allowing suicide, just not taking
154 * full advantage of them. */
155 enum go_ruleset {
156 RULES_CHINESE, /* default value */
157 RULES_AGA,
158 RULES_NEW_ZEALAND,
159 RULES_JAPANESE,
160 RULES_STONES_ONLY, /* do not count eyes */
161 /* http://home.snafu.de/jasiek/siming.html */
162 /* Simplified ING rules - RULES_CHINESE with handicaps
163 * counting as points and pass stones. Also should
164 * allow suicide, but Pachi will never suicide
165 * nevertheless. */
166 /* XXX: I couldn't find the point about pass stones
167 * in the rule text, but it is Robert Jasiek's
168 * interpretation of them... These rules were
169 * used e.g. at the EGC2012 13x13 tournament.
170 * They are not supported by KGS. */
171 RULES_SIMING,
172 } rules;
174 char *fbookfile;
175 struct fbook *fbook;
177 /* Iterator offsets for foreach_neighbor*() */
178 int nei8[8], dnei[4];
180 int moves;
181 struct move last_move;
182 struct move last_move2; /* second-to-last move */
183 FB_ONLY(struct move last_move3); /* just before last_move2, only set if last_move is pass */
184 FB_ONLY(struct move last_move4); /* just before last_move3, only set if last_move & last_move2 are pass */
185 /* Whether we tried to add a hash twice; board_play*() can
186 * set this, but it will still carry out the move as well! */
187 FB_ONLY(bool superko_violation);
189 /* The following two structures are goban maps and are indexed by
190 * coord.pos. The map is surrounded by a one-point margin from
191 * S_OFFBOARD stones in order to speed up some internal loops.
192 * Some of the foreach iterators below might include these points;
193 * you need to handle them yourselves, if you need to. */
195 /* Stones played on the board */
196 enum stone *b; /* enum stone */
197 /* Group id the stones are part of; 0 == no group */
198 group_t *g;
199 /* Positions of next stones in the stone group; 0 == last stone */
200 coord_t *p;
201 /* Neighboring colors; numbers of neighbors of index color */
202 struct neighbor_colors *n;
203 /* Zobrist hash for each position */
204 hash_t *h;
205 #ifdef BOARD_SPATHASH
206 /* For spatial hashes, we use only 24 bits. */
207 /* [0] is d==1, we don't keep hash for d==0. */
208 /* We keep hashes for black-to-play ([][0]) and white-to-play
209 * ([][1], reversed stone colors since we match all patterns as
210 * black-to-play). */
211 FB_ONLY(uint32_t (*spathash))[BOARD_SPATHASH_MAXD][2];
212 #endif
213 #ifdef BOARD_PAT3
214 /* 3x3 pattern code for each position; see pattern3.h for encoding
215 * specification. The information is only valid for empty points. */
216 FB_ONLY(hash3_t *pat3);
217 #endif
218 #ifdef BOARD_TRAITS
219 /* Incrementally matched point traits information, black-to-play
220 * ([][0]) and white-to-play ([][1]). */
221 /* The information is only valid for empty points. */
222 FB_ONLY(struct btraits (*t)[2]);
223 #endif
224 /* Cached information on x-y coordinates so that we avoid division. */
225 uint8_t (*coord)[2];
227 /* Group information - indexed by gid (which is coord of base group stone) */
228 struct group *gi;
230 /* Positions of free positions - queue (not map) */
231 /* Note that free position here is any valid move; including single-point eyes!
232 * However, pass is not included. */
233 FB_ONLY(coord_t *f); FB_ONLY(int flen);
235 #ifdef WANT_BOARD_C
236 /* Queue of capturable groups */
237 FB_ONLY(group_t *c); FB_ONLY(int clen);
238 #endif
240 #ifdef BOARD_TRAITS
241 /* Queue of positions that need their traits updated */
242 FB_ONLY(coord_t *tq); FB_ONLY(int tqlen);
243 #endif
245 /* Symmetry information */
246 FB_ONLY(struct board_symmetry symmetry);
248 /* Last ko played on the board. */
249 FB_ONLY(struct move last_ko);
250 FB_ONLY(int last_ko_age);
252 /* Basic ko check */
253 struct move ko;
255 #ifdef BOARD_UNDO_CHECKS
256 /* Guard against invalid quick_play() / quick_undo() uses */
257 int quicked;
258 #endif
260 /* Engine-specific state; persistent through board development,
261 * is reset only at clear_board. */
262 void *es;
264 /* Playout-specific state; persistent through board development,
265 * but its lifetime is maintained in play_random_game(); it should
266 * not be set outside of it. */
267 void *ps;
270 /* --- PRIVATE DATA --- */
272 /* For superko check: */
274 /* Board "history" - hashes encountered. Size of the hash should be
275 * >> board_size^2. */
276 #define history_hash_bits 12
277 #define history_hash_mask ((1 << history_hash_bits) - 1)
278 #define history_hash_prev(i) ((i - 1) & history_hash_mask)
279 #define history_hash_next(i) ((i + 1) & history_hash_mask)
280 FB_ONLY(hash_t history_hash)[1 << history_hash_bits];
281 /* Hash of current board position. */
282 FB_ONLY(hash_t hash);
283 /* Hash of current board position quadrants. */
284 FB_ONLY(hash_t qhash)[4];
287 struct undo_merge {
288 group_t group;
289 coord_t last;
290 struct group info;
293 struct undo_enemy {
294 group_t group;
295 struct group info;
296 coord_t stones[BOARD_MAX_MOVES]; // TODO try small array
299 struct board_undo {
300 struct move last_move2;
301 struct move ko;
302 struct move last_ko;
303 int last_ko_age;
305 coord_t next_at;
307 coord_t inserted;
308 struct undo_merge merged[4];
309 int nmerged;
310 int nmerged_tmp;
312 struct undo_enemy enemies[4];
313 int nenemies;
314 int captures; /* number of stones captured */
318 #ifdef BOARD_SIZE
319 /* Avoid unused variable warnings */
320 #define board_size(b_) (((b_) == (b_)) ? BOARD_SIZE + 2 : 0)
321 #define board_size2(b_) (board_size(b_) * board_size(b_))
322 #define real_board_size(b_) (((b_) == (b_)) ? BOARD_SIZE : 0)
323 #else
324 #define board_size(b_) ((b_)->size)
325 #define board_size2(b_) ((b_)->size2)
326 #define real_board_size(b_) ((b_)->size - 2)
327 #endif
329 /* This is a shortcut for taking different action on smaller
330 * and large boards (e.g. picking different variable defaults).
331 * This is of course less optimal than fine-tuning dependency
332 * function of values on board size, but that is difficult and
333 * possibly not very rewarding if you are interested just in
334 * 9x9 and 19x19. */
335 #define board_large(b_) (board_size(b_)-2 >= 15)
336 #define board_small(b_) (board_size(b_)-2 <= 9)
338 #if BOARD_SIZE == 19
339 # define board_bits2(b_) 9
340 #elif BOARD_SIZE == 13
341 # define board_bits2(b_) 8
342 #elif BOARD_SIZE == 9
343 # define board_bits2(b_) 7
344 #else
345 # define board_bits2(b_) ((b_)->bits2)
346 #endif
348 #define board_at(b_, c) ((b_)->b[c])
349 #define board_atxy(b_, x, y) ((b_)->b[(x) + board_size(b_) * (y)])
351 #define group_at(b_, c) ((b_)->g[c])
352 #define group_atxy(b_, x, y) ((b_)->g[(x) + board_size(b_) * (y)])
354 /* Warning! Neighbor count is not kept up-to-date for S_NONE! */
355 #define neighbor_count_at(b_, coord, color) ((b_)->n[coord].colors[(enum stone) color])
356 #define set_neighbor_count_at(b_, coord, color, count) (neighbor_count_at(b_, coord, color) = (count))
357 #define inc_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)++)
358 #define dec_neighbor_count_at(b_, coord, color) (neighbor_count_at(b_, coord, color)--)
359 #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))
361 #define trait_at(b_, coord, color) (b_)->t[coord][(color) - 1]
363 #define groupnext_at(b_, c) ((b_)->p[c])
364 #define groupnext_atxy(b_, x, y) ((b_)->p[(x) + board_size(b_) * (y)])
366 #define group_base(g_) (g_)
367 #define group_is_onestone(b_, g_) (groupnext_at(b_, group_base(g_)) == 0)
368 #define board_group_info(b_, g_) ((b_)->gi[(g_)])
369 #define board_group_captured(b_, g_) (board_group_info(b_, g_).libs == 0)
370 /* board_group_other_lib() makes sense only for groups with two liberties. */
371 #define board_group_other_lib(b_, g_, l_) (board_group_info(b_, g_).lib[board_group_info(b_, g_).lib[0] != (l_) ? 0 : 1])
373 #define hash_at(b_, coord, color) ((b_)->h[((color) == S_BLACK ? board_size2(b_) : 0) + coord])
375 struct board *board_init(char *fbookfile);
376 struct board *board_copy(struct board *board2, struct board *board1);
377 void board_done_noalloc(struct board *board);
378 void board_done(struct board *board);
379 /* size here is without the S_OFFBOARD margin. */
380 void board_resize(struct board *board, int size);
381 void board_clear(struct board *board);
383 typedef char *(*board_cprint)(struct board *b, coord_t c, char *s, char *end, void *data);
384 typedef char *(*board_print_handler)(struct board *b, coord_t c, void *data);
385 void board_print(struct board *board, FILE *f);
386 void board_print_custom(struct board *board, FILE *f, board_cprint cprint, void *data);
387 void board_hprint(struct board *board, FILE *f, board_print_handler handler, void *data);
389 /* Debugging: Compare 2 boards byte by byte. Don't use that for sorting =) */
390 int board_cmp(struct board *b1, struct board *b2);
391 /* Same but only care about fields maintained by quick_play() / quick_undo() */
392 int board_quick_cmp(struct board *b1, struct board *b2);
394 /* Place given handicap on the board; coordinates are printed to f. */
395 void board_handicap(struct board *board, int stones, FILE *f);
397 /* Returns group id, 0 on allowed suicide, pass or resign, -1 on error */
398 int board_play(struct board *board, struct move *m);
399 /* Like above, but plays random move; the move coordinate is recorded
400 * to *coord. This method will never fill your own eye. pass is played
401 * when no move can be played. You can impose extra restrictions if you
402 * supply your own permit function; the permit function can also modify
403 * the move coordinate to redirect the move elsewhere. */
404 typedef bool (*ppr_permit)(void *data, struct board *b, struct move *m);
405 void board_play_random(struct board *b, enum stone color, coord_t *coord, ppr_permit permit, void *permit_data);
407 /* Undo, supported only for pass moves. Returns -1 on error, 0 otherwise. */
408 int board_undo(struct board *board);
410 /* Returns true if given move can be played. */
411 static bool board_is_valid_play(struct board *b, enum stone color, coord_t coord);
412 static bool board_is_valid_move(struct board *b, struct move *m);
413 /* Returns true if ko was just taken. */
414 static bool board_playing_ko_threat(struct board *b);
415 /* Returns 0 or ID of neighboring group in atari. */
416 static group_t board_get_atari_neighbor(struct board *b, coord_t coord, enum stone group_color);
417 /* Returns true if the move is not obvious self-atari. */
418 static bool board_safe_to_play(struct board *b, coord_t coord, enum stone color);
420 /* Determine number of stones in a group, up to @max stones. */
421 static int group_stone_count(struct board *b, group_t group, int max);
423 #ifndef QUICK_BOARD_CODE
424 /* Adjust symmetry information as if given coordinate has been played. */
425 void board_symmetry_update(struct board *b, struct board_symmetry *symmetry, coord_t c);
426 /* Check if coordinates are within symmetry base. (If false, they can
427 * be derived from the base.) */
428 static bool board_coord_in_symmetry(struct board *b, coord_t c);
429 #endif
431 /* Returns true if given coordinate has all neighbors of given color or the edge. */
432 static bool board_is_eyelike(struct board *board, coord_t coord, enum stone eye_color);
433 /* Returns true if given coordinate could be a false eye; this check makes
434 * sense only if you already know the coordinate is_eyelike(). */
435 bool board_is_false_eyelike(struct board *board, coord_t coord, enum stone eye_color);
436 /* Returns true if given coordinate is a 1-pt eye (checks against false eyes, or
437 * at least tries to). */
438 bool board_is_one_point_eye(struct board *board, coord_t c, enum stone eye_color);
439 /* Returns color of a 1pt eye owner, S_NONE if not an eye. */
440 enum stone board_get_one_point_eye(struct board *board, coord_t c);
442 /* board_official_score() is the scoring method for yielding score suitable
443 * for external presentation. For fast scoring of entirely filled boards
444 * (e.g. playouts), use board_fast_score(). */
445 /* Positive: W wins */
446 /* Compare number of stones + 1pt eyes. */
447 floating_t board_fast_score(struct board *board);
448 /* Tromp-Taylor scoring, assuming given groups are actually dead. */
449 struct move_queue;
450 floating_t board_official_score(struct board *board, struct move_queue *mq);
452 /* Set board rules according to given string. Returns false in case
453 * of unknown ruleset name. */
454 bool board_set_rules(struct board *board, char *name);
456 /* Quick play/undo to try out a move.
457 * WARNING Only core board structures are maintained !
458 * Code in between can't rely on anything else.
460 * Currently this means these can't be used:
461 * - incremental patterns (pat3)
462 * - hashes, superko_violation (spathash, hash, qhash, history_hash)
463 * - list of free positions (f / flen)
464 * - list of capturable groups (c / clen)
465 * - traits (btraits, t, tq, tqlen)
466 * - last_move3, last_move4, last_ko_age
467 * - symmetry information
469 * #define QUICK_BOARD_CODE at the top of your file to get compile-time
470 * error if you try to access a forbidden field.
472 * Invalid quick_play()/quick_undo() combinations (missing undo for example)
473 * are caught at next board_play() if BOARD_UNDO_CHECKS is defined.
475 int board_quick_play(struct board *board, struct move *m, struct board_undo *u);
476 void board_quick_undo(struct board *b, struct move *m, struct board_undo *u);
478 /* quick_play() + quick_undo() combo.
479 * Body is executed only if move is valid (silently ignored otherwise).
480 * Can break out in body, but definitely *NOT* return / jump around !
481 * (caught at run time if BOARD_UNDO_CHECKS defined). Can use
482 * with_move_return(val) to return value for non-nested with_move()'s
483 * though. */
484 #define with_move(board_, coord_, color_, body_) \
485 do { \
486 struct board *board__ = (board_); /* For with_move_return() */ \
487 struct move m_ = { .coord = (coord_), .color = (color_) }; \
488 struct board_undo u_; \
489 if (board_quick_play(board__, &m_, &u_) >= 0) { \
490 do { body_ } while(0); \
491 board_quick_undo(board__, &m_, &u_); \
493 } while (0)
495 /* Return value from within with_move() statement.
496 * Valid for non-nested with_move() *ONLY* */
497 #define with_move_return(val_) \
498 do { typeof(val_) val__ = (val_); board_quick_undo(board__, &m_, &u_); return val__; } while (0)
500 /* Same as with_move() but assert out in case of invalid move. */
501 #define with_move_strict(board_, coord_, color_, body_) \
502 do { \
503 struct board *board__ = (board_); /* For with_move_return() */ \
504 struct move m_ = { .coord = (coord_), .color = (color_) }; \
505 struct board_undo u_; \
506 assert (board_quick_play(board__, &m_, &u_) >= 0); \
507 do { body_ } while(0); \
508 board_quick_undo(board__, &m_, &u_); \
509 } while (0)
512 /** Iterators */
514 #define foreach_point(board_) \
515 do { \
516 coord_t c = 0; \
517 for (; c < board_size(board_) * board_size(board_); c++)
518 #define foreach_point_and_pass(board_) \
519 do { \
520 coord_t c = pass; \
521 for (; c < board_size(board_) * board_size(board_); c++)
522 #define foreach_point_end \
523 } while (0)
525 #define foreach_free_point(board_) \
526 do { \
527 int fmax__ = (board_)->flen; \
528 for (int f__ = 0; f__ < fmax__; f__++) { \
529 coord_t c = (board_)->f[f__];
530 #define foreach_free_point_end \
532 } while (0)
534 #define foreach_in_group(board_, group_) \
535 do { \
536 struct board *board__ = board_; \
537 coord_t c = group_base(group_); \
538 coord_t c2 = c; c2 = groupnext_at(board__, c2); \
539 do {
540 #define foreach_in_group_end \
541 c = c2; c2 = groupnext_at(board__, c2); \
542 } while (c != 0); \
543 } while (0)
545 /* NOT VALID inside of foreach_point() or another foreach_neighbor(), or rather
546 * on S_OFFBOARD coordinates. */
547 #define foreach_neighbor(board_, coord_, loop_body) \
548 do { \
549 struct board *board__ = board_; \
550 coord_t coord__ = coord_; \
551 coord_t c; \
552 c = coord__ - board_size(board__); do { loop_body } while (0); \
553 c = coord__ - 1; do { loop_body } while (0); \
554 c = coord__ + 1; do { loop_body } while (0); \
555 c = coord__ + board_size(board__); do { loop_body } while (0); \
556 } while (0)
558 #define foreach_8neighbor(board_, coord_) \
559 do { \
560 int fn__i; \
561 coord_t c = (coord_); \
562 for (fn__i = 0; fn__i < 8; fn__i++) { \
563 c += (board_)->nei8[fn__i];
564 #define foreach_8neighbor_end \
566 } while (0)
568 #define foreach_diag_neighbor(board_, coord_) \
569 do { \
570 int fn__i; \
571 coord_t c = (coord_); \
572 for (fn__i = 0; fn__i < 4; fn__i++) { \
573 c += (board_)->dnei[fn__i];
574 #define foreach_diag_neighbor_end \
576 } while (0)
579 static inline bool
580 board_is_eyelike(struct board *board, coord_t coord, enum stone eye_color)
582 return (neighbor_count_at(board, coord, eye_color)
583 + neighbor_count_at(board, coord, S_OFFBOARD)) == 4;
586 /* Group suicides allowed */
587 static inline bool
588 board_is_valid_play(struct board *board, enum stone color, coord_t coord)
590 if (board_at(board, coord) != S_NONE)
591 return false;
592 if (!board_is_eyelike(board, coord, stone_other(color)))
593 return true;
594 /* Play within {true,false} eye-ish formation */
595 if (board->ko.coord == coord && board->ko.color == color)
596 return false;
597 #ifdef BOARD_TRAITS
598 /* XXX: Disallows suicide. */
599 return trait_at(board, coord, color).cap > 0;
600 #else
601 int groups_in_atari = 0;
602 foreach_neighbor(board, coord, {
603 group_t g = group_at(board, c);
604 groups_in_atari += (board_group_info(board, g).libs == 1);
606 return !!groups_in_atari;
607 #endif
610 /* Check group suicides, slower than board_is_valid_play() */
611 static inline bool
612 board_is_valid_play_no_suicide(struct board *board, enum stone color, coord_t coord)
614 if (board_at(board, coord) != S_NONE)
615 return false;
616 if (immediate_liberty_count(board, coord) >= 1)
617 return true;
618 if (board_is_eyelike(board, coord, stone_other(color)) &&
619 board->ko.coord == coord && board->ko.color == color)
620 return false;
622 // Capturing something ?
623 #ifdef BOARD_TRAITS
624 /* XXX: Disallows suicide. */
625 if (trait_at(board, coord, color).cap > 0)
626 return true;
627 #else
628 foreach_neighbor(board, coord, {
629 if (board_at(board, c) == stone_other(color) &&
630 board_group_info(board, group_at(board, c)).libs == 1)
631 return true;
633 #endif
635 // Neighbour with 2 libs ?
636 foreach_neighbor(board, coord, {
637 if (board_at(board, c) == color &&
638 board_group_info(board, group_at(board, c)).libs > 1)
639 return true;
642 return false; // Suicide
646 static inline bool
647 board_is_valid_move(struct board *board, struct move *m)
649 return board_is_valid_play(board, m->color, m->coord);
652 static inline bool
653 board_playing_ko_threat(struct board *b)
655 return !is_pass(b->ko.coord);
658 static inline group_t
659 board_get_atari_neighbor(struct board *b, coord_t coord, enum stone group_color)
661 #ifdef BOARD_TRAITS
662 if (!trait_at(b, coord, stone_other(group_color)).cap) return 0;
663 #endif
664 foreach_neighbor(b, coord, {
665 group_t g = group_at(b, c);
666 if (g && board_at(b, c) == group_color && board_group_info(b, g).libs == 1)
667 return g;
668 /* We return first match. */
670 return 0;
673 static inline bool
674 board_safe_to_play(struct board *b, coord_t coord, enum stone color)
676 /* number of free neighbors */
677 int libs = immediate_liberty_count(b, coord);
678 if (libs > 1)
679 return true;
681 #ifdef BOARD_TRAITS
682 /* number of capturable enemy groups */
683 if (trait_at(b, coord, color).cap > 0)
684 return true; // XXX: We don't account for snapback.
685 /* number of non-capturable friendly groups */
686 int noncap_ours = neighbor_count_at(b, coord, color) - trait_at(b, coord, stone_other(color)).cap;
687 if (noncap_ours < 1)
688 return false;
689 /*#else see below */
690 #endif
692 /* ok, but we need to check if they don't have just two libs. */
693 coord_t onelib = -1;
694 foreach_neighbor(b, coord, {
695 #ifndef BOARD_TRAITS
696 if (board_at(b, c) == stone_other(color) && board_group_info(b, group_at(b, c)).libs == 1)
697 return true; // can capture; no snapback check
698 #endif
699 if (board_at(b, c) != color) continue;
700 group_t g = group_at(b, c);
701 if (board_group_info(b, g).libs == 1) continue; // in atari
702 if (board_group_info(b, g).libs == 2) { // two liberties
703 if (libs > 0) return true; // we already have one real liberty
704 /* we might be connecting two 2-lib groups, which is ok;
705 * so remember the other liberty and just make sure it's
706 * not the same one */
707 if (onelib >= 0 && c != onelib) return true;
708 onelib = board_group_other_lib(b, g, c);
709 continue;
711 // many liberties
712 return true;
714 // no good support group
715 return false;
718 static inline int
719 group_stone_count(struct board *b, group_t group, int max)
721 int n = 0;
722 foreach_in_group(b, group) {
723 n++;
724 if (n >= max) return max;
725 } foreach_in_group_end;
726 return n;
729 #ifndef QUICK_BOARD_CODE
730 static inline bool
731 board_coord_in_symmetry(struct board *b, coord_t c)
733 if (coord_y(c, b) < b->symmetry.y1 || coord_y(c, b) > b->symmetry.y2)
734 return false;
735 if (coord_x(c, b) < b->symmetry.x1 || coord_x(c, b) > b->symmetry.x2)
736 return false;
737 if (b->symmetry.d) {
738 int x = coord_x(c, b);
739 if (b->symmetry.type == SYM_DIAG_DOWN)
740 x = board_size(b) - 1 - x;
741 if (x > coord_y(c, b))
742 return false;
744 return true;
747 #endif
750 #endif