1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* This is GNU Go, a Go program. Contact gnugo@gnu.org, or see *
* http://www.gnu.org/software/gnugo/ for more information. *
* *
* Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, *
* 2008 and 2009 by the Free Software Foundation. *
* *
* This program is free software; you can redistribute it and/or *
* modify it under the terms of the GNU General Public License as *
* published by the Free Software Foundation - version 3 or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License in file COPYING for more details. *
* *
* You should have received a copy of the GNU General Public *
* License along with this program; if not, write to the Free *
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02111, USA. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "gnugo.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "liberty.h"
static int find_backfilling_move(int move, int color, int *backfill_move,
int forbidden_moves[BOARDMAX]);
static int filllib_confirm_safety(int move, int color, int *defense_point);
/* Determine whether a point is adjacent to at least one own string which
* isn't dead.
*/
static int
living_neighbor(int pos, int color)
{
int k;
for (k = 0; k < 4; k++) {
if (board[pos + delta[k]] == color
&& dragon[pos + delta[k]].status != DEAD)
return 1;
}
return 0;
}
/* Determine whether (pos) effectively is a black or white point.
* The test for inessentiality is to avoid filling the liberties
* around a killing nakade string.
*/
static void
analyze_neighbor(int pos, int *found_black, int *found_white)
{
switch (board[pos]) {
case EMPTY:
if (!(*found_black)
&& living_neighbor(pos, BLACK)
&& safe_move(pos, WHITE) != WIN)
*found_black = 1;
if (!(*found_white)
&& living_neighbor(pos, WHITE)
&& safe_move(pos, BLACK) != WIN)
*found_white = 1;
break;
case BLACK:
if (!worm[pos].inessential && DRAGON2(pos).safety != INESSENTIAL) {
if (dragon[pos].status == ALIVE
|| dragon[pos].status == UNKNOWN)
*found_black = 1;
else
*found_white = 1;
}
break;
case WHITE:
if (!worm[pos].inessential && DRAGON2(pos).safety != INESSENTIAL) {
if (dragon[pos].status == ALIVE
|| dragon[pos].status == UNKNOWN)
*found_white = 1;
else
*found_black = 1;
}
break;
}
}
/* If no move of value can be found to play, this seeks to fill a
* common liberty, backfilling or back-capturing if necessary. When
* backfilling we take care to start from the right end, in the case
* that several backfilling moves are ultimately necessary.
*
* If a move for color is found, return 1, otherwise return 0.
* The move is returned in (*move).
*/
int
fill_liberty(int *move, int color)
{
int k;
int pos;
int other = OTHER_COLOR(color);
int defense_point;
int potential_color[BOARDMAX];
/* We first make a fast scan for intersections which are potential
* candidates for liberty filling. This is not very accurate, but it
* does filter out intersections which could never pass the real
* tests below but might still require a lot of tactical reading in
* the process.
*/
memset(potential_color, 0, sizeof(potential_color));
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (!IS_STONE(board[pos]))
continue;
if (worm[pos].inessential || DRAGON2(pos).safety == INESSENTIAL)
continue;
if (dragon[pos].status != ALIVE) {
for (k = 0; k < 4; k++) {
int pos2 = pos + delta[k];
if (board[pos2] == EMPTY)
potential_color[pos2] |= OTHER_COLOR(board[pos]);
}
}
if (dragon[pos].status != DEAD) {
for (k = 0; k < 12; k++) {
int d = delta[k%8];
if (k >= 8) {
if (board[pos + d] != EMPTY)
continue;
d *= 2;
}
if (board[pos + d] == EMPTY)
potential_color[pos + d] |= board[pos];
}
}
}
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
/* It seems we can't trust an empty liberty to be gray-colored
* either as a cave or as a cavity. Instead we look for empty
* intersections with at least one neighbor of each color, where
* dead stones count as enemy stones. We also count empty
* neighbors to either color if the opponent can't play there.
*/
int found_white = 0;
int found_black = 0;
if (board[pos] != EMPTY)
continue;
/* Quick rejection based on preliminary test above. */
if (potential_color[pos] != GRAY)
continue;
/* Loop over the neighbors. */
for (k = 0; k < 4; k++) {
int d = delta[k];
if (ON_BOARD(pos + d))
analyze_neighbor(pos + d, &found_black, &found_white);
}
/* Do we have neighbors of both colors? */
if (!(found_white && found_black))
continue;
/* Ok, we wish to play here, but maybe we can't. The following
* cases may occur:
* 1. Move is legal and safe.
* 2. Move is legal but not safe because it's in the middle of a seki.
* 3. Move is legal but not safe, can be played after backfilling.
* 4. Move is an illegal ko recapture.
* 5. Move is illegal but can be played after back-captures.
* 6. Move would violate confirm_safety.
*/
DEBUG(DEBUG_FILLLIB, "Filllib: Considering move at %1m.\n", pos);
/* Legal and tactically safe, play it if it passes
* confirm_safety test, i.e. that it isn't a blunder which
* causes problems for other strings.
*/
if (safe_move(pos, color) == WIN) {
DEBUG(DEBUG_FILLLIB, "Filllib: Tactically safe.\n");
if (filllib_confirm_safety(pos, color, &defense_point)) {
/* Safety confirmed. */
DEBUG(DEBUG_FILLLIB, "Filllib: Safety confirmed.\n");
*move = pos;
return 1;
}
else if (defense_point != NO_MOVE && is_legal(defense_point, color)) {
/* Safety not confirmed because the move at (pos) would set
* up a double threat. (defense_point) is assumed to defend
* against this threat.
*
* FIXME: We should verify that (defense_point) really is effective.
*/
DEBUG(DEBUG_FILLLIB,
"Filllib: Safety not confirmed, but %1m defends.\n",
defense_point);
*move = defense_point;
return 1;
}
else {
/* The move causes problems somewhere else on the board, so
* we have to discard it. If everything works right this
* should not happen at this time.
*/
DEBUG(DEBUG_FILLLIB, "Filllib: Safety not confirmed, discarded.\n");
TRACE("Warning: Blunder detected in fill_liberty().\n");
continue;
}
}
/* Try to play the move. */
if (trymove(pos, color, "fill_liberty", NO_MOVE)) {
int forbidden_moves[BOARDMAX];
popgo();
/* Legal, but not safe. Look for backfilling move. */
DEBUG(DEBUG_FILLLIB,
"Filllib: Legal but not safe, looking for backfilling move.\n");
memset(forbidden_moves, 0, sizeof(forbidden_moves));
while (find_backfilling_move(pos, color, move, forbidden_moves)) {
/* Mark as forbidden in case we need another turn in the loop. */
forbidden_moves[*move] = 1;
DEBUG(DEBUG_FILLLIB, "Filllib: Backfilling move at %1m.\n", *move);
/* In certain positions it may happen that an illegal move
* is found. This probably only can happen if we try to play
* a move inside a lost semeai. Anyway we should discard the
* move.
*/
if (!is_legal(*move, color)) {
DEBUG(DEBUG_FILLLIB, "Filllib: Was illegal, discarded.\n");
*move = NO_MOVE;
continue;
}
/* If the move turns out to be strategically unsafe, or
* setting up a double threat elsewhere, also discard it.
*/
if (!filllib_confirm_safety(*move, color, &defense_point)) {
DEBUG(DEBUG_FILLLIB,
"Filllib: Safety not confirmed, discarded.\n");
*move = NO_MOVE;
continue;
}
/* Seems to be ok. */
return 1;
}
/* No acceptable backfilling move found.
* If we captured some stones, this move should be ok anyway.
*/
if (does_capture_something(pos, color)) {
DEBUG(DEBUG_FILLLIB,
"Filllib: Not tactically safe, but captures stones.\n");
if (!filllib_confirm_safety(pos, color, &defense_point)) {
DEBUG(DEBUG_FILLLIB,
"Filllib: Safety not confirmed, discarded.\n");
continue;
}
*move = pos;
return 1;
}
}
else {
/* Move is illegal. Look for an attack on one of the neighbor
* worms. If found, return that move for back-capture.
*/
DEBUG(DEBUG_FILLLIB, "Filllib: Illegal, looking for back-capture.\n");
for (k = 0; k < 4; k++) {
int d = delta[k];
if (board[pos + d] == other
&& worm[pos + d].attack_codes[0] == WIN) {
*move = worm[pos + d].attack_points[0];
DEBUG(DEBUG_FILLLIB, "Filllib: Found at %1m.\n", *move);
return 1;
}
}
DEBUG(DEBUG_FILLLIB,
"Filllib: Nothing found, looking for ko back-capture.\n");
for (k = 0; k < 4; k++) {
int d = delta[k];
if (board[pos + d] == other
&& worm[pos + d].attack_codes[0] != 0
&& is_legal(worm[pos + d].attack_points[0], color)) {
*move = worm[pos + d].attack_points[0];
DEBUG(DEBUG_FILLLIB, "Filllib: Found at %1m.\n", *move);
return 1;
}
}
DEBUG(DEBUG_FILLLIB,
"Filllib: Nothing found, looking for threat to back-capture.\n");
for (k = 0; k < 4; k++) {
int d = delta[k];
if (board[pos + d] == other
&& worm[pos + d].attack_codes[0] != 0) {
/* Just pick some other liberty. */
/* FIXME: Something is odd about this code. */
int libs[2];
if (findlib(pos + d, 2, libs) > 1) {
if (is_legal(libs[0], color))
*move = libs[0];
else if (is_legal(libs[1], color))
*move = libs[1];
else
continue;
DEBUG(DEBUG_FILLLIB, "Filllib: Found at %1m.\n", *move);
return 1;
}
}
}
}
}
/* Nothing found. */
DEBUG(DEBUG_FILLLIB, "Filllib: No move found.\n");
return 0;
}
/* The strategy for finding a backfilling move is to first identify
* moves that
*
* 1. defends the position obtained after playing (move).
* 2. captures a stone adjacent to our neighbors to (move), before
* (move) is played.
*
* Then we check which of these are legal before (move) is played. If
* there is at least one, we take one of these arbitrarily as a
* backfilling move.
*
* Now it may happen that (move) still isn't a safe move. In that case
* we recurse to find a new backfilling move. To do things really
* correctly we should also give the opponent the opportunity to keep
* up the balance of the position by letting him do a backfilling move
* of his own. Maybe this could also be arranged by recursing this
* function. Currently we only do a half-hearted attempt to find
* opponent moves.
*
* The purpose of the forbidden_moves[] array is to get a new
* backfilling move if the first one later was found to be unsafe,
* like backfilling for J5 at F9 in filllib:45. With F9 marked as
* forbidden the correct move at G9 is found.
*/
static int adjs[MAXCHAIN];
static int libs[MAXLIBS];
static int
find_backfilling_move(int move, int color, int *backfill_move,
int forbidden_moves[BOARDMAX])
{
int k;
int liberties;
int neighbors;
int found_one = 0;
int apos = NO_MOVE;
int bpos = NO_MOVE;
int extra_pop = 0;
int success = 0;
int acode;
int saved_move = NO_MOVE;
int opponent_libs;
DEBUG(DEBUG_FILLLIB, "find_backfilling_move for %C %1m\n", color, move);
if (debug & DEBUG_FILLLIB)
dump_stack();
/* Play (move) and identify all liberties and adjacent strings. */
if (!trymove(move, color, "find_backfilling_move", move))
return 0; /* This shouldn't happen, I believe. */
/* The move wasn't safe, so there must be an attack for the
* opponent. Save it for later use.
*/
acode = attack(move, &apos);
gg_assert(acode != 0 && apos != NO_MOVE);
/* Find liberties. */
liberties = findlib(move, MAXLIBS, libs);
/* Find neighbors. */
neighbors = chainlinks(move, adjs);
/* Remove (move) again. */
popgo();
/* It's most fun to capture stones. Start by trying to take some
* neighbor off the board. If the attacking move does not directly
* reduce the number of liberties of the attacked string we don't
* trust it but keep it around if we don't find anything else. (See
* filllib:17 for a position where this matters.)
*
* It is also necessary to take care to first attack the string with
* the fewest liberties, which can probably be removed the fastest.
* See filllib:37 for an example (J5 tactically attacks K7 but the
* correct move is H5).
*
* FIXME: It seems we have to return immediately when we find an
* attacking move, because recursing for further backfilling might
* lead to moves which complete the capture but cannot be played
* before the attacking move itself. This is not ideal but probably
* good enough.
*
* In order to avoid losing unnecessary points while capturing dead
* stones, we try first to capture stones in atari, second defending
* at a liberty, and third capture stones with two or more
* liberties. See filllib:43 for a position where capturing dead
* stones (B10 or C8) loses a point compared to defending at a
* liberty (C6).
*/
for (opponent_libs = 1; opponent_libs <= 1; opponent_libs++) {
for (k = 0; k < neighbors; k++) {
if (opponent_libs < 5 && countlib(adjs[k]) != opponent_libs)
continue;
if (attack(adjs[k], &bpos) == WIN) {
if (forbidden_moves[bpos])
continue;
if (liberty_of_string(bpos, adjs[k])) {
*backfill_move = bpos;
return 1;
}
else
saved_move = bpos;
}
}
}
/* Otherwise look for a safe move at a liberty. */
if (!found_one) {
for (k = 0; k < liberties; k++) {
if (!forbidden_moves[libs[k]] && safe_move(libs[k], color) == WIN) {
*backfill_move = libs[k];
found_one = 1;
break;
}
}
}
if (!found_one) {
for (opponent_libs = 2; opponent_libs <= 5; opponent_libs++) {
for (k = 0; k < neighbors; k++) {
if (opponent_libs < 5 && countlib(adjs[k]) != opponent_libs)
continue;
if (attack(adjs[k], &bpos) == WIN) {
if (forbidden_moves[bpos])
continue;
if (liberty_of_string(bpos, adjs[k])) {
*backfill_move = bpos;
return 1;
}
else
saved_move = bpos;
}
}
}
}
/* If no luck so far, try with superstring liberties. */
if (!found_one) {
trymove(move, color, "find_backfilling_move", move);
find_proper_superstring_liberties(move, &liberties, libs, 0);
popgo();
for (k = 0; k < liberties; k++) {
if (!forbidden_moves[libs[k]] && safe_move(libs[k], color) == WIN) {
*backfill_move = libs[k];
found_one = 1;
break;
}
}
}
/* If no luck so far, try attacking superstring neighbors. */
if (!found_one) {
trymove(move, color, "find_backfilling_move", move);
superstring_chainlinks(move, &neighbors, adjs, 4);
popgo();
for (k = 0; k < neighbors; k++) {
if (attack(adjs[k], &bpos) == WIN) {
if (!forbidden_moves[bpos] && liberty_of_string(bpos, adjs[k])) {
*backfill_move = bpos;
return 1;
}
}
}
}
if (found_one) {
ASSERT1(!forbidden_moves[*backfill_move], *backfill_move);
if (!trymove(*backfill_move, color, "find_backfilling_move", move))
return 0; /* This really shouldn't happen. */
/* Allow opponent to get a move in here. */
if (trymove(apos, OTHER_COLOR(color), "find_backfilling_move", move))
extra_pop = 1;
/* If still not safe, recurse to find a new backfilling move. */
if (safe_move(move, color) == WIN)
success = 1;
else
success = find_backfilling_move(move, color, backfill_move,
forbidden_moves);
/* Pop move(s) and return. */
if (extra_pop)
popgo();
popgo();
}
if (!success && saved_move != NO_MOVE) {
ASSERT1(!forbidden_moves[saved_move], saved_move);
*backfill_move = saved_move;
success = 1;
}
if (!success)
*backfill_move = NO_MOVE;
return success;
}
/* Confirm that (move) is a safe move for color. In addition to
* calling the global confirm_safety(), this function also calls the
* owl code to verify the strategical viability of the move.
*/
static int
filllib_confirm_safety(int move, int color, int *defense_point)
{
int k;
int apos = NO_MOVE;
int save_verbose;
gg_assert(stackp == 0);
gg_assert(defense_point != NULL);
*defense_point = NO_MOVE;
/* Before we can call the owl code, we need to find a neighbor of
* our color.
*/
for (k = 0; k < 4; k++)
if (board[move + delta[k]] == color) {
apos = move + delta[k];
break;
}
/* If none found, look for a neighbor of an attacked adjacent string. */
if (apos == NO_MOVE)
for (k = 0; k < 4; k++) {
int pos2 = move + delta[k];
if (board[pos2] == OTHER_COLOR(color)
&& !play_attack_defend_n(color, 0, 1, move, pos2)) {
int adj;
adj = chainlinks(pos2, adjs);
/* It seems unlikely that we would ever get no adjacent strings
* here, but if it should happen we simply give up and say the
* move is unsafe.
*/
if (adj == 0)
return 0;
apos = adjs[0];
break;
}
}
/* Next attempt are diagonal neighbors. */
if (apos == NO_MOVE) {
for (k = 4; k < 8; k++)
if (board[move + delta[k]] == color) {
apos = move + delta[k];
break;
}
}
/* And two steps away. */
if (apos == NO_MOVE) {
for (k = 0; k < 4; k++)
if (board[move + 2 * delta[k]] == color) {
apos = move + 2 * delta[k];
break;
}
}
/* We should have found something by now. If not something's
* probably broken elsewhere. Declare the move unsafe if it happens.
*/
if (apos == NO_MOVE)
return 0;
/* Ask the owl code whether this move is strategically viable. */
save_verbose = verbose;
if (verbose > 0)
verbose--;
if (!owl_does_defend(move, apos, NULL))
return 0;
verbose = save_verbose;
return confirm_safety(move, color, defense_point, NULL);
}
/*
* Local Variables:
* tab-width: 8
* c-basic-offset: 2
* End:
*/
|