Mon, 07 Apr 2014 14:08:57 +0200
fixed checkmate and completed implementation (more testing is still advised)
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@23 | 93 | void apply_move(GameState *gamestate, Move *move) { |
universe@19 | 94 | uint8_t piece = move->piece & PIECE_MASK; |
universe@19 | 95 | uint8_t color = move->piece & COLOR_MASK; |
universe@19 | 96 | |
universe@19 | 97 | /* en passant capture */ |
universe@19 | 98 | if (move->capture && piece == PAWN && |
universe@23 | 99 | mdst(gamestate->board, move) == 0) { |
universe@23 | 100 | gamestate->board[move->fromrow][move->tofile] = 0; |
universe@19 | 101 | } |
universe@19 | 102 | |
universe@19 | 103 | /* remove old en passant threats */ |
universe@19 | 104 | for (uint8_t file = 0 ; file < 8 ; file++) { |
universe@23 | 105 | gamestate->board[3][file] &= ~ENPASSANT_THREAT; |
universe@23 | 106 | gamestate->board[4][file] &= ~ENPASSANT_THREAT; |
universe@19 | 107 | } |
universe@19 | 108 | |
universe@19 | 109 | /* add new en passant threat */ |
universe@19 | 110 | if (piece == PAWN && ( |
universe@19 | 111 | (move->fromrow == 1 && move->torow == 3) || |
universe@19 | 112 | (move->fromrow == 6 && move->torow == 4))) { |
universe@19 | 113 | move->piece |= ENPASSANT_THREAT; |
universe@19 | 114 | } |
universe@19 | 115 | |
universe@19 | 116 | /* move (and maybe capture or promote) */ |
universe@23 | 117 | msrc(gamestate->board, move) = 0; |
universe@19 | 118 | if (move->promotion) { |
universe@23 | 119 | mdst(gamestate->board, move) = move->promotion; |
universe@19 | 120 | } else { |
universe@23 | 121 | mdst(gamestate->board, move) = move->piece; |
universe@19 | 122 | } |
universe@19 | 123 | |
universe@19 | 124 | /* castling */ |
universe@19 | 125 | if (piece == KING && |
universe@19 | 126 | move->fromfile == fileidx('e')) { |
universe@19 | 127 | |
universe@19 | 128 | if (move->tofile == fileidx('g')) { |
universe@23 | 129 | gamestate->board[move->torow][fileidx('h')] = 0; |
universe@23 | 130 | gamestate->board[move->torow][fileidx('f')] = color|ROOK; |
universe@19 | 131 | } else if (move->tofile == fileidx('c')) { |
universe@23 | 132 | gamestate->board[move->torow][fileidx('a')] = 0; |
universe@23 | 133 | gamestate->board[move->torow][fileidx('d')] = color|ROOK; |
universe@19 | 134 | } |
universe@19 | 135 | } |
universe@28 | 136 | |
universe@23 | 137 | addmove(gamestate, move); |
universe@19 | 138 | } |
universe@19 | 139 | |
universe@29 | 140 | static _Bool validate_move_rules(GameState *gamestate, Move *move) { |
universe@19 | 141 | /* validate indices (don't trust opponent) */ |
universe@19 | 142 | if (!chkidx(move)) { |
universe@19 | 143 | return 0; |
universe@19 | 144 | } |
universe@19 | 145 | |
universe@21 | 146 | /* must move */ |
universe@21 | 147 | if (move->fromfile == move->tofile && move->fromrow == move->torow) { |
universe@21 | 148 | return 0; |
universe@21 | 149 | } |
universe@21 | 150 | |
universe@19 | 151 | /* does piece exist */ |
universe@29 | 152 | if (msrc(gamestate->board, move) != move->piece) { |
universe@29 | 153 | return 0; |
universe@29 | 154 | } |
universe@19 | 155 | |
universe@19 | 156 | /* can't capture own pieces */ |
universe@29 | 157 | if ((mdst(gamestate->board, move) & COLOR_MASK) |
universe@29 | 158 | == (move->piece & COLOR_MASK)) { |
universe@19 | 159 | return 0; |
universe@19 | 160 | } |
universe@19 | 161 | |
universe@19 | 162 | /* validate individual rules */ |
universe@19 | 163 | switch (move->piece & PIECE_MASK) { |
universe@19 | 164 | case PAWN: |
universe@29 | 165 | return pawn_chkrules(gamestate, move) && |
universe@29 | 166 | !pawn_isblocked(gamestate, move); |
universe@19 | 167 | case ROOK: |
universe@29 | 168 | return rook_chkrules(move) && |
universe@29 | 169 | !rook_isblocked(gamestate, move); |
universe@19 | 170 | case KNIGHT: |
universe@29 | 171 | return knight_chkrules(move); /* knight is never blocked */ |
universe@19 | 172 | case BISHOP: |
universe@29 | 173 | return bishop_chkrules(move) && |
universe@29 | 174 | !bishop_isblocked(gamestate, move); |
universe@19 | 175 | case QUEEN: |
universe@29 | 176 | return queen_chkrules(move) && |
universe@29 | 177 | !queen_isblocked(gamestate, move); |
universe@19 | 178 | case KING: |
universe@29 | 179 | return king_chkrules(gamestate, move) && |
universe@29 | 180 | !king_isblocked(gamestate, move); |
universe@19 | 181 | default: |
universe@29 | 182 | return 0; |
universe@19 | 183 | } |
universe@29 | 184 | } |
universe@29 | 185 | |
universe@29 | 186 | _Bool validate_move(GameState *gamestate, Move *move) { |
universe@19 | 187 | |
universe@29 | 188 | _Bool result = validate_move_rules(gamestate, move); |
universe@29 | 189 | |
universe@29 | 190 | /* cancel processing to save resources */ |
universe@25 | 191 | if (!result) { |
universe@25 | 192 | return 0; |
universe@25 | 193 | } |
universe@25 | 194 | |
universe@28 | 195 | /* find kings for check validation */ |
universe@29 | 196 | uint8_t piececolor = (move->piece & COLOR_MASK); |
universe@29 | 197 | |
universe@28 | 198 | uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 0; |
universe@28 | 199 | for (uint8_t row = 0 ; row < 8 ; row++) { |
universe@28 | 200 | for (uint8_t file = 0 ; file < 8 ; file++) { |
universe@28 | 201 | if (gamestate->board[row][file] == |
universe@28 | 202 | (piececolor == WHITE?WKING:BKING)) { |
universe@28 | 203 | mykingfile = file; |
universe@28 | 204 | mykingrow = row; |
universe@28 | 205 | } else if (gamestate->board[row][file] == |
universe@28 | 206 | (piececolor == WHITE?BKING:WKING)) { |
universe@28 | 207 | opkingfile = file; |
universe@28 | 208 | opkingrow = row; |
universe@28 | 209 | } |
universe@28 | 210 | } |
universe@28 | 211 | } |
universe@28 | 212 | |
universe@28 | 213 | /* simulation move for check validation */ |
universe@28 | 214 | GameState simulation; |
universe@28 | 215 | memcpy(&simulation, gamestate, sizeof(GameState)); |
universe@28 | 216 | apply_move(&simulation, move); |
universe@28 | 217 | |
universe@28 | 218 | /* don't move into or stay in check position */ |
universe@28 | 219 | if (is_covered(&simulation, mykingrow, mykingfile, |
universe@28 | 220 | opponent_color(piececolor))) { |
universe@28 | 221 | return 0; |
universe@28 | 222 | } |
universe@19 | 223 | |
universe@25 | 224 | /* correct check and checkmate flags (move is still valid) */ |
universe@29 | 225 | Move threats[16]; |
universe@29 | 226 | uint8_t threatcount; |
universe@29 | 227 | move->check = get_threats(&simulation, opkingrow, opkingfile, |
universe@29 | 228 | piececolor, threats, &threatcount); |
universe@19 | 229 | |
universe@28 | 230 | if (move->check) { |
universe@28 | 231 | /* determine possible escape fields */ |
universe@28 | 232 | _Bool canescape = 0; |
universe@28 | 233 | for (int dr = -1 ; dr <= 1 && !canescape ; dr++) { |
universe@28 | 234 | for (int df = -1 ; df <= 1 && !canescape ; df++) { |
universe@28 | 235 | if (!(dr == 0 && df == 0) && |
universe@28 | 236 | isidx(opkingrow + dr) && isidx(opkingfile + df)) { |
universe@28 | 237 | |
universe@28 | 238 | /* escape field neither blocked nor covered */ |
universe@28 | 239 | if ((simulation.board[opkingrow + dr][opkingfile + df] |
universe@28 | 240 | & COLOR_MASK) != opponent_color(piececolor)) { |
universe@28 | 241 | canescape |= !is_covered(&simulation, |
universe@28 | 242 | opkingrow + dr, opkingfile + df, piececolor); |
universe@28 | 243 | } |
universe@28 | 244 | } |
universe@28 | 245 | } |
universe@28 | 246 | } |
universe@29 | 247 | /* can't escape, can he capture? */ |
universe@29 | 248 | if (!canescape && threatcount == 1) { |
universe@29 | 249 | canescape = is_attacked(&simulation, threats[0].fromrow, |
universe@29 | 250 | threats[0].fromfile, opponent_color(piececolor)); |
universe@29 | 251 | } |
universe@29 | 252 | |
universe@29 | 253 | /* can't capture, can he block? */ |
universe@29 | 254 | if (!canescape && threatcount == 1) { |
universe@29 | 255 | Move *threat = &(threats[0]); |
universe@29 | 256 | uint8_t threatpiece = threat->piece & PIECE_MASK; |
universe@29 | 257 | |
universe@29 | 258 | /* knight, pawns and the king cannot be blocked */ |
universe@29 | 259 | if (threatpiece == BISHOP || threatpiece == ROOK |
universe@29 | 260 | || threatpiece == QUEEN) { |
universe@29 | 261 | if (threat->fromrow == threat->torow) { |
universe@29 | 262 | /* rook aspect (on row) */ |
universe@29 | 263 | int d = threat->tofile > threat->fromfile ? 1 : -1; |
universe@29 | 264 | uint8_t file = threat->fromfile; |
universe@29 | 265 | while (!canescape && file != threat->tofile - d) { |
universe@29 | 266 | file += d; |
universe@29 | 267 | canescape |= is_protected(&simulation, |
universe@29 | 268 | threat->torow, file, opponent_color(piececolor)); |
universe@29 | 269 | } |
universe@29 | 270 | } else if (threat->fromfile == threat->tofile) { |
universe@29 | 271 | /* rook aspect (on file) */ |
universe@29 | 272 | int d = threat->torow > threat->fromrow ? 1 : -1; |
universe@29 | 273 | uint8_t row = threat->fromrow; |
universe@29 | 274 | while (!canescape && row != threat->torow - d) { |
universe@29 | 275 | row += d; |
universe@29 | 276 | canescape |= is_protected(&simulation, |
universe@29 | 277 | row, threat->tofile, opponent_color(piececolor)); |
universe@29 | 278 | } |
universe@29 | 279 | } else { |
universe@29 | 280 | /* bishop aspect */ |
universe@29 | 281 | int dr = move->torow > move->fromrow ? 1 : -1; |
universe@29 | 282 | int df = move->tofile > move->fromfile ? 1 : -1; |
universe@29 | 283 | |
universe@29 | 284 | uint8_t row = move->fromrow; |
universe@29 | 285 | uint8_t file = move->fromfile; |
universe@29 | 286 | while (!canescape && file != move->tofile - df |
universe@29 | 287 | && row != move->torow - dr) { |
universe@29 | 288 | row += dr; |
universe@29 | 289 | file += df; |
universe@29 | 290 | canescape |= is_protected(&simulation, row, file, |
universe@29 | 291 | opponent_color(piececolor)); |
universe@29 | 292 | } |
universe@29 | 293 | } |
universe@29 | 294 | } |
universe@29 | 295 | } |
universe@29 | 296 | |
universe@28 | 297 | if (!canescape) { |
universe@29 | 298 | gamestate->checkmate = 1; |
universe@28 | 299 | } |
universe@28 | 300 | } |
universe@28 | 301 | |
universe@28 | 302 | return 1; |
universe@19 | 303 | } |
universe@19 | 304 | |
universe@23 | 305 | int eval_move(GameState *gamestate, char *mstr, Move *move) { |
universe@19 | 306 | memset(move, 0, sizeof(Move)); |
universe@19 | 307 | move->fromfile = POS_UNSPECIFIED; |
universe@19 | 308 | move->fromrow = POS_UNSPECIFIED; |
universe@19 | 309 | |
universe@19 | 310 | size_t len = strlen(mstr); |
universe@19 | 311 | |
universe@19 | 312 | /* evaluate check/checkmate flags */ |
universe@19 | 313 | if (mstr[len-1] == '+') { |
universe@19 | 314 | len--; mstr[len] = '\0'; |
universe@19 | 315 | move->check = 1; |
universe@19 | 316 | } else if (mstr[len-1] == '#') { |
universe@19 | 317 | len--; mstr[len] = '\0'; |
universe@27 | 318 | /* ignore - validation should set game state */ |
universe@19 | 319 | } |
universe@19 | 320 | |
universe@19 | 321 | /* evaluate promotion */ |
universe@19 | 322 | if (len > 3 && mstr[len-2] == '=') { |
universe@19 | 323 | move->promotion = getpiece(mstr[len-1]); |
universe@19 | 324 | if (!move->promotion) { |
universe@19 | 325 | return INVALID_MOVE_SYNTAX; |
universe@19 | 326 | } else { |
universe@23 | 327 | move->promotion |= gamestate->mycolor; |
universe@19 | 328 | len -= 2; |
universe@19 | 329 | mstr[len] = 0; |
universe@19 | 330 | } |
universe@19 | 331 | } |
universe@19 | 332 | |
universe@19 | 333 | if (len == 2) { |
universe@19 | 334 | /* pawn move (e.g. "e4") */ |
universe@19 | 335 | move->piece = PAWN; |
universe@19 | 336 | move->tofile = fileidx(mstr[0]); |
universe@19 | 337 | move->torow = rowidx(mstr[1]); |
universe@19 | 338 | } else if (len == 3) { |
universe@19 | 339 | if (strcmp(mstr, "O-O") == 0) { |
universe@19 | 340 | /* king side castling */ |
universe@19 | 341 | move->piece = KING; |
universe@19 | 342 | move->fromfile = fileidx('e'); |
universe@19 | 343 | move->tofile = fileidx('g'); |
universe@23 | 344 | move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7; |
universe@19 | 345 | } else { |
universe@19 | 346 | /* move (e.g. "Nf3") */ |
universe@19 | 347 | move->piece = getpiece(mstr[0]); |
universe@19 | 348 | move->tofile = fileidx(mstr[1]); |
universe@19 | 349 | move->torow = rowidx(mstr[2]); |
universe@19 | 350 | } |
universe@19 | 351 | |
universe@19 | 352 | } else if (len == 4) { |
universe@19 | 353 | move->piece = getpiece(mstr[0]); |
universe@19 | 354 | if (!move->piece) { |
universe@19 | 355 | move->piece = PAWN; |
universe@19 | 356 | move->fromfile = fileidx(mstr[0]); |
universe@19 | 357 | } |
universe@19 | 358 | if (mstr[1] == 'x') { |
universe@19 | 359 | /* capture (e.g. "Nxf3", "dxe5") */ |
universe@19 | 360 | move->capture = 1; |
universe@19 | 361 | } else { |
universe@19 | 362 | /* move (e.g. "Ndf3", "N2c3", "e2e4") */ |
universe@19 | 363 | if (isfile(mstr[1])) { |
universe@19 | 364 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 365 | if (move->piece == PAWN) { |
universe@19 | 366 | move->piece = 0; |
universe@19 | 367 | } |
universe@19 | 368 | } else { |
universe@19 | 369 | move->fromrow = rowidx(mstr[1]); |
universe@19 | 370 | } |
universe@19 | 371 | } |
universe@19 | 372 | move->tofile = fileidx(mstr[2]); |
universe@19 | 373 | move->torow = rowidx(mstr[3]); |
universe@19 | 374 | } else if (len == 5) { |
universe@19 | 375 | if (strcmp(mstr, "O-O-O") == 0) { |
universe@19 | 376 | /* queen side castling "O-O-O" */ |
universe@19 | 377 | move->piece = KING; |
universe@19 | 378 | move->fromfile = fileidx('e'); |
universe@19 | 379 | move->tofile = fileidx('c'); |
universe@23 | 380 | move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7; |
universe@19 | 381 | } else { |
universe@19 | 382 | move->piece = getpiece(mstr[0]); |
universe@19 | 383 | if (mstr[2] == 'x') { |
universe@19 | 384 | move->capture = 1; |
universe@19 | 385 | if (move->piece) { |
universe@19 | 386 | /* capture (e.g. "Ndxf3") */ |
universe@19 | 387 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 388 | } else { |
universe@19 | 389 | /* long notation capture (e.g. "e5xf6") */ |
universe@19 | 390 | move->piece = PAWN; |
universe@19 | 391 | move->fromfile = fileidx(mstr[0]); |
universe@19 | 392 | move->fromrow = rowidx(mstr[1]); |
universe@19 | 393 | } |
universe@19 | 394 | } else { |
universe@19 | 395 | /* long notation move (e.g. "Nc5a4") */ |
universe@19 | 396 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 397 | move->fromrow = rowidx(mstr[2]); |
universe@19 | 398 | } |
universe@19 | 399 | move->tofile = fileidx(mstr[3]); |
universe@19 | 400 | move->torow = rowidx(mstr[4]); |
universe@19 | 401 | } |
universe@19 | 402 | } else if (len == 6) { |
universe@19 | 403 | /* long notation capture (e.g. "Nc5xf3") */ |
universe@19 | 404 | if (mstr[3] == 'x') { |
universe@19 | 405 | move->capture = 1; |
universe@19 | 406 | move->piece = getpiece(mstr[0]); |
universe@19 | 407 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 408 | move->fromrow = rowidx(mstr[2]); |
universe@19 | 409 | move->tofile = fileidx(mstr[4]); |
universe@19 | 410 | move->torow = rowidx(mstr[5]); |
universe@19 | 411 | } |
universe@19 | 412 | } |
universe@19 | 413 | |
universe@19 | 414 | |
universe@19 | 415 | if (move->piece) { |
universe@23 | 416 | if (move->piece == PAWN |
universe@23 | 417 | && move->torow == (gamestate->mycolor==WHITE?7:0) |
universe@19 | 418 | && !move->promotion) { |
universe@19 | 419 | return NEED_PROMOTION; |
universe@19 | 420 | } |
universe@19 | 421 | |
universe@23 | 422 | move->piece |= gamestate->mycolor; |
universe@19 | 423 | if (move->fromfile == POS_UNSPECIFIED |
universe@19 | 424 | || move->fromrow == POS_UNSPECIFIED) { |
universe@23 | 425 | return getlocation(gamestate, move); |
universe@19 | 426 | } else { |
universe@19 | 427 | return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; |
universe@19 | 428 | } |
universe@19 | 429 | } else { |
universe@19 | 430 | return INVALID_MOVE_SYNTAX; |
universe@19 | 431 | } |
universe@19 | 432 | } |
universe@29 | 433 | |
universe@29 | 434 | _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file, |
universe@29 | 435 | uint8_t color, Move *threats, uint8_t *threatcount) { |
universe@29 | 436 | Move candidates[16]; |
universe@29 | 437 | int candidatecount = 0; |
universe@29 | 438 | for (uint8_t r = 0 ; r < 8 ; r++) { |
universe@29 | 439 | for (uint8_t f = 0 ; f < 8 ; f++) { |
universe@29 | 440 | if ((gamestate->board[r][f] & COLOR_MASK) == color) { |
universe@29 | 441 | memset(&(candidates[candidatecount]), 0, sizeof(Move)); |
universe@29 | 442 | candidates[candidatecount].piece = gamestate->board[r][f]; |
universe@29 | 443 | candidates[candidatecount].fromrow = r; |
universe@29 | 444 | candidates[candidatecount].fromfile = f; |
universe@29 | 445 | candidates[candidatecount].torow = row; |
universe@29 | 446 | candidates[candidatecount].tofile = file; |
universe@29 | 447 | candidatecount++; |
universe@29 | 448 | } |
universe@29 | 449 | } |
universe@29 | 450 | } |
universe@29 | 451 | |
universe@29 | 452 | if (threatcount) { |
universe@29 | 453 | *threatcount = 0; |
universe@29 | 454 | } |
universe@29 | 455 | |
universe@29 | 456 | |
universe@29 | 457 | _Bool result = 0; |
universe@29 | 458 | |
universe@29 | 459 | for (int i = 0 ; i < candidatecount ; i++) { |
universe@29 | 460 | if (validate_move_rules(gamestate, &(candidates[i]))) { |
universe@29 | 461 | result = 1; |
universe@29 | 462 | if (threats && threatcount) { |
universe@29 | 463 | threats[(*threatcount)++] = candidates[i]; |
universe@29 | 464 | } |
universe@29 | 465 | } |
universe@29 | 466 | } |
universe@29 | 467 | |
universe@29 | 468 | return result; |
universe@29 | 469 | } |
universe@29 | 470 | |
universe@29 | 471 | _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file, |
universe@29 | 472 | uint8_t color, Move *threats, uint8_t *threatcount) { |
universe@29 | 473 | |
universe@29 | 474 | Move candidates[16]; |
universe@29 | 475 | uint8_t candidatecount; |
universe@29 | 476 | if (get_threats(gamestate, row, file, color, candidates, &candidatecount)) { |
universe@29 | 477 | |
universe@29 | 478 | if (threatcount) { |
universe@29 | 479 | *threatcount = 0; |
universe@29 | 480 | } |
universe@29 | 481 | _Bool result = 0; |
universe@29 | 482 | uint8_t kingfile = 0, kingrow = 0; |
universe@29 | 483 | for (uint8_t row = 0 ; row < 8 ; row++) { |
universe@29 | 484 | for (uint8_t file = 0 ; file < 8 ; file++) { |
universe@29 | 485 | if ((gamestate->board[row][file] & COLOR_MASK) == color) { |
universe@29 | 486 | kingfile = file; |
universe@29 | 487 | kingrow = row; |
universe@29 | 488 | } |
universe@29 | 489 | } |
universe@29 | 490 | } |
universe@29 | 491 | |
universe@29 | 492 | for (uint8_t i = 0 ; i < candidatecount ; i++) { |
universe@29 | 493 | GameState simulation; |
universe@29 | 494 | memcpy(&simulation, gamestate, sizeof(GameState)); |
universe@29 | 495 | apply_move(&simulation, &(candidates[i])); |
universe@29 | 496 | if (!is_covered(&simulation, kingrow, kingfile, |
universe@29 | 497 | opponent_color(color))) { |
universe@29 | 498 | result = 1; |
universe@29 | 499 | if (threats && threatcount) { |
universe@29 | 500 | threats[(*threatcount)++] = candidates[i]; |
universe@29 | 501 | } |
universe@29 | 502 | } |
universe@29 | 503 | } |
universe@29 | 504 | |
universe@29 | 505 | return result; |
universe@29 | 506 | } else { |
universe@29 | 507 | return 0; |
universe@29 | 508 | } |
universe@29 | 509 | } |
universe@29 | 510 | |
universe@29 | 511 | _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file, |
universe@29 | 512 | uint8_t color) { |
universe@29 | 513 | |
universe@29 | 514 | Move threats[16]; |
universe@29 | 515 | uint8_t threatcount; |
universe@29 | 516 | if (get_real_threats(gamestate, row, file, color, threats, &threatcount)) { |
universe@29 | 517 | for (int i = 0 ; i < threatcount ; i++) { |
universe@29 | 518 | if (threats[i].piece != (color|KING)) { |
universe@29 | 519 | return 1; |
universe@29 | 520 | } |
universe@29 | 521 | } |
universe@29 | 522 | return 0; |
universe@29 | 523 | } else { |
universe@29 | 524 | return 0; |
universe@29 | 525 | } |
universe@29 | 526 | } |