src/chess/rules.c

Thu, 10 Apr 2014 11:44:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 10 Apr 2014 11:44:55 +0200
changeset 36
ebe0c961e9a6
parent 33
866025982aa9
child 40
47162a7621da
permissions
-rw-r--r--

reduced awesome great nanosecond precision so we can compile on OS X

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@23 36 void gamestate_cleanup(GameState *gamestate) {
universe@23 37 MoveList *elem;
universe@23 38 elem = gamestate->movelist;
universe@23 39 while (elem) {
universe@23 40 MoveList *cur = elem;
universe@23 41 elem = elem->next;
universe@23 42 free(cur);
universe@23 43 };
universe@23 44 }
universe@23 45
universe@23 46 static void addmove(GameState* gamestate, Move *move) {
universe@23 47 MoveList *elem = malloc(sizeof(MoveList));
universe@23 48 elem->next = NULL;
universe@23 49 elem->move = *move;
universe@23 50
universe@36 51 gettimeofday(&(elem->move.timestamp), NULL);
universe@33 52
universe@23 53 if (gamestate->lastmove) {
universe@36 54 struct timeval *lasttstamp = &(gamestate->lastmove->move.timestamp);
universe@33 55 time_t sec = elem->move.timestamp.tv_sec - lasttstamp->tv_sec;
universe@36 56 suseconds_t micros;
universe@36 57 if (elem->move.timestamp.tv_usec < lasttstamp->tv_usec) {
universe@36 58 micros = 1e6L-(lasttstamp->tv_usec - elem->move.timestamp.tv_usec);
universe@33 59 sec--;
universe@33 60 } else {
universe@36 61 micros = elem->move.timestamp.tv_usec - lasttstamp->tv_usec;
universe@33 62 }
universe@33 63
universe@33 64 elem->move.movetime.tv_sec = sec;
universe@36 65 elem->move.movetime.tv_usec = micros;
universe@33 66
universe@23 67 gamestate->lastmove->next = elem;
universe@23 68 gamestate->lastmove = elem;
universe@23 69 } else {
universe@36 70 elem->move.movetime.tv_usec = 0;
universe@33 71 elem->move.movetime.tv_sec = 0;
universe@23 72 gamestate->movelist = gamestate->lastmove = elem;
universe@23 73 }
universe@23 74 }
universe@19 75
universe@19 76 char getpiecechr(uint8_t piece) {
universe@19 77 switch (piece & PIECE_MASK) {
universe@19 78 case ROOK: return 'R';
universe@19 79 case KNIGHT: return 'N';
universe@19 80 case BISHOP: return 'B';
universe@19 81 case QUEEN: return 'Q';
universe@19 82 case KING: return 'K';
universe@19 83 default: return '\0';
universe@19 84 }
universe@19 85 }
universe@19 86
universe@19 87 uint8_t getpiece(char c) {
universe@19 88 switch (c) {
universe@19 89 case 'R': return ROOK;
universe@19 90 case 'N': return KNIGHT;
universe@19 91 case 'B': return BISHOP;
universe@19 92 case 'Q': return QUEEN;
universe@19 93 case 'K': return KING;
universe@19 94 default: return 0;
universe@19 95 }
universe@19 96 }
universe@19 97
universe@23 98 static int getlocation(GameState *gamestate, Move *move) {
universe@19 99 uint8_t piece = move->piece & PIECE_MASK;
universe@19 100 switch (piece) {
universe@23 101 case PAWN: return pawn_getlocation(gamestate, move);
universe@23 102 case ROOK: return rook_getlocation(gamestate, move);
universe@23 103 case KNIGHT: return knight_getlocation(gamestate, move);
universe@23 104 case BISHOP: return bishop_getlocation(gamestate, move);
universe@23 105 case QUEEN: return queen_getlocation(gamestate, move);
universe@23 106 case KING: return king_getlocation(gamestate, move);
universe@19 107 default: return INVALID_MOVE_SYNTAX;
universe@19 108 }
universe@19 109 }
universe@19 110
universe@23 111 void apply_move(GameState *gamestate, Move *move) {
universe@19 112 uint8_t piece = move->piece & PIECE_MASK;
universe@19 113 uint8_t color = move->piece & COLOR_MASK;
universe@19 114
universe@19 115 /* en passant capture */
universe@19 116 if (move->capture && piece == PAWN &&
universe@23 117 mdst(gamestate->board, move) == 0) {
universe@23 118 gamestate->board[move->fromrow][move->tofile] = 0;
universe@19 119 }
universe@19 120
universe@19 121 /* remove old en passant threats */
universe@19 122 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@23 123 gamestate->board[3][file] &= ~ENPASSANT_THREAT;
universe@23 124 gamestate->board[4][file] &= ~ENPASSANT_THREAT;
universe@19 125 }
universe@19 126
universe@19 127 /* add new en passant threat */
universe@19 128 if (piece == PAWN && (
universe@19 129 (move->fromrow == 1 && move->torow == 3) ||
universe@19 130 (move->fromrow == 6 && move->torow == 4))) {
universe@19 131 move->piece |= ENPASSANT_THREAT;
universe@19 132 }
universe@19 133
universe@19 134 /* move (and maybe capture or promote) */
universe@23 135 msrc(gamestate->board, move) = 0;
universe@19 136 if (move->promotion) {
universe@23 137 mdst(gamestate->board, move) = move->promotion;
universe@19 138 } else {
universe@23 139 mdst(gamestate->board, move) = move->piece;
universe@19 140 }
universe@19 141
universe@19 142 /* castling */
universe@19 143 if (piece == KING &&
universe@19 144 move->fromfile == fileidx('e')) {
universe@19 145
universe@19 146 if (move->tofile == fileidx('g')) {
universe@23 147 gamestate->board[move->torow][fileidx('h')] = 0;
universe@23 148 gamestate->board[move->torow][fileidx('f')] = color|ROOK;
universe@19 149 } else if (move->tofile == fileidx('c')) {
universe@23 150 gamestate->board[move->torow][fileidx('a')] = 0;
universe@23 151 gamestate->board[move->torow][fileidx('d')] = color|ROOK;
universe@19 152 }
universe@19 153 }
universe@28 154
universe@23 155 addmove(gamestate, move);
universe@19 156 }
universe@19 157
universe@29 158 static _Bool validate_move_rules(GameState *gamestate, Move *move) {
universe@19 159 /* validate indices (don't trust opponent) */
universe@19 160 if (!chkidx(move)) {
universe@19 161 return 0;
universe@19 162 }
universe@19 163
universe@21 164 /* must move */
universe@21 165 if (move->fromfile == move->tofile && move->fromrow == move->torow) {
universe@21 166 return 0;
universe@21 167 }
universe@21 168
universe@19 169 /* does piece exist */
universe@29 170 if (msrc(gamestate->board, move) != move->piece) {
universe@29 171 return 0;
universe@29 172 }
universe@19 173
universe@19 174 /* can't capture own pieces */
universe@29 175 if ((mdst(gamestate->board, move) & COLOR_MASK)
universe@29 176 == (move->piece & COLOR_MASK)) {
universe@19 177 return 0;
universe@19 178 }
universe@19 179
universe@19 180 /* validate individual rules */
universe@19 181 switch (move->piece & PIECE_MASK) {
universe@19 182 case PAWN:
universe@29 183 return pawn_chkrules(gamestate, move) &&
universe@29 184 !pawn_isblocked(gamestate, move);
universe@19 185 case ROOK:
universe@29 186 return rook_chkrules(move) &&
universe@29 187 !rook_isblocked(gamestate, move);
universe@19 188 case KNIGHT:
universe@29 189 return knight_chkrules(move); /* knight is never blocked */
universe@19 190 case BISHOP:
universe@29 191 return bishop_chkrules(move) &&
universe@29 192 !bishop_isblocked(gamestate, move);
universe@19 193 case QUEEN:
universe@29 194 return queen_chkrules(move) &&
universe@29 195 !queen_isblocked(gamestate, move);
universe@19 196 case KING:
universe@29 197 return king_chkrules(gamestate, move) &&
universe@29 198 !king_isblocked(gamestate, move);
universe@19 199 default:
universe@29 200 return 0;
universe@19 201 }
universe@29 202 }
universe@29 203
universe@29 204 _Bool validate_move(GameState *gamestate, Move *move) {
universe@19 205
universe@29 206 _Bool result = validate_move_rules(gamestate, move);
universe@29 207
universe@29 208 /* cancel processing to save resources */
universe@25 209 if (!result) {
universe@25 210 return 0;
universe@25 211 }
universe@25 212
universe@28 213 /* find kings for check validation */
universe@29 214 uint8_t piececolor = (move->piece & COLOR_MASK);
universe@29 215
universe@28 216 uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0;
universe@28 217 for (uint8_t row = 0 ; row < 8 ; row++) {
universe@28 218 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@28 219 if (gamestate->board[row][file] ==
universe@28 220 (piececolor == WHITE?WKING:BKING)) {
universe@28 221 mykingfile = file;
universe@28 222 mykingrow = row;
universe@28 223 } else if (gamestate->board[row][file] ==
universe@28 224 (piececolor == WHITE?BKING:WKING)) {
universe@28 225 opkingfile = file;
universe@28 226 opkingrow = row;
universe@28 227 }
universe@28 228 }
universe@28 229 }
universe@28 230
universe@28 231 /* simulation move for check validation */
universe@28 232 GameState simulation;
universe@28 233 memcpy(&simulation, gamestate, sizeof(GameState));
universe@28 234 apply_move(&simulation, move);
universe@28 235
universe@28 236 /* don't move into or stay in check position */
universe@28 237 if (is_covered(&simulation, mykingrow, mykingfile,
universe@28 238 opponent_color(piececolor))) {
universe@28 239 return 0;
universe@28 240 }
universe@19 241
universe@25 242 /* correct check and checkmate flags (move is still valid) */
universe@29 243 Move threats[16];
universe@29 244 uint8_t threatcount;
universe@29 245 move->check = get_threats(&simulation, opkingrow, opkingfile,
universe@29 246 piececolor, threats, &threatcount);
universe@19 247
universe@28 248 if (move->check) {
universe@28 249 /* determine possible escape fields */
universe@28 250 _Bool canescape = 0;
universe@28 251 for (int dr = -1 ; dr <= 1 && !canescape ; dr++) {
universe@28 252 for (int df = -1 ; df <= 1 && !canescape ; df++) {
universe@28 253 if (!(dr == 0 && df == 0) &&
universe@28 254 isidx(opkingrow + dr) && isidx(opkingfile + df)) {
universe@28 255
universe@28 256 /* escape field neither blocked nor covered */
universe@28 257 if ((simulation.board[opkingrow + dr][opkingfile + df]
universe@28 258 & COLOR_MASK) != opponent_color(piececolor)) {
universe@28 259 canescape |= !is_covered(&simulation,
universe@28 260 opkingrow + dr, opkingfile + df, piececolor);
universe@28 261 }
universe@28 262 }
universe@28 263 }
universe@28 264 }
universe@29 265 /* can't escape, can he capture? */
universe@29 266 if (!canescape && threatcount == 1) {
universe@29 267 canescape = is_attacked(&simulation, threats[0].fromrow,
universe@29 268 threats[0].fromfile, opponent_color(piececolor));
universe@29 269 }
universe@29 270
universe@29 271 /* can't capture, can he block? */
universe@29 272 if (!canescape && threatcount == 1) {
universe@29 273 Move *threat = &(threats[0]);
universe@29 274 uint8_t threatpiece = threat->piece & PIECE_MASK;
universe@29 275
universe@29 276 /* knight, pawns and the king cannot be blocked */
universe@29 277 if (threatpiece == BISHOP || threatpiece == ROOK
universe@29 278 || threatpiece == QUEEN) {
universe@29 279 if (threat->fromrow == threat->torow) {
universe@29 280 /* rook aspect (on row) */
universe@29 281 int d = threat->tofile > threat->fromfile ? 1 : -1;
universe@29 282 uint8_t file = threat->fromfile;
universe@29 283 while (!canescape && file != threat->tofile - d) {
universe@29 284 file += d;
universe@29 285 canescape |= is_protected(&simulation,
universe@29 286 threat->torow, file, opponent_color(piececolor));
universe@29 287 }
universe@29 288 } else if (threat->fromfile == threat->tofile) {
universe@29 289 /* rook aspect (on file) */
universe@29 290 int d = threat->torow > threat->fromrow ? 1 : -1;
universe@29 291 uint8_t row = threat->fromrow;
universe@29 292 while (!canescape && row != threat->torow - d) {
universe@29 293 row += d;
universe@29 294 canescape |= is_protected(&simulation,
universe@29 295 row, threat->tofile, opponent_color(piececolor));
universe@29 296 }
universe@29 297 } else {
universe@29 298 /* bishop aspect */
universe@29 299 int dr = move->torow > move->fromrow ? 1 : -1;
universe@29 300 int df = move->tofile > move->fromfile ? 1 : -1;
universe@29 301
universe@29 302 uint8_t row = move->fromrow;
universe@29 303 uint8_t file = move->fromfile;
universe@29 304 while (!canescape && file != move->tofile - df
universe@29 305 && row != move->torow - dr) {
universe@29 306 row += dr;
universe@29 307 file += df;
universe@29 308 canescape |= is_protected(&simulation, row, file,
universe@29 309 opponent_color(piececolor));
universe@29 310 }
universe@29 311 }
universe@29 312 }
universe@29 313 }
universe@29 314
universe@28 315 if (!canescape) {
universe@29 316 gamestate->checkmate = 1;
universe@28 317 }
universe@28 318 }
universe@28 319
universe@28 320 return 1;
universe@19 321 }
universe@19 322
universe@23 323 int eval_move(GameState *gamestate, char *mstr, Move *move) {
universe@19 324 memset(move, 0, sizeof(Move));
universe@19 325 move->fromfile = POS_UNSPECIFIED;
universe@19 326 move->fromrow = POS_UNSPECIFIED;
universe@19 327
universe@19 328 size_t len = strlen(mstr);
universe@19 329
universe@19 330 /* evaluate check/checkmate flags */
universe@19 331 if (mstr[len-1] == '+') {
universe@19 332 len--; mstr[len] = '\0';
universe@19 333 move->check = 1;
universe@19 334 } else if (mstr[len-1] == '#') {
universe@19 335 len--; mstr[len] = '\0';
universe@27 336 /* ignore - validation should set game state */
universe@19 337 }
universe@19 338
universe@19 339 /* evaluate promotion */
universe@19 340 if (len > 3 && mstr[len-2] == '=') {
universe@19 341 move->promotion = getpiece(mstr[len-1]);
universe@19 342 if (!move->promotion) {
universe@19 343 return INVALID_MOVE_SYNTAX;
universe@19 344 } else {
universe@23 345 move->promotion |= gamestate->mycolor;
universe@19 346 len -= 2;
universe@19 347 mstr[len] = 0;
universe@19 348 }
universe@19 349 }
universe@19 350
universe@19 351 if (len == 2) {
universe@19 352 /* pawn move (e.g. "e4") */
universe@19 353 move->piece = PAWN;
universe@19 354 move->tofile = fileidx(mstr[0]);
universe@19 355 move->torow = rowidx(mstr[1]);
universe@19 356 } else if (len == 3) {
universe@19 357 if (strcmp(mstr, "O-O") == 0) {
universe@19 358 /* king side castling */
universe@19 359 move->piece = KING;
universe@19 360 move->fromfile = fileidx('e');
universe@19 361 move->tofile = fileidx('g');
universe@23 362 move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7;
universe@19 363 } else {
universe@19 364 /* move (e.g. "Nf3") */
universe@19 365 move->piece = getpiece(mstr[0]);
universe@19 366 move->tofile = fileidx(mstr[1]);
universe@19 367 move->torow = rowidx(mstr[2]);
universe@19 368 }
universe@19 369
universe@19 370 } else if (len == 4) {
universe@19 371 move->piece = getpiece(mstr[0]);
universe@19 372 if (!move->piece) {
universe@19 373 move->piece = PAWN;
universe@19 374 move->fromfile = fileidx(mstr[0]);
universe@19 375 }
universe@19 376 if (mstr[1] == 'x') {
universe@19 377 /* capture (e.g. "Nxf3", "dxe5") */
universe@19 378 move->capture = 1;
universe@19 379 } else {
universe@19 380 /* move (e.g. "Ndf3", "N2c3", "e2e4") */
universe@19 381 if (isfile(mstr[1])) {
universe@19 382 move->fromfile = fileidx(mstr[1]);
universe@19 383 if (move->piece == PAWN) {
universe@19 384 move->piece = 0;
universe@19 385 }
universe@19 386 } else {
universe@19 387 move->fromrow = rowidx(mstr[1]);
universe@19 388 }
universe@19 389 }
universe@19 390 move->tofile = fileidx(mstr[2]);
universe@19 391 move->torow = rowidx(mstr[3]);
universe@19 392 } else if (len == 5) {
universe@19 393 if (strcmp(mstr, "O-O-O") == 0) {
universe@19 394 /* queen side castling "O-O-O" */
universe@19 395 move->piece = KING;
universe@19 396 move->fromfile = fileidx('e');
universe@19 397 move->tofile = fileidx('c');
universe@23 398 move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7;
universe@19 399 } else {
universe@19 400 move->piece = getpiece(mstr[0]);
universe@19 401 if (mstr[2] == 'x') {
universe@19 402 move->capture = 1;
universe@19 403 if (move->piece) {
universe@19 404 /* capture (e.g. "Ndxf3") */
universe@19 405 move->fromfile = fileidx(mstr[1]);
universe@19 406 } else {
universe@19 407 /* long notation capture (e.g. "e5xf6") */
universe@19 408 move->piece = PAWN;
universe@19 409 move->fromfile = fileidx(mstr[0]);
universe@19 410 move->fromrow = rowidx(mstr[1]);
universe@19 411 }
universe@19 412 } else {
universe@19 413 /* long notation move (e.g. "Nc5a4") */
universe@19 414 move->fromfile = fileidx(mstr[1]);
universe@19 415 move->fromrow = rowidx(mstr[2]);
universe@19 416 }
universe@19 417 move->tofile = fileidx(mstr[3]);
universe@19 418 move->torow = rowidx(mstr[4]);
universe@19 419 }
universe@19 420 } else if (len == 6) {
universe@19 421 /* long notation capture (e.g. "Nc5xf3") */
universe@19 422 if (mstr[3] == 'x') {
universe@19 423 move->capture = 1;
universe@19 424 move->piece = getpiece(mstr[0]);
universe@19 425 move->fromfile = fileidx(mstr[1]);
universe@19 426 move->fromrow = rowidx(mstr[2]);
universe@19 427 move->tofile = fileidx(mstr[4]);
universe@19 428 move->torow = rowidx(mstr[5]);
universe@19 429 }
universe@19 430 }
universe@19 431
universe@19 432
universe@19 433 if (move->piece) {
universe@23 434 if (move->piece == PAWN
universe@23 435 && move->torow == (gamestate->mycolor==WHITE?7:0)
universe@19 436 && !move->promotion) {
universe@19 437 return NEED_PROMOTION;
universe@19 438 }
universe@19 439
universe@23 440 move->piece |= gamestate->mycolor;
universe@19 441 if (move->fromfile == POS_UNSPECIFIED
universe@19 442 || move->fromrow == POS_UNSPECIFIED) {
universe@23 443 return getlocation(gamestate, move);
universe@19 444 } else {
universe@19 445 return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;
universe@19 446 }
universe@19 447 } else {
universe@19 448 return INVALID_MOVE_SYNTAX;
universe@19 449 }
universe@19 450 }
universe@29 451
universe@29 452 _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file,
universe@29 453 uint8_t color, Move *threats, uint8_t *threatcount) {
universe@29 454 Move candidates[16];
universe@29 455 int candidatecount = 0;
universe@29 456 for (uint8_t r = 0 ; r < 8 ; r++) {
universe@29 457 for (uint8_t f = 0 ; f < 8 ; f++) {
universe@29 458 if ((gamestate->board[r][f] & COLOR_MASK) == color) {
universe@29 459 memset(&(candidates[candidatecount]), 0, sizeof(Move));
universe@29 460 candidates[candidatecount].piece = gamestate->board[r][f];
universe@29 461 candidates[candidatecount].fromrow = r;
universe@29 462 candidates[candidatecount].fromfile = f;
universe@29 463 candidates[candidatecount].torow = row;
universe@29 464 candidates[candidatecount].tofile = file;
universe@29 465 candidatecount++;
universe@29 466 }
universe@29 467 }
universe@29 468 }
universe@29 469
universe@29 470 if (threatcount) {
universe@29 471 *threatcount = 0;
universe@29 472 }
universe@29 473
universe@29 474
universe@29 475 _Bool result = 0;
universe@29 476
universe@29 477 for (int i = 0 ; i < candidatecount ; i++) {
universe@29 478 if (validate_move_rules(gamestate, &(candidates[i]))) {
universe@29 479 result = 1;
universe@29 480 if (threats && threatcount) {
universe@29 481 threats[(*threatcount)++] = candidates[i];
universe@29 482 }
universe@29 483 }
universe@29 484 }
universe@29 485
universe@29 486 return result;
universe@29 487 }
universe@29 488
universe@29 489 _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file,
universe@29 490 uint8_t color, Move *threats, uint8_t *threatcount) {
universe@29 491
universe@29 492 Move candidates[16];
universe@29 493 uint8_t candidatecount;
universe@29 494 if (get_threats(gamestate, row, file, color, candidates, &candidatecount)) {
universe@29 495
universe@29 496 if (threatcount) {
universe@29 497 *threatcount = 0;
universe@29 498 }
universe@29 499 _Bool result = 0;
universe@29 500 uint8_t kingfile = 0, kingrow = 0;
universe@29 501 for (uint8_t row = 0 ; row < 8 ; row++) {
universe@29 502 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@29 503 if ((gamestate->board[row][file] & COLOR_MASK) == color) {
universe@29 504 kingfile = file;
universe@29 505 kingrow = row;
universe@29 506 }
universe@29 507 }
universe@29 508 }
universe@29 509
universe@29 510 for (uint8_t i = 0 ; i < candidatecount ; i++) {
universe@29 511 GameState simulation;
universe@29 512 memcpy(&simulation, gamestate, sizeof(GameState));
universe@29 513 apply_move(&simulation, &(candidates[i]));
universe@29 514 if (!is_covered(&simulation, kingrow, kingfile,
universe@29 515 opponent_color(color))) {
universe@29 516 result = 1;
universe@29 517 if (threats && threatcount) {
universe@29 518 threats[(*threatcount)++] = candidates[i];
universe@29 519 }
universe@29 520 }
universe@29 521 }
universe@29 522
universe@29 523 return result;
universe@29 524 } else {
universe@29 525 return 0;
universe@29 526 }
universe@29 527 }
universe@29 528
universe@29 529 _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
universe@29 530 uint8_t color) {
universe@29 531
universe@29 532 Move threats[16];
universe@29 533 uint8_t threatcount;
universe@29 534 if (get_real_threats(gamestate, row, file, color, threats, &threatcount)) {
universe@29 535 for (int i = 0 ; i < threatcount ; i++) {
universe@29 536 if (threats[i].piece != (color|KING)) {
universe@29 537 return 1;
universe@29 538 }
universe@29 539 }
universe@29 540 return 0;
universe@29 541 } else {
universe@29 542 return 0;
universe@29 543 }
universe@29 544 }
universe@33 545
universe@33 546 uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
universe@33 547 uint8_t color) {
universe@33 548 if (!gameinfo->timecontrol) {
universe@33 549 return 0;
universe@33 550 }
universe@33 551
universe@33 552 if (gamestate->movelist) {
universe@33 553 uint16_t time = gameinfo->time;
universe@36 554 suseconds_t micros = 0;
universe@33 555
universe@33 556 MoveList *movelist = color == WHITE ?
universe@33 557 gamestate->movelist : gamestate->movelist->next;
universe@33 558
universe@33 559 while (movelist) {
universe@33 560 time += gameinfo->addtime;
universe@33 561
universe@36 562 struct timeval *movetime = &(movelist->move.movetime);
universe@33 563 if (movetime->tv_sec >= time) {
universe@33 564 return 0;
universe@33 565 }
universe@33 566
universe@33 567 time -= movetime->tv_sec;
universe@36 568 micros += movetime->tv_usec;
universe@33 569
universe@33 570 movelist = movelist->next ? movelist->next->next : NULL;
universe@33 571 }
universe@33 572
universe@33 573 time_t sec;
universe@33 574 movelist = gamestate->lastmove;
universe@33 575 if ((movelist->move.piece & COLOR_MASK) != color) {
universe@36 576 struct timeval *lastmovetstamp = &(movelist->move.timestamp);
universe@36 577 struct timeval currenttstamp;
universe@36 578 gettimeofday(&currenttstamp, NULL);
universe@36 579 micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec;
universe@33 580 sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec;
universe@33 581 if (sec >= time) {
universe@33 582 return 0;
universe@33 583 }
universe@33 584
universe@33 585 time -= sec;
universe@33 586 }
universe@33 587
universe@36 588 sec = micros / 1e6L;
universe@33 589
universe@33 590 if (sec >= time) {
universe@33 591 return 0;
universe@33 592 }
universe@33 593
universe@33 594 time -= sec;
universe@33 595
universe@33 596 return time;
universe@33 597 } else {
universe@33 598 return gameinfo->time;
universe@33 599 }
universe@33 600 }

mercurial