Fri, 04 Apr 2014 17:36:42 +0200
NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
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@23 | 50 | if (gamestate->lastmove) { |
universe@23 | 51 | gamestate->lastmove->next = elem; |
universe@23 | 52 | gamestate->lastmove = elem; |
universe@23 | 53 | } else { |
universe@23 | 54 | gamestate->movelist = gamestate->lastmove = elem; |
universe@23 | 55 | } |
universe@23 | 56 | } |
universe@19 | 57 | |
universe@19 | 58 | char getpiecechr(uint8_t piece) { |
universe@19 | 59 | switch (piece & PIECE_MASK) { |
universe@19 | 60 | case ROOK: return 'R'; |
universe@19 | 61 | case KNIGHT: return 'N'; |
universe@19 | 62 | case BISHOP: return 'B'; |
universe@19 | 63 | case QUEEN: return 'Q'; |
universe@19 | 64 | case KING: return 'K'; |
universe@19 | 65 | default: return '\0'; |
universe@19 | 66 | } |
universe@19 | 67 | } |
universe@19 | 68 | |
universe@19 | 69 | uint8_t getpiece(char c) { |
universe@19 | 70 | switch (c) { |
universe@19 | 71 | case 'R': return ROOK; |
universe@19 | 72 | case 'N': return KNIGHT; |
universe@19 | 73 | case 'B': return BISHOP; |
universe@19 | 74 | case 'Q': return QUEEN; |
universe@19 | 75 | case 'K': return KING; |
universe@19 | 76 | default: return 0; |
universe@19 | 77 | } |
universe@19 | 78 | } |
universe@19 | 79 | |
universe@23 | 80 | static int getlocation(GameState *gamestate, Move *move) { |
universe@19 | 81 | uint8_t piece = move->piece & PIECE_MASK; |
universe@19 | 82 | switch (piece) { |
universe@23 | 83 | case PAWN: return pawn_getlocation(gamestate, move); |
universe@23 | 84 | case ROOK: return rook_getlocation(gamestate, move); |
universe@23 | 85 | case KNIGHT: return knight_getlocation(gamestate, move); |
universe@23 | 86 | case BISHOP: return bishop_getlocation(gamestate, move); |
universe@23 | 87 | case QUEEN: return queen_getlocation(gamestate, move); |
universe@23 | 88 | case KING: return king_getlocation(gamestate, move); |
universe@19 | 89 | default: return INVALID_MOVE_SYNTAX; |
universe@19 | 90 | } |
universe@19 | 91 | } |
universe@19 | 92 | |
universe@28 | 93 | _Bool get_any_threat_for(GameState *gamestate, uint8_t row, uint8_t file, |
universe@28 | 94 | uint8_t color, Move *threat) { |
universe@25 | 95 | Move threats[16]; |
universe@25 | 96 | int threatcount = 0; |
universe@25 | 97 | for (uint8_t r = 0 ; r < 8 ; r++) { |
universe@25 | 98 | for (uint8_t f = 0 ; f < 8 ; f++) { |
universe@25 | 99 | if ((gamestate->board[r][f] & COLOR_MASK) == color) { |
universe@28 | 100 | memset(&(threats[threatcount]), 0, sizeof(Move)); |
universe@25 | 101 | threats[threatcount].piece = gamestate->board[r][f]; |
universe@25 | 102 | threats[threatcount].fromrow = r; |
universe@25 | 103 | threats[threatcount].fromfile = f; |
universe@25 | 104 | threats[threatcount].torow = row; |
universe@25 | 105 | threats[threatcount].tofile = file; |
universe@25 | 106 | threatcount++; |
universe@25 | 107 | } |
universe@25 | 108 | } |
universe@25 | 109 | } |
universe@25 | 110 | |
universe@25 | 111 | for (int i = 0 ; i < threatcount ; i++) { |
universe@25 | 112 | if (validate_move(gamestate, &(threats[i]))) { |
universe@28 | 113 | if (threat) { |
universe@28 | 114 | *threat = threats[i]; |
universe@28 | 115 | } |
universe@25 | 116 | return 1; |
universe@25 | 117 | } |
universe@25 | 118 | } |
universe@25 | 119 | |
universe@25 | 120 | return 0; |
universe@25 | 121 | } |
universe@19 | 122 | |
universe@23 | 123 | void apply_move(GameState *gamestate, Move *move) { |
universe@19 | 124 | uint8_t piece = move->piece & PIECE_MASK; |
universe@19 | 125 | uint8_t color = move->piece & COLOR_MASK; |
universe@19 | 126 | |
universe@19 | 127 | /* en passant capture */ |
universe@19 | 128 | if (move->capture && piece == PAWN && |
universe@23 | 129 | mdst(gamestate->board, move) == 0) { |
universe@23 | 130 | gamestate->board[move->fromrow][move->tofile] = 0; |
universe@19 | 131 | } |
universe@19 | 132 | |
universe@19 | 133 | /* remove old en passant threats */ |
universe@19 | 134 | for (uint8_t file = 0 ; file < 8 ; file++) { |
universe@23 | 135 | gamestate->board[3][file] &= ~ENPASSANT_THREAT; |
universe@23 | 136 | gamestate->board[4][file] &= ~ENPASSANT_THREAT; |
universe@19 | 137 | } |
universe@19 | 138 | |
universe@19 | 139 | /* add new en passant threat */ |
universe@19 | 140 | if (piece == PAWN && ( |
universe@19 | 141 | (move->fromrow == 1 && move->torow == 3) || |
universe@19 | 142 | (move->fromrow == 6 && move->torow == 4))) { |
universe@19 | 143 | move->piece |= ENPASSANT_THREAT; |
universe@19 | 144 | } |
universe@19 | 145 | |
universe@19 | 146 | /* move (and maybe capture or promote) */ |
universe@23 | 147 | msrc(gamestate->board, move) = 0; |
universe@19 | 148 | if (move->promotion) { |
universe@23 | 149 | mdst(gamestate->board, move) = move->promotion; |
universe@19 | 150 | } else { |
universe@23 | 151 | mdst(gamestate->board, move) = move->piece; |
universe@19 | 152 | } |
universe@19 | 153 | |
universe@19 | 154 | /* castling */ |
universe@19 | 155 | if (piece == KING && |
universe@19 | 156 | move->fromfile == fileidx('e')) { |
universe@19 | 157 | |
universe@19 | 158 | if (move->tofile == fileidx('g')) { |
universe@23 | 159 | gamestate->board[move->torow][fileidx('h')] = 0; |
universe@23 | 160 | gamestate->board[move->torow][fileidx('f')] = color|ROOK; |
universe@19 | 161 | } else if (move->tofile == fileidx('c')) { |
universe@23 | 162 | gamestate->board[move->torow][fileidx('a')] = 0; |
universe@23 | 163 | gamestate->board[move->torow][fileidx('d')] = color|ROOK; |
universe@19 | 164 | } |
universe@19 | 165 | } |
universe@28 | 166 | |
universe@23 | 167 | addmove(gamestate, move); |
universe@19 | 168 | } |
universe@19 | 169 | |
universe@23 | 170 | _Bool validate_move(GameState *gamestate, Move *move) { |
universe@19 | 171 | _Bool result; |
universe@19 | 172 | |
universe@19 | 173 | /* validate indices (don't trust opponent) */ |
universe@19 | 174 | if (!chkidx(move)) { |
universe@19 | 175 | return 0; |
universe@19 | 176 | } |
universe@19 | 177 | |
universe@21 | 178 | /* must move */ |
universe@21 | 179 | if (move->fromfile == move->tofile && move->fromrow == move->torow) { |
universe@21 | 180 | return 0; |
universe@21 | 181 | } |
universe@21 | 182 | |
universe@19 | 183 | /* does piece exist */ |
universe@23 | 184 | result = msrc(gamestate->board, move) == move->piece; |
universe@19 | 185 | |
universe@19 | 186 | /* can't capture own pieces */ |
universe@28 | 187 | uint8_t piececolor = (move->piece & COLOR_MASK); |
universe@28 | 188 | if ((mdst(gamestate->board, move) & COLOR_MASK) == piececolor) { |
universe@19 | 189 | return 0; |
universe@19 | 190 | } |
universe@19 | 191 | |
universe@19 | 192 | /* validate individual rules */ |
universe@19 | 193 | switch (move->piece & PIECE_MASK) { |
universe@19 | 194 | case PAWN: |
universe@23 | 195 | result = result && pawn_chkrules(gamestate, move); |
universe@23 | 196 | result = result && !pawn_isblocked(gamestate, move); |
universe@19 | 197 | break; |
universe@19 | 198 | case ROOK: |
universe@19 | 199 | result = result && rook_chkrules(move); |
universe@23 | 200 | result = result && !rook_isblocked(gamestate, move); |
universe@19 | 201 | break; |
universe@19 | 202 | case KNIGHT: |
universe@19 | 203 | result = result && knight_chkrules(move); |
universe@23 | 204 | result = result && !knight_isblocked(gamestate, move); |
universe@19 | 205 | break; |
universe@19 | 206 | case BISHOP: |
universe@19 | 207 | result = result && bishop_chkrules(move); |
universe@23 | 208 | result = result && !bishop_isblocked(gamestate, move); |
universe@19 | 209 | break; |
universe@19 | 210 | case QUEEN: |
universe@19 | 211 | result = result && queen_chkrules(move); |
universe@23 | 212 | result = result && !queen_isblocked(gamestate, move); |
universe@19 | 213 | break; |
universe@19 | 214 | case KING: |
universe@23 | 215 | result = result && king_chkrules(gamestate, move); |
universe@23 | 216 | result = result && !king_isblocked(gamestate, move); |
universe@19 | 217 | break; |
universe@19 | 218 | default: |
universe@19 | 219 | result = 0; |
universe@19 | 220 | } |
universe@19 | 221 | |
universe@25 | 222 | /* cancel processing to avoid recursion overflow with is_covered() */ |
universe@25 | 223 | if (!result) { |
universe@25 | 224 | return 0; |
universe@25 | 225 | } |
universe@25 | 226 | |
universe@28 | 227 | /* find kings for check validation */ |
universe@28 | 228 | uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0; |
universe@28 | 229 | for (uint8_t row = 0 ; row < 8 ; row++) { |
universe@28 | 230 | for (uint8_t file = 0 ; file < 8 ; file++) { |
universe@28 | 231 | if (gamestate->board[row][file] == |
universe@28 | 232 | (piececolor == WHITE?WKING:BKING)) { |
universe@28 | 233 | mykingfile = file; |
universe@28 | 234 | mykingrow = row; |
universe@28 | 235 | } else if (gamestate->board[row][file] == |
universe@28 | 236 | (piececolor == WHITE?BKING:WKING)) { |
universe@28 | 237 | opkingfile = file; |
universe@28 | 238 | opkingrow = row; |
universe@28 | 239 | } |
universe@28 | 240 | } |
universe@28 | 241 | } |
universe@28 | 242 | |
universe@28 | 243 | /* simulation move for check validation */ |
universe@28 | 244 | GameState simulation; |
universe@28 | 245 | memcpy(&simulation, gamestate, sizeof(GameState)); |
universe@28 | 246 | apply_move(&simulation, move); |
universe@28 | 247 | |
universe@28 | 248 | /* don't move into or stay in check position */ |
universe@28 | 249 | if (is_covered(&simulation, mykingrow, mykingfile, |
universe@28 | 250 | opponent_color(piececolor))) { |
universe@28 | 251 | return 0; |
universe@28 | 252 | } |
universe@19 | 253 | |
universe@25 | 254 | /* correct check and checkmate flags (move is still valid) */ |
universe@28 | 255 | Move threat; |
universe@28 | 256 | move->check = get_any_threat_for(&simulation, opkingrow, opkingfile, |
universe@28 | 257 | piececolor, &threat); |
universe@19 | 258 | |
universe@28 | 259 | if (move->check) { |
universe@28 | 260 | /* determine possible escape fields */ |
universe@28 | 261 | _Bool canescape = 0; |
universe@28 | 262 | for (int dr = -1 ; dr <= 1 && !canescape ; dr++) { |
universe@28 | 263 | for (int df = -1 ; df <= 1 && !canescape ; df++) { |
universe@28 | 264 | if (!(dr == 0 && df == 0) && |
universe@28 | 265 | isidx(opkingrow + dr) && isidx(opkingfile + df)) { |
universe@28 | 266 | |
universe@28 | 267 | /* escape field neither blocked nor covered */ |
universe@28 | 268 | if ((simulation.board[opkingrow + dr][opkingfile + df] |
universe@28 | 269 | & COLOR_MASK) != opponent_color(piececolor)) { |
universe@28 | 270 | canescape |= !is_covered(&simulation, |
universe@28 | 271 | opkingrow + dr, opkingfile + df, piececolor); |
universe@28 | 272 | } |
universe@28 | 273 | } |
universe@28 | 274 | } |
universe@28 | 275 | } |
universe@28 | 276 | /* can't escape, can we capture? */ |
universe@28 | 277 | if (!canescape) { |
universe@28 | 278 | canescape = is_covered(&simulation, threat.fromrow, |
universe@28 | 279 | threat.fromfile, opponent_color(piececolor)); |
universe@28 | 280 | |
universe@28 | 281 | /* can't capture, can we block? */ |
universe@28 | 282 | // TODO: make it so |
universe@28 | 283 | |
universe@28 | 284 | if (!canescape) { |
universe@28 | 285 | gamestate->checkmate = 1; |
universe@28 | 286 | } |
universe@28 | 287 | } |
universe@28 | 288 | } |
universe@28 | 289 | |
universe@28 | 290 | return 1; |
universe@19 | 291 | } |
universe@19 | 292 | |
universe@23 | 293 | int eval_move(GameState *gamestate, char *mstr, Move *move) { |
universe@19 | 294 | memset(move, 0, sizeof(Move)); |
universe@19 | 295 | move->fromfile = POS_UNSPECIFIED; |
universe@19 | 296 | move->fromrow = POS_UNSPECIFIED; |
universe@19 | 297 | |
universe@19 | 298 | size_t len = strlen(mstr); |
universe@19 | 299 | |
universe@19 | 300 | /* evaluate check/checkmate flags */ |
universe@19 | 301 | if (mstr[len-1] == '+') { |
universe@19 | 302 | len--; mstr[len] = '\0'; |
universe@19 | 303 | move->check = 1; |
universe@19 | 304 | } else if (mstr[len-1] == '#') { |
universe@19 | 305 | len--; mstr[len] = '\0'; |
universe@27 | 306 | /* ignore - validation should set game state */ |
universe@19 | 307 | } |
universe@19 | 308 | |
universe@19 | 309 | /* evaluate promotion */ |
universe@19 | 310 | if (len > 3 && mstr[len-2] == '=') { |
universe@19 | 311 | move->promotion = getpiece(mstr[len-1]); |
universe@19 | 312 | if (!move->promotion) { |
universe@19 | 313 | return INVALID_MOVE_SYNTAX; |
universe@19 | 314 | } else { |
universe@23 | 315 | move->promotion |= gamestate->mycolor; |
universe@19 | 316 | len -= 2; |
universe@19 | 317 | mstr[len] = 0; |
universe@19 | 318 | } |
universe@19 | 319 | } |
universe@19 | 320 | |
universe@19 | 321 | if (len == 2) { |
universe@19 | 322 | /* pawn move (e.g. "e4") */ |
universe@19 | 323 | move->piece = PAWN; |
universe@19 | 324 | move->tofile = fileidx(mstr[0]); |
universe@19 | 325 | move->torow = rowidx(mstr[1]); |
universe@19 | 326 | } else if (len == 3) { |
universe@19 | 327 | if (strcmp(mstr, "O-O") == 0) { |
universe@19 | 328 | /* king side castling */ |
universe@19 | 329 | move->piece = KING; |
universe@19 | 330 | move->fromfile = fileidx('e'); |
universe@19 | 331 | move->tofile = fileidx('g'); |
universe@23 | 332 | move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7; |
universe@19 | 333 | } else { |
universe@19 | 334 | /* move (e.g. "Nf3") */ |
universe@19 | 335 | move->piece = getpiece(mstr[0]); |
universe@19 | 336 | move->tofile = fileidx(mstr[1]); |
universe@19 | 337 | move->torow = rowidx(mstr[2]); |
universe@19 | 338 | } |
universe@19 | 339 | |
universe@19 | 340 | } else if (len == 4) { |
universe@19 | 341 | move->piece = getpiece(mstr[0]); |
universe@19 | 342 | if (!move->piece) { |
universe@19 | 343 | move->piece = PAWN; |
universe@19 | 344 | move->fromfile = fileidx(mstr[0]); |
universe@19 | 345 | } |
universe@19 | 346 | if (mstr[1] == 'x') { |
universe@19 | 347 | /* capture (e.g. "Nxf3", "dxe5") */ |
universe@19 | 348 | move->capture = 1; |
universe@19 | 349 | } else { |
universe@19 | 350 | /* move (e.g. "Ndf3", "N2c3", "e2e4") */ |
universe@19 | 351 | if (isfile(mstr[1])) { |
universe@19 | 352 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 353 | if (move->piece == PAWN) { |
universe@19 | 354 | move->piece = 0; |
universe@19 | 355 | } |
universe@19 | 356 | } else { |
universe@19 | 357 | move->fromrow = rowidx(mstr[1]); |
universe@19 | 358 | } |
universe@19 | 359 | } |
universe@19 | 360 | move->tofile = fileidx(mstr[2]); |
universe@19 | 361 | move->torow = rowidx(mstr[3]); |
universe@19 | 362 | } else if (len == 5) { |
universe@19 | 363 | if (strcmp(mstr, "O-O-O") == 0) { |
universe@19 | 364 | /* queen side castling "O-O-O" */ |
universe@19 | 365 | move->piece = KING; |
universe@19 | 366 | move->fromfile = fileidx('e'); |
universe@19 | 367 | move->tofile = fileidx('c'); |
universe@23 | 368 | move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7; |
universe@19 | 369 | } else { |
universe@19 | 370 | move->piece = getpiece(mstr[0]); |
universe@19 | 371 | if (mstr[2] == 'x') { |
universe@19 | 372 | move->capture = 1; |
universe@19 | 373 | if (move->piece) { |
universe@19 | 374 | /* capture (e.g. "Ndxf3") */ |
universe@19 | 375 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 376 | } else { |
universe@19 | 377 | /* long notation capture (e.g. "e5xf6") */ |
universe@19 | 378 | move->piece = PAWN; |
universe@19 | 379 | move->fromfile = fileidx(mstr[0]); |
universe@19 | 380 | move->fromrow = rowidx(mstr[1]); |
universe@19 | 381 | } |
universe@19 | 382 | } else { |
universe@19 | 383 | /* long notation move (e.g. "Nc5a4") */ |
universe@19 | 384 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 385 | move->fromrow = rowidx(mstr[2]); |
universe@19 | 386 | } |
universe@19 | 387 | move->tofile = fileidx(mstr[3]); |
universe@19 | 388 | move->torow = rowidx(mstr[4]); |
universe@19 | 389 | } |
universe@19 | 390 | } else if (len == 6) { |
universe@19 | 391 | /* long notation capture (e.g. "Nc5xf3") */ |
universe@19 | 392 | if (mstr[3] == 'x') { |
universe@19 | 393 | move->capture = 1; |
universe@19 | 394 | move->piece = getpiece(mstr[0]); |
universe@19 | 395 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 396 | move->fromrow = rowidx(mstr[2]); |
universe@19 | 397 | move->tofile = fileidx(mstr[4]); |
universe@19 | 398 | move->torow = rowidx(mstr[5]); |
universe@19 | 399 | } |
universe@19 | 400 | } |
universe@19 | 401 | |
universe@19 | 402 | |
universe@19 | 403 | if (move->piece) { |
universe@23 | 404 | if (move->piece == PAWN |
universe@23 | 405 | && move->torow == (gamestate->mycolor==WHITE?7:0) |
universe@19 | 406 | && !move->promotion) { |
universe@19 | 407 | return NEED_PROMOTION; |
universe@19 | 408 | } |
universe@19 | 409 | |
universe@23 | 410 | move->piece |= gamestate->mycolor; |
universe@19 | 411 | if (move->fromfile == POS_UNSPECIFIED |
universe@19 | 412 | || move->fromrow == POS_UNSPECIFIED) { |
universe@23 | 413 | return getlocation(gamestate, move); |
universe@19 | 414 | } else { |
universe@19 | 415 | return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; |
universe@19 | 416 | } |
universe@19 | 417 | } else { |
universe@19 | 418 | return INVALID_MOVE_SYNTAX; |
universe@19 | 419 | } |
universe@19 | 420 | } |