src/chess/rules.c

Mon, 31 Mar 2014 14:00:58 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 31 Mar 2014 14:00:58 +0200
changeset 21
2e5846019b4f
parent 19
6a26114297a1
child 23
824c9522ce66
permissions
-rw-r--r--

implemented rook + some fixes

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@19 33
universe@19 34 char getpiecechr(uint8_t piece) {
universe@19 35 switch (piece & PIECE_MASK) {
universe@19 36 case ROOK: return 'R';
universe@19 37 case KNIGHT: return 'N';
universe@19 38 case BISHOP: return 'B';
universe@19 39 case QUEEN: return 'Q';
universe@19 40 case KING: return 'K';
universe@19 41 default: return '\0';
universe@19 42 }
universe@19 43 }
universe@19 44
universe@19 45 uint8_t getpiece(char c) {
universe@19 46 switch (c) {
universe@19 47 case 'R': return ROOK;
universe@19 48 case 'N': return KNIGHT;
universe@19 49 case 'B': return BISHOP;
universe@19 50 case 'Q': return QUEEN;
universe@19 51 case 'K': return KING;
universe@19 52 default: return 0;
universe@19 53 }
universe@19 54 }
universe@19 55
universe@19 56 /**
universe@19 57 * Guesses the location of a piece for short algebraic notation.
universe@19 58 *
universe@19 59 * @param board the current state of the board
universe@19 60 * @param move the move date to operate on
universe@19 61 * @return status code (see rules/rules.h for the codes)
universe@19 62 */
universe@19 63 static int getlocation(Board board, Move *move) {
universe@19 64 uint8_t piece = move->piece & PIECE_MASK;
universe@19 65 switch (piece) {
universe@19 66 case PAWN: return pawn_getlocation(board, move);
universe@19 67 case ROOK: return rook_getlocation(board, move);
universe@19 68 case KNIGHT: return knight_getlocation(board, move);
universe@19 69 case BISHOP: return bishop_getlocation(board, move);
universe@19 70 case QUEEN: return queen_getlocation(board, move);
universe@19 71 case KING: return king_getlocation(board, move);
universe@19 72 default: return INVALID_MOVE_SYNTAX;
universe@19 73 }
universe@19 74 }
universe@19 75
universe@19 76
universe@19 77 void apply_move(Board board, Move *move) {
universe@19 78 uint8_t piece = move->piece & PIECE_MASK;
universe@19 79 uint8_t color = move->piece & COLOR_MASK;
universe@19 80
universe@19 81 /* en passant capture */
universe@19 82 if (move->capture && piece == PAWN &&
universe@19 83 mdst(board, move) == 0) {
universe@19 84 board[move->fromrow][move->tofile] = 0;
universe@19 85 }
universe@19 86
universe@19 87 /* remove old en passant threats */
universe@19 88 for (uint8_t file = 0 ; file < 8 ; file++) {
universe@19 89 board[3][file] &= ~ENPASSANT_THREAT;
universe@19 90 board[4][file] &= ~ENPASSANT_THREAT;
universe@19 91 }
universe@19 92
universe@19 93 /* add new en passant threat */
universe@19 94 if (piece == PAWN && (
universe@19 95 (move->fromrow == 1 && move->torow == 3) ||
universe@19 96 (move->fromrow == 6 && move->torow == 4))) {
universe@19 97 move->piece |= ENPASSANT_THREAT;
universe@19 98 }
universe@19 99
universe@19 100 /* move (and maybe capture or promote) */
universe@19 101 msrc(board, move) = 0;
universe@19 102 if (move->promotion) {
universe@19 103 mdst(board, move) = move->promotion;
universe@19 104 } else {
universe@19 105 mdst(board, move) = move->piece;
universe@19 106 }
universe@19 107
universe@19 108 /* castling */
universe@19 109 if (piece == KING &&
universe@19 110 move->fromfile == fileidx('e')) {
universe@19 111
universe@19 112 if (move->tofile == fileidx('g')) {
universe@19 113 board[move->torow][fileidx('h')] = 0;
universe@19 114 board[move->torow][fileidx('f')] = color|ROOK;
universe@19 115 } else if (move->tofile == fileidx('c')) {
universe@19 116 board[move->torow][fileidx('a')] = 0;
universe@19 117 board[move->torow][fileidx('d')] = color|ROOK;
universe@19 118 }
universe@19 119 }
universe@19 120 }
universe@19 121
universe@19 122 _Bool validate_move(Board board, Move *move) {
universe@19 123 _Bool result;
universe@19 124
universe@19 125 /* validate indices (don't trust opponent) */
universe@19 126 if (!chkidx(move)) {
universe@19 127 return 0;
universe@19 128 }
universe@19 129
universe@21 130 /* must move */
universe@21 131 if (move->fromfile == move->tofile && move->fromrow == move->torow) {
universe@21 132 return 0;
universe@21 133 }
universe@21 134
universe@19 135 /* does piece exist */
universe@19 136 result = msrc(board, move) == move->piece;
universe@19 137
universe@19 138 /* can't capture own pieces */
universe@19 139 if ((mdst(board, move) & COLOR_MASK) == (move->piece & COLOR_MASK)) {
universe@19 140 return 0;
universe@19 141 }
universe@19 142
universe@19 143 /* validate individual rules */
universe@19 144 switch (move->piece & PIECE_MASK) {
universe@19 145 case PAWN:
universe@19 146 result = result && pawn_chkrules(board, move);
universe@19 147 result = result && !pawn_isblocked(board, move);
universe@19 148 break;
universe@19 149 case ROOK:
universe@19 150 result = result && rook_chkrules(move);
universe@19 151 result = result && !rook_isblocked(board, move);
universe@19 152 break;
universe@19 153 case KNIGHT:
universe@19 154 result = result && knight_chkrules(move);
universe@19 155 result = result && !knight_isblocked(board, move);
universe@19 156 break;
universe@19 157 case BISHOP:
universe@19 158 result = result && bishop_chkrules(move);
universe@19 159 result = result && !bishop_isblocked(board, move);
universe@19 160 break;
universe@19 161 case QUEEN:
universe@19 162 result = result && queen_chkrules(move);
universe@19 163 result = result && !queen_isblocked(board, move);
universe@19 164 break;
universe@19 165 case KING:
universe@19 166 result = result && king_chkrules(board, move);
universe@19 167 result = result && !king_isblocked(board, move);
universe@19 168 break;
universe@19 169 default:
universe@19 170 result = 0;
universe@19 171 }
universe@19 172
universe@19 173 /* is piece pinned */
universe@19 174 // TODO: make it so
universe@19 175
universe@19 176 /* correct check and checkmate flags */
universe@19 177 // TODO: make it so
universe@19 178
universe@19 179 return result;
universe@19 180 }
universe@19 181
universe@19 182 int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
universe@19 183 memset(move, 0, sizeof(Move));
universe@19 184 move->fromfile = POS_UNSPECIFIED;
universe@19 185 move->fromrow = POS_UNSPECIFIED;
universe@19 186
universe@19 187 size_t len = strlen(mstr);
universe@19 188
universe@19 189 /* evaluate check/checkmate flags */
universe@19 190 if (mstr[len-1] == '+') {
universe@19 191 len--; mstr[len] = '\0';
universe@19 192 move->check = 1;
universe@19 193 } else if (mstr[len-1] == '#') {
universe@19 194 len--; mstr[len] = '\0';
universe@19 195 move->checkmate = 1;
universe@19 196 }
universe@19 197
universe@19 198 /* evaluate promotion */
universe@19 199 if (len > 3 && mstr[len-2] == '=') {
universe@19 200 move->promotion = getpiece(mstr[len-1]);
universe@19 201 if (!move->promotion) {
universe@19 202 return INVALID_MOVE_SYNTAX;
universe@19 203 } else {
universe@19 204 move->promotion |= mycolor;
universe@19 205 len -= 2;
universe@19 206 mstr[len] = 0;
universe@19 207 }
universe@19 208 }
universe@19 209
universe@19 210 if (len == 2) {
universe@19 211 /* pawn move (e.g. "e4") */
universe@19 212 move->piece = PAWN;
universe@19 213 move->tofile = fileidx(mstr[0]);
universe@19 214 move->torow = rowidx(mstr[1]);
universe@19 215 } else if (len == 3) {
universe@19 216 if (strcmp(mstr, "O-O") == 0) {
universe@19 217 /* king side castling */
universe@19 218 move->piece = KING;
universe@19 219 move->fromfile = fileidx('e');
universe@19 220 move->tofile = fileidx('g');
universe@19 221 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
universe@19 222 } else {
universe@19 223 /* move (e.g. "Nf3") */
universe@19 224 move->piece = getpiece(mstr[0]);
universe@19 225 move->tofile = fileidx(mstr[1]);
universe@19 226 move->torow = rowidx(mstr[2]);
universe@19 227 }
universe@19 228
universe@19 229 } else if (len == 4) {
universe@19 230 move->piece = getpiece(mstr[0]);
universe@19 231 if (!move->piece) {
universe@19 232 move->piece = PAWN;
universe@19 233 move->fromfile = fileidx(mstr[0]);
universe@19 234 }
universe@19 235 if (mstr[1] == 'x') {
universe@19 236 /* capture (e.g. "Nxf3", "dxe5") */
universe@19 237 move->capture = 1;
universe@19 238 } else {
universe@19 239 /* move (e.g. "Ndf3", "N2c3", "e2e4") */
universe@19 240 if (isfile(mstr[1])) {
universe@19 241 move->fromfile = fileidx(mstr[1]);
universe@19 242 if (move->piece == PAWN) {
universe@19 243 move->piece = 0;
universe@19 244 }
universe@19 245 } else {
universe@19 246 move->fromrow = rowidx(mstr[1]);
universe@19 247 }
universe@19 248 }
universe@19 249 move->tofile = fileidx(mstr[2]);
universe@19 250 move->torow = rowidx(mstr[3]);
universe@19 251 } else if (len == 5) {
universe@19 252 if (strcmp(mstr, "O-O-O") == 0) {
universe@19 253 /* queen side castling "O-O-O" */
universe@19 254 move->piece = KING;
universe@19 255 move->fromfile = fileidx('e');
universe@19 256 move->tofile = fileidx('c');
universe@19 257 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
universe@19 258 } else {
universe@19 259 move->piece = getpiece(mstr[0]);
universe@19 260 if (mstr[2] == 'x') {
universe@19 261 move->capture = 1;
universe@19 262 if (move->piece) {
universe@19 263 /* capture (e.g. "Ndxf3") */
universe@19 264 move->fromfile = fileidx(mstr[1]);
universe@19 265 } else {
universe@19 266 /* long notation capture (e.g. "e5xf6") */
universe@19 267 move->piece = PAWN;
universe@19 268 move->fromfile = fileidx(mstr[0]);
universe@19 269 move->fromrow = rowidx(mstr[1]);
universe@19 270 }
universe@19 271 } else {
universe@19 272 /* long notation move (e.g. "Nc5a4") */
universe@19 273 move->fromfile = fileidx(mstr[1]);
universe@19 274 move->fromrow = rowidx(mstr[2]);
universe@19 275 }
universe@19 276 move->tofile = fileidx(mstr[3]);
universe@19 277 move->torow = rowidx(mstr[4]);
universe@19 278 }
universe@19 279 } else if (len == 6) {
universe@19 280 /* long notation capture (e.g. "Nc5xf3") */
universe@19 281 if (mstr[3] == 'x') {
universe@19 282 move->capture = 1;
universe@19 283 move->piece = getpiece(mstr[0]);
universe@19 284 move->fromfile = fileidx(mstr[1]);
universe@19 285 move->fromrow = rowidx(mstr[2]);
universe@19 286 move->tofile = fileidx(mstr[4]);
universe@19 287 move->torow = rowidx(mstr[5]);
universe@19 288 }
universe@19 289 }
universe@19 290
universe@19 291
universe@19 292 if (move->piece) {
universe@19 293 if (move->piece == PAWN && move->torow == (mycolor==WHITE?7:0)
universe@19 294 && !move->promotion) {
universe@19 295 return NEED_PROMOTION;
universe@19 296 }
universe@19 297
universe@19 298 move->piece |= mycolor;
universe@19 299 if (move->fromfile == POS_UNSPECIFIED
universe@19 300 || move->fromrow == POS_UNSPECIFIED) {
universe@19 301 return getlocation(board, move);
universe@19 302 } else {
universe@19 303 return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;
universe@19 304 }
universe@19 305 } else {
universe@19 306 return INVALID_MOVE_SYNTAX;
universe@19 307 }
universe@19 308 }

mercurial