middle_ladder_walk(): Take next escape move as a parameter
[pachi.git] / tactics / ladder.c
blobae31b66d58ee60c978486ff4893ea93d9ee54dd2
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 "tactics/ladder.h"
11 bool
12 is_border_ladder(struct board *b, coord_t coord, enum stone lcolor)
14 int x = coord_x(coord, b), y = coord_y(coord, b);
16 if (DEBUGL(5))
17 fprintf(stderr, "border ladder\n");
18 /* Direction along border; xd is horiz. border, yd vertical. */
19 int xd = 0, yd = 0;
20 if (board_atxy(b, x + 1, y) == S_OFFBOARD || board_atxy(b, x - 1, y) == S_OFFBOARD)
21 yd = 1;
22 else
23 xd = 1;
24 /* Direction from the border; -1 is above/left, 1 is below/right. */
25 int dd = (board_atxy(b, x + yd, y + xd) == S_OFFBOARD) ? 1 : -1;
26 if (DEBUGL(6))
27 fprintf(stderr, "xd %d yd %d dd %d\n", xd, yd, dd);
28 /* | ? ?
29 * | . O #
30 * | c X #
31 * | . O #
32 * | ? ? */
33 /* This is normally caught, unless we have friends both above
34 * and below... */
35 if (board_atxy(b, x + xd * 2, y + yd * 2) == lcolor
36 && board_atxy(b, x - xd * 2, y - yd * 2) == lcolor)
37 return false;
38 /* ...or can't block where we need because of shortage
39 * of liberties. */
40 int libs1 = board_group_info(b, group_atxy(b, x + xd - yd * dd, y + yd - xd * dd)).libs;
41 int libs2 = board_group_info(b, group_atxy(b, x - xd - yd * dd, y - yd - xd * dd)).libs;
42 if (DEBUGL(6))
43 fprintf(stderr, "libs1 %d libs2 %d\n", libs1, libs2);
44 if (libs1 < 2 || libs2 < 2)
45 return false;
46 if (board_atxy(b, x + xd * 2, y + yd * 2) == lcolor && libs1 < 3)
47 return false;
48 if (board_atxy(b, x - xd * 2, y - yd * 2) == lcolor && libs2 < 3)
49 return false;
50 return true;
54 /* This is a rather expensive ladder reader. It can read out any sequences
55 * where laddered group should be kept at two liberties. The recursion
56 * always makes a "to-be-laddered" move and then considers the chaser's
57 * two alternatives (usually, one of them is trivially refutable). The
58 * function returns true if there is a branch that ends up with laddered
59 * group captured, false if not (i.e. for each branch, laddered group can
60 * gain three liberties). */
62 static bool
63 middle_ladder_walk(struct board *b, group_t laddered, coord_t nextmove, enum stone lcolor)
65 assert(board_group_info(b, laddered).libs == 1);
67 /* First, escape. */
68 if (DEBUGL(6))
69 fprintf(stderr, " ladder escape %s\n", coord2sstr(nextmove, b));
70 struct move m = { nextmove, lcolor };
71 int res = board_play(b, &m);
72 laddered = group_at(b, laddered);
73 assert(res >= 0);
74 if (DEBUGL(8)) {
75 board_print(b, stderr);
76 fprintf(stderr, "%s c %d\n", coord2sstr(laddered, b), board_group_info(b, laddered).libs);
79 if (board_group_info(b, laddered).libs == 1) {
80 if (DEBUGL(6))
81 fprintf(stderr, "* we can capture now\n");
82 return true;
84 if (board_group_info(b, laddered).libs > 2) {
85 if (DEBUGL(6))
86 fprintf(stderr, "* we are free now\n");
87 return false;
90 foreach_neighbor(b, m.coord, {
91 if (board_at(b, c) == stone_other(lcolor) && board_group_info(b, group_at(b, c)).libs == 1) {
92 /* We can capture one of the ladder stones
93 * anytime later. */
94 /* XXX: If we were very lucky, capturing
95 * this stone will not help us escape.
96 * That should be pretty rate. */
97 if (DEBUGL(6))
98 fprintf(stderr, "* can capture chaser\n");
99 return false;
103 /* Now, consider alternatives. */
104 int liblist[2], libs = 0;
105 for (int i = 0; i < 2; i++) {
106 coord_t ataristone = board_group_info(b, laddered).lib[i];
107 coord_t escape = board_group_info(b, laddered).lib[1 - i];
108 if (immediate_liberty_count(b, escape) > 2 + coord_is_adjecent(ataristone, escape, b)) {
109 /* Too much free space, ignore. */
110 continue;
112 liblist[libs++] = i;
115 /* Try out the alternatives. */
116 bool is_ladder = false;
117 for (int i = 0; !is_ladder && i < libs; i++) {
118 struct board *b2 = b;
119 struct board b2s;
120 if (i != libs - 1) {
121 board_copy(&b2s, b);
122 b2 = &b2s;
125 coord_t ataristone = board_group_info(b2, laddered).lib[liblist[i]];
126 // coord_t escape = board_group_info(b2, laddered).lib[1 - liblist[i]];
127 struct move m = { ataristone, stone_other(lcolor) };
128 int res = board_play(b2, &m);
129 /* If we just played self-atari, abandon ship. */
130 /* XXX: If we were very lucky, capturing this stone will
131 * not help us escape. That should be pretty rate. */
132 if (DEBUGL(6))
133 fprintf(stderr, "(%d=%d) ladder atari %s (%d libs)\n", i, res, coord2sstr(ataristone, b2), board_group_info(b2, group_at(b2, ataristone)).libs);
134 if (res >= 0 && board_group_info(b2, group_at(b2, ataristone)).libs > 1)
135 is_ladder = middle_ladder_walk(b2, laddered, board_group_info(b2, laddered).lib[0], lcolor);
137 if (i != libs - 1) {
138 board_done_noalloc(&b2s);
141 if (DEBUGL(6))
142 fprintf(stderr, "propagating %d\n", is_ladder);
143 return is_ladder;
146 bool
147 is_middle_ladder(struct board *b, coord_t coord, group_t laddered, enum stone lcolor)
149 /* TODO: Remove the redundant parameters. */
150 assert(board_group_info(b, laddered).libs == 1);
151 assert(board_group_info(b, laddered).lib[0] == coord);
152 assert(board_at(b, laddered) == lcolor);
154 /* If we can move into empty space or do not have enough space
155 * to escape, this is obviously not a ladder. */
156 if (immediate_liberty_count(b, coord) != 2) {
157 if (DEBUGL(5))
158 fprintf(stderr, "no ladder, wrong free space\n");
159 return false;
162 /* A fair chance for a ladder. Group in atari, with some but limited
163 * space to escape. Time for the expensive stuff - set up a temporary
164 * board and start selective 2-liberty search. */
165 struct board b2;
166 board_copy(&b2, b);
167 bool is_ladder = middle_ladder_walk(&b2, laddered, board_group_info(&b2, laddered).lib[0], lcolor);
168 board_done_noalloc(&b2);
169 return is_ladder;
172 bool
173 wouldbe_ladder(struct board *b, group_t group, coord_t escapelib, coord_t chaselib, enum stone lcolor)
175 assert(board_group_info(b, group).libs == 2);
176 assert(board_at(b, group) == lcolor);
178 if (DEBUGL(6))
179 fprintf(stderr, "would-be ladder check - does %s %s play out chasing move %s?\n",
180 stone2str(lcolor), coord2sstr(escapelib, b), coord2sstr(chaselib, b));
182 if (!coord_is_8adjecent(escapelib, chaselib, b)) {
183 if (DEBUGL(5))
184 fprintf(stderr, "cannot determine ladder for remote simulated stone\n");
185 return false;
188 if (neighbor_count_at(b, chaselib, lcolor) != 1 || immediate_liberty_count(b, chaselib) != 2) {
189 if (DEBUGL(5))
190 fprintf(stderr, "overly trivial for a ladder\n");
191 return false;
194 bool is_ladder = false;
195 struct board b2;
196 board_copy(&b2, b);
198 struct move m = { chaselib, stone_other(lcolor) };
199 int res = board_play(&b2, &m);
200 if (res >= 0)
201 is_ladder = middle_ladder_walk(&b2, group, board_group_info(&b2, group).lib[0], lcolor);
203 board_done_noalloc(&b2);
204 return is_ladder;