Mon, 31 Mar 2014 15:03:25 +0200
introduced game state structure
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@19 | 93 | |
universe@23 | 94 | void apply_move(GameState *gamestate, Move *move) { |
universe@19 | 95 | uint8_t piece = move->piece & PIECE_MASK; |
universe@19 | 96 | uint8_t color = move->piece & COLOR_MASK; |
universe@19 | 97 | |
universe@19 | 98 | /* en passant capture */ |
universe@19 | 99 | if (move->capture && piece == PAWN && |
universe@23 | 100 | mdst(gamestate->board, move) == 0) { |
universe@23 | 101 | gamestate->board[move->fromrow][move->tofile] = 0; |
universe@19 | 102 | } |
universe@19 | 103 | |
universe@19 | 104 | /* remove old en passant threats */ |
universe@19 | 105 | for (uint8_t file = 0 ; file < 8 ; file++) { |
universe@23 | 106 | gamestate->board[3][file] &= ~ENPASSANT_THREAT; |
universe@23 | 107 | gamestate->board[4][file] &= ~ENPASSANT_THREAT; |
universe@19 | 108 | } |
universe@19 | 109 | |
universe@19 | 110 | /* add new en passant threat */ |
universe@19 | 111 | if (piece == PAWN && ( |
universe@19 | 112 | (move->fromrow == 1 && move->torow == 3) || |
universe@19 | 113 | (move->fromrow == 6 && move->torow == 4))) { |
universe@19 | 114 | move->piece |= ENPASSANT_THREAT; |
universe@19 | 115 | } |
universe@19 | 116 | |
universe@19 | 117 | /* move (and maybe capture or promote) */ |
universe@23 | 118 | msrc(gamestate->board, move) = 0; |
universe@19 | 119 | if (move->promotion) { |
universe@23 | 120 | mdst(gamestate->board, move) = move->promotion; |
universe@19 | 121 | } else { |
universe@23 | 122 | mdst(gamestate->board, move) = move->piece; |
universe@19 | 123 | } |
universe@19 | 124 | |
universe@19 | 125 | /* castling */ |
universe@19 | 126 | if (piece == KING && |
universe@19 | 127 | move->fromfile == fileidx('e')) { |
universe@19 | 128 | |
universe@19 | 129 | if (move->tofile == fileidx('g')) { |
universe@23 | 130 | gamestate->board[move->torow][fileidx('h')] = 0; |
universe@23 | 131 | gamestate->board[move->torow][fileidx('f')] = color|ROOK; |
universe@19 | 132 | } else if (move->tofile == fileidx('c')) { |
universe@23 | 133 | gamestate->board[move->torow][fileidx('a')] = 0; |
universe@23 | 134 | gamestate->board[move->torow][fileidx('d')] = color|ROOK; |
universe@19 | 135 | } |
universe@19 | 136 | } |
universe@23 | 137 | |
universe@23 | 138 | addmove(gamestate, move); |
universe@19 | 139 | } |
universe@19 | 140 | |
universe@23 | 141 | _Bool validate_move(GameState *gamestate, Move *move) { |
universe@19 | 142 | _Bool result; |
universe@19 | 143 | |
universe@19 | 144 | /* validate indices (don't trust opponent) */ |
universe@19 | 145 | if (!chkidx(move)) { |
universe@19 | 146 | return 0; |
universe@19 | 147 | } |
universe@19 | 148 | |
universe@21 | 149 | /* must move */ |
universe@21 | 150 | if (move->fromfile == move->tofile && move->fromrow == move->torow) { |
universe@21 | 151 | return 0; |
universe@21 | 152 | } |
universe@21 | 153 | |
universe@19 | 154 | /* does piece exist */ |
universe@23 | 155 | result = msrc(gamestate->board, move) == move->piece; |
universe@19 | 156 | |
universe@19 | 157 | /* can't capture own pieces */ |
universe@23 | 158 | if ((mdst(gamestate->board, move) & COLOR_MASK) |
universe@23 | 159 | == (move->piece & COLOR_MASK)) { |
universe@19 | 160 | return 0; |
universe@19 | 161 | } |
universe@19 | 162 | |
universe@19 | 163 | /* validate individual rules */ |
universe@19 | 164 | switch (move->piece & PIECE_MASK) { |
universe@19 | 165 | case PAWN: |
universe@23 | 166 | result = result && pawn_chkrules(gamestate, move); |
universe@23 | 167 | result = result && !pawn_isblocked(gamestate, move); |
universe@19 | 168 | break; |
universe@19 | 169 | case ROOK: |
universe@19 | 170 | result = result && rook_chkrules(move); |
universe@23 | 171 | result = result && !rook_isblocked(gamestate, move); |
universe@19 | 172 | break; |
universe@19 | 173 | case KNIGHT: |
universe@19 | 174 | result = result && knight_chkrules(move); |
universe@23 | 175 | result = result && !knight_isblocked(gamestate, move); |
universe@19 | 176 | break; |
universe@19 | 177 | case BISHOP: |
universe@19 | 178 | result = result && bishop_chkrules(move); |
universe@23 | 179 | result = result && !bishop_isblocked(gamestate, move); |
universe@19 | 180 | break; |
universe@19 | 181 | case QUEEN: |
universe@19 | 182 | result = result && queen_chkrules(move); |
universe@23 | 183 | result = result && !queen_isblocked(gamestate, move); |
universe@19 | 184 | break; |
universe@19 | 185 | case KING: |
universe@23 | 186 | result = result && king_chkrules(gamestate, move); |
universe@23 | 187 | result = result && !king_isblocked(gamestate, move); |
universe@19 | 188 | break; |
universe@19 | 189 | default: |
universe@19 | 190 | result = 0; |
universe@19 | 191 | } |
universe@19 | 192 | |
universe@19 | 193 | /* is piece pinned */ |
universe@19 | 194 | // TODO: make it so |
universe@19 | 195 | |
universe@19 | 196 | /* correct check and checkmate flags */ |
universe@19 | 197 | // TODO: make it so |
universe@19 | 198 | |
universe@19 | 199 | return result; |
universe@19 | 200 | } |
universe@19 | 201 | |
universe@23 | 202 | int eval_move(GameState *gamestate, char *mstr, Move *move) { |
universe@19 | 203 | memset(move, 0, sizeof(Move)); |
universe@19 | 204 | move->fromfile = POS_UNSPECIFIED; |
universe@19 | 205 | move->fromrow = POS_UNSPECIFIED; |
universe@19 | 206 | |
universe@19 | 207 | size_t len = strlen(mstr); |
universe@19 | 208 | |
universe@19 | 209 | /* evaluate check/checkmate flags */ |
universe@19 | 210 | if (mstr[len-1] == '+') { |
universe@19 | 211 | len--; mstr[len] = '\0'; |
universe@19 | 212 | move->check = 1; |
universe@19 | 213 | } else if (mstr[len-1] == '#') { |
universe@19 | 214 | len--; mstr[len] = '\0'; |
universe@19 | 215 | move->checkmate = 1; |
universe@19 | 216 | } |
universe@19 | 217 | |
universe@19 | 218 | /* evaluate promotion */ |
universe@19 | 219 | if (len > 3 && mstr[len-2] == '=') { |
universe@19 | 220 | move->promotion = getpiece(mstr[len-1]); |
universe@19 | 221 | if (!move->promotion) { |
universe@19 | 222 | return INVALID_MOVE_SYNTAX; |
universe@19 | 223 | } else { |
universe@23 | 224 | move->promotion |= gamestate->mycolor; |
universe@19 | 225 | len -= 2; |
universe@19 | 226 | mstr[len] = 0; |
universe@19 | 227 | } |
universe@19 | 228 | } |
universe@19 | 229 | |
universe@19 | 230 | if (len == 2) { |
universe@19 | 231 | /* pawn move (e.g. "e4") */ |
universe@19 | 232 | move->piece = PAWN; |
universe@19 | 233 | move->tofile = fileidx(mstr[0]); |
universe@19 | 234 | move->torow = rowidx(mstr[1]); |
universe@19 | 235 | } else if (len == 3) { |
universe@19 | 236 | if (strcmp(mstr, "O-O") == 0) { |
universe@19 | 237 | /* king side castling */ |
universe@19 | 238 | move->piece = KING; |
universe@19 | 239 | move->fromfile = fileidx('e'); |
universe@19 | 240 | move->tofile = fileidx('g'); |
universe@23 | 241 | move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7; |
universe@19 | 242 | } else { |
universe@19 | 243 | /* move (e.g. "Nf3") */ |
universe@19 | 244 | move->piece = getpiece(mstr[0]); |
universe@19 | 245 | move->tofile = fileidx(mstr[1]); |
universe@19 | 246 | move->torow = rowidx(mstr[2]); |
universe@19 | 247 | } |
universe@19 | 248 | |
universe@19 | 249 | } else if (len == 4) { |
universe@19 | 250 | move->piece = getpiece(mstr[0]); |
universe@19 | 251 | if (!move->piece) { |
universe@19 | 252 | move->piece = PAWN; |
universe@19 | 253 | move->fromfile = fileidx(mstr[0]); |
universe@19 | 254 | } |
universe@19 | 255 | if (mstr[1] == 'x') { |
universe@19 | 256 | /* capture (e.g. "Nxf3", "dxe5") */ |
universe@19 | 257 | move->capture = 1; |
universe@19 | 258 | } else { |
universe@19 | 259 | /* move (e.g. "Ndf3", "N2c3", "e2e4") */ |
universe@19 | 260 | if (isfile(mstr[1])) { |
universe@19 | 261 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 262 | if (move->piece == PAWN) { |
universe@19 | 263 | move->piece = 0; |
universe@19 | 264 | } |
universe@19 | 265 | } else { |
universe@19 | 266 | move->fromrow = rowidx(mstr[1]); |
universe@19 | 267 | } |
universe@19 | 268 | } |
universe@19 | 269 | move->tofile = fileidx(mstr[2]); |
universe@19 | 270 | move->torow = rowidx(mstr[3]); |
universe@19 | 271 | } else if (len == 5) { |
universe@19 | 272 | if (strcmp(mstr, "O-O-O") == 0) { |
universe@19 | 273 | /* queen side castling "O-O-O" */ |
universe@19 | 274 | move->piece = KING; |
universe@19 | 275 | move->fromfile = fileidx('e'); |
universe@19 | 276 | move->tofile = fileidx('c'); |
universe@23 | 277 | move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7; |
universe@19 | 278 | } else { |
universe@19 | 279 | move->piece = getpiece(mstr[0]); |
universe@19 | 280 | if (mstr[2] == 'x') { |
universe@19 | 281 | move->capture = 1; |
universe@19 | 282 | if (move->piece) { |
universe@19 | 283 | /* capture (e.g. "Ndxf3") */ |
universe@19 | 284 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 285 | } else { |
universe@19 | 286 | /* long notation capture (e.g. "e5xf6") */ |
universe@19 | 287 | move->piece = PAWN; |
universe@19 | 288 | move->fromfile = fileidx(mstr[0]); |
universe@19 | 289 | move->fromrow = rowidx(mstr[1]); |
universe@19 | 290 | } |
universe@19 | 291 | } else { |
universe@19 | 292 | /* long notation move (e.g. "Nc5a4") */ |
universe@19 | 293 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 294 | move->fromrow = rowidx(mstr[2]); |
universe@19 | 295 | } |
universe@19 | 296 | move->tofile = fileidx(mstr[3]); |
universe@19 | 297 | move->torow = rowidx(mstr[4]); |
universe@19 | 298 | } |
universe@19 | 299 | } else if (len == 6) { |
universe@19 | 300 | /* long notation capture (e.g. "Nc5xf3") */ |
universe@19 | 301 | if (mstr[3] == 'x') { |
universe@19 | 302 | move->capture = 1; |
universe@19 | 303 | move->piece = getpiece(mstr[0]); |
universe@19 | 304 | move->fromfile = fileidx(mstr[1]); |
universe@19 | 305 | move->fromrow = rowidx(mstr[2]); |
universe@19 | 306 | move->tofile = fileidx(mstr[4]); |
universe@19 | 307 | move->torow = rowidx(mstr[5]); |
universe@19 | 308 | } |
universe@19 | 309 | } |
universe@19 | 310 | |
universe@19 | 311 | |
universe@19 | 312 | if (move->piece) { |
universe@23 | 313 | if (move->piece == PAWN |
universe@23 | 314 | && move->torow == (gamestate->mycolor==WHITE?7:0) |
universe@19 | 315 | && !move->promotion) { |
universe@19 | 316 | return NEED_PROMOTION; |
universe@19 | 317 | } |
universe@19 | 318 | |
universe@23 | 319 | move->piece |= gamestate->mycolor; |
universe@19 | 320 | if (move->fromfile == POS_UNSPECIFIED |
universe@19 | 321 | || move->fromrow == POS_UNSPECIFIED) { |
universe@23 | 322 | return getlocation(gamestate, move); |
universe@19 | 323 | } else { |
universe@19 | 324 | return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; |
universe@19 | 325 | } |
universe@19 | 326 | } else { |
universe@19 | 327 | return INVALID_MOVE_SYNTAX; |
universe@19 | 328 | } |
universe@19 | 329 | } |