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