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 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
* 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. *
\* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
/* This file contains functions that deals with threats and,
* especially, combinations of threats.
*/
#include "gnugo.h"
#include <string.h>
#include "liberty.h"
#include "gg_utils.h"
#include "patterns.h"
static void find_double_threats(int color);
/* Generate move reasons for combination attacks and defenses against
* them.
*
* This is one of the move generators called from genmove().
*/
void
combinations(int color)
{
int save_verbose;
int attack_point;
signed char defense_points[BOARDMAX];
int other = OTHER_COLOR(color);
int aa_val;
/* Find intersections with multiple threats. */
find_double_threats(color);
save_verbose = verbose;
if (verbose > 0)
verbose--;
if (save_verbose)
gprintf("\nlooking for combination attacks ...\n");
aa_val = atari_atari(color, &attack_point, NULL, save_verbose);
if (aa_val > 0) {
if (save_verbose)
gprintf("Combination attack for %C with size %d found at %1m\n",
color, aa_val, attack_point);
add_my_atari_atari_move(attack_point, aa_val);
}
aa_val = atari_atari(other, &attack_point, defense_points, save_verbose);
if (aa_val > 0) {
int pos;
if (save_verbose)
gprintf("Combination attack for %C with size %d found at %1m\n",
other, aa_val, attack_point);
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (ON_BOARD(pos) && defense_points[pos]) {
add_your_atari_atari_move(pos, aa_val);
if (save_verbose)
gprintf("- defense at %1m\n", pos);
}
}
}
verbose = save_verbose;
}
#define MAX_THREATENED_STRINGS 10 /* Should be enough for one intersection */
static void
find_double_threats(int color)
{
int ii;
int k;
int l;
for (ii = BOARDMIN; ii < BOARDMAX; ii++) {
int num_a_threatened_groups;
int a_threatened_groups[MAX_THREATENED_STRINGS];
#if 0
int num_d_threatened_groups;
int d_threatened_groups[MAX_THREATENED_STRINGS];
#endif
if (!ON_BOARD(ii))
continue;
/* Generate an EITHER_MOVE move reasons for each pair of the
* threatened strings. We must also remove the threats, because
* otherwise we would get followup points for them as well.
*
* FIXME:
* - This is perhaps not the best way to do it, but realistically
* it will be seldom that more than two strings are threatened
* at the same point. Still, we should find a better way.
* - EITHER_MOVE should be generalized to more than two strings.
*/
num_a_threatened_groups = get_attack_threats(ii, MAX_THREATENED_STRINGS,
a_threatened_groups);
if (num_a_threatened_groups > 1) {
if (trymove(ii, color, "find_double_threats-A", ii)) {
for (k = 0; k < num_a_threatened_groups - 1; ++k)
for (l = k + 1; l < num_a_threatened_groups; ++l) {
/* Note: If we used attack_either() here instead of trymove()
* and !defend_both(), we would not make use of the fact
* that we already know of a common threat point for
* the two strings.
* Besides, attack_either is currently (3.1.11) not very good.
*
* The call to attack() is intended to detect the case
* where the move at ii is a snapback capture.
*/
if (board[a_threatened_groups[k]] == EMPTY
|| board[a_threatened_groups[l]] == EMPTY) {
if (!attack(ii, NULL)) {
TRACE("Double threat at %1m, either %1m or %1m attacked.\n",
ii, a_threatened_groups[k], a_threatened_groups[l]);
add_either_move(ii, ATTACK_STRING, a_threatened_groups[k],
ATTACK_STRING, a_threatened_groups[l]);
remove_attack_threat_move(ii, a_threatened_groups[k]);
remove_attack_threat_move(ii, a_threatened_groups[l]);
}
}
else if (!defend_both(a_threatened_groups[k],
a_threatened_groups[l])) {
TRACE("Double threat at %1m, either %1m or %1m attacked.\n",
ii, a_threatened_groups[k], a_threatened_groups[l]);
add_either_move(ii, ATTACK_STRING, a_threatened_groups[k],
ATTACK_STRING, a_threatened_groups[l]);
remove_attack_threat_move(ii, a_threatened_groups[k]);
remove_attack_threat_move(ii, a_threatened_groups[l]);
}
}
popgo();
}
}
}
/* FIXME:
* TODO:
* - defense threats
* - combinations of owl threats and other threats
* - combinations of threats to cut and connect
* - combinations of breakins into enemy territory
*/
}
/* ================================================================ */
/* Combination attacks */
/* ================================================================ */
/* atari_atari(color, *move) looks for a series of ataris on
* strings of the other color culminating in the capture of
* a string which is thought to be invulnerable by the reading
* code. Such a move can be missed since it may be that each
* string involved individually can be rescued, but nevertheless
* one of them can be caught. The simplest example is a double
* atari. The return value is the size of the smallest opponent
* worm.
*
* One danger with this scheme is that the first atari
* tried might be irrelevant to the actual combination.
* To detect this possibility, once we've found a combination,
* we mark that first move as forbidden, then try again. If
* no combination of the same size or larger turns up, then
* the first move was indeed essential.
*
* For the purpose of the move generation, returns the
* size of the smallest of the worms under attack.
*/
/* Local struct to keep track of atari_atari attack moves and what
* they threat.
*/
#define AA_MAX_TARGETS_PER_MOVE 4
#define MAX_AA_DIST 5
struct aa_move {
int move;
int target[AA_MAX_TARGETS_PER_MOVE];
};
#define AA_MAX_MOVES MAX_BOARD * MAX_BOARD
static int aa_status[BOARDMAX]; /* ALIVE, DEAD or CRITICAL */
static int forbidden[BOARDMAX];
static int aa_values[BOARDMAX];
static void compute_aa_status(int color,
const signed char safe_stones[BOARDMAX]);
static void compute_aa_values(int color);
static int get_aa_status(int pos);
static int do_atari_atari(int color, int *attack_point, int *defense_point,
signed char all_potential_defenses[BOARDMAX],
int last_friendly, int save_verbose, int minsize,
signed char goal[BOARDMAX]);
static int atari_atari_succeeded(int color, int *attack_point,
int *defense_point, int last_friendly,
int save_verbose, int minsize);
static void atari_atari_find_attack_moves(int color, int minsize,
struct aa_move attacks[AA_MAX_MOVES],
signed char goal[BOARDMAX]);
static void atari_atari_attack_patterns(int color, int minsize,
struct aa_move attacks[AA_MAX_MOVES],
signed char goal[BOARDMAX]);
static void atari_atari_attack_callback(int anchor, int color,
struct pattern *pattern,
int ll, void *data);
static int atari_atari_find_defense_moves(int targets[AA_MAX_TARGETS_PER_MOVE],
int moves[AA_MAX_MOVES]);
static int get_aa_value(int str);
static int update_aa_goal(signed char goal[BOARDMAX],
signed char new_goal[BOARDMAX],
int apos, int color);
static void aa_init_moves(struct aa_move attacks[AA_MAX_MOVES]);
static void aa_add_move(struct aa_move attacks[AA_MAX_MOVES],
int move, int target);
static int aa_move_known(struct aa_move attacks[AA_MAX_MOVES],
int move, int target);
static void aa_sort_moves(struct aa_move attacks[AA_MAX_MOVES]);
/* Set to 1 if you want verbose traces from this function. */
int
atari_atari(int color, int *attack_move, signed char defense_moves[BOARDMAX],
int save_verbose)
{
int other = OTHER_COLOR(color);
int apos;
int dpos;
int aa_val;
signed char saved_defense_moves[BOARDMAX];
/* Collect worm statuses of opponent's worms. We need to
* know this because we only want to report unexpected
* results. For example, we do not want to report success
* if we find we can kill a worm which is already dead.
* The worm status of empty points is set to UNKNOWN to signal
* that stones added along the way need special attention.
*/
if (aa_depth < 2)
return 0;
memset(forbidden, 0, sizeof(forbidden));
compute_aa_status(color, NULL);
compute_aa_values(color);
if (defense_moves)
memset(defense_moves, 0, BOARDMAX);
aa_val = do_atari_atari(color, &apos, &dpos, defense_moves, NO_MOVE,
save_verbose, 0, NULL);
if (aa_val == 0)
return 0;
/* We try excluding the first atari found and see if the
* combination still works. Repeat until failure.
*/
while (1) {
int new_aa_val;
if (attack_move)
*attack_move = apos;
forbidden[apos] = 1;
if (defense_moves) {
memcpy(saved_defense_moves, defense_moves, BOARDMAX);
memset(defense_moves, 0, BOARDMAX);
}
new_aa_val = do_atari_atari(color, &apos, &dpos, defense_moves, NO_MOVE,
save_verbose, aa_val, NULL);
/* The last do_atari_atari call fails. When do_atari_atari fails,
* it does not change the value of (apos), so these correspond
* to a move that works and is necessary.
*/
if (new_aa_val == 0)
break;
else
aa_val = new_aa_val;
}
if (defense_moves) {
int pos;
memcpy(defense_moves, saved_defense_moves, BOARDMAX);
/* defense_moves[] contains potential defense moves. Now we
* examine which of them really work.
*/
forbidden[apos] = 0;
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (!ON_BOARD(pos) || !defense_moves[pos])
continue;
if (!trymove(pos, other, "atari_atari", NO_MOVE)) {
defense_moves[pos] = 0;
if (save_verbose)
gprintf("%1m deleted defense point, illegal\n", pos);
continue;
}
if (attack(pos, NULL)) {
defense_moves[pos] = 0;
popgo();
if (save_verbose)
gprintf("%1m deleted defense point, unsafe\n", pos);
continue;
}
if (do_atari_atari(color, &apos, &dpos, NULL, NO_MOVE,
save_verbose, aa_val, NULL) > 0) {
if (save_verbose)
gprintf("%1m deleted defense point, didn't work\n", pos);
defense_moves[pos] = 0;
}
popgo();
}
}
return aa_val;
}
/* Wrapper around atari_atari_blunder_size. Check whether a
* combination attack of size at least minsize appears after move
* at (move) has been made.
* The arrays saved_dragons[] and saved_worms[] should be one for
* stones belonging to dragons or worms respectively, which are
* supposedly saved by (move).
*
* FIXME: We probably want to change the calling convention of this
* function to return all defense moves.
*/
int
atari_atari_confirm_safety(int color, int move, int *defense, int minsize,
const signed char saved_dragons[BOARDMAX],
const signed char saved_worms[BOARDMAX])
{
signed char safe_stones[BOARDMAX];
signed char defense_moves[BOARDMAX];
int pos;
int blunder_size;
mark_safe_stones(color, move, saved_dragons, saved_worms, safe_stones);
blunder_size = atari_atari_blunder_size(color, move, defense_moves,
safe_stones);
if (defense) {
/* Return one arbitrary defense move. */
*defense = NO_MOVE;
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
if (ON_BOARD(pos) && defense_moves[pos]) {
*defense = pos;
break;
}
}
return blunder_size >= minsize;
}
/* This function checks whether any new combination attack appears after
* move at (move) has been made, and returns its size (in points).
* safe_stones marks which of our stones are supposedly safe after this move.
*/
int
atari_atari_blunder_size(int color, int move,
signed char defense_moves[BOARDMAX],
const signed char safe_stones[BOARDMAX])
{
int apos;
int defense_point = NO_MOVE;
int aa_val, after_aa_val;
int other = OTHER_COLOR(color);
signed char defense_points[BOARDMAX];
int last_forbidden = NO_MOVE;
/* If aa_depth is too small, we can't see any combination attacks,
* so in this respect the move is safe enough.
*/
if (aa_depth < 2)
return 0;
memset(forbidden, 0, sizeof(forbidden));
memset(defense_points, 0, sizeof(defense_points));
compute_aa_status(other, safe_stones);
compute_aa_values(other);
/* Accept illegal ko capture here. */
if (!tryko(move, color, NULL))
/* Really shouldn't happen. */
abortgo(__FILE__, __LINE__, "trymove", move);
increase_depth_values();
aa_val = do_atari_atari(other, &apos, &defense_point, defense_points,
NO_MOVE, 0, 0, NULL);
after_aa_val = aa_val;
if (aa_val == 0 || defense_point == NO_MOVE) {
/* No sufficiently large combination attack, so the move is safe from
* this danger.
*
* On rare occasions do_atari_atari might find a combination
* but no defense. In this case we assume that the combination
* is illusory.
*/
popgo();
decrease_depth_values();
return 0;
}
while (aa_val >= after_aa_val && defense_point != NO_MOVE) {
/* Try dropping moves from the combination and see if it still
* works. What we really want is to get the proper defense move
* into (*defense).
*/
forbidden[apos] = 1;
last_forbidden = apos;
aa_val = do_atari_atari(other, &apos, &defense_point, NULL,
NO_MOVE, 0, aa_val, NULL);
}
popgo();
decrease_depth_values();
/* We know that a combination exists, but we don't know if
* the original move at (aa) was really relevant. So we
* try omitting it and see if a combination is still found.
*/
compute_aa_status(other, NULL);
compute_aa_values(other);
forbidden[last_forbidden] = 0;
aa_val = do_atari_atari(other, NULL, NULL, NULL, NO_MOVE, 0, 0, NULL);
if (aa_val >= after_aa_val)
return 0;
/* Try the potential defense moves to see which are effective. */
if (defense_moves) {
int pos;
/* defense_points[] contains potential defense moves. Now we
* examine which of them really work.
*/
/* FIXME: Maybe these should be moved after the tryko() below? */
compute_aa_status(other, safe_stones);
compute_aa_values(other);
memcpy(defense_moves, defense_points, sizeof(defense_points));
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (!ON_BOARD(pos) || !defense_moves[pos] || pos == move)
continue;
if (!trymove(pos, color, "atari_atari", NO_MOVE)) {
defense_moves[pos] = 0;
continue;
}
increase_depth_values();
if (attack(pos, NULL)) {
defense_moves[pos] = 0;
decrease_depth_values();
popgo();
continue;
}
/* Accept illegal ko capture here. */
if (!tryko(move, color, NULL))
/* Really shouldn't happen. */
abortgo(__FILE__, __LINE__, "trymove", move);
increase_depth_values();
if (do_atari_atari(other, &apos, &defense_point, NULL, NO_MOVE,
0, after_aa_val, NULL) >= after_aa_val)
defense_moves[pos] = 0;
decrease_depth_values();
popgo();
decrease_depth_values();
popgo();
}
}
return after_aa_val - aa_val;
}
/* ---------------------------------------------------------------- */
/* Helper functions for atari_atari. */
/* ---------------------------------------------------------------- */
/* Helper function for computing the aa_status for all opponent's strings.
* If safe_stones is given, we just copy the information from there.
* If called at stackp > 0, safe_stones must be provided since the
* dragon_data is not valid then.
*/
static void
compute_aa_status(int color, const signed char safe_stones[BOARDMAX])
{
int other = OTHER_COLOR(color);
int pos;
SGFTree *save_sgf_dumptree = sgf_dumptree;
int save_count_variations = count_variations;
int save_verbose = verbose;
gg_assert(safe_stones || stackp == 0);
sgf_dumptree = NULL;
count_variations = 0;
if (verbose)
verbose--;
/* Collect worm statuses of opponent's worms. We need to
* know this because we only want to report unexpected
* results. For example, we do not want to report success
* if we find we can kill a worm which is already dead.
* The worm status of empty points is set to UNKNOWN to signal
* that stones added along the way need special attention.
*/
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] == other) {
if (safe_stones) {
if (safe_stones[pos])
aa_status[pos] = ALIVE;
else
aa_status[pos] = DEAD;
}
else {
if (dragon[pos].status == DEAD)
aa_status[pos] = DEAD;
else if (dragon[pos].status == CRITICAL)
aa_status[pos] = CRITICAL;
else if (worm[pos].attack_codes[0] != 0) {
if (worm[pos].defense_codes[0] != 0)
aa_status[pos] = CRITICAL;
else
aa_status[pos] = DEAD;
}
else
aa_status[pos] = ALIVE;
}
}
else if (ON_BOARD(pos))
aa_status[pos] = UNKNOWN;
}
/* reclassify a worm with 2 liberties as INSUBSTANTIAL if capturing
* it does not result in a live group.
*/
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] == other
&& find_origin(pos) == pos
&& countlib(pos) == 2
&& aa_status[pos] == ALIVE) {
int libs[2];
findlib(pos, 2, libs);
/* Don't waste time running owl_substantial() if we can't safely
* atari anyway.
*/
if (is_self_atari(libs[0], color)
&& is_self_atari(libs[1], color))
continue;
if (!owl_substantial(pos)) {
int pos2;
for (pos2 = BOARDMIN; pos2 < BOARDMAX; pos2++)
if (board[pos2] == other && find_origin(pos2) == pos)
aa_status[pos2] = INSUBSTANTIAL;
}
}
}
if (debug & DEBUG_ATARI_ATARI) {
gprintf("compute_aa_status() for %C\n", color);
gprintf("aa_status: (ALIVE worms not listed)\n");
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] == other && is_worm_origin(pos, pos)) {
const char *status = "UNKNOWN (shouldn't happen)";
if (aa_status[pos] == DEAD)
status = "DEAD";
else if (aa_status[pos] == CRITICAL)
status = "CRITICAL";
else if (aa_status[pos] == INSUBSTANTIAL)
status = "INSUBSTANTIAL";
if (aa_status[pos] != ALIVE)
gprintf("%1M: %s\n", pos, status);
}
}
}
sgf_dumptree = save_sgf_dumptree;
count_variations = save_count_variations;
verbose = save_verbose;
}
/* Helper function for retrieving the aa_status for a string. We can't
* reliably do this simply by looking up aa_status[pos] since this is
* only valid at vertices which were non-empty at the start of the
* reading. For later added stones, we need to find their aa_status by
* locating a part of the string which was a worm at the beginning of
* the reading.
*/
static int
get_aa_status(int pos)
{
int stones[MAX_BOARD * MAX_BOARD];
int num_stones;
int k;
if (aa_status[pos] != UNKNOWN)
return aa_status[pos];
num_stones = findstones(pos, MAX_BOARD * MAX_BOARD, stones);
for (k = 0; k < num_stones; k++)
if (aa_status[stones[k]] != UNKNOWN)
return aa_status[stones[k]];
return UNKNOWN;
}
/* Helper function for atari_atari. Here worms is the number of
* opponent worms involved in the combination, and (last_friendly) is
* the location of the last friendly move played. Moves marked
* with the forbidden array are not tried. If no move is found,
* the values of *attack_point and *defense_point are not changed.
*
* If not NULL, *attack_point is left pointing to the location of the
* attacking move, and *defense_point points to a move defending the
* combination. In rare cases a defensive move might not be found. If
* a non-static function calling do_atari_atari gets a return value of
* 1 but NO_MOVE as the defense point, this should be treated as
* equivalent to a return value of 0.
*
* The goal array limits where we are allowed to consider threats.
* Only strings for which goal is set to 1 may be threatened. If goal
* is NULL, anything may be attacked. Thus goal is typically NULL when
* do_atari_atari() is called from an external function. After the
* first threat has been made, the goal array is set to one in a
* neighborhood of the move and after subsequent threats it is
* expanded with neighborhoods of those moves. The details of this can
* be found in the function update_aa_goal().
*/
static int
do_atari_atari(int color, int *attack_point, int *defense_point,
signed char all_potential_defenses[BOARDMAX], int last_friendly,
int save_verbose, int minsize, signed char goal[BOARDMAX])
{
int other = OTHER_COLOR(color);
int k;
struct aa_move attacks[AA_MAX_MOVES];
int num_defense_moves;
int defense_moves[AA_MAX_MOVES];
int pos;
SGFTree *save_sgf_dumptree;
int save_count_variations;
if (debug & DEBUG_ATARI_ATARI) {
gprintf("%odo_atari_atari: ");
dump_stack();
gprintf("%oforbidden moves: ");
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
if (ON_BOARD(pos) && forbidden[pos])
gprintf("%o%1m ", pos);
gprintf("\n");
gprintf("%ogoal: ");
if (!goal)
gprintf("none");
else {
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
if (ON_BOARD(pos) && goal[pos])
gprintf("%o%1m ", pos);
}
gprintf("\n");
}
/* First look for strings adjacent to the last friendly move played
* (or to another stone in the same string) which can be
* unexpectedly attacked. If so, the combination attack
* has succeeded.
*/
if (last_friendly != NO_MOVE) {
int retval;
save_sgf_dumptree = sgf_dumptree;
save_count_variations = count_variations;
sgf_dumptree = NULL;
count_variations = 0;
retval = atari_atari_succeeded(color, attack_point, defense_point,
last_friendly, save_verbose, minsize);
sgf_dumptree = save_sgf_dumptree;
count_variations = save_count_variations;
if (retval != 0) {
if (sgf_dumptree)
/* FIXME: Better message. */
sgftreeAddComment(sgf_dumptree, "attack found");
return retval;
}
}
if (stackp > aa_depth)
return 0;
/* Find attack moves. These are typically ataris but may also be
* more general.
*/
save_sgf_dumptree = sgf_dumptree;
save_count_variations = count_variations;
sgf_dumptree = NULL;
count_variations = 0;
atari_atari_find_attack_moves(color, minsize, attacks, goal);
sgf_dumptree = save_sgf_dumptree;
count_variations = save_count_variations;
/* Try the attacking moves and let the opponent defend. Then call
* ourselves recursively.
*/
for (k = 0; attacks[k].move != NO_MOVE; k++) {
int aa_val;
int str = attacks[k].target[0];
int apos = attacks[k].move;
int bpos;
int r;
if (!trymove(apos, color, "do_atari_atari-A", str))
continue;
if (all_potential_defenses) {
all_potential_defenses[apos] = 1;
if (countlib(apos) <= 2) {
int libs[2];
int num_libs = findlib(apos, 2, libs);
all_potential_defenses[libs[0]] = 1;
if (num_libs == 2)
all_potential_defenses[libs[1]] = 1;
}
}
if (!IS_STONE(board[str])) {
/* Error situation. This could be caused by a wrong matcher status. */
if (save_verbose || (debug & DEBUG_ATARI_ATARI))
gprintf("%oError condition found by atari_atari\n");
popgo();
return 0;
}
/* Try to defend the stone (str) which is threatened. */
aa_val = get_aa_value(str);
/* Pick up defense moves. */
save_sgf_dumptree = sgf_dumptree;
save_count_variations = count_variations;
sgf_dumptree = NULL;
count_variations = 0;
num_defense_moves = atari_atari_find_defense_moves(attacks[k].target,
defense_moves);
sgf_dumptree = save_sgf_dumptree;
count_variations = save_count_variations;
for (r = 0; r < num_defense_moves; r++) {
bpos = defense_moves[r];
if (all_potential_defenses)
all_potential_defenses[bpos] = 1;
if (trymove(bpos, other, "do_atari_atari-B", str)) {
int new_aa_val;
signed char new_goal[BOARDMAX];
/* These moves may have been irrelevant for later
* reading, so in order to avoid horizon problems, we
* need to temporarily increase the depth values.
*/
modify_depth_values(2);
update_aa_goal(goal, new_goal, apos, color);
new_aa_val = do_atari_atari(color, NULL, defense_point,
all_potential_defenses,
apos, save_verbose, minsize, new_goal);
modify_depth_values(-2);
if (new_aa_val < aa_val)
aa_val = new_aa_val;
popgo();
}
/* Defense successful, no need to try any further. */
if (aa_val == 0)
break;
}
/* Undo the attacking move. */
popgo();
if (aa_val == 0)
continue;
/* atari_atari successful */
if (num_defense_moves == 0) {
if (save_verbose || (debug & DEBUG_ATARI_ATARI)) {
gprintf("%oThe worm %1m can be attacked at %1m after ", str, apos);
dump_stack();
}
if (sgf_dumptree)
/* FIXME: Better message. */
sgftreeAddComment(sgf_dumptree, "attack found");
}
if (attack_point)
*attack_point = apos;
if (defense_point) {
save_sgf_dumptree = sgf_dumptree;
save_count_variations = count_variations;
sgf_dumptree = NULL;
count_variations = 0;
if (!find_defense(str, defense_point))
*defense_point = NO_MOVE;
/* If no defense point is known and (apos) is a safe
* move for other, it probably defends the combination.
*/
if ((*defense_point == NO_MOVE || !safe_move(*defense_point, other))
&& safe_move(apos, other))
*defense_point = apos;
sgf_dumptree = save_sgf_dumptree;
count_variations = save_count_variations;
}
DEBUG(DEBUG_ATARI_ATARI, "%oreturn value:%d (%1m)\n", aa_val, str);
return aa_val;
}
/* No atari_atari attack. */
return 0;
}
static int
atari_atari_succeeded(int color, int *attack_point, int *defense_point,
int last_friendly, int save_verbose, int minsize)
{
int pos;
int apos;
int other = OTHER_COLOR(color);
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] != other)
continue;
if (pos != find_origin(pos))
continue;
if (minsize > 0
&& get_aa_value(pos) < minsize)
continue;
if (get_aa_status(pos) != ALIVE)
continue;
if (board[last_friendly] != EMPTY
&& !adjacent_strings(last_friendly, pos))
continue;
if (board[last_friendly] == EMPTY
&& !liberty_of_string(last_friendly, pos))
continue;
if (debug & DEBUG_ATARI_ATARI)
gprintf("Considering attack of %1m. depth = %d.\n", pos, depth);
if (attack(pos, &apos) && !forbidden[apos]) {
if (save_verbose || (debug & DEBUG_ATARI_ATARI)) {
gprintf("%oThe worm %1m can be attacked at %1m after ", pos, apos);
dump_stack();
}
if (attack_point)
*attack_point = apos;
/* We look for a move defending the combination.
* Normally this is found by find_defense but failing
* that, if the attacking move is a safe move for color,
* it probably defends.
*/
if (defense_point) {
if (!find_defense(pos, defense_point)) {
if (safe_move(apos, other))
*defense_point = apos;
else
*defense_point = NO_MOVE;
}
}
DEBUG(DEBUG_ATARI_ATARI, "%oreturn value:%d (%1m)\n",
get_aa_value(pos), pos);
return get_aa_value(pos);
}
}
return 0;
}
#define MAX_THREAT_MOVES MAX_TACTICAL_POINTS
static void
atari_atari_find_attack_moves(int color, int minsize,
struct aa_move attacks[AA_MAX_MOVES],
signed char goal[BOARDMAX])
{
int k;
int r;
aa_init_moves(attacks);
atari_atari_attack_patterns(color, minsize, attacks, goal);
/* Sort the attack moves. */
aa_sort_moves(attacks);
if (debug & DEBUG_ATARI_ATARI) {
gprintf("Attack moves:");
for (k = 0; k < AA_MAX_MOVES && attacks[k].move != NO_MOVE; k++) {
gprintf("%o %1m(", attacks[k].move);
for (r = 0; r < AA_MAX_TARGETS_PER_MOVE; r++) {
if (attacks[k].target[r] == NO_MOVE)
break;
gprintf("%o%s%1m", r == 0 ? "" : ",", attacks[k].target[r]);
}
gprintf("%o)");
}
gprintf("%o\n");
}
}
/* FIXME: Move these to a struct and pass to callback through the
* *data parameter.
*/
static int current_minsize;
static struct aa_move *current_attacks;
static int conditional_attack_point[BOARDMAX];
static void
atari_atari_attack_patterns(int color, int minsize,
struct aa_move attacks[AA_MAX_MOVES],
signed char goal[BOARDMAX])
{
signed char revised_goal[BOARDMAX];
current_minsize = minsize;
current_attacks = attacks;
memset(conditional_attack_point, 0, sizeof(conditional_attack_point));
/* If goal is NULL and there are forbidden moves we need to compute
* a new goal around the forbidden moves.
*/
if (goal == NULL && update_aa_goal(goal, revised_goal, NO_MOVE, color))
goal = revised_goal;
#if 0
if (goal != NULL) {
int pos;
gprintf("goal:");
for (pos = BOARDMIN; pos < BOARDMAX; pos++)
if (ON_BOARD(pos) && goal[pos])
gprintf("%o %1m", pos);
gprintf("%o\n");
}
#endif
matchpat(atari_atari_attack_callback, color, &aa_attackpat_db, NULL, goal);
}
/* Try to attack every X string in the pattern, whether there is an attack
* before or not. Only exclude already known attacking moves.
*/
static void
atari_atari_attack_callback(int anchor, int color,
struct pattern *pattern, int ll, void *data)
{
int move;
int k;
UNUSED(data);
move = AFFINE_TRANSFORM(pattern->move_offset, ll, anchor);
if (forbidden[move])
return;
/* If the pattern has a constraint, call the autohelper to see
* if the pattern must be rejected.
*/
if (pattern->autohelper_flag & HAVE_CONSTRAINT)
if (!pattern->autohelper(ll, move, color, 0))
return;
/* If the pattern has a helper, call it to see if the pattern must
* be rejected.
*/
if (pattern->helper)
if (!pattern->helper(pattern, ll, move, color))
return;
/* Loop through pattern elements in search of X strings to
* threaten to attack.
*/
for (k = 0; k < pattern->patlen; ++k) { /* match each point */
if (pattern->patn[k].att == ATT_X) {
/* transform pattern real coordinate */
int str = find_origin(AFFINE_TRANSFORM(pattern->patn[k].offset,
ll, anchor));
if (current_minsize > 0
&& get_aa_value(str) < current_minsize)
continue;
if (aa_move_known(current_attacks, move, str))
continue;
if (get_aa_status(str) != ALIVE)
continue;
/* Usually we don't want to play self atari. However, if we
* capture in snapback it's okay. For s class patterns we don't
* have this requirement.
*/
if (!(pattern->class & CLASS_s) && is_self_atari(move, color)) {
if (countlib(str) > 2)
continue;
if (!safe_move(move, color))
continue;
}
/*
* Play (move) and see if there is an attack.
*/
if (trymove(move, color, "attack_callback", str)) {
int acode;
int attack_point = NO_MOVE;
if (!board[str])
acode = WIN;
else
acode = attack(str, &attack_point);
popgo();
if (acode != 0) {
if ((pattern->class & CLASS_c)
&& !aa_move_known(current_attacks, move, NO_MOVE)) {
/* Conditional pattern. */
DEBUG(DEBUG_ATARI_ATARI,
"aa_attack pattern %s+%d (conditional) found threat on %1m at %1m with code %d\n",
pattern->name, ll, str, move, acode);
if (conditional_attack_point[move] == NO_MOVE)
conditional_attack_point[move] = str;
else if (conditional_attack_point[move] != str) {
aa_add_move(current_attacks, move,
conditional_attack_point[move]);
aa_add_move(current_attacks, move, str);
}
}
else {
aa_add_move(current_attacks, move, str);
DEBUG(DEBUG_ATARI_ATARI,
"aa_attack pattern %s+%d found threat on %1m at %1m with code %d\n",
pattern->name, ll, str, move, acode);
}
}
}
}
}
}
static int
atari_atari_find_defense_moves(int targets[AA_MAX_TARGETS_PER_MOVE],
int moves[AA_MAX_MOVES])
{
int num_moves = 0;
int move;
int k;
int liberties;
int libs[4];
int neighbors;
int adjs[MAXCHAIN];
int mx[BOARDMAX];
int r, s;
memset(mx, 0, sizeof(mx));
for (r = 0; r < AA_MAX_TARGETS_PER_MOVE && targets[r] != NO_MOVE; r++) {
int str = targets[r];
/* If the attack move happened to remove (str), there's no defense. */
if (board[str] == EMPTY)
continue;
/* Because we know (str) is threatened there is an
* attack and we can be sure find_defense() will give a
* useful defense point if it returns non-zero. Usually we
* would need to call attack_and_defend() to be certain of
* this.
*/
if (!find_defense(str, &move))
continue;
moves[num_moves++] = move;
if (num_moves == AA_MAX_MOVES)
return num_moves;
mx[move] = 1;
/* Consider all moves to attack a neighbor or to play on a liberty. */
liberties = findlib(str, 4, libs);
for (k = 0; k < liberties; k++) {
if (!mx[libs[k]]
&& trymove(libs[k], board[str], "aa_defend-A", str)) {
if (attack(str, NULL) == 0) {
moves[num_moves++] = libs[k];
mx[libs[k]] = 1;
}
popgo();
if (num_moves == AA_MAX_MOVES)
return num_moves;
}
}
neighbors = chainlinks(str, adjs);
for (k = 0; k < neighbors; k++) {
int attack_point;
if (attack(adjs[k], &attack_point) == WIN
&& !mx[attack_point]) {
moves[num_moves++] = attack_point;
if (num_moves == AA_MAX_MOVES)
return num_moves;
mx[attack_point] = 1;
}
/* If the neighbor has at most three liberties, try all of them
* for defense, except self-ataris.
*/
liberties = findlib(adjs[k], 3, libs);
if (liberties <= 3) {
for (s = 0; s < liberties; s++) {
if (!mx[libs[s]]
&& !is_self_atari(libs[s], board[str])
&& trymove(libs[s], board[str], "aa_defend-B", str)) {
if (attack(str, NULL) == 0) {
moves[num_moves++] = libs[s];
mx[libs[s]] = 1;
}
popgo();
if (num_moves == AA_MAX_MOVES)
return num_moves;
}
}
}
}
if (debug & DEBUG_ATARI_ATARI) {
gprintf("Defense moves for %1m:", str);
for (k = 0; k < num_moves; k++)
gprintf("%o %1m", moves[k]);
gprintf("%o\n");
}
}
return num_moves;
}
/* Try to guess the value of the strings. We do this by adding twice
* the number of stones to the number of liberties and second order
* liberties within the moyo around the string. This is of course
* quite crude since it doesn't take into account any strategic
* effects, e.g. a string being cutting stones.
*/
static void
compute_aa_values(int color)
{
int other = OTHER_COLOR(color);
int pos;
int value;
int liberties;
int libs[MAXLIBS];
int mx[BOARDMAX];
int r, k;
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (board[pos] != other
|| pos != find_origin(pos)
|| aa_status[pos] != ALIVE) {
aa_values[pos] = 0;
continue;
}
memset(mx, 0, sizeof(mx));
liberties = findlib(pos, MAXLIBS, libs);
value = 2 * countstones(pos);
for (r = 0; r < liberties; r++) {
if (!mx[libs[r]]
&& (whose_moyo(&initial_black_influence, libs[r]) == other
|| whose_moyo(&initial_white_influence, libs[r]) == other)) {
mx[libs[r]] = 1;
value++;
}
for (k = 0; k < 4; k++) {
int librd = libs[r] + delta[k];
if (!ON_BOARD1(librd) || mx[librd])
continue;
mx[librd] = 1;
if (board[librd] == EMPTY
&& (whose_moyo(&initial_black_influence, librd) == other
|| (whose_moyo(&initial_white_influence, librd) == other)))
value++;
}
}
aa_values[pos] = value;
if (1)
DEBUG(DEBUG_ATARI_ATARI, "aa_value for %1m = %d\n", pos, value);
}
}
/* The aa_value for a string is the sum of the aa_values for all
* included strings in the original position. This will systematically
* overvalue strings which consist of multiple original strings, but
* this is okay since the defender very rarely should defend a string
* first and then sacrifice it later.
*/
static int
get_aa_value(int str)
{
int stones[MAX_BOARD * MAX_BOARD];
int k;
int num_stones = findstones(str, MAX_BOARD * MAX_BOARD, stones);
int value = 0;
for (k = 0; k < num_stones; k++)
value += aa_values[stones[k]];
return value;
}
/* update_aa_goal(goal, new_goal, apos, color) extends the goal array
* with vertices in a neighborhood of apos. The algorithm is that
* starting at apos, a distance measure is computed to nearby
* vertices. The distance increases with one for each step through
* empty vertices and by a liberty depending number when passing
* through strings of the attacked color. Strings with 3 or fewer
* liberties are free to pass through while strings with more
* liberties cost (libs - 3) to pass through. Stones with a distance
* of 5 or less are included in the goal.
*
* Additionally neighborhoods of the moves in the forbidden array are
* included in the goal, to make it possible to limit the goal to a
* specific area from the beginning. This is needed when trying to
* decide which moves are relevant to the combination.
*/
#define ENQUEUE(pos, dist) \
do { \
if ((dist) <= MAX_AA_DIST) { \
if (dists[pos] == 0) { \
queue[queue_end++] = (pos); \
dists[pos] = (dist); \
} \
else if (dists[pos] < (dist)) \
dists[pos] = (dist); \
} \
} while (0);
static int
update_aa_goal(signed char goal[BOARDMAX], signed char new_goal[BOARDMAX],
int apos, int color)
{
int other = OTHER_COLOR(color);
int dists[BOARDMAX];
int queue[MAX_BOARD * MAX_BOARD];
int queue_end = 0;
int k, r, s;
int pos;
if (goal == NULL)
memset(new_goal, 0, BOARDMAX);
else
memcpy(new_goal, goal, BOARDMAX);
memset(dists, 0, sizeof(dists));
if (apos != NO_MOVE) {
dists[apos] = 1;
queue[queue_end++] = apos;
}
#if 0
/* Disabled for now, since it does nothing but break atari_atari:16
* and trevorc:1540. It could be reactivated when the rest of the
* function would be modified in order to garanty that a forbidden
* move is strictly equivalent to a played move in terms of goal
* mapping. I doubt it would be anything worth though...
*/
for (pos = BOARDMIN; pos < BOARDMAX; pos++) {
if (ON_BOARD(pos) && forbidden[pos]) {
dists[pos] = 1;
queue[queue_end++] = pos;
}
}
#endif
if (queue_end == 0)
return 0;
for (r = 0; r < queue_end; r++) {
int smallest_dist = MAX_BOARD * MAX_BOARD;
int best_index = -1;
gg_assert(queue_end < MAX_BOARD * MAX_BOARD);
for (k = r; k < queue_end; k++) {
if (dists[queue[k]] < smallest_dist) {
smallest_dist = dists[queue[k]];
best_index = k;
}
}
if (best_index != r) {
int tmp = queue[r];
queue[r] = queue[best_index];
queue[best_index] = tmp;
}
pos = queue[r];
if (board[pos] == other)
new_goal[pos] = 1;
/* FIXME: We shouldn't let dead opponent stones stop the
* propagation of distance.
*
* As a partial fix we include pos == apos in a test below.
*/
for (k = 0; k < 4; k++) {
int pos2 = pos + delta[k];
if (!ON_BOARD(pos2))
continue;
if ((board[pos] != color || pos == apos) && board[pos2] == EMPTY) {
ENQUEUE(pos2, dists[pos] + 1);
}
else if (board[pos] != other && board[pos2] == other) {
int stones[MAX_BOARD * MAX_BOARD];
int size = findstones(pos2, MAX_BOARD * MAX_BOARD, stones);
int libs = countlib(pos2);
int deltadist = libs - 3;
if (deltadist < 0)
deltadist = 0;
for (s = 0; s < size; s++)
ENQUEUE(stones[s], dists[pos] + deltadist);
}
}
}
return 1;
}
/* Initialize an array with atari_atari attacks. The convention is that
* the array ends when a NO_MOVE is encountered in the move field.
*/
static void
aa_init_moves(struct aa_move attacks[AA_MAX_MOVES])
{
attacks[0].move = NO_MOVE;
}
/* Add an atari_atari attack move to a struct aa_move array. If the
* move already is included in the array, we check whether the target
* also is known for that move and add it if not.
*/
static void
aa_add_move(struct aa_move attacks[AA_MAX_MOVES], int move, int target)
{
int k;
int r;
for (k = 0; k < AA_MAX_MOVES; k++)
if (attacks[k].move == move || attacks[k].move == NO_MOVE)
break;
/* If the array is full, give up. */
if (k == AA_MAX_MOVES)
return;
target = find_origin(target);
/* New move. */
if (attacks[k].move == NO_MOVE) {
attacks[k].move = move;
attacks[k].target[0] = target;
if (AA_MAX_TARGETS_PER_MOVE > 0)
attacks[k].target[1] = NO_MOVE;
if (k < AA_MAX_MOVES - 1)
attacks[k+1].move = NO_MOVE;
return;
}
/* Known move, maybe new target. */
for (r = 0; r < AA_MAX_TARGETS_PER_MOVE; r++)
if (attacks[k].target[r] == target || attacks[k].target[r] == NO_MOVE)
break;
/* No place for more targets. */
if (r == AA_MAX_TARGETS_PER_MOVE)
return;
/* Target known. */
if (attacks[k].target[r] == target)
return;
/* Add target. */
attacks[k].target[r] = target;
if (r < AA_MAX_TARGETS_PER_MOVE - 1)
attacks[k].target[r + 1] = NO_MOVE;
}
/* Check whether an atari_atari attack move is included in an struct
* aa_move array. If target is not NO_MOVE, we also require that the
* target is known for the move.
*/
static int
aa_move_known(struct aa_move attacks[AA_MAX_MOVES], int move, int target)
{
int k;
int r;
for (k = 0; k < AA_MAX_MOVES; k++)
if (attacks[k].move == move || attacks[k].move == NO_MOVE)
break;
/* If the array is full, give up and claim the move to be known. */
if (k == AA_MAX_MOVES)
return 1;
/* Unknown move. */
if (attacks[k].move == NO_MOVE)
return 0;
/* Move known, but how about the target?
* If no target specified, just return 1.
*/
if (target == NO_MOVE)
return 1;
target = find_origin(target);
for (r = 0; r < AA_MAX_TARGETS_PER_MOVE; r++)
if (attacks[k].target[r] == target || attacks[k].target[r] == NO_MOVE)
break;
/* No place for more targets. Give up and claim the target to be known. */
if (r == AA_MAX_TARGETS_PER_MOVE)
return 1;
/* Target known. */
if (attacks[k].target[r] == target)
return 1;
/* Unknown target. */
return 0;
}
/* Auxiliary function for aa_sort_moves(). */
static int
target_comp_func(const void *a, const void *b)
{
int asize = get_aa_value(*((const int *) a));
int bsize = get_aa_value(*((const int *) b));
return asize - bsize;
}
/* Auxiliary function for aa_sort_moves(). */
static int
move_comp_func(const void *a, const void *b)
{
const struct aa_move *aa = a;
const struct aa_move *bb = b;
int asize = get_aa_value(aa->target[0]);
int bsize = get_aa_value(bb->target[0]);
return asize - bsize;
}
/* Sort the attack moves. For each move the targets are sorted in
* decreasing size. Then the moves are sorted with increasing size
* of their first target.
*/
static void
aa_sort_moves(struct aa_move attacks[AA_MAX_MOVES])
{
int k;
int r;
int number_of_attacks;
int number_of_targets;
for (k = 0; k < AA_MAX_MOVES && attacks[k].move != NO_MOVE; k++) {
for (r = 0; r < AA_MAX_TARGETS_PER_MOVE; r++)
if (attacks[k].target[r] == NO_MOVE)
break;
number_of_targets = r;
gg_sort(attacks[k].target, number_of_targets,
sizeof(attacks[k].target[0]), target_comp_func);
}
number_of_attacks = k;
gg_sort(attacks, number_of_attacks, sizeof(attacks[0]), move_comp_func);
}
#if 0
/* Returns true if a move by (color) at (pos) is atari on something.
* Currently unused.
*/
static int
is_atari(int pos, int color)
{
int other = OTHER_COLOR(color);
if (!is_legal(pos, color))
return 0;
if (board[SOUTH(pos)] == other
&& countlib(SOUTH(pos)) == 2)
return 1;
if (board[WEST(pos)] == other
&& countlib(WEST(pos)) == 2)
return 1;
if (board[NORTH(pos)] == other
&& countlib(NORTH(pos)) == 2)
return 1;
if (board[EAST(pos)] == other
&& countlib(EAST(pos)) == 2)
return 1;
return 0;
}
#endif
/*
* Local Variables:
* tab-width: 8
* c-basic-offset: 2
* End:
*/
|