src/chess/rules.c

Tue, 18 Sep 2018 15:02:08 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 18 Sep 2018 15:02:08 +0200
changeset 69
c8f2c280cff7
parent 68
b34de5ce7d0e
permissions
-rw-r--r--

adds unicode support

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

mercurial