src/chess/rules.c

Wed, 29 Aug 2018 13:45:13 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 29 Aug 2018 13:45:13 +0200
changeset 63
611332453da0
parent 62
564af8a16828
child 64
4eda5df55f86
permissions
-rw-r--r--

move log has now three columns and starts scrolling after 45 moves

universe@19 1 /*
universe@19 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@19 3 *
universe@55 4 * Copyright 2016 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@51 43
universe@48 44 return simulation;
universe@48 45 }
universe@48 46
universe@50 47 void gamestate_init(GameState *gamestate) {
universe@50 48 memset(gamestate, 0, sizeof(GameState));
universe@50 49
universe@50 50 Board initboard = {
universe@50 51 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
universe@50 52 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
universe@50 53 {0, 0, 0, 0, 0, 0, 0, 0},
universe@50 54 {0, 0, 0, 0, 0, 0, 0, 0},
universe@50 55 {0, 0, 0, 0, 0, 0, 0, 0},
universe@50 56 {0, 0, 0, 0, 0, 0, 0, 0},
universe@50 57 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
universe@50 58 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
universe@50 59 };
universe@50 60 memcpy(gamestate->board, initboard, sizeof(Board));
universe@50 61 }
universe@50 62
universe@23 63 void gamestate_cleanup(GameState *gamestate) {
universe@23 64 MoveList *elem;
universe@23 65 elem = gamestate->movelist;
universe@23 66 while (elem) {
universe@23 67 MoveList *cur = elem;
universe@23 68 elem = elem->next;
universe@23 69 free(cur);
universe@23 70 };
universe@23 71 }
universe@23 72
universe@49 73 /* MUST be called IMMEDIATLY after applying a move to work correctly */
universe@49 74 static void format_move(GameState *gamestate, Move *move) {
universe@49 75 char *string = move->string;
universe@49 76
universe@49 77 /* at least 8 characters should be available, wipe them out */
universe@49 78 memset(string, 0, 8);
universe@49 79
universe@49 80 /* special formats for castling */
universe@49 81 if ((move->piece&PIECE_MASK) == KING &&
universe@49 82 abs(move->tofile-move->fromfile) == 2) {
universe@49 83 if (move->tofile==fileidx('c')) {
universe@49 84 memcpy(string, "O-O-O", 5);
universe@49 85 } else {
universe@49 86 memcpy(string, "O-O", 3);
universe@49 87 }
universe@49 88 }
universe@49 89
universe@49 90 /* start by notating the piece character */
universe@49 91 string[0] = getpiecechr(move->piece);
universe@49 92 int idx = string[0] ? 1 : 0;
universe@49 93
universe@49 94 /* find out how many source information we do need */
universe@49 95 uint8_t piece = move->piece & PIECE_MASK;
universe@49 96 if (piece == PAWN) {
universe@49 97 if (move->capture) {
universe@49 98 string[idx++] = filechr(move->fromfile);
universe@49 99 }
universe@49 100 } else if (piece != KING) {
universe@49 101 Move threats[16];
universe@49 102 uint8_t threatcount;
universe@49 103 get_real_threats(gamestate, move->torow, move->tofile,
universe@49 104 move->piece&COLOR_MASK, threats, &threatcount);
universe@49 105 if (threatcount > 1) {
universe@49 106 int ambrows = 0, ambfiles = 0;
universe@49 107 for (uint8_t i = 0 ; i < threatcount ; i++) {
universe@49 108 if (threats[i].fromrow == move->fromrow) {
universe@49 109 ambrows++;
universe@49 110 }
universe@49 111 if (threats[i].fromfile == move->fromfile) {
universe@49 112 ambfiles++;
universe@49 113 }
universe@49 114 }
universe@49 115 /* ambiguous row, name file */
universe@49 116 if (ambrows > 1) {
universe@49 117 string[idx++] = filechr(move->fromfile);
universe@49 118 }
universe@49 119 /* ambiguous file, name row */
universe@49 120 if (ambfiles > 1) {
universe@49 121 string[idx++] = filechr(move->fromrow);
universe@49 122 }
universe@49 123 }
universe@49 124 }
universe@49 125
universe@49 126 /* capturing? */
universe@49 127 if (move->capture) {
universe@49 128 string[idx++] = 'x';
universe@49 129 }
universe@49 130
universe@49 131 /* destination */
universe@49 132 string[idx++] = filechr(move->tofile);
universe@49 133 string[idx++] = rowchr(move->torow);
universe@49 134
universe@49 135 /* promotion? */
universe@49 136 if (move->promotion) {
universe@49 137 string[idx++] = '=';
universe@49 138 string[idx++] = getpiecechr(move->promotion);
universe@49 139 }
universe@49 140
universe@49 141 /* check? */
universe@49 142 if (move->check) {
universe@49 143 /* works only, if this function is called when applying the move */
universe@49 144 string[idx++] = gamestate->checkmate?'#':'+';
universe@49 145 }
universe@49 146 }
universe@49 147
universe@23 148 static void addmove(GameState* gamestate, Move *move) {
universe@23 149 MoveList *elem = malloc(sizeof(MoveList));
universe@23 150 elem->next = NULL;
universe@23 151 elem->move = *move;
universe@23 152
universe@40 153 struct timeval curtimestamp;
universe@40 154 gettimeofday(&curtimestamp, NULL);
universe@40 155 elem->move.timestamp.tv_sec = curtimestamp.tv_sec;
universe@40 156 elem->move.timestamp.tv_usec = curtimestamp.tv_usec;
universe@33 157
universe@23 158 if (gamestate->lastmove) {
universe@40 159 struct movetimeval *lasttstamp = &(gamestate->lastmove->move.timestamp);
universe@40 160 uint64_t sec = curtimestamp.tv_sec - lasttstamp->tv_sec;
universe@36 161 suseconds_t micros;
universe@40 162 if (curtimestamp.tv_usec < lasttstamp->tv_usec) {
universe@40 163 micros = 1e6L-(lasttstamp->tv_usec - curtimestamp.tv_usec);
universe@33 164 sec--;
universe@33 165 } else {
universe@40 166 micros = curtimestamp.tv_usec - lasttstamp->tv_usec;
universe@33 167 }
universe@33 168
universe@33 169 elem->move.movetime.tv_sec = sec;
universe@36 170 elem->move.movetime.tv_usec = micros;
universe@33 171
universe@23 172 gamestate->lastmove->next = elem;
universe@23 173 gamestate->lastmove = elem;
universe@63 174 gamestate->movecount++;
universe@23 175 } else {
universe@36 176 elem->move.movetime.tv_usec = 0;
universe@33 177 elem->move.movetime.tv_sec = 0;
universe@23 178 gamestate->movelist = gamestate->lastmove = elem;
universe@63 179 gamestate->movecount = 1;
universe@23 180 }
universe@23 181 }
universe@19 182
universe@19 183 char getpiecechr(uint8_t piece) {
universe@19 184 switch (piece & PIECE_MASK) {
universe@19 185 case ROOK: return 'R';
universe@19 186 case KNIGHT: return 'N';
universe@19 187 case BISHOP: return 'B';
universe@19 188 case QUEEN: return 'Q';
universe@19 189 case KING: return 'K';
universe@19 190 default: return '\0';
universe@19 191 }
universe@19 192 }
universe@19 193
universe@19 194 uint8_t getpiece(char c) {
universe@19 195 switch (c) {
universe@19 196 case 'R': return ROOK;
universe@19 197 case 'N': return KNIGHT;
universe@19 198 case 'B': return BISHOP;
universe@19 199 case 'Q': return QUEEN;
universe@19 200 case 'K': return KING;
universe@19 201 default: return 0;
universe@19 202 }
universe@19 203 }
universe@19 204
universe@49 205 static void apply_move_impl(GameState *gamestate, Move *move, _Bool simulate) {
universe@19 206 uint8_t piece = move->piece & PIECE_MASK;
universe@19 207 uint8_t color = move->piece & COLOR_MASK;
universe@19 208
universe@19 209 /* en passant capture */
universe@19 210 if (move->capture && piece == PAWN &&
universe@23 211 mdst(gamestate->board, move) == 0) {
universe@23 212 gamestate->board[move->fromrow][move->tofile] = 0;
universe@19 213 }
universe@19 214
universe@19 215 /* remove old en passant threats */
universe@19 216 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@23 217 gamestate->board[3][file] &= ~ENPASSANT_THREAT;
universe@23 218 gamestate->board[4][file] &= ~ENPASSANT_THREAT;
universe@19 219 }
universe@19 220
universe@19 221 /* add new en passant threat */
universe@19 222 if (piece == PAWN && (
universe@19 223 (move->fromrow == 1 && move->torow == 3) ||
universe@19 224 (move->fromrow == 6 && move->torow == 4))) {
universe@19 225 move->piece |= ENPASSANT_THREAT;
universe@19 226 }
universe@19 227
universe@19 228 /* move (and maybe capture or promote) */
universe@23 229 msrc(gamestate->board, move) = 0;
universe@19 230 if (move->promotion) {
universe@23 231 mdst(gamestate->board, move) = move->promotion;
universe@19 232 } else {
universe@23 233 mdst(gamestate->board, move) = move->piece;
universe@19 234 }
universe@19 235
universe@19 236 /* castling */
universe@49 237 if (piece == KING && move->fromfile == fileidx('e')) {
universe@19 238
universe@19 239 if (move->tofile == fileidx('g')) {
universe@23 240 gamestate->board[move->torow][fileidx('h')] = 0;
universe@23 241 gamestate->board[move->torow][fileidx('f')] = color|ROOK;
universe@19 242 } else if (move->tofile == fileidx('c')) {
universe@23 243 gamestate->board[move->torow][fileidx('a')] = 0;
universe@23 244 gamestate->board[move->torow][fileidx('d')] = color|ROOK;
universe@19 245 }
universe@19 246 }
universe@28 247
universe@49 248 if (!simulate) {
universe@49 249 if (!move->string[0]) {
universe@49 250 format_move(gamestate, move);
universe@49 251 }
universe@49 252 }
universe@49 253 /* add move, even in simulation (checkmate test needs it) */
universe@23 254 addmove(gamestate, move);
universe@19 255 }
universe@19 256
universe@49 257 void apply_move(GameState *gamestate, Move *move) {
universe@49 258 apply_move_impl(gamestate, move, 0);
universe@49 259 }
universe@49 260
universe@48 261 static int validate_move_rules(GameState *gamestate, Move *move) {
universe@19 262 /* validate indices (don't trust opponent) */
universe@19 263 if (!chkidx(move)) {
universe@48 264 return INVALID_POSITION;
universe@19 265 }
universe@19 266
universe@21 267 /* must move */
universe@21 268 if (move->fromfile == move->tofile && move->fromrow == move->torow) {
universe@48 269 return INVALID_MOVE_SYNTAX;
universe@21 270 }
universe@21 271
universe@19 272 /* does piece exist */
universe@47 273 if ((msrc(gamestate->board, move)&(PIECE_MASK|COLOR_MASK))
universe@47 274 != (move->piece&(PIECE_MASK|COLOR_MASK))) {
universe@48 275 return INVALID_POSITION;
universe@29 276 }
universe@19 277
universe@19 278 /* can't capture own pieces */
universe@29 279 if ((mdst(gamestate->board, move) & COLOR_MASK)
universe@47 280 == (move->piece & COLOR_MASK)) {
universe@48 281 return RULES_VIOLATED;
universe@47 282 }
universe@47 283
universe@47 284 /* must capture, if and only if destination is occupied */
universe@47 285 if ((mdst(gamestate->board, move) == 0 && move->capture) ||
universe@47 286 (mdst(gamestate->board, move) != 0 && !move->capture)) {
universe@48 287 return INVALID_MOVE_SYNTAX;
universe@19 288 }
universe@19 289
universe@19 290 /* validate individual rules */
universe@48 291 _Bool chkrules;
universe@19 292 switch (move->piece & PIECE_MASK) {
universe@19 293 case PAWN:
universe@48 294 chkrules = pawn_chkrules(gamestate, move) &&
universe@29 295 !pawn_isblocked(gamestate, move);
universe@48 296 break;
universe@19 297 case ROOK:
universe@48 298 chkrules = rook_chkrules(move) &&
universe@29 299 !rook_isblocked(gamestate, move);
universe@48 300 break;
universe@19 301 case KNIGHT:
universe@48 302 chkrules = knight_chkrules(move); /* knight is never blocked */
universe@48 303 break;
universe@19 304 case BISHOP:
universe@48 305 chkrules = bishop_chkrules(move) &&
universe@29 306 !bishop_isblocked(gamestate, move);
universe@48 307 break;
universe@19 308 case QUEEN:
universe@48 309 chkrules = queen_chkrules(move) &&
universe@29 310 !queen_isblocked(gamestate, move);
universe@48 311 break;
universe@19 312 case KING:
universe@48 313 chkrules = king_chkrules(gamestate, move) &&
universe@29 314 !king_isblocked(gamestate, move);
universe@48 315 break;
universe@19 316 default:
universe@48 317 return INVALID_MOVE_SYNTAX;
universe@19 318 }
universe@48 319
universe@48 320 return chkrules ? VALID_MOVE_SEMANTICS : RULES_VIOLATED;
universe@29 321 }
universe@29 322
universe@48 323 int validate_move(GameState *gamestate, Move *move) {
universe@19 324
universe@48 325 int result = validate_move_rules(gamestate, move);
universe@29 326
universe@29 327 /* cancel processing to save resources */
universe@48 328 if (result != VALID_MOVE_SEMANTICS) {
universe@48 329 return result;
universe@25 330 }
universe@25 331
universe@62 332 /* simulate move for check validation */
universe@62 333 GameState simulation = gamestate_copy_sim(gamestate);
universe@62 334 Move simmove = *move;
universe@62 335 apply_move_impl(&simulation, &simmove, 1);
universe@62 336
universe@28 337 /* find kings for check validation */
universe@29 338 uint8_t piececolor = (move->piece & COLOR_MASK);
universe@29 339
universe@28 340 uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0;
universe@28 341 for (uint8_t row = 0 ; row < 8 ; row++) {
universe@28 342 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@62 343 if (simulation.board[row][file] ==
universe@28 344 (piececolor == WHITE?WKING:BKING)) {
universe@28 345 mykingfile = file;
universe@28 346 mykingrow = row;
universe@62 347 } else if (simulation.board[row][file] ==
universe@28 348 (piececolor == WHITE?BKING:WKING)) {
universe@28 349 opkingfile = file;
universe@28 350 opkingrow = row;
universe@28 351 }
universe@28 352 }
universe@28 353 }
universe@28 354
universe@28 355 /* don't move into or stay in check position */
universe@28 356 if (is_covered(&simulation, mykingrow, mykingfile,
universe@28 357 opponent_color(piececolor))) {
universe@48 358
universe@48 359 gamestate_cleanup(&simulation);
universe@48 360 if ((move->piece & PIECE_MASK) == KING) {
universe@48 361 return KING_MOVES_INTO_CHECK;
universe@48 362 } else {
universe@48 363 /* last move is always not null in this case */
universe@48 364 return gamestate->lastmove->move.check ?
universe@48 365 KING_IN_CHECK : PIECE_PINNED;
universe@48 366 }
universe@28 367 }
universe@19 368
universe@25 369 /* correct check and checkmate flags (move is still valid) */
universe@29 370 Move threats[16];
universe@29 371 uint8_t threatcount;
universe@29 372 move->check = get_threats(&simulation, opkingrow, opkingfile,
universe@29 373 piececolor, threats, &threatcount);
universe@19 374
universe@28 375 if (move->check) {
universe@28 376 /* determine possible escape fields */
universe@28 377 _Bool canescape = 0;
universe@28 378 for (int dr = -1 ; dr <= 1 && !canescape ; dr++) {
universe@28 379 for (int df = -1 ; df <= 1 && !canescape ; df++) {
universe@28 380 if (!(dr == 0 && df == 0) &&
universe@28 381 isidx(opkingrow + dr) && isidx(opkingfile + df)) {
universe@28 382
universe@28 383 /* escape field neither blocked nor covered */
universe@28 384 if ((simulation.board[opkingrow + dr][opkingfile + df]
universe@28 385 & COLOR_MASK) != opponent_color(piececolor)) {
universe@28 386 canescape |= !is_covered(&simulation,
universe@28 387 opkingrow + dr, opkingfile + df, piececolor);
universe@28 388 }
universe@28 389 }
universe@28 390 }
universe@28 391 }
universe@29 392 /* can't escape, can he capture? */
universe@29 393 if (!canescape && threatcount == 1) {
universe@29 394 canescape = is_attacked(&simulation, threats[0].fromrow,
universe@29 395 threats[0].fromfile, opponent_color(piececolor));
universe@29 396 }
universe@29 397
universe@29 398 /* can't capture, can he block? */
universe@29 399 if (!canescape && threatcount == 1) {
universe@29 400 Move *threat = &(threats[0]);
universe@29 401 uint8_t threatpiece = threat->piece & PIECE_MASK;
universe@29 402
universe@29 403 /* knight, pawns and the king cannot be blocked */
universe@29 404 if (threatpiece == BISHOP || threatpiece == ROOK
universe@29 405 || threatpiece == QUEEN) {
universe@29 406 if (threat->fromrow == threat->torow) {
universe@29 407 /* rook aspect (on row) */
universe@29 408 int d = threat->tofile > threat->fromfile ? 1 : -1;
universe@29 409 uint8_t file = threat->fromfile;
universe@29 410 while (!canescape && file != threat->tofile - d) {
universe@29 411 file += d;
universe@29 412 canescape |= is_protected(&simulation,
universe@29 413 threat->torow, file, opponent_color(piececolor));
universe@29 414 }
universe@29 415 } else if (threat->fromfile == threat->tofile) {
universe@29 416 /* rook aspect (on file) */
universe@29 417 int d = threat->torow > threat->fromrow ? 1 : -1;
universe@29 418 uint8_t row = threat->fromrow;
universe@29 419 while (!canescape && row != threat->torow - d) {
universe@29 420 row += d;
universe@29 421 canescape |= is_protected(&simulation,
universe@29 422 row, threat->tofile, opponent_color(piececolor));
universe@29 423 }
universe@29 424 } else {
universe@29 425 /* bishop aspect */
universe@51 426 int dr = threat->torow > threat->fromrow ? 1 : -1;
universe@51 427 int df = threat->tofile > threat->fromfile ? 1 : -1;
universe@29 428
universe@51 429 uint8_t row = threat->fromrow;
universe@51 430 uint8_t file = threat->fromfile;
universe@51 431 while (!canescape && file != threat->tofile - df
universe@51 432 && row != threat->torow - dr) {
universe@29 433 row += dr;
universe@29 434 file += df;
universe@29 435 canescape |= is_protected(&simulation, row, file,
universe@29 436 opponent_color(piececolor));
universe@29 437 }
universe@29 438 }
universe@29 439 }
universe@29 440 }
universe@29 441
universe@28 442 if (!canescape) {
universe@29 443 gamestate->checkmate = 1;
universe@28 444 }
universe@28 445 }
universe@28 446
universe@48 447 gamestate_cleanup(&simulation);
universe@48 448
universe@48 449 return VALID_MOVE_SEMANTICS;
universe@19 450 }
universe@19 451
universe@47 452 _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file,
universe@47 453 uint8_t color, Move *threats, uint8_t *threatcount) {
universe@47 454 Move candidates[32];
universe@47 455 int candidatecount = 0;
universe@47 456 for (uint8_t r = 0 ; r < 8 ; r++) {
universe@47 457 for (uint8_t f = 0 ; f < 8 ; f++) {
universe@47 458 if ((gamestate->board[r][f] & COLOR_MASK) == color) {
universe@47 459 // non-capturing move
universe@47 460 memset(&(candidates[candidatecount]), 0, sizeof(Move));
universe@47 461 candidates[candidatecount].piece = gamestate->board[r][f];
universe@47 462 candidates[candidatecount].fromrow = r;
universe@47 463 candidates[candidatecount].fromfile = f;
universe@47 464 candidates[candidatecount].torow = row;
universe@47 465 candidates[candidatecount].tofile = file;
universe@47 466 candidatecount++;
universe@47 467
universe@47 468 // capturing move
universe@47 469 memcpy(&(candidates[candidatecount]),
universe@47 470 &(candidates[candidatecount-1]), sizeof(Move));
universe@47 471 candidates[candidatecount].capture = 1;
universe@47 472 candidatecount++;
universe@47 473 }
universe@47 474 }
universe@47 475 }
universe@47 476
universe@47 477 if (threatcount) {
universe@47 478 *threatcount = 0;
universe@47 479 }
universe@47 480
universe@47 481
universe@47 482 _Bool result = 0;
universe@47 483
universe@47 484 for (int i = 0 ; i < candidatecount ; i++) {
universe@48 485 if (validate_move_rules(gamestate, &(candidates[i]))
universe@48 486 == VALID_MOVE_SEMANTICS) {
universe@47 487 result = 1;
universe@47 488 if (threats && threatcount) {
universe@47 489 threats[(*threatcount)++] = candidates[i];
universe@47 490 }
universe@47 491 }
universe@47 492 }
universe@47 493
universe@47 494 return result;
universe@47 495 }
universe@47 496
universe@47 497 _Bool is_pinned(GameState *gamestate, Move *move) {
universe@47 498 uint8_t color = move->piece & COLOR_MASK;
universe@47 499
universe@62 500 GameState simulation = gamestate_copy_sim(gamestate);
universe@62 501 Move simmove = *move;
universe@62 502 apply_move(&simulation, &simmove);
universe@62 503
universe@47 504 uint8_t kingfile = 0, kingrow = 0;
universe@47 505 for (uint8_t row = 0 ; row < 8 ; row++) {
universe@47 506 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@62 507 if (simulation.board[row][file] == (color|KING)) {
universe@47 508 kingfile = file;
universe@47 509 kingrow = row;
universe@47 510 }
universe@47 511 }
universe@47 512 }
universe@62 513
universe@48 514 _Bool covered = is_covered(&simulation,
universe@48 515 kingrow, kingfile, opponent_color(color));
universe@48 516 gamestate_cleanup(&simulation);
universe@48 517
universe@48 518 return covered;
universe@47 519 }
universe@47 520
universe@47 521 _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file,
universe@47 522 uint8_t color, Move *threats, uint8_t *threatcount) {
universe@47 523
universe@47 524 if (threatcount) {
universe@47 525 *threatcount = 0;
universe@47 526 }
universe@47 527
universe@47 528 Move candidates[16];
universe@47 529 uint8_t candidatecount;
universe@47 530 if (get_threats(gamestate, row, file, color, candidates, &candidatecount)) {
universe@47 531
universe@47 532 _Bool result = 0;
universe@47 533 uint8_t kingfile = 0, kingrow = 0;
universe@47 534 for (uint8_t row = 0 ; row < 8 ; row++) {
universe@47 535 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@47 536 if (gamestate->board[row][file] == (color|KING)) {
universe@47 537 kingfile = file;
universe@47 538 kingrow = row;
universe@47 539 }
universe@47 540 }
universe@47 541 }
universe@47 542
universe@47 543 for (uint8_t i = 0 ; i < candidatecount ; i++) {
universe@48 544 GameState simulation = gamestate_copy_sim(gamestate);
universe@47 545 Move simmove = candidates[i];
universe@47 546 apply_move(&simulation, &simmove);
universe@47 547 if (!is_covered(&simulation, kingrow, kingfile,
universe@47 548 opponent_color(color))) {
universe@47 549 result = 1;
universe@47 550 if (threats && threatcount) {
universe@47 551 threats[(*threatcount)++] = candidates[i];
universe@47 552 }
universe@47 553 }
universe@47 554 }
universe@47 555
universe@47 556 return result;
universe@47 557 } else {
universe@47 558 return 0;
universe@47 559 }
universe@47 560 }
universe@48 561
universe@47 562 static int getlocation(GameState *gamestate, Move *move) {
universe@47 563
universe@47 564 uint8_t color = move->piece & COLOR_MASK;
universe@47 565 _Bool incheck = gamestate->lastmove?gamestate->lastmove->move.check:0;
universe@47 566
universe@47 567 Move threats[16], *threat = NULL;
universe@47 568 uint8_t threatcount;
universe@47 569
universe@47 570 if (get_threats(gamestate, move->torow, move->tofile, color,
universe@47 571 threats, &threatcount)) {
universe@47 572
universe@49 573 int reason = INVALID_POSITION;
universe@49 574
universe@47 575 // find threats for the specified position
universe@62 576 for (uint8_t i = 0 ; i < threatcount ; i++) {
universe@47 577 if ((threats[i].piece & (PIECE_MASK | COLOR_MASK))
universe@47 578 == move->piece &&
universe@47 579 (move->fromrow == POS_UNSPECIFIED ||
universe@47 580 move->fromrow == threats[i].fromrow) &&
universe@47 581 (move->fromfile == POS_UNSPECIFIED ||
universe@47 582 move->fromfile == threats[i].fromfile)) {
universe@47 583
universe@47 584 if (threat) {
universe@47 585 return AMBIGUOUS_MOVE;
universe@47 586 } else {
universe@49 587 // found threat is no real threat
universe@49 588 if (is_pinned(gamestate, &(threats[i]))) {
universe@49 589 reason = incheck?KING_IN_CHECK:PIECE_PINNED;
universe@49 590 } else {
universe@49 591 threat = &(threats[i]);
universe@49 592 }
universe@47 593 }
universe@47 594 }
universe@47 595 }
universe@47 596
universe@47 597 // can't threaten specified position
universe@47 598 if (!threat) {
universe@49 599 return reason;
universe@47 600 }
universe@47 601
universe@49 602 memcpy(move, threat, sizeof(Move));
universe@49 603 return VALID_MOVE_SYNTAX;
universe@47 604 } else {
universe@47 605 return INVALID_POSITION;
universe@47 606 }
universe@47 607 }
universe@47 608
universe@50 609 int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color) {
universe@19 610 memset(move, 0, sizeof(Move));
universe@19 611 move->fromfile = POS_UNSPECIFIED;
universe@19 612 move->fromrow = POS_UNSPECIFIED;
universe@19 613
universe@19 614 size_t len = strlen(mstr);
universe@48 615 if (len < 1 || len > 6) {
universe@48 616 return INVALID_MOVE_SYNTAX;
universe@48 617 }
universe@19 618
universe@19 619 /* evaluate check/checkmate flags */
universe@19 620 if (mstr[len-1] == '+') {
universe@19 621 len--; mstr[len] = '\0';
universe@19 622 move->check = 1;
universe@19 623 } else if (mstr[len-1] == '#') {
universe@19 624 len--; mstr[len] = '\0';
universe@27 625 /* ignore - validation should set game state */
universe@19 626 }
universe@19 627
universe@19 628 /* evaluate promotion */
universe@19 629 if (len > 3 && mstr[len-2] == '=') {
universe@19 630 move->promotion = getpiece(mstr[len-1]);
universe@19 631 if (!move->promotion) {
universe@19 632 return INVALID_MOVE_SYNTAX;
universe@19 633 } else {
universe@50 634 move->promotion |= color;
universe@19 635 len -= 2;
universe@19 636 mstr[len] = 0;
universe@19 637 }
universe@19 638 }
universe@19 639
universe@19 640 if (len == 2) {
universe@19 641 /* pawn move (e.g. "e4") */
universe@19 642 move->piece = PAWN;
universe@19 643 move->tofile = fileidx(mstr[0]);
universe@19 644 move->torow = rowidx(mstr[1]);
universe@19 645 } else if (len == 3) {
universe@19 646 if (strcmp(mstr, "O-O") == 0) {
universe@19 647 /* king side castling */
universe@19 648 move->piece = KING;
universe@19 649 move->fromfile = fileidx('e');
universe@19 650 move->tofile = fileidx('g');
universe@50 651 move->fromrow = move->torow = color == WHITE ? 0 : 7;
universe@19 652 } else {
universe@19 653 /* move (e.g. "Nf3") */
universe@19 654 move->piece = getpiece(mstr[0]);
universe@19 655 move->tofile = fileidx(mstr[1]);
universe@19 656 move->torow = rowidx(mstr[2]);
universe@19 657 }
universe@19 658 } else if (len == 4) {
universe@19 659 move->piece = getpiece(mstr[0]);
universe@19 660 if (!move->piece) {
universe@19 661 move->piece = PAWN;
universe@19 662 move->fromfile = fileidx(mstr[0]);
universe@19 663 }
universe@19 664 if (mstr[1] == 'x') {
universe@19 665 /* capture (e.g. "Nxf3", "dxe5") */
universe@19 666 move->capture = 1;
universe@19 667 } else {
universe@19 668 /* move (e.g. "Ndf3", "N2c3", "e2e4") */
universe@19 669 if (isfile(mstr[1])) {
universe@19 670 move->fromfile = fileidx(mstr[1]);
universe@19 671 if (move->piece == PAWN) {
universe@19 672 move->piece = 0;
universe@19 673 }
universe@19 674 } else {
universe@19 675 move->fromrow = rowidx(mstr[1]);
universe@19 676 }
universe@19 677 }
universe@19 678 move->tofile = fileidx(mstr[2]);
universe@19 679 move->torow = rowidx(mstr[3]);
universe@19 680 } else if (len == 5) {
universe@19 681 if (strcmp(mstr, "O-O-O") == 0) {
universe@19 682 /* queen side castling "O-O-O" */
universe@19 683 move->piece = KING;
universe@19 684 move->fromfile = fileidx('e');
universe@19 685 move->tofile = fileidx('c');
universe@50 686 move->fromrow = move->torow = color == WHITE ? 0 : 7;
universe@19 687 } else {
universe@19 688 move->piece = getpiece(mstr[0]);
universe@19 689 if (mstr[2] == 'x') {
universe@19 690 move->capture = 1;
universe@19 691 if (move->piece) {
universe@19 692 /* capture (e.g. "Ndxf3") */
universe@19 693 move->fromfile = fileidx(mstr[1]);
universe@19 694 } else {
universe@19 695 /* long notation capture (e.g. "e5xf6") */
universe@19 696 move->piece = PAWN;
universe@19 697 move->fromfile = fileidx(mstr[0]);
universe@19 698 move->fromrow = rowidx(mstr[1]);
universe@19 699 }
universe@19 700 } else {
universe@19 701 /* long notation move (e.g. "Nc5a4") */
universe@19 702 move->fromfile = fileidx(mstr[1]);
universe@19 703 move->fromrow = rowidx(mstr[2]);
universe@19 704 }
universe@19 705 move->tofile = fileidx(mstr[3]);
universe@19 706 move->torow = rowidx(mstr[4]);
universe@19 707 }
universe@19 708 } else if (len == 6) {
universe@19 709 /* long notation capture (e.g. "Nc5xf3") */
universe@19 710 if (mstr[3] == 'x') {
universe@19 711 move->capture = 1;
universe@19 712 move->piece = getpiece(mstr[0]);
universe@19 713 move->fromfile = fileidx(mstr[1]);
universe@19 714 move->fromrow = rowidx(mstr[2]);
universe@19 715 move->tofile = fileidx(mstr[4]);
universe@19 716 move->torow = rowidx(mstr[5]);
universe@19 717 }
universe@19 718 }
universe@19 719
universe@19 720
universe@19 721 if (move->piece) {
universe@23 722 if (move->piece == PAWN
universe@50 723 && move->torow == (color==WHITE?7:0)
universe@19 724 && !move->promotion) {
universe@19 725 return NEED_PROMOTION;
universe@19 726 }
universe@19 727
universe@50 728 move->piece |= color;
universe@19 729 if (move->fromfile == POS_UNSPECIFIED
universe@19 730 || move->fromrow == POS_UNSPECIFIED) {
universe@23 731 return getlocation(gamestate, move);
universe@19 732 } else {
universe@19 733 return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;
universe@19 734 }
universe@19 735 } else {
universe@19 736 return INVALID_MOVE_SYNTAX;
universe@19 737 }
universe@19 738 }
universe@29 739
universe@29 740 _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
universe@29 741 uint8_t color) {
universe@29 742
universe@29 743 Move threats[16];
universe@29 744 uint8_t threatcount;
universe@29 745 if (get_real_threats(gamestate, row, file, color, threats, &threatcount)) {
universe@29 746 for (int i = 0 ; i < threatcount ; i++) {
universe@29 747 if (threats[i].piece != (color|KING)) {
universe@29 748 return 1;
universe@29 749 }
universe@29 750 }
universe@29 751 return 0;
universe@29 752 } else {
universe@29 753 return 0;
universe@29 754 }
universe@29 755 }
universe@33 756
universe@33 757 uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
universe@33 758 uint8_t color) {
universe@33 759 if (!gameinfo->timecontrol) {
universe@33 760 return 0;
universe@33 761 }
universe@33 762
universe@33 763 if (gamestate->movelist) {
universe@33 764 uint16_t time = gameinfo->time;
universe@36 765 suseconds_t micros = 0;
universe@33 766
universe@33 767 MoveList *movelist = color == WHITE ?
universe@33 768 gamestate->movelist : gamestate->movelist->next;
universe@33 769
universe@33 770 while (movelist) {
universe@33 771 time += gameinfo->addtime;
universe@33 772
universe@40 773 struct movetimeval *movetime = &(movelist->move.movetime);
universe@33 774 if (movetime->tv_sec >= time) {
universe@33 775 return 0;
universe@33 776 }
universe@33 777
universe@33 778 time -= movetime->tv_sec;
universe@36 779 micros += movetime->tv_usec;
universe@33 780
universe@33 781 movelist = movelist->next ? movelist->next->next : NULL;
universe@33 782 }
universe@33 783
universe@33 784 time_t sec;
universe@33 785 movelist = gamestate->lastmove;
universe@33 786 if ((movelist->move.piece & COLOR_MASK) != color) {
universe@40 787 struct movetimeval *lastmovetstamp = &(movelist->move.timestamp);
universe@36 788 struct timeval currenttstamp;
universe@36 789 gettimeofday(&currenttstamp, NULL);
universe@36 790 micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec;
universe@33 791 sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec;
universe@33 792 if (sec >= time) {
universe@33 793 return 0;
universe@33 794 }
universe@33 795
universe@33 796 time -= sec;
universe@33 797 }
universe@33 798
universe@36 799 sec = micros / 1e6L;
universe@33 800
universe@33 801 if (sec >= time) {
universe@33 802 return 0;
universe@33 803 }
universe@33 804
universe@33 805 time -= sec;
universe@33 806
universe@33 807 return time;
universe@33 808 } else {
universe@33 809 return gameinfo->time;
universe@33 810 }
universe@33 811 }

mercurial