board_undo: define QUICK_BOARD_CODE to get compiler error with forbidden fields.
[pachi.git] / tactics / 2lib.c
bloba6771f7d93d1e2979eb35caa4e7bdd4185f98f6b
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/2lib.h"
12 #include "tactics/selfatari.h"
15 /* Whether to avoid capturing/atariing doomed groups (this is big
16 * performance hit and may reduce playouts balance; it does increase
17 * the strength, but not quite proportionally to the performance). */
18 //#define NO_DOOMED_GROUPS
21 static bool
22 miai_2lib(struct board *b, group_t group, enum stone color)
24 bool can_connect = false, can_pull_out = false;
25 /* We have miai if we can either connect on both libs,
26 * or connect on one lib and escape on another. (Just
27 * having two escape routes can be risky.) We must make
28 * sure that we don't consider following as miai:
29 * X X X O
30 * X . . O
31 * O O X O - left dot would be pull-out, right dot connect */
32 foreach_neighbor(b, board_group_info(b, group).lib[0], {
33 enum stone cc = board_at(b, c);
34 if (cc == S_NONE && cc != board_at(b, board_group_info(b, group).lib[1])) {
35 can_pull_out = true;
36 } else if (cc != color) {
37 continue;
40 group_t cg = group_at(b, c);
41 if (cg && cg != group && board_group_info(b, cg).libs > 1)
42 can_connect = true;
43 });
44 foreach_neighbor(b, board_group_info(b, group).lib[1], {
45 enum stone cc = board_at(b, c);
46 if (c == board_group_info(b, group).lib[0])
47 continue;
48 if (cc == S_NONE && can_connect) {
49 return true;
50 } else if (cc != color) {
51 continue;
54 group_t cg = group_at(b, c);
55 if (cg && cg != group && board_group_info(b, cg).libs > 1)
56 return (can_connect || can_pull_out);
57 });
58 return false;
61 static bool
62 defense_is_hopeless(struct board *b, group_t group, enum stone owner,
63 enum stone to_play, coord_t lib, coord_t otherlib,
64 bool use)
66 /* If we are the defender not connecting out, do not
67 * escape with moves that do not gain liberties anyway
68 * - either the new extension has just single extra
69 * liberty, or the "gained" liberties are shared. */
70 /* XXX: We do not check connecting to a short-on-liberty
71 * group (e.g. ourselves). */
72 if (DEBUGL(7))
73 fprintf(stderr, "\tif_check %d and defending %d and uscount %d ilcount %d\n",
74 use, to_play == owner,
75 neighbor_count_at(b, lib, owner),
76 immediate_liberty_count(b, lib));
77 if (!use)
78 return false;
79 if (to_play == owner && neighbor_count_at(b, lib, owner) == 1) {
80 if (immediate_liberty_count(b, lib) == 1)
81 return true;
82 if (immediate_liberty_count(b, lib) == 2
83 && coord_is_adjecent(lib, otherlib, b))
84 return true;
86 return false;
89 void
90 can_atari_group(struct board *b, group_t group, enum stone owner,
91 enum stone to_play, struct move_queue *q,
92 int tag, bool use_def_no_hopeless)
94 bool have[2] = { false, false };
95 bool preference[2] = { true, true };
96 for (int i = 0; i < 2; i++) {
97 coord_t lib = board_group_info(b, group).lib[i];
98 assert(board_at(b, lib) == S_NONE);
99 if (!board_is_valid_play(b, to_play, lib))
100 continue;
102 if (DEBUGL(6))
103 fprintf(stderr, "- checking liberty %s of %s %s, filled by %s\n",
104 coord2sstr(lib, b),
105 stone2str(owner), coord2sstr(group, b),
106 stone2str(to_play));
108 /* Don't play at the spot if it is extremely short
109 * of liberties... */
110 /* XXX: This looks harmful, could significantly
111 * prefer atari to throwin:
113 * XXXOOOOOXX
114 * .OO.....OX
115 * XXXOOOOOOX */
116 #if 0
117 if (neighbor_count_at(b, lib, stone_other(owner)) + immediate_liberty_count(b, lib) < 2)
118 continue;
119 #endif
121 /* Prevent hopeless escape attempts. */
122 if (defense_is_hopeless(b, group, owner, to_play, lib,
123 board_group_info(b, group).lib[1 - i],
124 use_def_no_hopeless))
125 continue;
127 #ifdef NO_DOOMED_GROUPS
128 /* If the owner can't play at the spot, we don't want
129 * to bother either. */
130 if (is_bad_selfatari(b, owner, lib))
131 continue;
132 #endif
134 /* Of course we don't want to play bad selfatari
135 * ourselves, if we are the attacker... */
136 if (
137 #ifdef NO_DOOMED_GROUPS
138 to_play != owner &&
139 #endif
140 is_bad_selfatari(b, to_play, lib)) {
141 if (DEBUGL(7))
142 fprintf(stderr, "\tliberty is selfatari\n");
143 coord_t coord = pass;
144 group_t bygroup = 0;
145 if (to_play != owner) {
146 /* Okay! We are attacker; maybe we just need
147 * to connect a false eye before atari - this
148 * is very common in the corner. */
149 coord = selfatari_cousin(b, to_play, lib, &bygroup);
151 if (is_pass(coord))
152 continue;
153 /* Ok, connect, but prefer not to. */
154 enum stone byowner = board_at(b, bygroup);
155 if (DEBUGL(7))
156 fprintf(stderr, "\treluctantly switching to cousin %s (group %s %s)\n",
157 coord2sstr(coord, b), coord2sstr(bygroup, b), stone2str(byowner));
158 /* One more thing - is the cousin sensible defense
159 * for the other group? */
160 if (defense_is_hopeless(b, bygroup, byowner, to_play,
161 coord, lib,
162 use_def_no_hopeless))
163 continue;
164 lib = coord;
165 preference[i] = false;
167 /* By now, we must be decided we add the move to the
168 * queue! [comment intentionally misindented] */
172 have[i] = true;
174 /* If the move is too "lumpy", prefer the alternative:
176 * #######
177 * ..O.X.X <- always play the left one!
178 * OXXXXXX */
179 if (neighbor_count_at(b, lib, to_play) + neighbor_count_at(b, lib, S_OFFBOARD) >= 3) {
180 if (DEBUGL(7))
181 fprintf(stderr, "\tlumpy: mine %d + edge %d\n",
182 neighbor_count_at(b, lib, to_play),
183 neighbor_count_at(b, lib, S_OFFBOARD));
184 preference[i] = false;
187 if (DEBUGL(6))
188 fprintf(stderr, "+ liberty %s ready with preference %d\n", coord2sstr(lib, b), preference[i]);
190 /* If we prefer only one of the moves, pick that one. */
191 if (i == 1 && have[0] && preference[0] != preference[1]) {
192 if (!preference[0]) {
193 if (q->move[q->moves - 1] == board_group_info(b, group).lib[0])
194 q->moves--;
195 /* ...else{ may happen, since we call
196 * mq_nodup() and the move might have
197 * been there earlier. */
198 } else {
199 assert(!preference[1]);
200 continue;
204 /* Tasty! Crispy! Good! */
205 mq_add(q, lib, tag);
206 mq_nodup(q);
209 if (DEBUGL(7)) {
210 char label[256];
211 snprintf(label, 256, "= final %s %s liberties to play by %s",
212 stone2str(owner), coord2sstr(group, b),
213 stone2str(to_play));
214 mq_print(q, b, label);
218 void
219 group_2lib_check(struct board *b, group_t group, enum stone to_play, struct move_queue *q, int tag, bool use_miaisafe, bool use_def_no_hopeless)
221 enum stone color = board_at(b, group_base(group));
222 assert(color != S_OFFBOARD && color != S_NONE);
224 if (DEBUGL(5))
225 fprintf(stderr, "[%s] 2lib check of color %d\n",
226 coord2sstr(group, b), color);
228 /* Do not try to atari groups that cannot be harmed. */
229 if (use_miaisafe && miai_2lib(b, group, color))
230 return;
232 can_atari_group(b, group, color, to_play, q, tag, use_def_no_hopeless);
234 /* Can we counter-atari another group, if we are the defender? */
235 if (to_play != color)
236 return;
237 foreach_in_group(b, group) {
238 foreach_neighbor(b, c, {
239 if (board_at(b, c) != stone_other(color))
240 continue;
241 group_t g2 = group_at(b, c);
242 if (board_group_info(b, g2).libs == 1) {
243 /* We can capture a neighbor. */
244 mq_add(q, board_group_info(b, g2).lib[0], tag);
245 mq_nodup(q);
246 continue;
248 if (board_group_info(b, g2).libs != 2)
249 continue;
250 can_atari_group(b, g2, stone_other(color), to_play, q, tag, use_def_no_hopeless);
252 } foreach_in_group_end;