Merge commit '460bf'
[pachi/ann.git] / tactics / 2lib.c
blob5bb8ec9c3c59e65af6d8e01755ebb117f7fd18ff
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 #define DEBUG
6 #include "board.h"
7 #include "debug.h"
8 #include "mq.h"
9 #include "tactics/2lib.h"
10 #include "tactics/selfatari.h"
13 /* Whether to avoid capturing/atariing doomed groups (this is big
14 * performance hit and may reduce playouts balance; it does increase
15 * the strength, but not quite proportionally to the performance). */
16 //#define NO_DOOMED_GROUPS
19 static bool
20 miai_2lib(struct board *b, group_t group, enum stone color)
22 bool can_connect = false, can_pull_out = false;
23 /* We have miai if we can either connect on both libs,
24 * or connect on one lib and escape on another. (Just
25 * having two escape routes can be risky.) We must make
26 * sure that we don't consider following as miai:
27 * X X X O
28 * X . . O
29 * O O X O - left dot would be pull-out, right dot connect */
30 foreach_neighbor(b, board_group_info(b, group).lib[0], {
31 enum stone cc = board_at(b, c);
32 if (cc == S_NONE && cc != board_at(b, board_group_info(b, group).lib[1])) {
33 can_pull_out = true;
34 } else if (cc != color) {
35 continue;
38 group_t cg = group_at(b, c);
39 if (cg && cg != group && board_group_info(b, cg).libs > 1)
40 can_connect = true;
41 });
42 foreach_neighbor(b, board_group_info(b, group).lib[1], {
43 enum stone cc = board_at(b, c);
44 if (c == board_group_info(b, group).lib[0])
45 continue;
46 if (cc == S_NONE && can_connect) {
47 return true;
48 } else if (cc != color) {
49 continue;
52 group_t cg = group_at(b, c);
53 if (cg && cg != group && board_group_info(b, cg).libs > 1)
54 return (can_connect || can_pull_out);
55 });
56 return false;
59 void
60 check_group_atari(struct board *b, group_t group, enum stone owner,
61 enum stone to_play, struct move_queue *q, int tag)
63 for (int i = 0; i < 2; i++) {
64 coord_t lib = board_group_info(b, group).lib[i];
65 assert(board_at(b, lib) == S_NONE);
66 if (!board_is_valid_play(b, to_play, lib))
67 continue;
69 /* Don't play at the spot if it is extremely short
70 * of liberties... */
71 /* XXX: This looks harmful, could significantly
72 * prefer atari to throwin:
74 * XXXOOOOOXX
75 * .OO.....OX
76 * XXXOOOOOOX */
77 #if 0
78 if (neighbor_count_at(b, lib, stone_other(owner)) + immediate_liberty_count(b, lib) < 2)
79 continue;
80 #endif
82 /* If the move is too "lumpy", do not play it:
84 * #######
85 * ..O.X.X <- always play the left one!
86 * OXXXXXX */
87 if (neighbor_count_at(b, lib, stone_other(owner)) + neighbor_count_at(b, lib, S_OFFBOARD) >= 3)
88 continue;
90 /* If we are the defender not connecting out, do not
91 * escape with moves that do not gain liberties anyway
92 * - either the new extension has just single extra
93 * liberty, or the "gained" liberties are shared. */
94 /* XXX: We do not check connecting to a short-on-liberty
95 * group (e.g. ourselves). */
96 if (to_play == owner && neighbor_count_at(b, lib, owner) == 1) {
97 if (immediate_liberty_count(b, lib) == 1)
98 continue;
99 if (immediate_liberty_count(b, lib) == 2
100 && coord_is_adjecent(lib, board_group_info(b, group).lib[1 - i], b))
101 continue;
104 #ifdef NO_DOOMED_GROUPS
105 /* If the owner can't play at the spot, we don't want
106 * to bother either. */
107 if (is_bad_selfatari(b, owner, lib))
108 continue;
109 #endif
111 /* Of course we don't want to play bad selfatari
112 * ourselves, if we are the attacker... */
113 if (
114 #ifdef NO_DOOMED_GROUPS
115 to_play != owner &&
116 #endif
117 is_bad_selfatari(b, to_play, lib))
118 continue;
120 /* Tasty! Crispy! Good! */
121 mq_add(q, lib, tag);
122 mq_nodup(q);
126 void
127 group_2lib_check(struct board *b, group_t group, enum stone to_play, struct move_queue *q, int tag)
129 enum stone color = board_at(b, group_base(group));
130 assert(color != S_OFFBOARD && color != S_NONE);
132 if (DEBUGL(5))
133 fprintf(stderr, "[%s] 2lib check of color %d\n",
134 coord2sstr(group, b), color);
136 /* Do not try to atari groups that cannot be harmed. */
137 if (miai_2lib(b, group, color))
138 return;
140 check_group_atari(b, group, color, to_play, q, tag);
142 /* Can we counter-atari another group, if we are the defender? */
143 if (to_play != color)
144 return;
145 foreach_in_group(b, group) {
146 foreach_neighbor(b, c, {
147 if (board_at(b, c) != stone_other(color))
148 continue;
149 group_t g2 = group_at(b, c);
150 if (board_group_info(b, g2).libs == 1) {
151 /* We can capture a neighbor. */
152 mq_add(q, board_group_info(b, g2).lib[0], tag);
153 mq_nodup(q);
154 continue;
156 if (board_group_info(b, g2).libs != 2)
157 continue;
158 check_group_atari(b, g2, color, to_play, q, tag);
160 } foreach_in_group_end;