selfatari_cousin(): Suggest counter-captures
[pachi/peepo.git] / tactics / ladder.c
blobadd6f43dfea54926021e65cdf091c1cc816c05ea
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 #define DEBUG
6 #include "board.h"
7 #include "debug.h"
8 #include "mq.h"
9 #include "tactics/1lib.h"
10 #include "tactics/ladder.h"
13 bool
14 is_border_ladder(struct board *b, coord_t coord, enum stone lcolor)
16 int x = coord_x(coord, b), y = coord_y(coord, b);
18 if (DEBUGL(5))
19 fprintf(stderr, "border ladder\n");
20 /* Direction along border; xd is horiz. border, yd vertical. */
21 int xd = 0, yd = 0;
22 if (board_atxy(b, x + 1, y) == S_OFFBOARD || board_atxy(b, x - 1, y) == S_OFFBOARD)
23 yd = 1;
24 else
25 xd = 1;
26 /* Direction from the border; -1 is above/left, 1 is below/right. */
27 int dd = (board_atxy(b, x + yd, y + xd) == S_OFFBOARD) ? 1 : -1;
28 if (DEBUGL(6))
29 fprintf(stderr, "xd %d yd %d dd %d\n", xd, yd, dd);
30 /* | ? ?
31 * | . O #
32 * | c X #
33 * | . O #
34 * | ? ? */
35 /* This is normally caught, unless we have friends both above
36 * and below... */
37 if (board_atxy(b, x + xd * 2, y + yd * 2) == lcolor
38 && board_atxy(b, x - xd * 2, y - yd * 2) == lcolor)
39 return false;
41 /* ...or can't block where we need because of shortage
42 * of liberties. */
43 group_t g1 = group_atxy(b, x + xd - yd * dd, y + yd - xd * dd);
44 int libs1 = board_group_info(b, g1).libs;
45 group_t g2 = group_atxy(b, x - xd - yd * dd, y - yd - xd * dd);
46 int libs2 = board_group_info(b, g2).libs;
47 if (DEBUGL(6))
48 fprintf(stderr, "libs1 %d libs2 %d\n", libs1, libs2);
49 /* Already in atari? */
50 if (libs1 < 2 || libs2 < 2)
51 return false;
52 /* Would be self-atari? */
53 if (libs1 < 3 && (board_atxy(b, x + xd * 2, y + yd * 2) != S_NONE
54 || coord_is_adjecent(board_group_info(b, g1).lib[0], board_group_info(b, g1).lib[1], b)))
55 return false;
56 if (libs2 < 3 && (board_atxy(b, x - xd * 2, y - yd * 2) != S_NONE
57 || coord_is_adjecent(board_group_info(b, g2).lib[0], board_group_info(b, g2).lib[1], b)))
58 return false;
59 return true;
63 /* This is a rather expensive ladder reader. It can read out any sequences
64 * where laddered group should be kept at two liberties. The recursion
65 * always makes a "to-be-laddered" move and then considers the chaser's
66 * two alternatives (usually, one of them is trivially refutable). The
67 * function returns true if there is a branch that ends up with laddered
68 * group captured, false if not (i.e. for each branch, laddered group can
69 * gain three liberties). */
71 static bool
72 middle_ladder_walk(struct board *b, struct board *bset, group_t laddered, coord_t nextmove, enum stone lcolor)
74 assert(board_group_info(b, laddered).libs == 1);
76 /* First, escape. */
77 if (DEBUGL(6))
78 fprintf(stderr, " ladder escape %s\n", coord2sstr(nextmove, b));
79 struct move m = { nextmove, lcolor };
80 int res = board_play(b, &m);
81 laddered = group_at(b, laddered);
82 assert(res >= 0);
83 if (DEBUGL(8)) {
84 board_print(b, stderr);
85 fprintf(stderr, "%s c %d\n", coord2sstr(laddered, b), board_group_info(b, laddered).libs);
88 if (board_group_info(b, laddered).libs == 1) {
89 if (DEBUGL(6))
90 fprintf(stderr, "* we can capture now\n");
91 return true;
93 if (board_group_info(b, laddered).libs > 2) {
94 if (DEBUGL(6))
95 fprintf(stderr, "* we are free now\n");
96 return false;
99 foreach_neighbor(b, m.coord, {
100 if (board_at(b, c) == stone_other(lcolor) && board_group_info(b, group_at(b, c)).libs == 1) {
101 /* We can capture one of the ladder stones
102 * anytime later. */
103 /* XXX: If we were very lucky, capturing
104 * this stone will not help us escape.
105 * That should be pretty rate. */
106 if (DEBUGL(6))
107 fprintf(stderr, "* can capture chaser\n");
108 return false;
112 /* Now, consider alternatives. */
113 int liblist[2], libs = 0;
114 for (int i = 0; i < 2; i++) {
115 coord_t ataristone = board_group_info(b, laddered).lib[i];
116 coord_t escape = board_group_info(b, laddered).lib[1 - i];
117 if (immediate_liberty_count(b, escape) > 2 + coord_is_adjecent(ataristone, escape, b)) {
118 /* Too much free space, ignore. */
119 continue;
121 liblist[libs++] = i;
124 /* Try out the alternatives. */
125 bool is_ladder = false;
126 for (int i = 0; !is_ladder && i < libs; i++) {
127 struct board *b2 = b;
128 if (i != libs - 1) {
129 b2 = bset++;
130 board_copy(b2, b);
133 coord_t ataristone = board_group_info(b2, laddered).lib[liblist[i]];
134 // coord_t escape = board_group_info(b2, laddered).lib[1 - liblist[i]];
135 struct move m = { ataristone, stone_other(lcolor) };
136 int res = board_play(b2, &m);
137 /* If we just played self-atari, abandon ship. */
138 /* XXX: If we were very lucky, capturing this stone will
139 * not help us escape. That should be pretty rate. */
140 if (DEBUGL(6))
141 fprintf(stderr, "(%d=%d) ladder atari %s (%d libs)\n", i, res, coord2sstr(ataristone, b2), board_group_info(b2, group_at(b2, ataristone)).libs);
142 if (res >= 0 && board_group_info(b2, group_at(b2, ataristone)).libs > 1)
143 is_ladder = middle_ladder_walk(b2, bset, laddered, board_group_info(b2, laddered).lib[0], lcolor);
145 if (i != libs - 1) {
146 board_done_noalloc(b2);
149 if (DEBUGL(6))
150 fprintf(stderr, "propagating %d\n", is_ladder);
151 return is_ladder;
154 bool
155 is_middle_ladder(struct board *b, coord_t coord, group_t laddered, enum stone lcolor)
157 /* TODO: Remove the redundant parameters. */
158 assert(board_group_info(b, laddered).libs == 1);
159 assert(board_group_info(b, laddered).lib[0] == coord);
160 assert(board_at(b, laddered) == lcolor);
162 /* If we can move into empty space or do not have enough space
163 * to escape, this is obviously not a ladder. */
164 if (immediate_liberty_count(b, coord) != 2) {
165 if (DEBUGL(5))
166 fprintf(stderr, "no ladder, wrong free space\n");
167 return false;
170 /* A fair chance for a ladder. Group in atari, with some but limited
171 * space to escape. Time for the expensive stuff - set up a temporary
172 * board and start selective 2-liberty search. */
174 struct board *bset = malloc2(BOARD_MAX_SIZE * 2 * sizeof(struct board));
176 struct move_queue ccq = { .moves = 0 };
177 if (can_countercapture(b, lcolor, laddered, lcolor, &ccq, 0)) {
178 /* We could escape by countercapturing a group.
179 * Investigate. */
180 assert(ccq.moves > 0);
181 for (unsigned int i = 0; i < ccq.moves; i++) {
182 struct board b2;
183 board_copy(&b2, b);
184 bool is_ladder = middle_ladder_walk(&b2, bset, laddered, ccq.move[i], lcolor);
185 board_done_noalloc(&b2);
186 if (!is_ladder) {
187 free(bset);
188 return false;
193 struct board b2;
194 board_copy(&b2, b);
195 bool is_ladder = middle_ladder_walk(&b2, bset, laddered, board_group_info(&b2, laddered).lib[0], lcolor);
196 board_done_noalloc(&b2);
197 free(bset);
198 return is_ladder;
201 bool
202 wouldbe_ladder(struct board *b, group_t group, coord_t escapelib, coord_t chaselib, enum stone lcolor)
204 assert(board_group_info(b, group).libs == 2);
205 assert(board_at(b, group) == lcolor);
207 if (DEBUGL(6))
208 fprintf(stderr, "would-be ladder check - does %s %s play out chasing move %s?\n",
209 stone2str(lcolor), coord2sstr(escapelib, b), coord2sstr(chaselib, b));
211 if (!coord_is_8adjecent(escapelib, chaselib, b)) {
212 if (DEBUGL(5))
213 fprintf(stderr, "cannot determine ladder for remote simulated stone\n");
214 return false;
217 if (neighbor_count_at(b, chaselib, lcolor) != 1 || immediate_liberty_count(b, chaselib) != 2) {
218 if (DEBUGL(5))
219 fprintf(stderr, "overly trivial for a ladder\n");
220 return false;
223 bool is_ladder = false;
224 struct board *bset = malloc2(BOARD_MAX_SIZE * 2 * sizeof(struct board));
225 struct board b2;
226 board_copy(&b2, b);
228 struct move m = { chaselib, stone_other(lcolor) };
229 int res = board_play(&b2, &m);
230 if (res >= 0)
231 is_ladder = middle_ladder_walk(&b2, bset, group, board_group_info(&b2, group).lib[0], lcolor);
233 board_done_noalloc(&b2);
234 free(bset);
235 return is_ladder;