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