1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/chess/rules.c Mon Mar 31 11:16:32 2014 +0200 1.3 @@ -0,0 +1,303 @@ 1.4 +/* 1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 1.6 + * 1.7 + * Copyright 2014 Mike Becker. All rights reserved. 1.8 + * 1.9 + * Redistribution and use in source and binary forms, with or without 1.10 + * modification, are permitted provided that the following conditions are met: 1.11 + * 1.12 + * 1. Redistributions of source code must retain the above copyright 1.13 + * notice, this list of conditions and the following disclaimer. 1.14 + * 1.15 + * 2. Redistributions in binary form must reproduce the above copyright 1.16 + * notice, this list of conditions and the following disclaimer in the 1.17 + * documentation and/or other materials provided with the distribution. 1.18 + * 1.19 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 1.20 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.21 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1.22 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 1.23 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 1.24 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 1.25 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 1.26 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 1.27 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 1.28 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 1.29 + * POSSIBILITY OF SUCH DAMAGE. 1.30 + * 1.31 + */ 1.32 + 1.33 +#include "rules.h" 1.34 +#include "chess.h" 1.35 +#include <string.h> 1.36 + 1.37 +char getpiecechr(uint8_t piece) { 1.38 + switch (piece & PIECE_MASK) { 1.39 + case ROOK: return 'R'; 1.40 + case KNIGHT: return 'N'; 1.41 + case BISHOP: return 'B'; 1.42 + case QUEEN: return 'Q'; 1.43 + case KING: return 'K'; 1.44 + default: return '\0'; 1.45 + } 1.46 +} 1.47 + 1.48 +uint8_t getpiece(char c) { 1.49 + switch (c) { 1.50 + case 'R': return ROOK; 1.51 + case 'N': return KNIGHT; 1.52 + case 'B': return BISHOP; 1.53 + case 'Q': return QUEEN; 1.54 + case 'K': return KING; 1.55 + default: return 0; 1.56 + } 1.57 +} 1.58 + 1.59 +/** 1.60 + * Guesses the location of a piece for short algebraic notation. 1.61 + * 1.62 + * @param board the current state of the board 1.63 + * @param move the move date to operate on 1.64 + * @return status code (see rules/rules.h for the codes) 1.65 + */ 1.66 +static int getlocation(Board board, Move *move) { 1.67 + uint8_t piece = move->piece & PIECE_MASK; 1.68 + switch (piece) { 1.69 + case PAWN: return pawn_getlocation(board, move); 1.70 + case ROOK: return rook_getlocation(board, move); 1.71 + case KNIGHT: return knight_getlocation(board, move); 1.72 + case BISHOP: return bishop_getlocation(board, move); 1.73 + case QUEEN: return queen_getlocation(board, move); 1.74 + case KING: return king_getlocation(board, move); 1.75 + default: return INVALID_MOVE_SYNTAX; 1.76 + } 1.77 +} 1.78 + 1.79 + 1.80 +void apply_move(Board board, Move *move) { 1.81 + uint8_t piece = move->piece & PIECE_MASK; 1.82 + uint8_t color = move->piece & COLOR_MASK; 1.83 + 1.84 + /* en passant capture */ 1.85 + if (move->capture && piece == PAWN && 1.86 + mdst(board, move) == 0) { 1.87 + board[move->fromrow][move->tofile] = 0; 1.88 + } 1.89 + 1.90 + /* remove old en passant threats */ 1.91 + for (uint8_t file = 0 ; file < 8 ; file++) { 1.92 + board[3][file] &= ~ENPASSANT_THREAT; 1.93 + board[4][file] &= ~ENPASSANT_THREAT; 1.94 + } 1.95 + 1.96 + /* add new en passant threat */ 1.97 + if (piece == PAWN && ( 1.98 + (move->fromrow == 1 && move->torow == 3) || 1.99 + (move->fromrow == 6 && move->torow == 4))) { 1.100 + move->piece |= ENPASSANT_THREAT; 1.101 + } 1.102 + 1.103 + /* move (and maybe capture or promote) */ 1.104 + msrc(board, move) = 0; 1.105 + if (move->promotion) { 1.106 + mdst(board, move) = move->promotion; 1.107 + } else { 1.108 + mdst(board, move) = move->piece; 1.109 + } 1.110 + 1.111 + /* castling */ 1.112 + if (piece == KING && 1.113 + move->fromfile == fileidx('e')) { 1.114 + 1.115 + if (move->tofile == fileidx('g')) { 1.116 + board[move->torow][fileidx('h')] = 0; 1.117 + board[move->torow][fileidx('f')] = color|ROOK; 1.118 + } else if (move->tofile == fileidx('c')) { 1.119 + board[move->torow][fileidx('a')] = 0; 1.120 + board[move->torow][fileidx('d')] = color|ROOK; 1.121 + } 1.122 + } 1.123 +} 1.124 + 1.125 +_Bool validate_move(Board board, Move *move) { 1.126 + _Bool result; 1.127 + 1.128 + /* validate indices (don't trust opponent) */ 1.129 + if (!chkidx(move)) { 1.130 + return 0; 1.131 + } 1.132 + 1.133 + /* does piece exist */ 1.134 + result = msrc(board, move) == move->piece; 1.135 + 1.136 + /* can't capture own pieces */ 1.137 + if ((mdst(board, move) & COLOR_MASK) == (move->piece & COLOR_MASK)) { 1.138 + return 0; 1.139 + } 1.140 + 1.141 + /* validate individual rules */ 1.142 + switch (move->piece & PIECE_MASK) { 1.143 + case PAWN: 1.144 + result = result && pawn_chkrules(board, move); 1.145 + result = result && !pawn_isblocked(board, move); 1.146 + break; 1.147 + case ROOK: 1.148 + result = result && rook_chkrules(move); 1.149 + result = result && !rook_isblocked(board, move); 1.150 + break; 1.151 + case KNIGHT: 1.152 + result = result && knight_chkrules(move); 1.153 + result = result && !knight_isblocked(board, move); 1.154 + break; 1.155 + case BISHOP: 1.156 + result = result && bishop_chkrules(move); 1.157 + result = result && !bishop_isblocked(board, move); 1.158 + break; 1.159 + case QUEEN: 1.160 + result = result && queen_chkrules(move); 1.161 + result = result && !queen_isblocked(board, move); 1.162 + break; 1.163 + case KING: 1.164 + result = result && king_chkrules(board, move); 1.165 + result = result && !king_isblocked(board, move); 1.166 + break; 1.167 + default: 1.168 + result = 0; 1.169 + } 1.170 + 1.171 + /* is piece pinned */ 1.172 + // TODO: make it so 1.173 + 1.174 + /* correct check and checkmate flags */ 1.175 + // TODO: make it so 1.176 + 1.177 + return result; 1.178 +} 1.179 + 1.180 +int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) { 1.181 + memset(move, 0, sizeof(Move)); 1.182 + move->fromfile = POS_UNSPECIFIED; 1.183 + move->fromrow = POS_UNSPECIFIED; 1.184 + 1.185 + size_t len = strlen(mstr); 1.186 + 1.187 + /* evaluate check/checkmate flags */ 1.188 + if (mstr[len-1] == '+') { 1.189 + len--; mstr[len] = '\0'; 1.190 + move->check = 1; 1.191 + } else if (mstr[len-1] == '#') { 1.192 + len--; mstr[len] = '\0'; 1.193 + move->checkmate = 1; 1.194 + } 1.195 + 1.196 + /* evaluate promotion */ 1.197 + if (len > 3 && mstr[len-2] == '=') { 1.198 + move->promotion = getpiece(mstr[len-1]); 1.199 + if (!move->promotion) { 1.200 + return INVALID_MOVE_SYNTAX; 1.201 + } else { 1.202 + move->promotion |= mycolor; 1.203 + len -= 2; 1.204 + mstr[len] = 0; 1.205 + } 1.206 + } 1.207 + 1.208 + if (len == 2) { 1.209 + /* pawn move (e.g. "e4") */ 1.210 + move->piece = PAWN; 1.211 + move->tofile = fileidx(mstr[0]); 1.212 + move->torow = rowidx(mstr[1]); 1.213 + } else if (len == 3) { 1.214 + if (strcmp(mstr, "O-O") == 0) { 1.215 + /* king side castling */ 1.216 + move->piece = KING; 1.217 + move->fromfile = fileidx('e'); 1.218 + move->tofile = fileidx('g'); 1.219 + move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; 1.220 + } else { 1.221 + /* move (e.g. "Nf3") */ 1.222 + move->piece = getpiece(mstr[0]); 1.223 + move->tofile = fileidx(mstr[1]); 1.224 + move->torow = rowidx(mstr[2]); 1.225 + } 1.226 + 1.227 + } else if (len == 4) { 1.228 + move->piece = getpiece(mstr[0]); 1.229 + if (!move->piece) { 1.230 + move->piece = PAWN; 1.231 + move->fromfile = fileidx(mstr[0]); 1.232 + } 1.233 + if (mstr[1] == 'x') { 1.234 + /* capture (e.g. "Nxf3", "dxe5") */ 1.235 + move->capture = 1; 1.236 + } else { 1.237 + /* move (e.g. "Ndf3", "N2c3", "e2e4") */ 1.238 + if (isfile(mstr[1])) { 1.239 + move->fromfile = fileidx(mstr[1]); 1.240 + if (move->piece == PAWN) { 1.241 + move->piece = 0; 1.242 + } 1.243 + } else { 1.244 + move->fromrow = rowidx(mstr[1]); 1.245 + } 1.246 + } 1.247 + move->tofile = fileidx(mstr[2]); 1.248 + move->torow = rowidx(mstr[3]); 1.249 + } else if (len == 5) { 1.250 + if (strcmp(mstr, "O-O-O") == 0) { 1.251 + /* queen side castling "O-O-O" */ 1.252 + move->piece = KING; 1.253 + move->fromfile = fileidx('e'); 1.254 + move->tofile = fileidx('c'); 1.255 + move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; 1.256 + } else { 1.257 + move->piece = getpiece(mstr[0]); 1.258 + if (mstr[2] == 'x') { 1.259 + move->capture = 1; 1.260 + if (move->piece) { 1.261 + /* capture (e.g. "Ndxf3") */ 1.262 + move->fromfile = fileidx(mstr[1]); 1.263 + } else { 1.264 + /* long notation capture (e.g. "e5xf6") */ 1.265 + move->piece = PAWN; 1.266 + move->fromfile = fileidx(mstr[0]); 1.267 + move->fromrow = rowidx(mstr[1]); 1.268 + } 1.269 + } else { 1.270 + /* long notation move (e.g. "Nc5a4") */ 1.271 + move->fromfile = fileidx(mstr[1]); 1.272 + move->fromrow = rowidx(mstr[2]); 1.273 + } 1.274 + move->tofile = fileidx(mstr[3]); 1.275 + move->torow = rowidx(mstr[4]); 1.276 + } 1.277 + } else if (len == 6) { 1.278 + /* long notation capture (e.g. "Nc5xf3") */ 1.279 + if (mstr[3] == 'x') { 1.280 + move->capture = 1; 1.281 + move->piece = getpiece(mstr[0]); 1.282 + move->fromfile = fileidx(mstr[1]); 1.283 + move->fromrow = rowidx(mstr[2]); 1.284 + move->tofile = fileidx(mstr[4]); 1.285 + move->torow = rowidx(mstr[5]); 1.286 + } 1.287 + } 1.288 + 1.289 + 1.290 + if (move->piece) { 1.291 + if (move->piece == PAWN && move->torow == (mycolor==WHITE?7:0) 1.292 + && !move->promotion) { 1.293 + return NEED_PROMOTION; 1.294 + } 1.295 + 1.296 + move->piece |= mycolor; 1.297 + if (move->fromfile == POS_UNSPECIFIED 1.298 + || move->fromrow == POS_UNSPECIFIED) { 1.299 + return getlocation(board, move); 1.300 + } else { 1.301 + return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; 1.302 + } 1.303 + } else { 1.304 + return INVALID_MOVE_SYNTAX; 1.305 + } 1.306 +}