9 #include "tactics/1lib.h"
10 #include "tactics/ladder.h"
11 #include "tactics/selfatari.h"
14 /* Whether to avoid capturing/atariing doomed groups (this is big
15 * performance hit and may reduce playouts balance; it does increase
16 * the strength, but not quite proportionally to the performance). */
17 //#define NO_DOOMED_GROUPS
21 can_play_on_lib(struct board
*b
, group_t g
, enum stone to_play
)
23 coord_t capture
= board_group_info(b
, g
).lib
[0];
25 fprintf(stderr
, "can capture group %d (%s)?\n",
26 g
, coord2sstr(capture
, b
));
27 /* Does playing on the liberty usefully capture the group? */
28 if (board_is_valid_play(b
, to_play
, capture
)
29 && !is_bad_selfatari(b
, to_play
, capture
))
35 /* For given position @c, decide if this is a group that is in danger from
36 * @capturer and @to_play can do anything about it (play at the last
37 * liberty to either capture or escape). */
38 /* Note that @to_play is important; e.g. consider snapback, it's good
39 * to play at the last liberty by attacker, but not defender. */
40 static __attribute__((always_inline
)) bool
41 capturable_group(struct board
*b
, enum stone capturer
, coord_t c
,
44 group_t g
= group_at(b
, c
);
45 if (likely(board_at(b
, c
) != stone_other(capturer
)
46 || board_group_info(b
, g
).libs
> 1))
49 return can_play_on_lib(b
, g
, to_play
);
52 /* For given atari group @group owned by @owner, decide if @to_play
53 * can save it / keep it in danger by dealing with one of the
54 * neighboring groups. */
56 can_countercapture(struct board
*b
, enum stone owner
, group_t g
,
57 enum stone to_play
, struct move_queue
*q
, int tag
)
62 unsigned int qmoves_prev
= q
? q
->moves
: 0;
64 foreach_in_group(b
, g
) {
65 foreach_neighbor(b
, c
, {
66 if (!capturable_group(b
, owner
, c
, to_play
))
72 mq_add(q
, board_group_info(b
, group_at(b
, c
)).lib
[0], tag
);
75 } foreach_in_group_end
;
77 bool can
= q
? q
->moves
> qmoves_prev
: false;
81 #ifdef NO_DOOMED_GROUPS
83 can_be_rescued(struct board
*b
, group_t group
, enum stone color
, int tag
)
85 /* Does playing on the liberty rescue the group? */
86 if (can_play_on_lib(b
, group
, color
))
89 /* Then, maybe we can capture one of our neighbors? */
90 return can_countercapture(b
, color
, group
, color
, NULL
, tag
);
95 group_atari_check(unsigned int alwaysccaprate
, struct board
*b
, group_t group
, enum stone to_play
,
96 struct move_queue
*q
, coord_t
*ladder
, int tag
)
98 enum stone color
= board_at(b
, group_base(group
));
99 coord_t lib
= board_group_info(b
, group
).lib
[0];
101 assert(color
!= S_OFFBOARD
&& color
!= S_NONE
);
103 fprintf(stderr
, "[%s] atariiiiiiiii %s of color %d\n",
104 coord2sstr(group
, b
), coord2sstr(lib
, b
), color
);
105 assert(board_at(b
, lib
) == S_NONE
);
107 if (to_play
!= color
) {
108 /* We are the attacker! In that case, do not try defending
109 * our group, since we can capture the culprit. */
110 #ifdef NO_DOOMED_GROUPS
111 /* Do not remove group that cannot be saved by the opponent. */
112 if (!can_be_rescued(b
, group
, color
, tag
))
115 if (can_play_on_lib(b
, group
, to_play
)) {
122 /* Can we capture some neighbor? */
123 bool ccap
= can_countercapture(b
, color
, group
, to_play
, q
, tag
);
124 if (ccap
&& !ladder
&& alwaysccaprate
> fast_random(100))
127 /* Otherwise, do not save kos. */
128 if (group_is_onestone(b
, group
)
129 && neighbor_count_at(b
, lib
, color
) + neighbor_count_at(b
, lib
, S_OFFBOARD
) == 4)
132 /* Do not suicide... */
133 if (!can_play_on_lib(b
, group
, to_play
))
136 fprintf(stderr
, "...escape route valid\n");
138 /* ...or play out ladders. */
139 if (is_ladder(b
, lib
, group
)) {
140 /* Sometimes we want to keep the ladder move in the
141 * queue in order to discourage it. */
148 fprintf(stderr
, "...no ladder\n");