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