selfatari_cousin(): fixed invalid can_countercapture() calls
[pachi.git] / tactics / nlib.c
blob3d91dcb1206390bb5892a1a64d3d85a669d5e630
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/nlib.h"
13 #include "tactics/selfatari.h"
16 void
17 group_nlib_defense_check(struct board *b, group_t group, enum stone to_play, struct move_queue *q, int tag)
19 enum stone color = to_play;
20 assert(color != S_OFFBOARD && color != S_NONE
21 && color == board_at(b, group_base(group)));
23 if (DEBUGL(5))
24 fprintf(stderr, "[%s] nlib defense check of color %d\n",
25 coord2sstr(group, b), color);
27 #if 0
28 /* XXX: The code below is specific for 3-liberty groups. Its impact
29 * needs to be tested first, and possibly moved to a more appropriate
30 * place. */
32 /* First, look at our liberties. */
33 int continuous = 0, enemy = 0, spacy = 0, eyes = 0;
34 for (int i = 0; i < 3; i++) {
35 coord_t c = board_group_info(b, group).lib[i];
36 eyes += board_is_one_point_eye(b, c, to_play);
37 continuous += coord_is_adjecent(c, board_group_info(b, group).lib[(i + 1) % 3], b);
38 enemy += neighbor_count_at(b, c, stone_other(color));
39 spacy += immediate_liberty_count(b, c) > 1;
42 /* Safe groups are boring. */
43 if (eyes > 1)
44 return;
46 /* If all our liberties are in single line and they are internal,
47 * this is likely a tiny three-point eyespace that we rather want
48 * to live at! */
49 assert(continuous < 3);
50 if (continuous == 2 && !enemy && spacy == 1) {
51 assert(!eyes);
52 int i;
53 for (i = 0; i < 3; i++)
54 if (immediate_liberty_count(b, board_group_info(b, group).lib[i]) == 2)
55 break;
56 /* Play at middle point. */
57 mq_add(q, board_group_info(b, group).lib[i], tag);
58 mq_nodup(q);
59 return;
61 #endif
63 /* "Escaping" (gaining more liberties) with many-liberty group
64 * is difficult. Do not even try. */
66 /* There is another way to gain safety - through winning semeai
67 * with another group. */
68 /* We will not look at taking liberties of enemy n-groups, since
69 * we do not try to gain liberties for own n-groups. That would
70 * be really unbalanced (and most of our liberty-taking moves
71 * would be really stupid, most likely). */
73 /* However, it is possible that we must start capturing a 2-lib
74 * neighbor right now, because of approach liberties. Therefore,
75 * we will check for this case. If we take a liberty of a group
76 * even if we could have waited another move, no big harm done
77 * either. */
79 foreach_in_group(b, group) {
80 foreach_neighbor(b, c, {
81 if (board_at(b, c) != stone_other(color))
82 continue;
83 group_t g2 = group_at(b, c);
84 if (board_group_info(b, g2).libs != 2)
85 continue;
86 can_atari_group(b, g2, stone_other(color), to_play, q, tag, true /* XXX */);
87 });
88 } foreach_in_group_end;