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 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
|
/*
* Program: pgn-extract: a Portable Game Notation (PGN) extractor.
* Copyright (C) 1994-2001 David Barnes
* 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; either version 1, 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 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* David Barnes may be contacted as D.J.Barnes@ukc.ac.uk
* http://www.cs.ukc.ac.uk/people/staff/djb/
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "bool.h"
#include "mymalloc.h"
#include "defs.h"
#include "typedef.h"
#include "tokens.h"
#include "taglist.h"
#include "lex.h"
#include "map.h"
#include "decode.h"
#include "apply.h"
/* Structures to hold the x,y displacements of the various
* piece movements.
*/
/* Define a list of possible Knight moves. */
#define NUM_KNIGHT_MOVES 8
static short Knight_moves[2*NUM_KNIGHT_MOVES] =
{ 1, 2,
1,-2,
2, 1,
2,-1,
-1, 2,
-1,-2,
-2, 1,
-2,-1,
};
/* Define a list of possible Bishop moves. */
#define NUM_BISHOP_MOVES 4
static short Bishop_moves[2*NUM_BISHOP_MOVES] =
{ 1, 1,
1,-1,
-1, 1,
-1,-1
};
/* Define a list of possible Rook moves. */
#define NUM_ROOK_MOVES 4
static short Rook_moves[2*NUM_ROOK_MOVES] =
{ 1, 0,
0, 1,
-1, 0,
0,-1
};
/* Define a list of possible King moves. */
#define NUM_KING_MOVES 8
static short King_moves[2*NUM_KING_MOVES] =
{ 1, 0,
1, 1,
1,-1,
0, 1,
0,-1,
-1, 0,
-1, 1,
-1,-1,
};
/* Define a list of possible Queen moves. */
#define NUM_QUEEN_MOVES 8
static short Queen_moves[2*NUM_QUEEN_MOVES] =
{ 1, 0,
1, 1,
1,-1,
0, 1,
0,-1,
-1, 0,
-1, 1,
-1,-1,
};
/* A table of hash values for square/piece/colour combinations.
* When a piece is moved, the hash value is xor-ed into a
* running description of the current board state.
*/
#define NUMBER_OF_PIECES 6
static HashCode HashTab[BOARDSIZE][BOARDSIZE][NUMBER_OF_PIECES][2];
/* Prototypes for functions in this file. */
/* Code to allocate and free MovePair structures. New moves are
* allocated from the move_pool, if it isn't empty. Old moves
* are freed to this pool.
* This is used to avoid too much fragmentation of the heap.
* Since the program will spend a lot of its life allocating and
* deallocating move structures, we might as well hang on to them.
*/
/* Keep a pool of free move structures. */
static MovePair *move_pool = NULL;
static MovePair *
malloc_move(void)
{ MovePair *move;
if(move_pool != NULL){
move = move_pool;
move_pool = move_pool->next;
}
else{
move = (MovePair *)MallocOrDie(sizeof(MovePair));
}
move->next = NULL;
return move;
}
/* Append another move pair onto moves, and return the new list. */
static MovePair *
append_move_pair(Col from_col, Rank from_rank, Col to_col, Rank to_rank,MovePair *moves)
{ MovePair *move = malloc_move();
move->from_rank = from_rank;
move->from_col = from_col;
move->to_rank = to_rank;
move->to_col = to_col;
move->next = moves;
return move;
}
/* Simply add the move to the free move pool. */
static void
free_move_pair(MovePair *move)
{
move->next = move_pool;
move_pool = move;
}
/* Free a whole list of moves to the move pool. */
void
free_move_pair_list(MovePair *move_list)
{
if(move_list != NULL){
free_move_pair_list(move_list->next);
free_move_pair(move_list);
}
}
/* Produce a hash value for each piece, square, colour combination.
* This code is a modified version of that to be found in
* Steven J. Edwards' SAN kit.
*/
#define SHIFT_LENGTH 7
void
init_hashtab(void)
{ Piece piece;
Colour colour;
Rank rank;
Col col;
static HashCode seed = 0;
for(col = FIRSTCOL; col <= LASTCOL; col++){
for(rank = FIRSTRANK; rank <= LASTRANK; rank++){
for(piece = PAWN; piece <= KING; piece++){
for(colour = BLACK; colour <= WHITE; colour++){
HashCode code;
#if 1
/* Try to use a wider range of the available values
* in an attempt to avoid spurious hash matches.
*/
seed = (seed * 1103515245L) + 12345678L;
#else
/* This is the version used up to and including version 13.8. */
seed = (seed * 1103515245L) + 12345L;
#endif
code = (seed >> SHIFT_LENGTH);
HashTab[col-FIRSTCOL][rank-FIRSTRANK][piece-PAWN][colour-BLACK] = code;
}
}
}
}
}
/* Look up the hash value for this combination. */
HashCode
HashLookup(Col col, Rank rank, Piece piece, Colour colour)
{
return HashTab[col-FIRSTCOL][rank-FIRSTRANK][piece-PAWN][colour-BLACK];
}
/* Is the given piece of the named colour? */
static Boolean
piece_is_colour(Piece coloured_piece, Colour colour)
{
return EXTRACT_COLOUR(coloured_piece) == colour;
}
/* Make the given move. This is assumed to have been thoroughly
* checked beforehand, and the from_ and to_ information to be
* complete. Update the board structure to reflect
* the full set of changes implied by it.
*/
void
make_move(Col from_col, Rank from_rank, Col to_col, Rank to_rank,
Piece piece, Colour colour,Board *board)
{ short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
short from_r = RankConvert(from_rank);
short from_c = ColConvert(from_col);
/* If a KING or ROOK is moved, this might affect castling rights. */
if(piece == KING){
if(colour == WHITE){
board->WKingCol = to_col;
board->WKingRank = to_rank;
board->WKingCastle =
board->WQueenCastle = FALSE;
}
else{
board->BKingCol = to_col;
board->BKingRank = to_rank;
board->BKingCastle =
board->BQueenCastle = FALSE;
}
}
else if(piece == ROOK){
/* Some castling rights may need disallowing. */
if(colour == WHITE){
if(from_rank == FIRSTRANK){
if(from_col == FIRSTCOL){
board->WQueenCastle = FALSE;
}
else if(from_col == LASTCOL){
board->WKingCastle = FALSE;
}
else{
/* No change. */
}
}
}
else{
if(from_rank == LASTRANK){
if(from_col == FIRSTCOL){
board->BQueenCastle = FALSE;
}
else if(from_col == LASTCOL){
board->BKingCastle = FALSE;
}
else{
/* No change. */
}
}
}
}
else{
/* Castling not in question from the piece being moved,
* but see below for checks on any captured piece.
*/
}
/* Check for en-passant rights resulting from this move. */
if(piece != PAWN){
/* The move cannot result in en-passant rights. */
board->EnPassant = FALSE;
}
else{
if(colour == WHITE){
if((from_rank == '2') && (to_rank == '4')){
/* This move permits an en-passant capture on the following move. */
board->EnPassant = TRUE;
board->ep_rank = to_rank-1;
board->ep_col = to_col;
}
else if((board->EnPassant) && (board->ep_rank == to_rank) &&
(board->ep_col == to_col)){
/* This is an ep capture. Remove the intermediate pawn. */
board->board[RankConvert(to_rank)-1][ColConvert(to_col)] = EMPTY;
board->hash_value ^= HashLookup(to_col,to_rank-1,PAWN,BLACK);
board->EnPassant = FALSE;
}
else{
board->EnPassant = FALSE;
}
}
else{
if((from_rank == '7') && (to_rank == '5')){
board->EnPassant = TRUE;
board->ep_rank = to_rank+1;
board->ep_col = to_col;
}
else if((board->EnPassant) && (board->ep_rank == to_rank) &&
(board->ep_col == to_col)){
/* This is an ep capture. Remove the intermediate pawn. */
board->board[RankConvert(to_rank)+1][ColConvert(to_col)] = EMPTY;
board->hash_value ^= HashLookup(to_col,to_rank+1,PAWN,WHITE);
board->EnPassant = FALSE;
}
else{
board->EnPassant = FALSE;
}
}
}
/* Clear the source square. */
board->board[from_r][from_c] = EMPTY;
board->hash_value ^= HashLookup(from_col,from_rank,piece,colour);
if(board->board[to_r][to_c] != EMPTY){
/* Delete the captured piece from the hash value. */
Piece coloured_piece = board->board[to_r][to_c];
Piece removed_piece;
Colour removed_colour;
removed_piece = EXTRACT_PIECE(coloured_piece);
removed_colour = EXTRACT_COLOUR(coloured_piece);
board->hash_value ^= HashLookup(to_col,to_rank,removed_piece,removed_colour);
/* See whether the removed piece is a Rook, as this could
* affect castling rights.
*/
if(removed_piece == ROOK){
if(removed_colour == WHITE){
if(to_rank == FIRSTRANK){
if(to_col == FIRSTCOL){
board->WQueenCastle = FALSE;
}
else if(to_col == LASTCOL){
board->WKingCastle = FALSE;
}
else{
/* No change. */
}
}
}
else{
if(to_rank == LASTRANK){
if(to_col == FIRSTCOL){
board->BQueenCastle = FALSE;
}
else if(to_col == LASTCOL){
board->BKingCastle = FALSE;
}
else{
/* No change. */
}
}
}
}
}
/* Place the piece at its destination. */
board->board[to_r][to_c] = MAKE_COLOURED_PIECE(colour,piece);
/* Insert the moved piece into the hash value. */
board->hash_value ^= HashLookup(to_col,to_rank,piece,colour);
}
/* Find pawn moves matching the to_ and from_ information.
* Depending on the input form of the move, some of this will be
* incomplete. For instance: e4 supplies just the to_ information
* whereas cb supplies some from_ and some to_.
*/
MovePair *
find_pawn_moves(Col from_col, Rank from_rank, Col to_col,Rank to_rank,
Colour colour, const Board *board)
{ short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
short from_r = RankConvert(from_rank);
short from_c = ColConvert(from_col);
MovePair *move_list = NULL;
MovePair *move = NULL;
/* White pawn moves are offset by +1, Black by -1. */
short offset = COLOUR_OFFSET(colour);
Piece piece_to_move = MAKE_COLOURED_PIECE(colour,PAWN);
if((to_col != 0) && (to_rank != 0)){
/* We know the complete destination. */
if(board->board[to_r][to_c] == EMPTY){
/* Destination must be empty for this form. */
if(board->board[to_r-offset][to_c] == piece_to_move){
/* MovePair of one square. */
move = append_move_pair(ToCol(to_c),ToRank(to_r-offset),
to_col,to_rank,NULL);
}
else if((board->board[to_r-offset][to_c] == EMPTY) &&
(to_rank == (colour == WHITE?'4':'5'))){
/* Special case of initial two square move. */
if(board->board[to_r-2*offset][to_c] == piece_to_move){
move = append_move_pair(ToCol(to_c),ToRank(to_r-2*offset),
to_col,to_rank,NULL);
}
}
else if(board->EnPassant &&
(board->ep_rank == to_rank) && (board->ep_col == to_col)){
/* Make sure that there is a valid pawn in position. */
if(from_col != 0){
from_r = to_r-offset;
if(board->board[from_r][from_c] == piece_to_move){
move = append_move_pair(ToCol(from_c),ToRank(from_r),
to_col,ToRank(to_r),NULL);
}
}
}
}
else if(piece_is_colour(board->board[to_r][to_c],OPPOSITE_COLOUR(colour))){
/* Capture on the destination square. */
if(from_col != 0){
/* We know the from column. */
from_r = to_r-offset;
if(board->board[from_r][from_c] == piece_to_move){
move = append_move_pair(ToCol(from_c),ToRank(from_r),
to_col,to_rank,NULL);
}
}
}
else{
/* We have no move. */
move = NULL;
}
move_list = move;
}
else if((from_col != 0) && (to_col != 0)){
/* Should be a diagonal capture. */
if(((from_col + 1) != to_col) && ((from_col - 1) != to_col)){
/* Inconsistent information. */
}
else{
if(from_rank != 0){
/* We have complete source information. Check its
* veracity.
*/
to_r = from_r - offset;
if(board->board[from_r][from_c] == piece_to_move){
Piece occupant = board->board[to_r][to_c];
if((occupant != EMPTY) &&
(piece_is_colour(occupant,OPPOSITE_COLOUR(colour)))){
move = append_move_pair(ToCol(from_c),ToRank(from_r),
to_col,ToRank(to_r),NULL);
}
else if(board->EnPassant && (board->ep_rank == ToRank(to_r)) &&
(board->ep_col == ToCol(to_c))){
move = append_move_pair(ToCol(from_c),ToRank(from_r),
to_col,ToRank(to_r),NULL);
}
}
}
else{
/* We must search the from_col and to_col for appropriate
* combinations. There may be more than one.
*/
short start_rank, end_rank;
/* Work out from where to start and end. */
if(colour == WHITE){
start_rank = RankConvert(FIRSTRANK+1);
end_rank = RankConvert(LASTRANK);
}
else{
start_rank = RankConvert(LASTRANK-1);
end_rank = RankConvert(FIRSTRANK);
}
for(from_r = start_rank; from_r != end_rank; from_r += offset){
to_r = from_r+offset;
if(board->board[from_r][from_c] == piece_to_move){
Piece occupant = board->board[to_r][to_c];
if((occupant != EMPTY) &&
(piece_is_colour(occupant,OPPOSITE_COLOUR(colour)))){
move_list = append_move_pair(ToCol(from_c),ToRank(from_r),
to_col,ToRank(to_r),move_list);
}
else if(board->EnPassant && (board->ep_rank == ToRank(to_r)) &&
(board->ep_col == ToCol(to_c))){
move_list = append_move_pair(ToCol(from_c),ToRank(from_r),
to_col,ToRank(to_r),move_list);
}
}
}
}
}
}
return move_list;
}
/* Find knight moves to the given square. */
MovePair *
find_knight_moves(Col to_col,Rank to_rank, Colour colour, const Board *board)
{ short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
unsigned ix;
MovePair *move_list = NULL;
Piece target_piece = MAKE_COLOURED_PIECE(colour,KNIGHT);
/* Pick up pairs of offsets from to_r,to_c to look for a Knight of
* the right colour.
*/
for(ix = 0; ix < 2*NUM_KNIGHT_MOVES; ix += 2){
short r = Knight_moves[ix]+to_r;
short c = Knight_moves[ix+1]+to_c;
if(board->board[r][c] == target_piece){
move_list = append_move_pair(ToCol(c),ToRank(r),to_col,to_rank,move_list);
}
}
return move_list;
}
/* Find bishop moves to the given square. */
MovePair *
find_bishop_moves(Col to_col,Rank to_rank, Colour colour, const Board *board)
{ short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
unsigned ix;
MovePair *move_list = NULL;
Piece target_piece = MAKE_COLOURED_PIECE(colour,BISHOP);
/* Pick up pairs of offsets from to_r,to_c to look for a Bishop of
* the right colour.
*/
for(ix = 0; ix < 2*NUM_BISHOP_MOVES; ix += 2){
short r = to_r, c = to_c;
/* Work backwards from the destination to find a bishop of
* the right colour.
*/
do{
r += Bishop_moves[ix];
c += Bishop_moves[ix+1];
} while(board->board[r][c] == EMPTY);
if(board->board[r][c] == target_piece){
move_list = append_move_pair(ToCol(c),ToRank(r),to_col,to_rank,move_list);
}
}
return move_list;
}
/* Find rook moves to the given square. */
MovePair *
find_rook_moves(Col to_col,Rank to_rank, Colour colour, const Board *board)
{ short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
unsigned ix;
MovePair *move_list = NULL;
Piece target_piece = MAKE_COLOURED_PIECE(colour,ROOK);
/* Pick up pairs of offsets from to_r,to_c to look for a Rook of
* the right colour.
*/
for(ix = 0; ix < 2*NUM_ROOK_MOVES; ix += 2){
short r = to_r, c = to_c;
/* Work backwards from the destination to find a rook of
* the right colour.
*/
do{
r += Rook_moves[ix];
c += Rook_moves[ix+1];
} while(board->board[r][c] == EMPTY);
if(board->board[r][c] == target_piece){
move_list = append_move_pair(ToCol(c),ToRank(r),to_col,to_rank,move_list);
}
}
return move_list;
}
/* Find queen moves to the given square. */
MovePair *
find_queen_moves(Col to_col,Rank to_rank, Colour colour, const Board *board)
{ short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
unsigned ix;
MovePair *move_list = NULL;
Piece target_piece = MAKE_COLOURED_PIECE(colour,QUEEN);
/* Pick up pairs of offsets from to_r,to_c to look for a Knight of
* the right colour.
*/
for(ix = 0; ix < 2*NUM_QUEEN_MOVES; ix += 2){
short r = to_r, c = to_c;
/* Work backwards from the destination to find a queen of
* the right colour.
*/
do{
r += Queen_moves[ix];
c += Queen_moves[ix+1];
} while(board->board[r][c] == EMPTY);
if(board->board[r][c] == target_piece){
move_list = append_move_pair(ToCol(c),ToRank(r),to_col,to_rank,move_list);
}
}
return move_list;
}
/* Find King moves to the given square. */
MovePair *
find_king_moves(Col to_col,Rank to_rank, Colour colour, const Board *board)
{ short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
unsigned ix;
MovePair *move_list = NULL;
Piece target_piece = MAKE_COLOURED_PIECE(colour,KING);
/* Pick up pairs of offsets from to_r,to_c to look for a King of
* the right colour.
*/
for(ix = 0; ix < 2*NUM_KING_MOVES; ix += 2){
short r = King_moves[ix]+to_r;
short c = King_moves[ix+1]+to_c;
if(board->board[r][c] == target_piece){
move_list = append_move_pair(ToCol(c),ToRank(r),to_col,to_rank,move_list);
}
}
return move_list;
}
/* Return true if the king of the given colour is
* in check on the board, FALSE otherwise.
*/
Boolean
king_is_in_check(const Board *board,Colour king_colour)
{ MovePair *replies = NULL;
/* Assume that there is no check. */
CheckStatus in_check = NOCHECK;
Col king_col;
Rank king_rank;
Colour opponent_colour = OPPOSITE_COLOUR(king_colour);
/* Find out where the king is now. */
if(king_colour == WHITE){
king_col = board->WKingCol;
king_rank = board->WKingRank;
}
else{
king_col = board->BKingCol;
king_rank = board->BKingRank;
}
/* Try and find one move that leaves this king in check.
* There is probably an optimal order for these tests, but I haven't
* tried to find it.
*/
if((king_col != LASTCOL) &&
((replies = find_pawn_moves(king_col+1,0,king_col,king_rank,
opponent_colour,board)) != NULL)){
/* King is in check from a pawn to its right. */
}
else if((king_col != FIRSTCOL) &&
((replies = find_pawn_moves(king_col-1,0,king_col,king_rank,
opponent_colour,board)) != NULL)){
/* King is in check from a pawn. to its left */
}
else if((replies =
find_knight_moves(king_col,king_rank,
opponent_colour,board)) != NULL){
/* King is in check from a knight. */
}
else if((replies =
find_bishop_moves(king_col,king_rank,
opponent_colour,board)) != NULL){
/* King is in check from a bishop. */
}
else if((replies =
find_rook_moves(king_col,king_rank,
opponent_colour,board)) != NULL){
/* King is in check from a rook. */
}
else if((replies =
find_queen_moves(king_col,king_rank,
opponent_colour,board)) != NULL){
/* King is in check from a queen. */
}
else if((replies =
find_king_moves(king_col,king_rank,
opponent_colour,board)) != NULL){
/* King is in check from a king. */
}
else{
/* King is safe. */
}
if(replies != NULL){
in_check = CHECK;
free_move_pair_list(replies);
}
return in_check;
}
/* possibles contains a list of possible moves of piece.
* This function should exclude all of those moves of this piece
* which leave its own king in check. The list of remaining legal
* moves is returned as result.
* This function operates by looking for at least one reply by the
* opponent that could capture the king of the given colour.
* Only one such move needs to be found to invalidate one of the
* possible moves.
*/
MovePair *
exclude_checks(Piece piece, Colour colour,MovePair *possibles, const Board *board)
{ /* As this function is not called recursively, it should be
* safe to retain a single copy of the board on the stack without risking
* overflow. This has been a problem in the past with the PC version.
*/
Board copy_board;
MovePair *valid_move_list = NULL;
MovePair *move;
/* For each possible move, make the move and see if it leaves the king
* in check.
*/
for(move = possibles; move != NULL; ){
/* Take a copy of the board before playing this next move. */
copy_board = *board;
make_move(move->from_col,move->from_rank,
move->to_col,move->to_rank,piece,colour,©_board);
if(king_is_in_check(©_board,colour) != NOCHECK){
MovePair *illegal_move = move;
move = move->next;
/* Free the illegal move. */
free_move_pair(illegal_move);
}
else{
/* King is safe and the move may be kept. */
MovePair *legal_move = move;
move = move->next;
legal_move->next = valid_move_list;
valid_move_list = legal_move;
}
}
return valid_move_list;
}
/* We must exclude the possibility of the king passing
* through check, or castling out of it.
*/
static Boolean
exclude_castling_across_checks(MoveClass class, Colour colour, const Board *board)
{ Boolean Ok = TRUE;
MovePair *move = malloc_move();
Rank rank = (colour == WHITE)? FIRSTRANK : LASTRANK;
Col to_col;
if(class == KINGSIDE_CASTLE){
/* Start where we are, because you can't castle out of check. */
for(to_col = 'e'; (to_col <= 'g') && Ok; to_col++){
move->from_col = 'e';
move->from_rank = rank;
move->to_col = to_col;
move->to_rank = rank;
move = exclude_checks(KING,colour,move,board);
if(move == NULL){
Ok = FALSE;
}
}
}
else{
/* Start where we are, because you can't castle out of check. */
for(to_col = 'e'; (to_col <= 'c') && Ok; to_col--){
move->from_col = 'e';
move->from_rank = rank;
move->to_col = to_col;
move->to_rank = rank;
move = exclude_checks(KING,colour,move,board);
if(move == NULL){
Ok = FALSE;
}
}
}
if(move != NULL){
free_move_pair(move);
}
return Ok;
}
/* Possibles is a list of possible moves of piece.
* Exclude all of those that either leave the king in check
* or those excluded by non-null information in from_col or from_rank.
*/
static MovePair *
exclude_moves(Piece piece, Colour colour,Col from_col, Rank from_rank,
MovePair *possibles, const Board *board)
{ MovePair *move_list = NULL;
/* See if we have disambiguating from_ information. */
if((from_col != 0) || (from_rank != 0)){
MovePair *move, *temp;
for(move = possibles; move != NULL; ){
Boolean excluded = FALSE;
if(from_col != 0){
/* The from_col is specified. */
if(move->from_col != from_col){
excluded = TRUE;
}
}
if((from_rank != 0) && !excluded){
if(move->from_rank != from_rank){
excluded = TRUE;
}
}
temp = move;
move = move->next;
if(!excluded){
/* Add it to the list of possibles. */
temp->next = move_list;
move_list = temp;
}
else{
/* Discard it. */
free_move_pair(temp);
}
}
}
else{
/* Everything is still possible. */
move_list = possibles;
}
if(move_list != NULL){
move_list = exclude_checks(piece,colour,move_list,board);
}
return move_list;
}
/* Make a pawn move.
* En-passant information in the original move text is not currently used
* to disambiguate pawn moves. E.g. with Black pawns on c4 and c5 after
* White move 1. d4 a reply 1... cdep will be rejected as ambiguous.
*/
static Boolean
pawn_move(Move *move_details, Colour colour, Board *board)
{ Col from_col = move_details->from_col;
Rank from_rank = move_details->from_rank;
Col to_col = move_details->to_col;
Rank to_rank = move_details->to_rank;
/* Find the basic set of moves that match the move_details criteria. */
MovePair *move_list;
Boolean Ok = TRUE;
/* Make sure that the col values are consistent with a pawn move. */
if((from_col != '\0') && (to_col != '\0') &&
/* Forward. */
(from_col != to_col) &&
/* Capture. */
(from_col != (to_col+1)) && (from_col != (to_col-1))){
/* Inconsistent. */
Ok = FALSE;
}
else if((move_list = find_pawn_moves(from_col,from_rank,to_col,to_rank,
colour,board)) == NULL){
Ok = FALSE;
}
else{
/* Exclude any moves that leave the king in check, or are disambiguate
* by from_information.
*/
move_list = exclude_moves(PAWN,colour,from_col,from_rank,move_list,board);
if(move_list != NULL){
if(move_list->next == NULL){
/* Unambiguous move. Some pawn moves will have supplied
* incomplete destinations (e.g. cd as opposed to cxd4)
* so pick up both from_ and to_ information.
*/
move_details->from_col = move_list->from_col;
move_details->from_rank = move_list->from_rank;
move_details->to_col = move_list->to_col;
move_details->to_rank = move_list->to_rank;
}
else{
/* Ambiguous. */
Ok = FALSE;
}
free_move_pair_list(move_list);
}
else{
/* Excluded. */
Ok = FALSE;
}
}
return Ok;
}
/* Make a pawn move that involves an explicit promotion to promoted_piece. */
static Boolean
promote(Move *move_details, Colour colour, Board *board)
{ Col from_col = move_details->from_col;
Rank from_rank = move_details->from_rank;
Col to_col = move_details->to_col;
Rank to_rank = move_details->to_rank;
MovePair *move_list;
Boolean Ok = FALSE;
if(to_rank == '\0'){
/* We can fill this in. */
if(colour == WHITE){
to_rank = LASTRANK;
}
else{
to_rank = FIRSTRANK;
}
}
/* Now check that to_rank makes sense for the given colour. */
if(((colour == WHITE) && (to_rank != LASTRANK)) ||
((colour == BLACK) && (to_rank != FIRSTRANK))){
fprintf(GlobalState.logfile,"Illegal pawn promotion to %c%c\n",to_col,to_rank);
}
else{
move_list = find_pawn_moves(from_col,from_rank,to_col,to_rank,colour,board);
if(move_list != NULL){
if(move_list->next == NULL){
/* Unambiguous move. Some pawn moves will have supplied
* incomplete destinations (e.g. cd as opposed to cxd8)
* so pick up both from_ and to_ information.
*/
move_details->from_col = move_list->from_col;
move_details->from_rank = move_list->from_rank;
move_details->to_col = move_list->to_col;
move_details->to_rank = move_list->to_rank;
Ok = TRUE;
}
else{
fprintf(GlobalState.logfile,"Ambiguous pawn move to %c%c\n",to_col,to_rank);
}
free_move_pair_list(move_list);
}
else{
fprintf(GlobalState.logfile,"Illegal pawn promotion to %c%c\n",to_col,to_rank);
}
}
return Ok;
}
/* Make a knight move, indicated by move_details.
* This may result in further information being added to move_details.
*/
static Boolean
knight_move(Move *move_details, Colour colour, Board *board)
{ Col from_col = move_details->from_col;
Rank from_rank = move_details->from_rank;
Col to_col = move_details->to_col;
Rank to_rank = move_details->to_rank;
short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
MovePair *move_list = find_knight_moves(to_col,to_rank,colour,board);
/* Assume everything will be ok. */
Boolean Ok = TRUE;
move_list = exclude_moves(KNIGHT,colour,from_col,from_rank,move_list,board);
if(move_list == NULL){
fprintf(GlobalState.logfile,"No knight move possible to %c%c.\n",to_col,to_rank);
Ok = FALSE;
}
else if(move_list->next == NULL){
/* Only one possible. Check for legality. */
Piece occupant = board->board[to_r][to_c];
if((occupant == EMPTY) || piece_is_colour(occupant,OPPOSITE_COLOUR(colour))){
move_details->from_col = move_list->from_col;
move_details->from_rank = move_list->from_rank;
}
else{
fprintf(GlobalState.logfile,"Knight destination square %c%c is illegal.\n",
to_col,to_rank);
Ok = FALSE;
}
free_move_pair(move_list);
}
else{
fprintf(GlobalState.logfile,"Ambiguous knight move to %c%c.\n",to_col,to_rank);
free_move_pair_list(move_list);
Ok = FALSE;
}
return Ok;
}
/* Make a bishop move, indicated by move_details.
* This may result in further information being added to move_details.
*/
static Boolean
bishop_move(Move *move_details, Colour colour, Board *board)
{ Col from_col = move_details->from_col;
Rank from_rank = move_details->from_rank;
Col to_col = move_details->to_col;
Rank to_rank = move_details->to_rank;
short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
MovePair *move_list = find_bishop_moves(to_col,to_rank,colour,board);
/* Assume that it is ok. */
Boolean Ok = TRUE;
move_list = exclude_moves(BISHOP,colour,from_col,from_rank,move_list,board);
if(move_list == NULL){
fprintf(GlobalState.logfile,"No bishop move possible to %c%c.\n",to_col,to_rank);
Ok = FALSE;
}
else if(move_list->next == NULL){
/* Only one possible. Check for legality. */
Piece occupant = board->board[to_r][to_c];
if((occupant == EMPTY) || piece_is_colour(occupant,OPPOSITE_COLOUR(colour))){
move_details->from_col = move_list->from_col;
move_details->from_rank = move_list->from_rank;
}
else{
fprintf(GlobalState.logfile,"Bishop's destination square %c%c is illegal.\n",
to_col,to_rank);
Ok = FALSE;
}
free_move_pair(move_list);
}
else{
fprintf(GlobalState.logfile,"Ambiguous bishop move to %c%c.\n",to_col,to_rank);
free_move_pair_list(move_list);
Ok = FALSE;
}
return Ok;
}
/* Make a rook move, indicated by move_details.
* This may result in further information being added to move_details.
*/
static Boolean
rook_move(Move *move_details, Colour colour, Board *board)
{ Col from_col = move_details->from_col;
Rank from_rank = move_details->from_rank;
Col to_col = move_details->to_col;
Rank to_rank = move_details->to_rank;
short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
MovePair *move_list = find_rook_moves(to_col,to_rank,colour,board);
/* Assume that it is ok. */
Boolean Ok = TRUE;
if(move_list == NULL){
fprintf(GlobalState.logfile,"No rook move possible to %c%c.\n",to_col,to_rank);
Ok = FALSE;
}
else{
move_list = exclude_moves(ROOK,colour,from_col,from_rank,move_list,board);
if(move_list == NULL){
fprintf(GlobalState.logfile,"Indicated rook move is excluded.\n");
Ok = FALSE;
}
else if(move_list->next == NULL){
/* Only one possible. Check for legality. */
Piece occupant = board->board[to_r][to_c];
if((occupant == EMPTY) || piece_is_colour(occupant,OPPOSITE_COLOUR(colour))){
move_details->from_col = move_list->from_col;
move_details->from_rank = move_list->from_rank;
}
else{
fprintf(GlobalState.logfile,
"Rook's destination square %c%c is illegal.\n",
to_col,to_rank);
Ok = FALSE;
}
free_move_pair(move_list);
}
else{
fprintf(GlobalState.logfile,"Ambiguous rook move to %c%c.\n",to_col,to_rank);
free_move_pair_list(move_list);
Ok = FALSE;
}
}
return Ok;
}
/* Find a queen move indicated by move_details.
* This may result in further information being added to move_details.
*/
static Boolean
queen_move(Move *move_details, Colour colour, Board *board)
{ Col from_col = move_details->from_col;
Rank from_rank = move_details->from_rank;
Col to_col = move_details->to_col;
Rank to_rank = move_details->to_rank;
short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
MovePair *move_list = find_queen_moves(to_col,to_rank,colour,board);
/* Assume that it is ok. */
Boolean Ok = TRUE;
move_list = exclude_moves(QUEEN,colour,from_col,from_rank,move_list,board);
if(move_list == NULL){
fprintf(GlobalState.logfile,"No queen move possible to %c%c.\n",to_col,to_rank);
Ok = FALSE;
}
else if(move_list->next == NULL){
/* Only one possible. Check for legality. */
Piece occupant = board->board[to_r][to_c];
if((occupant == EMPTY) || piece_is_colour(occupant,OPPOSITE_COLOUR(colour))){
move_details->from_col = move_list->from_col;
move_details->from_rank = move_list->from_rank;
}
else{
fprintf(GlobalState.logfile,"Queen's destination square %c%c is illegal.\n",
to_col,to_rank);
Ok = FALSE;
}
free_move_pair(move_list);
}
else{
fprintf(GlobalState.logfile,"Ambiguous queen move to %c%c.\n",to_col,to_rank);
free_move_pair_list(move_list);
Ok = FALSE;
}
return Ok;
}
/* Can colour castle kingside? */
static Boolean
can_castle_kingside(Colour colour, const Board *board)
{ /* Assume failure. */
Boolean Ok = FALSE;
Rank king_rank;
if(colour == WHITE){
king_rank = FIRSTRANK;
Ok = board->WKingCastle;
}
else{
king_rank = LASTRANK;
Ok = board->BKingCastle;
}
if(Ok){
/* It is permitted. */
short king_c = ColConvert('e');
short king_r = RankConvert(king_rank);
Piece coloured_king = MAKE_COLOURED_PIECE(colour,KING);
Piece coloured_rook = MAKE_COLOURED_PIECE(colour,ROOK);
if((board->board[king_r][king_c] == coloured_king) &&
(board->board[king_r][king_c+1] == EMPTY) &&
(board->board[king_r][king_c+2] == EMPTY) &&
(board->board[king_r][king_c+3] == coloured_rook)){
if(exclude_castling_across_checks(KINGSIDE_CASTLE,colour,board)){
Ok = TRUE;
}
else{
/* Can't castle across check. */
Ok = FALSE;
}
}
else{
/* Kingside castling is blocked. */
Ok = FALSE;
}
}
else{
/* Kingside castling is forbidden. */
}
return Ok;
}
/* Can colour castle queenside? */
static Boolean
can_castle_queenside(Colour colour, const Board *board)
{ /* Assume failure. */
Boolean Ok = FALSE;
Rank king_rank;
if(colour == WHITE){
king_rank = FIRSTRANK;
Ok = board->WQueenCastle;
}
else{
king_rank = LASTRANK;
Ok = board->BQueenCastle;
}
if(Ok){
/* It is permitted. */
short king_c = ColConvert('e');
short king_r = RankConvert(king_rank);
Piece coloured_king = MAKE_COLOURED_PIECE(colour,KING);
Piece coloured_rook = MAKE_COLOURED_PIECE(colour,ROOK);
if((board->board[king_r][king_c] == coloured_king) &&
(board->board[king_r][king_c-1] == EMPTY) &&
(board->board[king_r][king_c-2] == EMPTY) &&
(board->board[king_r][king_c-3] == EMPTY) &&
(board->board[king_r][king_c-4] == coloured_rook)){
if(exclude_castling_across_checks(QUEENSIDE_CASTLE,colour,board)){
Ok = TRUE;
}
else{
/* Can't castle across check. */
Ok = FALSE;
}
}
else{
/* Queenside castling is blocked. */
Ok = FALSE;
}
}
else{
/* Queenside castling is forbidden. */
}
return Ok;
}
/* Castle king side. */
static Boolean
kingside_castle(Move *move_details,Colour colour, Board *board)
{ Boolean Ok;
if(can_castle_kingside(colour,board)){
Rank rank = colour == WHITE?FIRSTRANK:LASTRANK;
move_details->from_col = 'e';
move_details->from_rank = rank;
move_details->to_col = 'g';
move_details->to_rank = rank;
Ok = TRUE;
}
else{
fprintf(GlobalState.logfile,"Kingside castling is forbidden to %s.\n",
colour == WHITE?"White":"Black");
Ok = FALSE;
}
return Ok;
}
static Boolean
queenside_castle(Move *move_details, Colour colour, Board *board)
{ Boolean Ok;
if(can_castle_queenside(colour,board)){
Rank rank = colour == WHITE?FIRSTRANK:LASTRANK;
move_details->from_col = 'e';
move_details->from_rank = rank;
move_details->to_col = 'c';
move_details->to_rank = rank;
Ok = TRUE;
}
else{
fprintf(GlobalState.logfile,"Queenside castling is forbidden to %s.\n",
colour == WHITE?"White":"Black");
Ok = FALSE;
}
return Ok;
}
/* Move the king according to move_details.
* This may result in further information being added to move_details.
*/
static Boolean
king_move(Move *move_details, Colour colour, Board *board)
{ Col from_col = move_details->from_col;
Rank from_rank = move_details->from_rank;
Col to_col = move_details->to_col;
Rank to_rank = move_details->to_rank;
short to_r = RankConvert(to_rank);
short to_c = ColConvert(to_col);
/* Find all possible king moves to the destination squares. */
MovePair *move_list = find_king_moves(to_col,to_rank,colour,board);
/* Assume that it is ok. */
Boolean Ok = TRUE;
/* Exclude disambiguated and illegal moves. */
move_list = exclude_moves(KING,colour,from_col,from_rank,move_list,board);
if(move_list == NULL){
fprintf(GlobalState.logfile,"No king move possible to %c%c.\n",to_col,to_rank);
Ok = FALSE;
}
else if(move_list->next == NULL){
/* Only one possible. Check for legality. */
Piece occupant = board->board[to_r][to_c];
if((occupant == EMPTY) || piece_is_colour(occupant,OPPOSITE_COLOUR(colour))){
move_details->from_col = move_list->from_col;
move_details->from_rank = move_list->from_rank;
}
else{
fprintf(GlobalState.logfile,"King's destination square %c%c is illegal.\n",
to_col,to_rank);
Ok = FALSE;
}
free_move_pair(move_list);
}
else{
fprintf(GlobalState.logfile,
"Ambiguous king move possible to %c%c.\n",to_col,to_rank);
fprintf(GlobalState.logfile,"Multiple kings?!\n");
free_move_pair_list(move_list);
Ok = FALSE;
}
return Ok;
}
/* Try to complete the full set of move information for
* move details.
* In the process, several fields of move_details are modified
* as accurate information is determined about the effect of the move.
* The from_ and to_ fields are completed, class may be refined, and
* captured_piece is filled in. The purpose of this is to make it
* possible to call apply_move with a full set of information on the
* move. In addition, once determine_move_details has done its job,
* it should be possible to use the information to reconstruct the effect
* of a move in reverse, if necessary. This would be required by a display
* program, for instance.
* NB: this function does not determine whether or not the move gives check.
*/
Boolean
determine_move_details(Colour colour,Move *move_details, Board *board)
{ Boolean Ok = FALSE;
if(move_details == NULL){
/* Shouldn't happen. */
fprintf(GlobalState.logfile,
"Internal error: Empty move details in apply_move.\n");
}
else if(move_details->move[0] == '\0'){
/* Shouldn't happen. */
fprintf(GlobalState.logfile,
"Internal error: Null move in apply_move.\n");
}
else{
/* We have something -- normal case. */
const unsigned char *move = move_details->move;
MoveClass class = move_details->class;
Boolean move_handled = FALSE;
/* A new piece on promotion. */
move_details->promoted_piece = EMPTY;
/* Because the decoding process did not have the current board
* position available, trap apparent pawn moves that maybe something
* else.
* At the moment, only do this when full positional information is
* available.
*/
if((class == PAWN_MOVE) &&
(move_details->from_col != 0) &&
(move_details->from_rank != 0) &&
(move_details->to_col != 0) &&
(move_details->to_rank != 0)){
/* Try to work out its details and handle it below. */
move_details = decode_algebraic(move_details,board);
/* Pick up the new class. */
class = move_details->class;
}
/* Deal with apparent pawn moves first. */
if((class == PAWN_MOVE) || (class == ENPASSANT_PAWN_MOVE) ||
(class == PAWN_MOVE_WITH_PROMOTION)){
move_details->piece_to_move = PAWN;
/* Fill in any promotional details. */
if(class == PAWN_MOVE){
/* Check for implicit promotion. */
if(((move_details->to_rank == LASTRANK) && (colour == WHITE)) ||
((move_details->to_rank == FIRSTRANK) && (colour == BLACK))){
/* Don't modify move_details->class as we are not
* adding the new piece to the move string.
*/
class = PAWN_MOVE_WITH_PROMOTION;
/* Since the move string doesn't tell us, we have to
* assume a queen.
*/
move_details->promoted_piece = QUEEN;
}
}
else if(class == ENPASSANT_PAWN_MOVE){
/* No promotion possible. */
}
else{
/* Pick up the promoted_piece from the move string. */
unsigned last_char = strlen((const char *)move)-1;
/* Skip any check character. */
while(is_check(move[last_char])){
last_char--;
}
/* Pick up what kind of piece it is. */
move_details->promoted_piece = is_piece(&move[last_char]);
switch(move_details->promoted_piece){
case QUEEN: case ROOK: case BISHOP: case KNIGHT:
/* Ok. */
break;
default:
fprintf(GlobalState.logfile,
"Unknown piece in promotion %s\n",move);
move_details->promoted_piece = EMPTY;
break;
}
}
if((class == PAWN_MOVE) || (class == ENPASSANT_PAWN_MOVE)){
/* Check out the details and confirm the move's class. */
Ok = pawn_move(move_details,colour,board);
/* See if we are dealing with and En Passant move. */
if(Ok){
if((board->EnPassant) &&
(board->ep_rank == move_details->to_rank) &&
(board->ep_col == move_details->to_col)){
move_details->class = class = ENPASSANT_PAWN_MOVE;
}
else{
/* Just in case the original designation was incorrect. */
move_details->class = class = PAWN_MOVE;
}
move_handled = TRUE;
}
}
else if (class == PAWN_MOVE_WITH_PROMOTION){
/* Handle a move involving promotion. */
Ok = promote(move_details,colour,board);
move_handled = TRUE;
}
else{
/* Shouldn't get here. */
}
if(!move_handled){
/* We failed to find the move, for some reason. */
/* See if it might be a Bishop move with a lower case 'b'. */
if(move_details->move[0] == 'b'){
/* See if we can find an valid Bishop alternative for it. */
unsigned char alternative_move[MAX_MOVE_LEN+1];
Move *alternative;
strcpy((char *) alternative_move,
(const char *)move_details->move);
*alternative_move = 'B';
alternative = decode_move((unsigned char *) alternative_move);
if(alternative != NULL){
Ok = bishop_move(alternative,colour,board);
if(Ok){
/* Copy the relevant details. */
move_details->class = alternative->class;
move_details->from_col = alternative->from_col;
move_details->from_rank = alternative->from_rank;
move_details->to_col = alternative->to_col;
move_details->to_rank = alternative->to_rank;
move_details->piece_to_move =
alternative->piece_to_move;
free_move_list(alternative);
move_handled = TRUE;
}
}
}
}
}
if(!move_handled){
/* Pick up any moves not handled as pawn moves.
* This includes algebraic moves that were originally assumed to
* be pawn moves.
*/
switch(class){
case PAWN_MOVE:
case ENPASSANT_PAWN_MOVE:
case PAWN_MOVE_WITH_PROMOTION:
/* No more tries left. */
break;
case PIECE_MOVE:
switch(move_details->piece_to_move){
case KING:
Ok = king_move(move_details,colour,board);
break;
case QUEEN:
Ok = queen_move(move_details,colour,board);
break;
case ROOK:
Ok = rook_move(move_details,colour,board);
break;
case KNIGHT:
Ok = knight_move(move_details,colour,board);
break;
case BISHOP:
Ok = bishop_move(move_details,colour,board);
break;
default:
Ok = FALSE;
fprintf(GlobalState.logfile,"Unknown piece move %s\n",move);
break;
}
break;
case KINGSIDE_CASTLE:
move_details->piece_to_move = KING;
Ok = kingside_castle(move_details,colour,board);
break;
case QUEENSIDE_CASTLE:
move_details->piece_to_move = KING;
Ok = queenside_castle(move_details,colour,board);
break;
case UNKNOWN_MOVE:
Ok = FALSE;
break;
default:
fprintf(GlobalState.logfile,
"Unknown move class in determine_move_details(%d).\n",
move_details->class);
break;
}
}
if(Ok){
/* Fill in the remaining items in move_details. */
short to_r = RankConvert(move_details->to_rank);
short to_c = ColConvert(move_details->to_col);
/* Keep track of any capture. */
if(board->board[to_r][to_c] != EMPTY){
move_details->captured_piece =
EXTRACT_PIECE(board->board[to_r][to_c]);
}
else if(move_details->class == ENPASSANT_PAWN_MOVE){
move_details->captured_piece = PAWN;
}
else{
move_details->captured_piece = EMPTY;
}
}
}
return Ok;
}
/* Generate a list of moves of a king or knight from from_col,from_rank.
* This does not include castling for the king, because it is used
* by the code that looks for ways to escape from check for which
* castling is illegal, of course.
*/
static MovePair *
GenerateSingleMoves(Colour colour,Piece piece,Board *board,Col from_col, Rank from_rank)
{ short from_r = RankConvert(from_rank);
short from_c = ColConvert(from_col);
unsigned ix;
MovePair *moves = NULL;
Colour target_colour = OPPOSITE_COLOUR(colour);
unsigned num_directions = piece == KING?NUM_KING_MOVES:NUM_KNIGHT_MOVES;
const short *Piece_moves = piece == KING?King_moves:Knight_moves;
/* Pick up pairs of offsets from from_r,from_c to look for an
* EMPTY square, or one containing a piece of OPPOSITE_COLOUR(colour).
*/
for(ix = 0; ix < 2*num_directions; ix += 2){
short r = Piece_moves[ix]+from_r;
short c = Piece_moves[ix+1]+from_c;
Piece occupant = board->board[r][c];
Boolean Ok = FALSE;
if(occupant == OFF){
/* Not a valid move. */
}
else if(board->board[r][c] == EMPTY){
Ok = TRUE;
}
else if(EXTRACT_COLOUR(occupant) == target_colour){
Ok = TRUE;
}
else{
}
if(Ok){
/* Fill in the details, and add it to the list. */
moves = append_move_pair(from_col,from_rank,ToCol(c),ToRank(r),moves);
}
}
if(moves != NULL){
moves = exclude_checks(piece,colour,moves,board);
}
return moves;
}
/* Generate a list of moves of a queen, rook or bishop from
* from_col,from_rank.
*/
static MovePair *
GenerateMultipleMoves(Colour colour,Piece piece,
Board *board,Col from_col, Rank from_rank)
{ short from_r = RankConvert(from_rank);
short from_c = ColConvert(from_col);
unsigned ix;
MovePair *moves = NULL;
Colour target_colour = OPPOSITE_COLOUR(colour);
unsigned num_directions = piece == QUEEN?NUM_QUEEN_MOVES:
piece == ROOK?NUM_ROOK_MOVES:NUM_BISHOP_MOVES;
const short *Piece_moves = piece == QUEEN?Queen_moves:
piece == ROOK?Rook_moves:Bishop_moves;
/* Pick up pairs of offsets from from_r,from_c to look for an
* EMPTY square, or one containing a piece of OPPOSITE_COLOUR(colour).
*/
for(ix = 0; ix < 2*num_directions; ix += 2){
short r = Piece_moves[ix]+from_r;
short c = Piece_moves[ix+1]+from_c;
Piece occupant = board->board[r][c];
/* Include EMPTY squares as possible moves. */
while(occupant == EMPTY){
/* Fill in the details, and add it to the list. */
moves = append_move_pair(from_col,from_rank,ToCol(c),ToRank(r),moves);
/* Move on to the next square in this direction. */
r += Piece_moves[ix];
c += Piece_moves[ix+1];
occupant = board->board[r][c];
}
/* We have come up against an obstruction. */
if(occupant == OFF){
/* Not a valid move. */
}
else if(EXTRACT_COLOUR(occupant) == target_colour){
moves = append_move_pair(from_col,from_rank,ToCol(c),ToRank(r),moves);
}
else{
/* Should be a piece of our own colour. */
}
}
if(moves != NULL){
moves = exclude_checks(piece,colour,moves,board);
}
return moves;
}
/* Generate a list of moves of a pawn from from_col,from_rank. */
static MovePair *
GeneratePawnMoves(Colour colour,Board *board,Col from_col, Rank from_rank)
{ MovePair *moves = NULL;
Piece piece = PAWN;
Colour target_colour = OPPOSITE_COLOUR(colour);
/* Determine the direction in which a pawn can move. */
short offset = colour == WHITE? 1 : -1;
short to_r, to_c = ColConvert(from_col);
/* Try single step ahead. */
to_r = RankConvert(from_rank)+offset;
if(board->board[to_r][to_c] == EMPTY){
/* Fill in the details, and add it to the list. */
moves = append_move_pair(from_col,from_rank,ToCol(to_c),ToRank(to_r),moves);
}
if(((colour == WHITE) && (from_rank == FIRSTRANK+1)) ||
((colour == BLACK) && (from_rank == LASTRANK-1))){
/* Try two steps. */
to_r = RankConvert(from_rank)+2*offset;
if(board->board[to_r][to_c] == EMPTY){
moves = append_move_pair(from_col,from_rank,ToCol(to_c),ToRank(to_r),moves);
}
}
/* Try to left. */
to_r = RankConvert(from_rank)+offset;
to_c = ColConvert(from_col)-1;
if(board->board[to_r][to_c] == OFF){
}
else if(board->board[to_r][to_c] == EMPTY){
if(board->EnPassant && (ToRank(to_r) == board->ep_rank) &&
(ToCol(to_c) == board->ep_col)){
moves = append_move_pair(from_col,from_rank,ToCol(to_c),ToRank(to_r),moves);
}
}
else if(EXTRACT_COLOUR(board->board[to_r][to_c]) == target_colour){
moves = append_move_pair(from_col,from_rank,ToCol(to_c),ToRank(to_r),moves);
}
else{
}
/* Try to right. */
to_r = RankConvert(from_rank)+offset;
to_c = ColConvert(from_col)+1;
if(board->board[to_r][to_c] == OFF){
}
else if(board->board[to_r][to_c] == EMPTY){
if(board->EnPassant && (ToRank(to_r) == board->ep_rank) &&
(ToCol(to_c) == board->ep_col)){
moves = append_move_pair(from_col,from_rank,ToCol(to_c),ToRank(to_r),moves);
}
}
else if(EXTRACT_COLOUR(board->board[to_r][to_c]) == target_colour){
moves = append_move_pair(from_col,from_rank,ToCol(to_c),ToRank(to_r),moves);
}
else{
}
if(moves != NULL){
moves = exclude_checks(piece,colour,moves,board);
}
return moves;
}
/* See whether the king of the given colour is in checkmate.
* Assuming that the king is in check, generate all possible moves
* for colour on board until at least one saving move is found.
* Excepted from this are the castling moves (not legal whilst in check)
* and underpromotions (inadequate in thwarting a check).
*/
Boolean
king_is_in_checkmate(Colour colour,Board *board)
{ Rank rank;
Col col;
MovePair *moves = NULL;
Boolean in_checkmate = FALSE;
/* Search the board for pieces of the right colour.
* Keep going until we have exhausted all pieces, or until
* we have found a saving move.
*/
for(rank = LASTRANK; (rank >= FIRSTRANK) && (moves == NULL); rank--){
for(col = FIRSTCOL; (col <= LASTCOL) && (moves == NULL); col++){
short r = RankConvert(rank);
short c = ColConvert(col);
Piece occupant = board->board[r][c];
if((occupant != EMPTY) && (colour == EXTRACT_COLOUR(occupant))){
/* This square is occupied by a piece of the required colour. */
Piece piece = EXTRACT_PIECE(occupant);
switch(piece){
case KING:
case KNIGHT:
moves = GenerateSingleMoves(colour,piece,board,col,rank);
break;
case QUEEN:
case ROOK:
case BISHOP:
moves = GenerateMultipleMoves(colour,piece,board,col,rank);
break;
case PAWN:
moves = GeneratePawnMoves(colour,board,col,rank);
break;
default:
fprintf(GlobalState.logfile,
"Internal error: unknown piece %d in king_is_in_checmate().\n",
piece);
}
}
}
}
if(moves != NULL){
/* No checkmate. Free the move list. */
free_move_pair_list(moves);
}
else{
in_checkmate = TRUE;
}
return in_checkmate;
}
#if INCLUDE_UNUSED_FUNCTIONS
/* Return an approximation of how many moves there are for
* board->to_move on board.
* This may be exact, but I haven't checked.
* This is not currently used, but I found it useful at one
* point for generating game statistics.
*/
static unsigned
approx_how_many_moves(Board *board)
{ Rank rank;
Col col;
Colour colour = board->to_move;
unsigned num_moves = 0;
/* Search the board for pieces of the right colour.
* Keep going until we have exhausted all pieces, or until
* we have found a saving move.
*/
for(rank = LASTRANK; rank >= FIRSTRANK; rank--){
for(col = FIRSTCOL; col <= LASTCOL; col++){
short r = RankConvert(rank);
short c = ColConvert(col);
Piece occupant = board->board[r][c];
MovePair *moves = NULL;
if((occupant != EMPTY) && (colour == EXTRACT_COLOUR(occupant))){
/* This square is occupied by a piece of the required colour. */
Piece piece = EXTRACT_PIECE(occupant);
switch(piece){
case KING:
case KNIGHT:
moves = GenerateSingleMoves(colour,piece,board,col,rank);
break;
case QUEEN:
case ROOK:
case BISHOP:
moves = GenerateMultipleMoves(colour,piece,board,col,rank);
break;
case PAWN:
moves = GeneratePawnMoves(colour,board,col,rank);
break;
default:
fprintf(GlobalState.logfile,
"Internal error: unknown piece %d in king_is_in_checmate().\n",
piece);
}
if(moves != NULL){
/* At least one move. */
MovePair *m;
for(m = moves; m != NULL; m = m->next){
num_moves++;
}
/* Free the move list. */
free_move_pair_list(moves);
}
}
}
}
return num_moves;
}
/* Find all moves for the colour board->to_move on board. */
/* Currently unused. */
static MovePair *
find_all_moves(Board *board)
{ Rank rank;
Col col;
Colour colour = board->to_move;
/* All moves for colour. */
MovePair *all_moves = NULL;
/* Pick up each piece of the required colour. */
for(rank = LASTRANK; rank >= FIRSTRANK; rank--){
short r = RankConvert(rank);
for(col = FIRSTCOL; col <= LASTCOL; col++){
short c = ColConvert(col);
Piece occupant = board->board[r][c];
if((occupant != EMPTY) && (colour == EXTRACT_COLOUR(occupant))){
/* This square is occupied by a piece of the required colour. */
Piece piece = EXTRACT_PIECE(occupant);
/* List of moves for this piece. */
MovePair *moves = NULL;
switch(piece){
case KING:
moves = GenerateSingleMoves(colour,piece,board,col,rank);
/* Add any castling, as this is not covered
* by GenerateSingleMoves.
*/
if(can_castle_kingside(colour,board)){
MovePair *m = (MovePair *)
MallocOrDie(sizeof(MovePair));
m->from_col = 'e';
m->from_rank = rank;
m->to_col = 'g';
m->to_rank = rank;
/* Prepend. */
m->next = moves;
moves = m;
}
if(can_castle_queenside(colour,board)){
MovePair *m = (MovePair *)
MallocOrDie(sizeof(MovePair));
m->from_col = 'e';
m->from_rank = rank;
m->to_col = 'c';
m->to_rank = rank;
/* Prepend. */
m->next = moves;
moves = m;
}
break;
case KNIGHT:
moves = GenerateSingleMoves(colour,piece,board,col,rank);
break;
case QUEEN:
case ROOK:
case BISHOP:
moves = GenerateMultipleMoves(colour,piece,board,col,rank);
break;
case PAWN:
moves = GeneratePawnMoves(colour,board,col,rank);
break;
default:
fprintf(GlobalState.logfile,
"Internal error: unknown piece %d in king_is_in_checmate().\n",
piece);
}
if(moves != NULL){
/* At least one move.
* Append what we have so far to this list.
* Find the last one.
*/
MovePair *m;
for(m = moves; m->next != NULL; m = m->next){
}
m->next = all_moves;
all_moves = moves;
}
}
}
}
return all_moves;
}
#endif
|