src/chess/rules.c

Wed, 09 Apr 2014 12:07:47 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 09 Apr 2014 12:07:47 +0200
changeset 34
c4d4b8a8f902
parent 33
866025982aa9
child 36
ebe0c961e9a6
permissions
-rw-r--r--

added nonblocking read for network games + minor build system fixes

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

mercurial