time_in_byoyomi(): Rewrite, smoother to read and more flexible now
[pachi.git] / tactics.h
blob2e49238fde87633b71567e83102e14056bb50dd2
1 #ifndef ZZGO_TACTICS_H
2 #define ZZGO_TACTICS_H
4 /* Advanced tactical checks non-essential to the board implementation. */
6 #include "board.h"
8 struct move_queue;
10 /* Check if this move is undesirable self-atari (resulting group would have
11 * only single liberty and not capture anything; ko is allowed); we mostly
12 * want to avoid these moves. The function actually does a rather elaborate
13 * tactical check, allowing self-atari moves that are nakade, eye falsification
14 * or throw-ins. */
15 static bool is_bad_selfatari(struct board *b, enum stone color, coord_t to);
17 /* Check if escaping on this liberty by given group in atari would play out
18 * a simple ladder. */
19 /* Two ways of ladder reading can be enabled separately; simple first-line
20 * ladders and trivial middle-board ladders. */
21 static bool is_ladder(struct board *b, coord_t coord, group_t laddered,
22 bool border_ladders, bool middle_ladders);
24 /* Checks if there are any stones in n-vincinity of coord. */
25 bool board_stone_radar(struct board *b, coord_t coord, int distance);
27 /* Measure various distances on the board: */
28 /* Distance from the edge; on edge returns 0. */
29 static int coord_edge_distance(coord_t c, struct board *b);
30 /* Distance of two points in gridcular metric - this metric defines
31 * circle-like structures on the square grid. */
32 static int coord_gridcular_distance(coord_t c1, coord_t c2, struct board *b);
34 /* Construct a "common fate graph" from given coordinate; that is, a weighted
35 * graph of intersections where edges between all neighbors have weight 1,
36 * but edges between neighbors of same color have weight 0. Thus, this is
37 * "stone chain" metric in a sense. */
38 /* The output are distanes from start stored in given [board_size2()] array;
39 * intersections further away than maxdist have all distance maxdist+1 set. */
40 void cfg_distances(struct board *b, coord_t start, int *distances, int maxdist);
42 /* Compute an extra komi describing the "effective handicap" black receives
43 * (returns 0 for even game with 7.5 komi). */
44 /* This is just an approximation since in reality, handicap seems to be usually
45 * non-linear. */
46 float board_effective_handicap(struct board *b);
48 /* Decide if the given player wins counting on the board, considering
49 * that given groups are dead. (To get the list of dead groups, use
50 * e.g. groups_of_status().) */
51 bool pass_is_safe(struct board *b, enum stone color, struct move_queue *mq);
54 bool is_bad_selfatari_slow(struct board *b, enum stone color, coord_t to);
55 static inline bool
56 is_bad_selfatari(struct board *b, enum stone color, coord_t to)
58 /* More than one immediate liberty, thumbs up! */
59 if (immediate_liberty_count(b, to) > 1)
60 return false;
62 return is_bad_selfatari_slow(b, color, to);
65 bool is_border_ladder(struct board *b, coord_t coord, enum stone lcolor);
66 bool is_middle_ladder(struct board *b, coord_t coord, enum stone lcolor);
67 static inline bool
68 is_ladder(struct board *b, coord_t coord, group_t laddered,
69 bool border_ladders, bool middle_ladders)
71 enum stone lcolor = board_at(b, group_base(laddered));
73 if (DEBUGL(6))
74 fprintf(stderr, "ladder check - does %s play out %s's laddered group %s?\n",
75 coord2sstr(coord, b), stone2str(lcolor), coord2sstr(laddered, b));
77 /* First, special-case first-line "ladders". This is a huge chunk
78 * of ladders we actually meet and want to play. */
79 if (border_ladders
80 && neighbor_count_at(b, coord, S_OFFBOARD) == 1
81 && neighbor_count_at(b, coord, lcolor) == 1) {
82 bool l = is_border_ladder(b, coord, lcolor);
83 if (DEBUGL(6)) fprintf(stderr, "border ladder solution: %d\n", l);
84 return l;
87 if (middle_ladders) {
88 bool l = is_middle_ladder(b, coord, lcolor);
89 if (DEBUGL(6)) fprintf(stderr, "middle ladder solution: %d\n", l);
90 return l;
93 if (DEBUGL(6)) fprintf(stderr, "no ladder to be checked\n");
94 return false;
98 static inline int
99 coord_edge_distance(coord_t c, struct board *b)
101 int x = coord_x(c, b), y = coord_y(c, b);
102 int dx = x > board_size(b) / 2 ? board_size(b) - 1 - x : x;
103 int dy = y > board_size(b) / 2 ? board_size(b) - 1 - y : y;
104 return (dx < dy ? dx : dy) - 1 /* S_OFFBOARD */;
107 static inline int
108 coord_gridcular_distance(coord_t c1, coord_t c2, struct board *b)
110 int dx = abs(coord_dx(c1, c2, b)), dy = abs(coord_dy(c1, c2, b));
111 return dx + dy + (dx > dy ? dx : dy);
114 #endif