src/chess/rules.c

Wed, 11 Jun 2014 15:38:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 11 Jun 2014 15:38:01 +0200
changeset 48
0cedda2544da
parent 47
d726e4b46c33
child 49
02c509a44e98
permissions
-rw-r--r--

added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate

universe@19 1 /*
universe@19 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@19 3 *
universe@19 4 * Copyright 2014 Mike Becker. All rights reserved.
universe@19 5 *
universe@19 6 * Redistribution and use in source and binary forms, with or without
universe@19 7 * modification, are permitted provided that the following conditions are met:
universe@19 8 *
universe@19 9 * 1. Redistributions of source code must retain the above copyright
universe@19 10 * notice, this list of conditions and the following disclaimer.
universe@19 11 *
universe@19 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@19 13 * notice, this list of conditions and the following disclaimer in the
universe@19 14 * documentation and/or other materials provided with the distribution.
universe@19 15 *
universe@19 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@19 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@19 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@19 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@19 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@19 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@19 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@19 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@19 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@19 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@19 26 * POSSIBILITY OF SUCH DAMAGE.
universe@19 27 *
universe@19 28 */
universe@19 29
universe@19 30 #include "rules.h"
universe@19 31 #include "chess.h"
universe@19 32 #include <string.h>
universe@23 33 #include <stdlib.h>
universe@36 34 #include <sys/time.h>
universe@23 35
universe@48 36 static GameState gamestate_copy_sim(GameState *gamestate) {
universe@48 37 GameState simulation = *gamestate;
universe@48 38 if (simulation.lastmove) {
universe@48 39 MoveList *lastmovecopy = malloc(sizeof(MoveList));
universe@48 40 *lastmovecopy = *(simulation.lastmove);
universe@48 41 simulation.movelist = simulation.lastmove = lastmovecopy;
universe@48 42 }
universe@48 43
universe@48 44 return simulation;
universe@48 45 }
universe@48 46
universe@23 47 void gamestate_cleanup(GameState *gamestate) {
universe@23 48 MoveList *elem;
universe@23 49 elem = gamestate->movelist;
universe@23 50 while (elem) {
universe@23 51 MoveList *cur = elem;
universe@23 52 elem = elem->next;
universe@23 53 free(cur);
universe@23 54 };
universe@23 55 }
universe@23 56
universe@23 57 static void addmove(GameState* gamestate, Move *move) {
universe@23 58 MoveList *elem = malloc(sizeof(MoveList));
universe@23 59 elem->next = NULL;
universe@23 60 elem->move = *move;
universe@23 61
universe@40 62 struct timeval curtimestamp;
universe@40 63 gettimeofday(&curtimestamp, NULL);
universe@40 64 elem->move.timestamp.tv_sec = curtimestamp.tv_sec;
universe@40 65 elem->move.timestamp.tv_usec = curtimestamp.tv_usec;
universe@33 66
universe@23 67 if (gamestate->lastmove) {
universe@40 68 struct movetimeval *lasttstamp = &(gamestate->lastmove->move.timestamp);
universe@40 69 uint64_t sec = curtimestamp.tv_sec - lasttstamp->tv_sec;
universe@36 70 suseconds_t micros;
universe@40 71 if (curtimestamp.tv_usec < lasttstamp->tv_usec) {
universe@40 72 micros = 1e6L-(lasttstamp->tv_usec - curtimestamp.tv_usec);
universe@33 73 sec--;
universe@33 74 } else {
universe@40 75 micros = curtimestamp.tv_usec - lasttstamp->tv_usec;
universe@33 76 }
universe@33 77
universe@33 78 elem->move.movetime.tv_sec = sec;
universe@36 79 elem->move.movetime.tv_usec = micros;
universe@33 80
universe@23 81 gamestate->lastmove->next = elem;
universe@23 82 gamestate->lastmove = elem;
universe@23 83 } else {
universe@36 84 elem->move.movetime.tv_usec = 0;
universe@33 85 elem->move.movetime.tv_sec = 0;
universe@23 86 gamestate->movelist = gamestate->lastmove = elem;
universe@23 87 }
universe@23 88 }
universe@19 89
universe@19 90 char getpiecechr(uint8_t piece) {
universe@19 91 switch (piece & PIECE_MASK) {
universe@19 92 case ROOK: return 'R';
universe@19 93 case KNIGHT: return 'N';
universe@19 94 case BISHOP: return 'B';
universe@19 95 case QUEEN: return 'Q';
universe@19 96 case KING: return 'K';
universe@19 97 default: return '\0';
universe@19 98 }
universe@19 99 }
universe@19 100
universe@19 101 uint8_t getpiece(char c) {
universe@19 102 switch (c) {
universe@19 103 case 'R': return ROOK;
universe@19 104 case 'N': return KNIGHT;
universe@19 105 case 'B': return BISHOP;
universe@19 106 case 'Q': return QUEEN;
universe@19 107 case 'K': return KING;
universe@19 108 default: return 0;
universe@19 109 }
universe@19 110 }
universe@19 111
universe@23 112 void apply_move(GameState *gamestate, Move *move) {
universe@19 113 uint8_t piece = move->piece & PIECE_MASK;
universe@19 114 uint8_t color = move->piece & COLOR_MASK;
universe@19 115
universe@19 116 /* en passant capture */
universe@19 117 if (move->capture && piece == PAWN &&
universe@23 118 mdst(gamestate->board, move) == 0) {
universe@23 119 gamestate->board[move->fromrow][move->tofile] = 0;
universe@19 120 }
universe@19 121
universe@19 122 /* remove old en passant threats */
universe@19 123 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@23 124 gamestate->board[3][file] &= ~ENPASSANT_THREAT;
universe@23 125 gamestate->board[4][file] &= ~ENPASSANT_THREAT;
universe@19 126 }
universe@19 127
universe@19 128 /* add new en passant threat */
universe@19 129 if (piece == PAWN && (
universe@19 130 (move->fromrow == 1 && move->torow == 3) ||
universe@19 131 (move->fromrow == 6 && move->torow == 4))) {
universe@19 132 move->piece |= ENPASSANT_THREAT;
universe@19 133 }
universe@19 134
universe@19 135 /* move (and maybe capture or promote) */
universe@23 136 msrc(gamestate->board, move) = 0;
universe@19 137 if (move->promotion) {
universe@23 138 mdst(gamestate->board, move) = move->promotion;
universe@19 139 } else {
universe@23 140 mdst(gamestate->board, move) = move->piece;
universe@19 141 }
universe@19 142
universe@19 143 /* castling */
universe@19 144 if (piece == KING &&
universe@19 145 move->fromfile == fileidx('e')) {
universe@19 146
universe@19 147 if (move->tofile == fileidx('g')) {
universe@23 148 gamestate->board[move->torow][fileidx('h')] = 0;
universe@23 149 gamestate->board[move->torow][fileidx('f')] = color|ROOK;
universe@19 150 } else if (move->tofile == fileidx('c')) {
universe@23 151 gamestate->board[move->torow][fileidx('a')] = 0;
universe@23 152 gamestate->board[move->torow][fileidx('d')] = color|ROOK;
universe@19 153 }
universe@19 154 }
universe@28 155
universe@23 156 addmove(gamestate, move);
universe@19 157 }
universe@19 158
universe@48 159 static int validate_move_rules(GameState *gamestate, Move *move) {
universe@19 160 /* validate indices (don't trust opponent) */
universe@19 161 if (!chkidx(move)) {
universe@48 162 return INVALID_POSITION;
universe@19 163 }
universe@19 164
universe@21 165 /* must move */
universe@21 166 if (move->fromfile == move->tofile && move->fromrow == move->torow) {
universe@48 167 return INVALID_MOVE_SYNTAX;
universe@21 168 }
universe@21 169
universe@19 170 /* does piece exist */
universe@47 171 if ((msrc(gamestate->board, move)&(PIECE_MASK|COLOR_MASK))
universe@47 172 != (move->piece&(PIECE_MASK|COLOR_MASK))) {
universe@48 173 return INVALID_POSITION;
universe@29 174 }
universe@19 175
universe@19 176 /* can't capture own pieces */
universe@29 177 if ((mdst(gamestate->board, move) & COLOR_MASK)
universe@47 178 == (move->piece & COLOR_MASK)) {
universe@48 179 return RULES_VIOLATED;
universe@47 180 }
universe@47 181
universe@47 182 /* must capture, if and only if destination is occupied */
universe@47 183 if ((mdst(gamestate->board, move) == 0 && move->capture) ||
universe@47 184 (mdst(gamestate->board, move) != 0 && !move->capture)) {
universe@48 185 return INVALID_MOVE_SYNTAX;
universe@19 186 }
universe@19 187
universe@19 188 /* validate individual rules */
universe@48 189 _Bool chkrules;
universe@19 190 switch (move->piece & PIECE_MASK) {
universe@19 191 case PAWN:
universe@48 192 chkrules = pawn_chkrules(gamestate, move) &&
universe@29 193 !pawn_isblocked(gamestate, move);
universe@48 194 break;
universe@19 195 case ROOK:
universe@48 196 chkrules = rook_chkrules(move) &&
universe@29 197 !rook_isblocked(gamestate, move);
universe@48 198 break;
universe@19 199 case KNIGHT:
universe@48 200 chkrules = knight_chkrules(move); /* knight is never blocked */
universe@48 201 break;
universe@19 202 case BISHOP:
universe@48 203 chkrules = bishop_chkrules(move) &&
universe@29 204 !bishop_isblocked(gamestate, move);
universe@48 205 break;
universe@19 206 case QUEEN:
universe@48 207 chkrules = queen_chkrules(move) &&
universe@29 208 !queen_isblocked(gamestate, move);
universe@48 209 break;
universe@19 210 case KING:
universe@48 211 chkrules = king_chkrules(gamestate, move) &&
universe@29 212 !king_isblocked(gamestate, move);
universe@48 213 break;
universe@19 214 default:
universe@48 215 return INVALID_MOVE_SYNTAX;
universe@19 216 }
universe@48 217
universe@48 218 return chkrules ? VALID_MOVE_SEMANTICS : RULES_VIOLATED;
universe@29 219 }
universe@29 220
universe@48 221 int validate_move(GameState *gamestate, Move *move) {
universe@19 222
universe@48 223 int result = validate_move_rules(gamestate, move);
universe@29 224
universe@29 225 /* cancel processing to save resources */
universe@48 226 if (result != VALID_MOVE_SEMANTICS) {
universe@48 227 return result;
universe@25 228 }
universe@25 229
universe@28 230 /* find kings for check validation */
universe@29 231 uint8_t piececolor = (move->piece & COLOR_MASK);
universe@29 232
universe@28 233 uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0;
universe@28 234 for (uint8_t row = 0 ; row < 8 ; row++) {
universe@28 235 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@28 236 if (gamestate->board[row][file] ==
universe@28 237 (piececolor == WHITE?WKING:BKING)) {
universe@28 238 mykingfile = file;
universe@28 239 mykingrow = row;
universe@28 240 } else if (gamestate->board[row][file] ==
universe@28 241 (piececolor == WHITE?BKING:WKING)) {
universe@28 242 opkingfile = file;
universe@28 243 opkingrow = row;
universe@28 244 }
universe@28 245 }
universe@28 246 }
universe@28 247
universe@48 248 /* simulate move for check validation */
universe@48 249 GameState simulation = gamestate_copy_sim(gamestate);
universe@47 250 Move simmove = *move;
universe@47 251 apply_move(&simulation, &simmove);
universe@28 252
universe@28 253 /* don't move into or stay in check position */
universe@28 254 if (is_covered(&simulation, mykingrow, mykingfile,
universe@28 255 opponent_color(piececolor))) {
universe@48 256
universe@48 257 gamestate_cleanup(&simulation);
universe@48 258 if ((move->piece & PIECE_MASK) == KING) {
universe@48 259 return KING_MOVES_INTO_CHECK;
universe@48 260 } else {
universe@48 261 /* last move is always not null in this case */
universe@48 262 return gamestate->lastmove->move.check ?
universe@48 263 KING_IN_CHECK : PIECE_PINNED;
universe@48 264 }
universe@28 265 }
universe@19 266
universe@25 267 /* correct check and checkmate flags (move is still valid) */
universe@29 268 Move threats[16];
universe@29 269 uint8_t threatcount;
universe@29 270 move->check = get_threats(&simulation, opkingrow, opkingfile,
universe@29 271 piececolor, threats, &threatcount);
universe@19 272
universe@28 273 if (move->check) {
universe@28 274 /* determine possible escape fields */
universe@28 275 _Bool canescape = 0;
universe@28 276 for (int dr = -1 ; dr <= 1 && !canescape ; dr++) {
universe@28 277 for (int df = -1 ; df <= 1 && !canescape ; df++) {
universe@28 278 if (!(dr == 0 && df == 0) &&
universe@28 279 isidx(opkingrow + dr) && isidx(opkingfile + df)) {
universe@28 280
universe@28 281 /* escape field neither blocked nor covered */
universe@28 282 if ((simulation.board[opkingrow + dr][opkingfile + df]
universe@28 283 & COLOR_MASK) != opponent_color(piececolor)) {
universe@28 284 canescape |= !is_covered(&simulation,
universe@28 285 opkingrow + dr, opkingfile + df, piececolor);
universe@28 286 }
universe@28 287 }
universe@28 288 }
universe@28 289 }
universe@29 290 /* can't escape, can he capture? */
universe@29 291 if (!canescape && threatcount == 1) {
universe@29 292 canescape = is_attacked(&simulation, threats[0].fromrow,
universe@29 293 threats[0].fromfile, opponent_color(piececolor));
universe@29 294 }
universe@29 295
universe@29 296 /* can't capture, can he block? */
universe@29 297 if (!canescape && threatcount == 1) {
universe@29 298 Move *threat = &(threats[0]);
universe@29 299 uint8_t threatpiece = threat->piece & PIECE_MASK;
universe@29 300
universe@29 301 /* knight, pawns and the king cannot be blocked */
universe@29 302 if (threatpiece == BISHOP || threatpiece == ROOK
universe@29 303 || threatpiece == QUEEN) {
universe@29 304 if (threat->fromrow == threat->torow) {
universe@29 305 /* rook aspect (on row) */
universe@29 306 int d = threat->tofile > threat->fromfile ? 1 : -1;
universe@29 307 uint8_t file = threat->fromfile;
universe@29 308 while (!canescape && file != threat->tofile - d) {
universe@29 309 file += d;
universe@29 310 canescape |= is_protected(&simulation,
universe@29 311 threat->torow, file, opponent_color(piececolor));
universe@29 312 }
universe@29 313 } else if (threat->fromfile == threat->tofile) {
universe@29 314 /* rook aspect (on file) */
universe@29 315 int d = threat->torow > threat->fromrow ? 1 : -1;
universe@29 316 uint8_t row = threat->fromrow;
universe@29 317 while (!canescape && row != threat->torow - d) {
universe@29 318 row += d;
universe@29 319 canescape |= is_protected(&simulation,
universe@29 320 row, threat->tofile, opponent_color(piececolor));
universe@29 321 }
universe@29 322 } else {
universe@29 323 /* bishop aspect */
universe@29 324 int dr = move->torow > move->fromrow ? 1 : -1;
universe@29 325 int df = move->tofile > move->fromfile ? 1 : -1;
universe@29 326
universe@29 327 uint8_t row = move->fromrow;
universe@29 328 uint8_t file = move->fromfile;
universe@29 329 while (!canescape && file != move->tofile - df
universe@29 330 && row != move->torow - dr) {
universe@29 331 row += dr;
universe@29 332 file += df;
universe@29 333 canescape |= is_protected(&simulation, row, file,
universe@29 334 opponent_color(piececolor));
universe@29 335 }
universe@29 336 }
universe@29 337 }
universe@29 338 }
universe@29 339
universe@28 340 if (!canescape) {
universe@29 341 gamestate->checkmate = 1;
universe@28 342 }
universe@28 343 }
universe@28 344
universe@48 345 gamestate_cleanup(&simulation);
universe@48 346
universe@48 347 return VALID_MOVE_SEMANTICS;
universe@19 348 }
universe@19 349
universe@47 350 _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file,
universe@47 351 uint8_t color, Move *threats, uint8_t *threatcount) {
universe@47 352 Move candidates[32];
universe@47 353 int candidatecount = 0;
universe@47 354 for (uint8_t r = 0 ; r < 8 ; r++) {
universe@47 355 for (uint8_t f = 0 ; f < 8 ; f++) {
universe@47 356 if ((gamestate->board[r][f] & COLOR_MASK) == color) {
universe@47 357 // non-capturing move
universe@47 358 memset(&(candidates[candidatecount]), 0, sizeof(Move));
universe@47 359 candidates[candidatecount].piece = gamestate->board[r][f];
universe@47 360 candidates[candidatecount].fromrow = r;
universe@47 361 candidates[candidatecount].fromfile = f;
universe@47 362 candidates[candidatecount].torow = row;
universe@47 363 candidates[candidatecount].tofile = file;
universe@47 364 candidatecount++;
universe@47 365
universe@47 366 // capturing move
universe@47 367 memcpy(&(candidates[candidatecount]),
universe@47 368 &(candidates[candidatecount-1]), sizeof(Move));
universe@47 369 candidates[candidatecount].capture = 1;
universe@47 370 candidatecount++;
universe@47 371 }
universe@47 372 }
universe@47 373 }
universe@47 374
universe@47 375 if (threatcount) {
universe@47 376 *threatcount = 0;
universe@47 377 }
universe@47 378
universe@47 379
universe@47 380 _Bool result = 0;
universe@47 381
universe@47 382 for (int i = 0 ; i < candidatecount ; i++) {
universe@48 383 if (validate_move_rules(gamestate, &(candidates[i]))
universe@48 384 == VALID_MOVE_SEMANTICS) {
universe@47 385 result = 1;
universe@47 386 if (threats && threatcount) {
universe@47 387 threats[(*threatcount)++] = candidates[i];
universe@47 388 }
universe@47 389 }
universe@47 390 }
universe@47 391
universe@47 392 return result;
universe@47 393 }
universe@47 394
universe@47 395 _Bool is_pinned(GameState *gamestate, Move *move) {
universe@47 396 uint8_t color = move->piece & COLOR_MASK;
universe@47 397
universe@47 398 uint8_t kingfile = 0, kingrow = 0;
universe@47 399 for (uint8_t row = 0 ; row < 8 ; row++) {
universe@47 400 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@47 401 if (gamestate->board[row][file] == (color|KING)) {
universe@47 402 kingfile = file;
universe@47 403 kingrow = row;
universe@47 404 }
universe@47 405 }
universe@47 406 }
universe@47 407
universe@48 408 GameState simulation = gamestate_copy_sim(gamestate);
universe@47 409 Move simmove = *move;
universe@47 410 apply_move(&simulation, &simmove);
universe@48 411 _Bool covered = is_covered(&simulation,
universe@48 412 kingrow, kingfile, opponent_color(color));
universe@48 413 gamestate_cleanup(&simulation);
universe@48 414
universe@48 415 return covered;
universe@47 416 }
universe@47 417
universe@47 418 _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file,
universe@47 419 uint8_t color, Move *threats, uint8_t *threatcount) {
universe@47 420
universe@47 421 if (threatcount) {
universe@47 422 *threatcount = 0;
universe@47 423 }
universe@47 424
universe@47 425 Move candidates[16];
universe@47 426 uint8_t candidatecount;
universe@47 427 if (get_threats(gamestate, row, file, color, candidates, &candidatecount)) {
universe@47 428
universe@47 429 _Bool result = 0;
universe@47 430 uint8_t kingfile = 0, kingrow = 0;
universe@47 431 for (uint8_t row = 0 ; row < 8 ; row++) {
universe@47 432 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@47 433 if (gamestate->board[row][file] == (color|KING)) {
universe@47 434 kingfile = file;
universe@47 435 kingrow = row;
universe@47 436 }
universe@47 437 }
universe@47 438 }
universe@47 439
universe@47 440 for (uint8_t i = 0 ; i < candidatecount ; i++) {
universe@48 441 GameState simulation = gamestate_copy_sim(gamestate);
universe@47 442 Move simmove = candidates[i];
universe@47 443 apply_move(&simulation, &simmove);
universe@47 444 if (!is_covered(&simulation, kingrow, kingfile,
universe@47 445 opponent_color(color))) {
universe@47 446 result = 1;
universe@47 447 if (threats && threatcount) {
universe@47 448 threats[(*threatcount)++] = candidates[i];
universe@47 449 }
universe@47 450 }
universe@48 451 gamestate_cleanup(&simulation);
universe@47 452 }
universe@47 453
universe@47 454 return result;
universe@47 455 } else {
universe@47 456 return 0;
universe@47 457 }
universe@47 458 }
universe@48 459
universe@47 460 static int getlocation(GameState *gamestate, Move *move) {
universe@47 461
universe@47 462 uint8_t color = move->piece & COLOR_MASK;
universe@47 463 _Bool incheck = gamestate->lastmove?gamestate->lastmove->move.check:0;
universe@47 464
universe@47 465 Move threats[16], *threat = NULL;
universe@47 466 uint8_t threatcount;
universe@47 467
universe@47 468 if (get_threats(gamestate, move->torow, move->tofile, color,
universe@47 469 threats, &threatcount)) {
universe@47 470
universe@47 471 // find threats for the specified position
universe@47 472 for (uint8_t i = 0 ; i < threatcount ; i++) {
universe@47 473 if ((threats[i].piece & (PIECE_MASK | COLOR_MASK))
universe@47 474 == move->piece &&
universe@47 475 (move->fromrow == POS_UNSPECIFIED ||
universe@47 476 move->fromrow == threats[i].fromrow) &&
universe@47 477 (move->fromfile == POS_UNSPECIFIED ||
universe@47 478 move->fromfile == threats[i].fromfile)) {
universe@47 479
universe@47 480 if (threat) {
universe@47 481 return AMBIGUOUS_MOVE;
universe@47 482 } else {
universe@47 483 threat = &(threats[i]);
universe@47 484 }
universe@47 485 }
universe@47 486 }
universe@47 487
universe@47 488 // can't threaten specified position
universe@47 489 if (!threat) {
universe@47 490 return INVALID_POSITION;
universe@47 491 }
universe@47 492
universe@47 493 // found threat is no real threat
universe@47 494 if (is_pinned(gamestate, threat)) {
universe@47 495 return incheck?KING_IN_CHECK:PIECE_PINNED;
universe@47 496 } else {
universe@47 497 memcpy(move, threat, sizeof(Move));
universe@47 498 return VALID_MOVE_SYNTAX;
universe@47 499 }
universe@47 500 } else {
universe@47 501 return INVALID_POSITION;
universe@47 502 }
universe@47 503 }
universe@47 504
universe@23 505 int eval_move(GameState *gamestate, char *mstr, Move *move) {
universe@19 506 memset(move, 0, sizeof(Move));
universe@19 507 move->fromfile = POS_UNSPECIFIED;
universe@19 508 move->fromrow = POS_UNSPECIFIED;
universe@19 509
universe@19 510 size_t len = strlen(mstr);
universe@48 511 if (len < 1 || len > 6) {
universe@48 512 return INVALID_MOVE_SYNTAX;
universe@48 513 }
universe@19 514
universe@19 515 /* evaluate check/checkmate flags */
universe@19 516 if (mstr[len-1] == '+') {
universe@19 517 len--; mstr[len] = '\0';
universe@19 518 move->check = 1;
universe@19 519 } else if (mstr[len-1] == '#') {
universe@19 520 len--; mstr[len] = '\0';
universe@27 521 /* ignore - validation should set game state */
universe@19 522 }
universe@19 523
universe@19 524 /* evaluate promotion */
universe@19 525 if (len > 3 && mstr[len-2] == '=') {
universe@19 526 move->promotion = getpiece(mstr[len-1]);
universe@19 527 if (!move->promotion) {
universe@19 528 return INVALID_MOVE_SYNTAX;
universe@19 529 } else {
universe@23 530 move->promotion |= gamestate->mycolor;
universe@19 531 len -= 2;
universe@19 532 mstr[len] = 0;
universe@19 533 }
universe@19 534 }
universe@19 535
universe@19 536 if (len == 2) {
universe@19 537 /* pawn move (e.g. "e4") */
universe@19 538 move->piece = PAWN;
universe@19 539 move->tofile = fileidx(mstr[0]);
universe@19 540 move->torow = rowidx(mstr[1]);
universe@19 541 } else if (len == 3) {
universe@19 542 if (strcmp(mstr, "O-O") == 0) {
universe@19 543 /* king side castling */
universe@19 544 move->piece = KING;
universe@19 545 move->fromfile = fileidx('e');
universe@19 546 move->tofile = fileidx('g');
universe@23 547 move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7;
universe@19 548 } else {
universe@19 549 /* move (e.g. "Nf3") */
universe@19 550 move->piece = getpiece(mstr[0]);
universe@19 551 move->tofile = fileidx(mstr[1]);
universe@19 552 move->torow = rowidx(mstr[2]);
universe@19 553 }
universe@19 554 } else if (len == 4) {
universe@19 555 move->piece = getpiece(mstr[0]);
universe@19 556 if (!move->piece) {
universe@19 557 move->piece = PAWN;
universe@19 558 move->fromfile = fileidx(mstr[0]);
universe@19 559 }
universe@19 560 if (mstr[1] == 'x') {
universe@19 561 /* capture (e.g. "Nxf3", "dxe5") */
universe@19 562 move->capture = 1;
universe@19 563 } else {
universe@19 564 /* move (e.g. "Ndf3", "N2c3", "e2e4") */
universe@19 565 if (isfile(mstr[1])) {
universe@19 566 move->fromfile = fileidx(mstr[1]);
universe@19 567 if (move->piece == PAWN) {
universe@19 568 move->piece = 0;
universe@19 569 }
universe@19 570 } else {
universe@19 571 move->fromrow = rowidx(mstr[1]);
universe@19 572 }
universe@19 573 }
universe@19 574 move->tofile = fileidx(mstr[2]);
universe@19 575 move->torow = rowidx(mstr[3]);
universe@19 576 } else if (len == 5) {
universe@19 577 if (strcmp(mstr, "O-O-O") == 0) {
universe@19 578 /* queen side castling "O-O-O" */
universe@19 579 move->piece = KING;
universe@19 580 move->fromfile = fileidx('e');
universe@19 581 move->tofile = fileidx('c');
universe@23 582 move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7;
universe@19 583 } else {
universe@19 584 move->piece = getpiece(mstr[0]);
universe@19 585 if (mstr[2] == 'x') {
universe@19 586 move->capture = 1;
universe@19 587 if (move->piece) {
universe@19 588 /* capture (e.g. "Ndxf3") */
universe@19 589 move->fromfile = fileidx(mstr[1]);
universe@19 590 } else {
universe@19 591 /* long notation capture (e.g. "e5xf6") */
universe@19 592 move->piece = PAWN;
universe@19 593 move->fromfile = fileidx(mstr[0]);
universe@19 594 move->fromrow = rowidx(mstr[1]);
universe@19 595 }
universe@19 596 } else {
universe@19 597 /* long notation move (e.g. "Nc5a4") */
universe@19 598 move->fromfile = fileidx(mstr[1]);
universe@19 599 move->fromrow = rowidx(mstr[2]);
universe@19 600 }
universe@19 601 move->tofile = fileidx(mstr[3]);
universe@19 602 move->torow = rowidx(mstr[4]);
universe@19 603 }
universe@19 604 } else if (len == 6) {
universe@19 605 /* long notation capture (e.g. "Nc5xf3") */
universe@19 606 if (mstr[3] == 'x') {
universe@19 607 move->capture = 1;
universe@19 608 move->piece = getpiece(mstr[0]);
universe@19 609 move->fromfile = fileidx(mstr[1]);
universe@19 610 move->fromrow = rowidx(mstr[2]);
universe@19 611 move->tofile = fileidx(mstr[4]);
universe@19 612 move->torow = rowidx(mstr[5]);
universe@19 613 }
universe@19 614 }
universe@19 615
universe@19 616
universe@19 617 if (move->piece) {
universe@23 618 if (move->piece == PAWN
universe@23 619 && move->torow == (gamestate->mycolor==WHITE?7:0)
universe@19 620 && !move->promotion) {
universe@19 621 return NEED_PROMOTION;
universe@19 622 }
universe@19 623
universe@23 624 move->piece |= gamestate->mycolor;
universe@19 625 if (move->fromfile == POS_UNSPECIFIED
universe@19 626 || move->fromrow == POS_UNSPECIFIED) {
universe@23 627 return getlocation(gamestate, move);
universe@19 628 } else {
universe@19 629 return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;
universe@19 630 }
universe@19 631 } else {
universe@19 632 return INVALID_MOVE_SYNTAX;
universe@19 633 }
universe@19 634 }
universe@29 635
universe@29 636 _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
universe@29 637 uint8_t color) {
universe@29 638
universe@29 639 Move threats[16];
universe@29 640 uint8_t threatcount;
universe@29 641 if (get_real_threats(gamestate, row, file, color, threats, &threatcount)) {
universe@29 642 for (int i = 0 ; i < threatcount ; i++) {
universe@29 643 if (threats[i].piece != (color|KING)) {
universe@29 644 return 1;
universe@29 645 }
universe@29 646 }
universe@29 647 return 0;
universe@29 648 } else {
universe@29 649 return 0;
universe@29 650 }
universe@29 651 }
universe@33 652
universe@33 653 uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
universe@33 654 uint8_t color) {
universe@33 655 if (!gameinfo->timecontrol) {
universe@33 656 return 0;
universe@33 657 }
universe@33 658
universe@33 659 if (gamestate->movelist) {
universe@33 660 uint16_t time = gameinfo->time;
universe@36 661 suseconds_t micros = 0;
universe@33 662
universe@33 663 MoveList *movelist = color == WHITE ?
universe@33 664 gamestate->movelist : gamestate->movelist->next;
universe@33 665
universe@33 666 while (movelist) {
universe@33 667 time += gameinfo->addtime;
universe@33 668
universe@40 669 struct movetimeval *movetime = &(movelist->move.movetime);
universe@33 670 if (movetime->tv_sec >= time) {
universe@33 671 return 0;
universe@33 672 }
universe@33 673
universe@33 674 time -= movetime->tv_sec;
universe@36 675 micros += movetime->tv_usec;
universe@33 676
universe@33 677 movelist = movelist->next ? movelist->next->next : NULL;
universe@33 678 }
universe@33 679
universe@33 680 time_t sec;
universe@33 681 movelist = gamestate->lastmove;
universe@33 682 if ((movelist->move.piece & COLOR_MASK) != color) {
universe@40 683 struct movetimeval *lastmovetstamp = &(movelist->move.timestamp);
universe@36 684 struct timeval currenttstamp;
universe@36 685 gettimeofday(&currenttstamp, NULL);
universe@36 686 micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec;
universe@33 687 sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec;
universe@33 688 if (sec >= time) {
universe@33 689 return 0;
universe@33 690 }
universe@33 691
universe@33 692 time -= sec;
universe@33 693 }
universe@33 694
universe@36 695 sec = micros / 1e6L;
universe@33 696
universe@33 697 if (sec >= time) {
universe@33 698 return 0;
universe@33 699 }
universe@33 700
universe@33 701 time -= sec;
universe@33 702
universe@33 703 return time;
universe@33 704 } else {
universe@33 705 return gameinfo->time;
universe@33 706 }
universe@33 707 }

mercurial