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