patterns.gammaf: New set according to latest changes
[pachi/json.git] / tactics.c
blob920d861d21cd65110c47e8482b7ba05f28cf7004
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 #define DEBUG
6 #include "board.h"
7 #include "debug.h"
10 struct selfatari_state {
11 int groupcts[S_MAX];
12 group_t groupids[S_MAX][4];
14 /* This is set if this move puts a group out of _all_
15 * liberties; we need to watch out for snapback then. */
16 bool friend_has_no_libs;
17 /* We may have one liberty, but be looking for one more.
18 * In that case, @needs_more_lib is id of group
19 * already providing one, don't consider it again. */
20 group_t needs_more_lib;
21 /* ID of the first liberty, providing it again is not
22 * interesting. */
23 coord_t needs_more_lib_except;
26 static int
27 examine_friendly_groups(struct board *b, enum stone color, coord_t to, struct selfatari_state *s)
29 for (int i = 0; i < s->groupcts[color]; i++) {
30 /* We can escape by connecting to this group if it's
31 * not in atari. */
32 group_t g = s->groupids[color][i];
34 if (board_group_info(b, g).libs == 1) {
35 if (!s->needs_more_lib)
36 s->friend_has_no_libs = true;
37 // or we already have a friend with 1 lib
38 continue;
41 /* Could we self-atari the group here? */
42 if (board_group_info(b, g).libs > 2)
43 return false;
45 /* We need to have another liberty, and
46 * it must not be the other liberty of
47 * the group. */
48 int lib2 = board_group_info(b, g).lib[0];
49 if (lib2 == to) lib2 = board_group_info(b, g).lib[1];
50 /* Maybe we already looked at another
51 * group providing one liberty? */
52 if (s->needs_more_lib && s->needs_more_lib != g
53 && s->needs_more_lib_except != lib2)
54 return false;
56 /* Can we get the liberty locally? */
57 /* Yes if we are route to more liberties... */
58 if (s->groupcts[S_NONE] > 1)
59 return false;
60 /* ...or one liberty, but not lib2. */
61 if (s->groupcts[S_NONE] > 0
62 && !coord_is_adjecent(lib2, to, b))
63 return false;
65 /* ...ok, then we can still contribute a liberty
66 * later by capturing something. */
67 s->needs_more_lib = g;
68 s->needs_more_lib_except = lib2;
69 s->friend_has_no_libs = false;
72 return -1;
75 static int
76 examine_enemy_groups(struct board *b, enum stone color, coord_t to, struct selfatari_state *s)
78 /* We may be able to gain a liberty by capturing this group. */
79 group_t can_capture = 0;
81 /* Examine enemy groups: */
82 for (int i = 0; i < s->groupcts[stone_other(color)]; i++) {
83 /* We can escape by capturing this group if it's in atari. */
84 group_t g = s->groupids[stone_other(color)][i];
85 if (board_group_info(b, g).libs > 1)
86 continue;
88 /* But we need to get to at least two liberties by this;
89 * we already have one outside liberty, or the group is
90 * more than 1 stone (in that case, capturing is always
91 * nice!). */
92 if (s->groupcts[S_NONE] > 0 || !group_is_onestone(b, g))
93 return false;
94 /* ...or, it's a ko stone, */
95 if (neighbor_count_at(b, g, color) + neighbor_count_at(b, g, S_OFFBOARD) == 3) {
96 /* and we don't have a group to save: then, just taking
97 * single stone means snapback! */
98 if (!s->friend_has_no_libs)
99 return false;
101 /* ...or, we already have one indirect liberty provided
102 * by another group. */
103 if (s->needs_more_lib || (can_capture && can_capture != g))
104 return false;
105 can_capture = g;
109 if (DEBUGL(6))
110 fprintf(stderr, "no cap group\n");
112 if (!s->needs_more_lib && !can_capture && !s->groupcts[S_NONE]) {
113 /* We have no hope for more fancy tactics - this move is simply
114 * a suicide, not even a self-atari. */
115 if (DEBUGL(6))
116 fprintf(stderr, "suicide\n");
117 return true;
119 /* XXX: I wonder if it makes sense to continue if we actually
120 * just !s->needs_more_lib. */
122 return -1;
125 static int
126 setup_nakade_or_snapback(struct board *b, enum stone color, coord_t to, struct selfatari_state *s)
128 /* There is another possibility - we can self-atari if it is
129 * a nakade: we put an enemy group in atari from the inside. */
130 /* This branch also allows eyes falsification:
131 * O O O . . (This is different from throw-in to false eye
132 * X X O O . checked below in that there is no X stone at the
133 * X . X O . right of the star point in this diagram.)
134 * X X X O O
135 * X O * . . */
136 /* TODO: Allow to only nakade if the created shape is dead
137 * (http://senseis.xmp.net/?Nakade). */
139 /* This branch also covers snapback, which is kind of special
140 * nakade case. ;-) */
141 for (int i = 0; i < s->groupcts[stone_other(color)]; i++) {
142 group_t g = s->groupids[stone_other(color)][i];
143 if (board_group_info(b, g).libs != 2)
144 goto next_group;
145 /* Simple check not to re-examine the same group. */
146 if (i > 0 && s->groupids[stone_other(color)][i] == s->groupids[stone_other(color)][i - 1])
147 continue;
149 /* We must make sure the other liberty of that group:
150 * (i) is an internal liberty
151 * (ii) filling it to capture our group will not gain
152 * safety */
154 /* Let's look at the other liberty neighbors: */
155 int lib2 = board_group_info(b, g).lib[0];
156 if (lib2 == to) lib2 = board_group_info(b, g).lib[1];
157 foreach_neighbor(b, lib2, {
158 /* This neighbor of course does not contribute
159 * anything to the enemy. */
160 if (board_at(b, c) == S_OFFBOARD)
161 continue;
163 /* If the other liberty has empty neighbor,
164 * it must be the original liberty; otherwise,
165 * since the whole group has only 2 liberties,
166 * the other liberty may not be internal and
167 * we are nakade'ing eyeless group from outside,
168 * which is stupid. */
169 if (board_at(b, c) == S_NONE) {
170 if (c == to)
171 continue;
172 else
173 goto next_group;
176 int g2 = group_at(b, c);
177 /* If the neighbor is of our color, it must
178 * be our group; if it is a different group,
179 * it must not be in atari. */
180 /* X X X X We will not allow play on 'a',
181 * X X a X because 'b' would capture two
182 * X O b X different groups, forming two
183 * X X X X eyes. */
184 if (board_at(b, c) == color) {
185 if (board_group_info(b, group_at(b, c)).libs > 1)
186 continue;
187 /* Our group == one of the groups
188 * we (@to) are connected to. */
189 int j;
190 for (j = 0; j < 4; j++)
191 if (s->groupids[color][j] == g2)
192 break;
193 if (j == 4)
194 goto next_group;
195 continue;
198 /* The neighbor is enemy color. It's ok if
199 * it's still the same group or this is its
200 * only liberty. */
201 if (g == g2 || board_group_info(b, g2).libs == 1)
202 continue;
203 /* Otherwise, it must have the exact same
204 * liberties as the original enemy group. */
205 if (board_group_info(b, g2).libs == 2
206 && (board_group_info(b, g2).lib[0] == to
207 || board_group_info(b, g2).lib[1] == to))
208 continue;
210 goto next_group;
213 /* Now, we must distinguish between nakade and eye
214 * falsification; we must not falsify an eye by more
215 * than two stones. */
216 if (s->groupcts[color] < 1 ||
217 (s->groupcts[color] == 1 && group_is_onestone(b, s->groupids[color][0])))
218 return false;
220 /* We would create more than 2-stone group; in that
221 * case, the liberty of our result must be lib2,
222 * indicating this really is a nakade. */
223 for (int j = 0; j < s->groupcts[color]; j++) {
224 group_t g2 = s->groupids[color][j];
225 assert(board_group_info(b, g2).libs <= 2);
226 if (board_group_info(b, g2).libs == 2) {
227 if (board_group_info(b, g2).lib[0] != lib2
228 && board_group_info(b, g2).lib[1] != lib2)
229 goto next_group;
230 } else {
231 assert(board_group_info(b, g2).lib[0] == to);
235 return false;
236 next_group:
237 /* Unless we are dealing with snapback setup, we don't need to look
238 * further. */
239 if (!s->groupcts[color])
240 return -1;
243 return -1;
246 static int
247 check_throwin(struct board *b, enum stone color, coord_t to, struct selfatari_state *s)
249 /* We can be throwing-in to false eye:
250 * X X X O X X X O X X X X X
251 * X . * X * O . X * O O . X
252 * # # # # # # # # # # # # # */
253 /* We cannot sensibly throw-in into a corner. */
254 if (neighbor_count_at(b, to, S_OFFBOARD) < 2
255 && neighbor_count_at(b, to, stone_other(color))
256 + neighbor_count_at(b, to, S_OFFBOARD) == 3
257 && board_is_false_eyelike(b, &to, stone_other(color))) {
258 assert(s->groupcts[color] <= 1);
259 /* Single-stone throw-in may be ok... */
260 if (s->groupcts[color] == 0) {
261 /* O X . There is one problem - when it's
262 * . * X actually not a throw-in!
263 * # # # */
264 foreach_neighbor(b, to, {
265 if (board_at(b, c) == S_NONE) {
266 /* Is the empty neighbor an escape path? */
267 /* (Note that one S_NONE neighbor is already @to.) */
268 if (neighbor_count_at(b, c, stone_other(color))
269 + neighbor_count_at(b, c, S_OFFBOARD) < 2)
270 return -1;
273 return false;
276 /* Multi-stone throwin...? */
277 assert(s->groupcts[color] == 1);
278 group_t g = s->groupids[color][0];
280 assert(board_group_info(b, g).libs <= 2);
281 /* Suicide is definitely NOT ok, no matter what else
282 * we could test. */
283 if (board_group_info(b, g).libs == 1)
284 return true;
286 /* In that case, we must be connected to at most one stone,
287 * or throwin will not destroy any eyes. */
288 if (group_is_onestone(b, g))
289 return false;
291 return -1;
294 bool
295 is_bad_selfatari_slow(struct board *b, enum stone color, coord_t to)
297 if (DEBUGL(5))
298 fprintf(stderr, "sar check %s %s\n", stone2str(color), coord2sstr(to, b));
299 /* Assess if we actually gain any liberties by this escape route.
300 * Note that this is not 100% as we cannot check whether we are
301 * connecting out or just to ourselves. */
303 struct selfatari_state s;
304 memset(&s, 0, sizeof(s));
305 int d;
307 foreach_neighbor(b, to, {
308 enum stone color = board_at(b, c);
309 s.groupids[color][s.groupcts[color]++] = group_at(b, c);
312 /* We have shortage of liberties; that's the point. */
313 assert(s.groupcts[S_NONE] <= 1);
315 d = examine_friendly_groups(b, color, to, &s);
316 if (d >= 0)
317 return d;
319 if (DEBUGL(6))
320 fprintf(stderr, "no friendly group\n");
322 d = examine_enemy_groups(b, color, to, &s);
323 if (d >= 0)
324 return d;
326 if (DEBUGL(6))
327 fprintf(stderr, "no escape\n");
329 d = setup_nakade_or_snapback(b, color, to, &s);
330 if (d >= 0)
331 return d;
333 if (DEBUGL(6))
334 fprintf(stderr, "no nakade group\n");
336 d = check_throwin(b, color, to, &s);
337 if (d >= 0)
338 return d;
340 if (DEBUGL(6))
341 fprintf(stderr, "no throw-in group\n");
343 /* No way to pull out, no way to connect out. This really
344 * is a bad self-atari! */
345 return true;
349 /* Is this ladder breaker friendly for the one who catches ladder. */
350 static bool
351 ladder_catcher(struct board *b, int x, int y, enum stone laddered)
353 enum stone breaker = board_atxy(b, x, y);
354 return breaker == stone_other(laddered) || breaker == S_OFFBOARD;
357 bool
358 is_border_ladder(struct board *b, coord_t coord, enum stone lcolor)
360 int x = coord_x(coord, b), y = coord_y(coord, b);
362 if (DEBUGL(5))
363 fprintf(stderr, "border ladder\n");
364 /* Direction along border; xd is horiz. border, yd vertical. */
365 int xd = 0, yd = 0;
366 if (board_atxy(b, x + 1, y) == S_OFFBOARD || board_atxy(b, x - 1, y) == S_OFFBOARD)
367 yd = 1;
368 else
369 xd = 1;
370 /* Direction from the border; -1 is above/left, 1 is below/right. */
371 int dd = (board_atxy(b, x + yd, y + xd) == S_OFFBOARD) ? 1 : -1;
372 if (DEBUGL(6))
373 fprintf(stderr, "xd %d yd %d dd %d\n", xd, yd, dd);
374 /* | ? ?
375 * | . O #
376 * | c X #
377 * | . O #
378 * | ? ? */
379 /* This is normally caught, unless we have friends both above
380 * and below... */
381 if (board_atxy(b, x + xd * 2, y + yd * 2) == lcolor
382 && board_atxy(b, x - xd * 2, y - yd * 2) == lcolor)
383 return false;
384 /* ...or can't block where we need because of shortage
385 * of liberties. */
386 int libs1 = board_group_info(b, group_atxy(b, x + xd - yd * dd, y + yd - xd * dd)).libs;
387 int libs2 = board_group_info(b, group_atxy(b, x - xd - yd * dd, y - yd - xd * dd)).libs;
388 if (DEBUGL(6))
389 fprintf(stderr, "libs1 %d libs2 %d\n", libs1, libs2);
390 if (libs1 < 2 && libs2 < 2)
391 return false;
392 if (board_atxy(b, x + xd * 2, y + yd * 2) == lcolor && libs1 < 3)
393 return false;
394 if (board_atxy(b, x - xd * 2, y - yd * 2) == lcolor && libs2 < 3)
395 return false;
396 return true;
399 /* This is very trivial and gets a lot of corner cases wrong.
400 * We need this to be just very fast. One important point is
401 * that we sometimes might not notice a ladder but if we do,
402 * it should always work; thus we can use this for strong
403 * negative hinting safely. */
404 bool
405 is_middle_ladder(struct board *b, coord_t coord, enum stone lcolor)
407 int x = coord_x(coord, b), y = coord_y(coord, b);
409 /* Figure out the ladder direction */
410 int xd, yd;
411 xd = board_atxy(b, x + 1, y) == S_NONE ? 1 : board_atxy(b, x - 1, y) == S_NONE ? -1 : 0;
412 yd = board_atxy(b, x, y + 1) == S_NONE ? 1 : board_atxy(b, x, y - 1) == S_NONE ? -1 : 0;
414 if (!xd || !yd) {
415 if (DEBUGL(5))
416 fprintf(stderr, "no ladder, too little space; self-atari?\n");
417 return false;
420 /* For given (xd,yd), we have two possibilities where to move
421 * next. Consider (-1,-1):
422 * n X . n c X
423 * c O X X O #
424 * X # # . X #
426 bool horiz_first = ladder_catcher(b, x, y - yd, lcolor); // left case
427 bool vert_first = ladder_catcher(b, x - xd, y, lcolor); // right case
429 /* We don't have to look at the other 'X' in the position - if it
430 * wouldn't be there, the group wouldn't be in atari. */
432 /* We do only tight ladders, not loose ladders. Furthermore,
433 * the ladders need to be simple:
434 * . X . . . X
435 * c O X supported . c O unsupported
436 * X # # X O #
438 assert(!(horiz_first && vert_first));
439 if (!horiz_first && !vert_first) {
440 /* TODO: In case of basic non-simple ladder, play out both variants. */
441 if (DEBUGL(5))
442 fprintf(stderr, "non-simple ladder\n");
443 return false;
446 /* We do that below for further moves, but now initially - check
447 * that at 'c', we aren't putting any of the catching stones
448 * in atari. */
449 #if 1 // this might be broken?
450 #define check_catcher_danger(b, x_, y_) do { \
451 if (board_atxy(b, (x_), (y_)) != S_OFFBOARD \
452 && board_group_info(b, group_atxy(b, (x_), (y_))).libs <= 2) { \
453 if (DEBUGL(5)) \
454 fprintf(stderr, "ladder failed - atari at the beginning\n"); \
455 return false; \
456 } } while (0)
458 if (horiz_first) {
459 check_catcher_danger(b, x, y - yd);
460 check_catcher_danger(b, x - xd, y + yd);
461 } else {
462 check_catcher_danger(b, x - xd, y);
463 check_catcher_danger(b, x + xd, y - yd);
465 #undef check_catcher_danger
466 #endif
468 #define ladder_check(xd1_, yd1_, xd2_, yd2_, xd3_, yd3_) \
469 if (board_atxy(b, x, y) != S_NONE) { \
470 /* Did we hit a stone when playing out ladder? */ \
471 if (ladder_catcher(b, x, y, lcolor)) \
472 return true; /* ladder works */ \
473 if (board_group_info(b, group_atxy(b, x, y)).lib[0] > 0) \
474 return false; /* friend that's not in atari himself */ \
475 } else { \
476 /* No. So we are at new position. \
477 * We need to check indirect ladder breakers. */ \
478 /* . 2 x 3 . \
479 * . x o O 1 <- only at O we can check for o at 2 \
480 * x o o x . otherwise x at O would be still deadly \
481 * o o x . . \
482 * We check for o and x at 1, these are vital. \
483 * We check only for o at 2; x at 2 would mean we \
484 * need to fork (one step earlier). */ \
485 coord_t c1 = coord_xy(b, x + (xd1_), y + (yd1_)); \
486 enum stone s1 = board_at(b, c1); \
487 if (s1 == lcolor) return false; \
488 if (s1 == stone_other(lcolor)) { \
489 /* One more thing - if the position at 3 is \
490 * friendly and safe, we escaped anyway! */ \
491 coord_t c3 = coord_xy(b, x + (xd3_), y + (yd3_)); \
492 return board_at(b, c3) != lcolor \
493 || board_group_info(b, group_at(b, c3)).libs < 2; \
495 enum stone s2 = board_atxy(b, x + (xd2_), y + (yd2_)); \
496 if (s2 == lcolor) return false; \
497 /* Then, can X actually "play" 1 in the ladder? */ \
498 if (neighbor_count_at(b, c1, lcolor) + neighbor_count_at(b, c1, S_OFFBOARD) >= 2) \
499 return false; /* It would be self-atari! */ \
501 #define ladder_horiz do { if (DEBUGL(6)) fprintf(stderr, "%d,%d horiz step (%d,%d)\n", x, y, xd, yd); x += xd; ladder_check(xd, 0, -2 * xd, yd, 0, yd); } while (0)
502 #define ladder_vert do { if (DEBUGL(6)) fprintf(stderr, "%d,%d vert step of (%d,%d)\n", x, y, xd, yd); y += yd; ladder_check(0, yd, xd, -2 * yd, xd, 0); } while (0)
504 if (ladder_catcher(b, x - xd, y, lcolor))
505 ladder_horiz;
506 do {
507 ladder_vert;
508 ladder_horiz;
509 } while (1);
513 bool
514 board_stone_radar(struct board *b, coord_t coord, int distance)
516 int bounds[4] = {
517 coord_x(coord, b) - distance,
518 coord_y(coord, b) - distance,
519 coord_x(coord, b) + distance,
520 coord_y(coord, b) + distance
522 for (int i = 0; i < 4; i++)
523 if (bounds[i] < 1)
524 bounds[i] = 1;
525 else if (bounds[i] > board_size(b) - 2)
526 bounds[i] = board_size(b) - 2;
527 for (int x = bounds[0]; x <= bounds[2]; x++)
528 for (int y = bounds[1]; y <= bounds[3]; y++)
529 if (board_atxy(b, x, y) != S_NONE) {
530 /* fprintf(stderr, "radar %d,%d,%d: %d,%d (%d)\n",
531 coord_x(coord, b), coord_y(coord, b),
532 distance, x, y, board_atxy(b, x, y)); */
533 return true;
535 return false;
539 void
540 cfg_distances(struct board *b, coord_t start, int *distances, int maxdist)
542 /* Queue for d+1 spots; no two spots of the same group
543 * should appear in the queue. */
544 #define qinc(x) (x = ((x + 1) >= board_size2(b) ? ((x) + 1 - board_size2(b)) : (x) + 1))
545 coord_t queue[board_size2(b)]; int qstart = 0, qstop = 0;
547 foreach_point(b) {
548 distances[c] = board_at(b, c) == S_OFFBOARD ? maxdist + 1 : -1;
549 } foreach_point_end;
551 queue[qstop++] = start;
552 for (int d = 0; d <= maxdist; d++) {
553 /* Process queued moves, while setting the queue
554 * for new wave. */
555 int qa = qstart, qb = qstop;
556 qstart = qstop;
557 for (int q = qa; q < qb; qinc(q)) {
558 #define cfg_one(coord, grp) do {\
559 distances[coord] = d; \
560 foreach_neighbor (b, coord, { \
561 if (distances[c] < 0 && (!grp || group_at(b, coord) != grp)) { \
562 queue[qstop] = c; \
563 qinc(qstop); \
565 }); \
566 } while (0)
567 coord_t cq = queue[q];
568 if (distances[cq] >= 0)
569 continue; /* We already looked here. */
570 if (board_at(b, cq) == S_NONE) {
571 cfg_one(cq, 0);
572 } else {
573 group_t g = group_at(b, cq);
574 foreach_in_group(b, g) {
575 cfg_one(c, g);
576 } foreach_in_group_end;
578 #undef cfg_one
582 foreach_point(b) {
583 if (distances[c] < 0)
584 distances[c] = maxdist + 1;
585 } foreach_point_end;
589 float
590 board_effective_handicap(struct board *b)
592 /* For very small/very large boards, we might want
593 * to account for different "base komi". */
594 float first_move = 7.5; // point value of move on empty board
595 assert(b->handicap != 1);
596 return (b->handicap ? b->handicap : 1) * first_move - b->komi;
600 bool
601 pass_is_safe(struct board *b, enum stone color, struct move_queue *mq)
603 float score = board_official_score(b, mq);
604 if (color == S_BLACK)
605 score = -score;
606 //fprintf(stderr, "%d score %f\n", color, score);
607 return (score > 0);