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
);
53 can_countercapture(struct board
*b
, enum stone owner
, group_t g
,
54 enum stone to_play
, struct move_queue
*q
, int tag
)
59 unsigned int qmoves_prev
= q
? q
->moves
: 0;
61 foreach_in_group(b
, g
) {
62 foreach_neighbor(b
, c
, {
63 if (!capturable_group(b
, owner
, c
, to_play
))
69 mq_add(q
, board_group_info(b
, group_at(b
, c
)).lib
[0], tag
);
72 } foreach_in_group_end
;
74 bool can
= q
? q
->moves
> qmoves_prev
: false;
78 #ifdef NO_DOOMED_GROUPS
80 can_be_rescued(struct board
*b
, group_t group
, enum stone color
, int tag
)
82 /* Does playing on the liberty rescue the group? */
83 if (can_play_on_lib(b
, group
, color
))
86 /* Then, maybe we can capture one of our neighbors? */
87 return can_countercapture(b
, color
, group
, color
, NULL
, tag
);
92 group_atari_check(unsigned int alwaysccaprate
, struct board
*b
, group_t group
, enum stone to_play
,
93 struct move_queue
*q
, coord_t
*ladder
, bool middle_ladder
, int tag
)
95 enum stone color
= board_at(b
, group_base(group
));
96 coord_t lib
= board_group_info(b
, group
).lib
[0];
98 assert(color
!= S_OFFBOARD
&& color
!= S_NONE
);
100 fprintf(stderr
, "[%s] atariiiiiiiii %s of color %d\n",
101 coord2sstr(group
, b
), coord2sstr(lib
, b
), color
);
102 assert(board_at(b
, lib
) == S_NONE
);
104 if (to_play
!= color
) {
105 /* We are the attacker! In that case, do not try defending
106 * our group, since we can capture the culprit. */
107 #ifdef NO_DOOMED_GROUPS
108 /* Do not remove group that cannot be saved by the opponent. */
109 if (!can_be_rescued(b
, group
, color
, tag
))
112 if (can_play_on_lib(b
, group
, to_play
)) {
119 /* Can we capture some neighbor? */
120 bool ccap
= can_countercapture(b
, color
, group
, to_play
, q
, tag
);
121 if (ccap
&& !ladder
&& alwaysccaprate
> fast_random(100))
124 /* Otherwise, do not save kos. */
125 if (group_is_onestone(b
, group
)
126 && neighbor_count_at(b
, lib
, color
) + neighbor_count_at(b
, lib
, S_OFFBOARD
) == 4)
129 /* Do not suicide... */
130 if (!can_play_on_lib(b
, group
, to_play
))
133 fprintf(stderr
, "...escape route valid\n");
135 /* ...or play out ladders (unless we can counter-capture anytime). */
137 if (is_ladder(b
, lib
, group
, middle_ladder
)) {
138 /* Sometimes we want to keep the ladder move in the
139 * queue in order to discourage it. */
144 } else if (DEBUGL(6))
145 fprintf(stderr
, "...no ladder\n");