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
|
#include "chess.h"
#include "data.h"
/* last modified 01/17/09 */
/*
*******************************************************************************
* *
* InputMove() is responsible for converting a move from a text string to *
* the internal move format. It allows the so-called "reduced algebraic *
* move format" which makes the origin square optional unless required for *
* clarity. It also accepts as little as required to remove ambiguity from *
* the move, by using GenerateMoves() to produce a set of legal moves *
* that the text can be applied against to eliminate those moves not *
* intended. Hopefully, only one move will remain after the elimination *
* and legality checks. *
* *
*******************************************************************************
*/
int InputMove(TREE * RESTRICT tree, char *text, int ply, int wtm, int silent,
int ponder_list) {
int moves[220], *mv, *mvp, *goodmove = 0;
int piece = -1, capture, promote, give_check;
int ffile, frank, tfile, trank;
int current, i, nleft;
char *goodchar, *tc;
char movetext[128];
static const char pieces[15] =
{ ' ', ' ', 'P', 'p', 'N', 'n', 'B', 'b', 'R', 'r',
'Q', 'q', 'K', 'k', '\0'
};
static const char pro_pieces[15] =
{ ' ', ' ', 'P', 'p', 'N', 'n', 'B', 'b', 'R', 'r', 'Q', 'q',
'K', 'k', '\0'
};
/*
************************************************************
* *
* First, we need to strip off the special characters for *
* check, mate, bad move, good move, and such that might *
* come from a PGN input file. *
* *
************************************************************
*/
if ((tc = strchr(text, '!')))
*tc = 0;
if ((tc = strchr(text, '?')))
*tc = 0;
/*
************************************************************
* *
* Check for full coordinate input (f1e1) and handle that *
* if needed. *
* *
************************************************************
*/
if (strlen(text) == 0)
return (0);
if ((text[0] >= 'a') && (text[0] <= 'h') && (text[1] >= '1') &&
(text[1] <= '8') && (text[2] >= 'a') && (text[2] <= 'h') &&
(text[3] >= '1') && (text[3] <= '8'))
return (InputMoveICS(tree, text, ply, wtm, silent, ponder_list));
/*
************************************************************
* *
* Initialize move structure. If we discover a parsing *
* error, this will cause us to return a move of "0" to *
* indicate some sort of error was detected. *
* *
************************************************************
*/
tree->position[MAXPLY] = tree->position[ply];
strcpy(movetext, text);
moves[0] = 0;
piece = 0;
capture = 0;
promote = 0;
give_check = 0;
frank = -1;
ffile = -1;
trank = -1;
tfile = -1;
goodchar = strchr(movetext, '#');
if (goodchar)
*goodchar = 0;
/*
************************************************************
* *
* First we look for castling moves which are a special *
* case with an unusual syntax compared to normal moves. *
* *
************************************************************
*/
if (!strcmp(movetext, "o-o") || !strcmp(movetext, "o-o+") ||
!strcmp(movetext, "O-O") || !strcmp(movetext, "O-O+") ||
!strcmp(movetext, "0-0") || !strcmp(movetext, "0-0+")) {
piece = king;
if (wtm) {
ffile = 4;
frank = 0;
tfile = 6;
trank = 0;
} else {
ffile = 4;
frank = 7;
tfile = 6;
trank = 7;
}
} else if (!strcmp(movetext, "o-o-o") || !strcmp(movetext, "o-o-o+") ||
!strcmp(movetext, "O-O-O") || !strcmp(movetext, "O-O-O+") ||
!strcmp(movetext, "0-0-0") || !strcmp(movetext, "0-0-0+")) {
piece = king;
if (wtm) {
ffile = 4;
frank = 0;
tfile = 2;
trank = 0;
} else {
ffile = 4;
frank = 7;
tfile = 2;
trank = 7;
}
} else {
/*
************************************************************
* *
* OK, it is not a castling move. Check for two "b" *
* characters which might be a piece (bishop) and a file *
* (b-file). The first "b" should be "B" but we allow *
* this to make typing input simpler. *
* *
************************************************************
*/
if ((movetext[0] == 'b') && (movetext[1] == 'b'))
movetext[0] = 'B';
/*
************************************************************
* *
* Check to see if there is a "+" character which means *
* that this move is a check. We can use this to later *
* eliminate all non-checking moves as possibilities. *
* *
************************************************************
*/
if (strchr(movetext, '+')) {
*strchr(movetext, '+') = 0;
give_check = 1;
}
/*
************************************************************
* *
* If this is a promotion, indicated by an "=" in the *
* text, we can pick up the promote-to piece and save it *
* to use later when eliminating moves. *
* *
************************************************************
*/
if (strchr(movetext, '=')) {
goodchar = strchr(movetext, '=');
goodchar++;
promote = (strchr(pro_pieces, *goodchar) - pro_pieces) >> 1;
*strchr(movetext, '=') = 0;
}
/*
************************************************************
* *
* Now for a kludge. ChessBase (and others) can't follow *
* the PGN standard of bxc8=Q for promotion, and instead *
* will produce "bxc8Q" omitting the PGN-standard "=" *
* character. We handle that here so that we can read *
* their non-standard moves. *
* *
************************************************************
*/
else {
char *prom = strchr(pro_pieces, movetext[strlen(movetext) - 1]);
if (prom) {
promote = (prom - pro_pieces) >> 1;
movetext[strlen(movetext) - 1] = 0;
}
}
/*
************************************************************
* *
* Next we extract the last rank/file designators from *
* the text, since the destination is required for all *
* valid non-castling moves. Note that we might not have *
* both a rank and file but we must have at least one. *
* *
************************************************************
*/
current = strlen(movetext) - 1;
trank = movetext[current] - '1';
if ((trank >= 0) && (trank <= 7))
movetext[current] = 0;
else
trank = -1;
current = strlen(movetext) - 1;
tfile = movetext[current] - 'a';
if ((tfile >= 0) && (tfile <= 7))
movetext[current] = 0;
else
tfile = -1;
if (strlen(movetext)) {
/*
************************************************************
* *
* The first character is the moving piece, unless it is *
* a pawn. In this case, the moving piece is omitted and *
* we know what it has to be. *
* *
************************************************************
*/
if (strchr(" PpNnBBRrQqKk", *movetext)) {
piece = (strchr(pieces, movetext[0]) - pieces) >> 1;
for (i = 0; i < (int) strlen(movetext); i++)
movetext[i] = movetext[i + 1];
}
/*
************************************************************
* *
* It is also possible that this move is a capture, which *
* is indicated by a "x" between either the source and *
* destination squares, or between the moving piece and *
* the destination. *
* *
************************************************************
*/
if ((strlen(movetext)) && (movetext[strlen(movetext) - 1] == 'x')) {
capture = 1;
movetext[strlen(movetext) - 1] = 0;
} else
capture = 0;
/*
************************************************************
* *
* It is possible to have no source square, but we could *
* have a complete algebraic square designation, or just *
* rank or file, needed to disambiguate the move. *
* *
************************************************************
*/
if (strlen(movetext)) {
ffile = movetext[0] - 'a';
if ((ffile < 0) || (ffile > 7)) {
ffile = -1;
frank = movetext[0] - '1';
if ((frank < 0) || (frank > 7))
piece = -1;
} else {
if (strlen(movetext) == 2) {
frank = movetext[1] - '1';
if ((frank < 0) || (frank > 7))
piece = -1;
}
}
}
}
}
/*
************************************************************
* *
* Now for the easy part. We first generate all moves *
* if not pondering, or else use a pre-computed list of *
* moves (if pondering) since the board position is not *
* correct for move input analysis. We then loop through *
* the list of moves, using the information we extracted *
* previously, and eliminate all moves that are (a) the *
* wrong piece type; (b) wrong source or destination *
* square; (c) wrong promotion type; (d) should be a *
* capture/check/promotion but is not or vice-versa. *
* *
************************************************************
*/
if (!piece)
piece = 1;
if (!ponder_list) {
mvp = GenerateCaptures(tree, MAXPLY, wtm, moves);
mvp = GenerateNoncaptures(tree, MAXPLY, wtm, mvp);
} else {
for (i = 0; i < num_ponder_moves; i++)
moves[i] = ponder_moves[i];
mvp = moves + num_ponder_moves;
}
for (mv = &moves[0]; mv < mvp; mv++) {
if (piece && (Piece(*mv) != piece))
*mv = 0;
if ((ffile >= 0) && (File(From(*mv)) != ffile))
*mv = 0;
if (capture && (!Captured(*mv)))
*mv = 0;
if (promote && (Promote(*mv) != promote))
*mv = 0;
if ((frank >= 0) && (Rank(From(*mv)) != frank))
*mv = 0;
if ((tfile >= 0) && (File(To(*mv)) != tfile))
*mv = 0;
if ((trank >= 0) && (Rank(To(*mv)) != trank))
*mv = 0;
if (!ponder_list && *mv) {
MakeMove(tree, MAXPLY, *mv, wtm);
if (Check(wtm) || (give_check && !Check(Flip(wtm)))) {
UnmakeMove(tree, MAXPLY, *mv, wtm);
*mv = 0;
} else
UnmakeMove(tree, MAXPLY, *mv, wtm);
}
}
/*
************************************************************
* *
* Once we have completed eliminating incorrect moves, we *
* hope to have exactly one move left. If none or left, *
* the entered move is illegal. If more than one is *
* left, the move entered is ambiguous. If appropriate, *
* we output some sort of diagnostic message and then *
* return. *
* *
************************************************************
*/
nleft = 0;
for (mv = &moves[0]; mv < mvp; mv++) {
if (*mv) {
nleft++;
goodmove = mv;
}
}
if (nleft == 1)
return (*goodmove);
if (!silent) {
if (nleft == 0)
Print(4095, "Illegal move: %s\n", text);
else if (piece < 0)
Print(4095, "Illegal move (unrecognizable): %s\n", text);
else
Print(4095, "Illegal move (ambiguous): %s\n", text);
}
return (0);
}
/* last modified 01/17/09 */
/*
*******************************************************************************
* *
* InputMoveICS() is responsible for converting a move from the ics format *
* [from][to][promote] to the program's internal format. *
* *
*******************************************************************************
*/
int InputMoveICS(TREE * RESTRICT tree, char *text, int ply, int wtm,
int silent, int ponder_list) {
int moves[220], *mv, *mvp, *goodmove = 0;
int piece = -1, promote;
int ffile, frank, tfile, trank;
int i, nleft;
char movetext[128];
static const char pieces[15] =
{ ' ', ' ', 'P', 'p', 'N', 'n', 'B', 'b', 'R', 'r',
'Q', 'q', 'K', 'k', '\0'
};
/*
************************************************************
* *
* Initialize move structure. If we discover a parsing *
* error, this will cause us to return a move of "0" to *
* indicate some sort of error was detected. *
* *
************************************************************
*/
if (strlen(text) == 0)
return (0);
tree->position[MAXPLY] = tree->position[ply];
strcpy(movetext, text);
moves[0] = 0;
promote = 0;
/*
************************************************************
* *
* First we look for castling moves which are a special *
* case with an unusual syntax compared to normal moves. *
* *
************************************************************
*/
if (!strcmp(movetext, "o-o") || !strcmp(movetext, "O-O") ||
!strcmp(movetext, "0-0")) {
piece = king;
if (wtm) {
ffile = 4;
frank = 0;
tfile = 6;
trank = 0;
} else {
ffile = 4;
frank = 7;
tfile = 6;
trank = 7;
}
} else if (!strcmp(movetext, "o-o-o") || !strcmp(movetext, "O-O-O") ||
!strcmp(movetext, "0-0-0")) {
piece = king;
if (wtm) {
ffile = 4;
frank = 0;
tfile = 2;
trank = 0;
} else {
ffile = 4;
frank = 7;
tfile = 2;
trank = 7;
}
} else {
/*
************************************************************
* *
* Next we extract both rank/file designators from the *
* text, since the destination is required for all valid *
* non-castling moves. *
* *
************************************************************
*/
ffile = movetext[0] - 'a';
frank = movetext[1] - '1';
tfile = movetext[2] - 'a';
trank = movetext[3] - '1';
/*
************************************************************
* *
* If this is a promotion, indicated by an "=" in the *
* text, we can pick up the promote-to piece and save it *
* to use later when eliminating moves. *
* *
************************************************************
*/
if (movetext[4] == '=')
promote = (strchr(pieces, movetext[5]) - pieces) >> 1;
else if ((movetext[4] != 0) && (movetext[4] != ' '))
promote = (strchr(pieces, movetext[4]) - pieces) >> 1;
}
/*
************************************************************
* *
* Now for the easy part. We first generate all moves *
* if not pondering, or else use a pre-computed list of *
* moves (if pondering) since the board position is not *
* correct for move input analysis. We then loop through *
* the list of moves, using the information we extracted *
* previously, and eliminate all moves that are (a) the *
* wrong piece type; (b) wrong source or destination *
* square; (c) wrong promotion type; (d) should be a *
* capture/check/promotion but is not or vice-versa. *
* *
************************************************************
*/
if (!ponder_list) {
mvp = GenerateCaptures(tree, MAXPLY, wtm, moves);
mvp = GenerateNoncaptures(tree, MAXPLY, wtm, mvp);
} else {
for (i = 0; i < num_ponder_moves; i++)
moves[i] = ponder_moves[i];
mvp = moves + num_ponder_moves;
}
for (mv = &moves[0]; mv < mvp; mv++) {
if (Promote(*mv) != promote)
*mv = 0;
if (Rank(From(*mv)) != frank)
*mv = 0;
if (File(From(*mv)) != ffile)
*mv = 0;
if (Rank(To(*mv)) != trank)
*mv = 0;
if (File(To(*mv)) != tfile)
*mv = 0;
if (!ponder_list && *mv) {
MakeMove(tree, MAXPLY, *mv, wtm);
if (Check(wtm)) {
UnmakeMove(tree, MAXPLY, *mv, wtm);
*mv = 0;
} else
UnmakeMove(tree, MAXPLY, *mv, wtm);
}
}
/*
************************************************************
* *
* Once we have completed eliminating incorrect moves, we *
* hope to have exactly one move left. If none or left, *
* the entered move is illegal. If more than one is *
* left, the move entered is ambiguous. If appropriate, *
* we output some sort of diagnostic message and then *
* return. *
* *
************************************************************
*/
nleft = 0;
for (mv = &moves[0]; mv < mvp; mv++) {
if (*mv) {
nleft++;
goodmove = mv;
}
}
if (nleft == 1)
return (*goodmove);
if (!silent) {
if (nleft == 0)
Print(4095, "Illegal move: %s\n", text);
else if (piece < 0)
Print(4095, "Illegal move (unrecognizable): %s\n", text);
else
Print(4095, "Illegal move (ambiguous): %s\n", text);
}
return (0);
}
|