5 #define QUICK_BOARD_CODE
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
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:
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])) {
36 } else if (cc
!= color
) {
40 group_t cg
= group_at(b
, c
);
41 if (cg
&& cg
!= group
&& board_group_info(b
, cg
).libs
> 1)
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])
48 if (cc
== S_NONE
&& can_connect
) {
50 } else if (cc
!= color
) {
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
);
62 defense_is_hopeless(struct board
*b
, group_t group
, enum stone owner
,
63 enum stone to_play
, coord_t lib
, coord_t otherlib
,
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). */
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
));
79 if (to_play
== owner
&& neighbor_count_at(b
, lib
, owner
) == 1) {
80 if (immediate_liberty_count(b
, lib
) == 1)
82 if (immediate_liberty_count(b
, lib
) == 2
83 && coord_is_adjecent(lib
, otherlib
, b
))
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
))
103 fprintf(stderr
, "- checking liberty %s of %s %s, filled by %s\n",
105 stone2str(owner
), coord2sstr(group
, b
),
108 /* Don't play at the spot if it is extremely short
110 /* XXX: This looks harmful, could significantly
111 * prefer atari to throwin:
117 if (neighbor_count_at(b
, lib
, stone_other(owner
)) + immediate_liberty_count(b
, lib
) < 2)
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
))
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
))
134 /* Of course we don't want to play bad selfatari
135 * ourselves, if we are the attacker... */
137 #ifdef NO_DOOMED_GROUPS
140 is_bad_selfatari(b
, to_play
, lib
)) {
142 fprintf(stderr
, "\tliberty is selfatari\n");
143 coord_t coord
= pass
;
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
);
153 /* Ok, connect, but prefer not to. */
154 enum stone byowner
= board_at(b
, bygroup
);
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
,
162 use_def_no_hopeless
))
165 preference
[i
] = false;
167 /* By now, we must be decided we add the move to the
168 * queue! [comment intentionally misindented] */
174 /* If the move is too "lumpy", prefer the alternative:
177 * ..O.X.X <- always play the left one!
179 if (neighbor_count_at(b
, lib
, to_play
) + neighbor_count_at(b
, lib
, S_OFFBOARD
) >= 3) {
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;
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])
195 /* ...else{ may happen, since we call
196 * mq_nodup() and the move might have
197 * been there earlier. */
199 assert(!preference
[1]);
204 /* Tasty! Crispy! Good! */
211 snprintf(label
, 256, "= final %s %s liberties to play by %s",
212 stone2str(owner
), coord2sstr(group
, b
),
214 mq_print(q
, b
, label
);
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
);
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
))
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
)
237 foreach_in_group(b
, group
) {
238 foreach_neighbor(b
, c
, {
239 if (board_at(b
, c
) != stone_other(color
))
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
);
248 if (board_group_info(b
, g2
).libs
!= 2)
250 can_atari_group(b
, g2
, stone_other(color
), to_play
, q
, tag
, use_def_no_hopeless
);
252 } foreach_in_group_end
;