middle_ladder_walk(): Fix not noticing ladders that skim corners at the edges
[pachi/json.git] / tactics / util.h
blob47e42cfb69ddd41c5069270ad87692b00c60b5a7
1 #ifndef ZZGO_TACTICS_UTIL_H
2 #define ZZGO_TACTICS_UTIL_H
4 /* Advanced tactical checks non-essential to the board implementation. */
6 #include "board.h"
7 #include "debug.h"
9 struct move_queue;
11 /* Checks if there are any stones in n-vincinity of coord. */
12 bool board_stone_radar(struct board *b, coord_t coord, int distance);
14 /* Measure various distances on the board: */
15 /* Distance from the edge; on edge returns 0. */
16 static int coord_edge_distance(coord_t c, struct board *b);
17 /* Distance of two points in gridcular metric - this metric defines
18 * circle-like structures on the square grid. */
19 static int coord_gridcular_distance(coord_t c1, coord_t c2, struct board *b);
21 /* Construct a "common fate graph" from given coordinate; that is, a weighted
22 * graph of intersections where edges between all neighbors have weight 1,
23 * but edges between neighbors of same color have weight 0. Thus, this is
24 * "stone chain" metric in a sense. */
25 /* The output are distanes from start stored in given [board_size2()] array;
26 * intersections further away than maxdist have all distance maxdist+1 set. */
27 void cfg_distances(struct board *b, coord_t start, int *distances, int maxdist);
29 /* Compute an extra komi describing the "effective handicap" black receives
30 * (returns 0 for even game with 7.5 komi). @stone_value is value of single
31 * handicap stone, 7 is a good default. */
32 /* This is just an approximation since in reality, handicap seems to be usually
33 * non-linear. */
34 floating_t board_effective_handicap(struct board *b, int first_move_value);
36 /* Decide if the given player wins counting on the board, considering
37 * that given groups are dead. (To get the list of dead groups, use
38 * e.g. groups_of_status().) */
39 bool pass_is_safe(struct board *b, enum stone color, struct move_queue *mq);
41 /* Returns estimated number of remaining moves for one player until end of game. */
42 int board_estimated_moves_left(struct board *b);
44 /* To avoid running out of time, assume we always have at least 30 more moves
45 * to play if we don't have more precise information from gtp time_left: */
46 #define MIN_MOVES_LEFT 30
49 static inline int
50 coord_edge_distance(coord_t c, struct board *b)
52 int x = coord_x(c, b), y = coord_y(c, b);
53 int dx = x > board_size(b) / 2 ? board_size(b) - 1 - x : x;
54 int dy = y > board_size(b) / 2 ? board_size(b) - 1 - y : y;
55 return (dx < dy ? dx : dy) - 1 /* S_OFFBOARD */;
58 static inline int
59 coord_gridcular_distance(coord_t c1, coord_t c2, struct board *b)
61 int dx = abs(coord_dx(c1, c2, b)), dy = abs(coord_dy(c1, c2, b));
62 return dx + dy + (dx > dy ? dx : dy);
65 #endif