src/chess/rules.c

Mon, 31 Mar 2014 11:16:32 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 31 Mar 2014 11:16:32 +0200
changeset 19
6a26114297a1
child 21
2e5846019b4f
permissions
-rw-r--r--

moved chess rules to separate lib

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@19 130 /* does piece exist */
universe@19 131 result = msrc(board, move) == move->piece;
universe@19 132
universe@19 133 /* can't capture own pieces */
universe@19 134 if ((mdst(board, move) & COLOR_MASK) == (move->piece & COLOR_MASK)) {
universe@19 135 return 0;
universe@19 136 }
universe@19 137
universe@19 138 /* validate individual rules */
universe@19 139 switch (move->piece & PIECE_MASK) {
universe@19 140 case PAWN:
universe@19 141 result = result && pawn_chkrules(board, move);
universe@19 142 result = result && !pawn_isblocked(board, move);
universe@19 143 break;
universe@19 144 case ROOK:
universe@19 145 result = result && rook_chkrules(move);
universe@19 146 result = result && !rook_isblocked(board, move);
universe@19 147 break;
universe@19 148 case KNIGHT:
universe@19 149 result = result && knight_chkrules(move);
universe@19 150 result = result && !knight_isblocked(board, move);
universe@19 151 break;
universe@19 152 case BISHOP:
universe@19 153 result = result && bishop_chkrules(move);
universe@19 154 result = result && !bishop_isblocked(board, move);
universe@19 155 break;
universe@19 156 case QUEEN:
universe@19 157 result = result && queen_chkrules(move);
universe@19 158 result = result && !queen_isblocked(board, move);
universe@19 159 break;
universe@19 160 case KING:
universe@19 161 result = result && king_chkrules(board, move);
universe@19 162 result = result && !king_isblocked(board, move);
universe@19 163 break;
universe@19 164 default:
universe@19 165 result = 0;
universe@19 166 }
universe@19 167
universe@19 168 /* is piece pinned */
universe@19 169 // TODO: make it so
universe@19 170
universe@19 171 /* correct check and checkmate flags */
universe@19 172 // TODO: make it so
universe@19 173
universe@19 174 return result;
universe@19 175 }
universe@19 176
universe@19 177 int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
universe@19 178 memset(move, 0, sizeof(Move));
universe@19 179 move->fromfile = POS_UNSPECIFIED;
universe@19 180 move->fromrow = POS_UNSPECIFIED;
universe@19 181
universe@19 182 size_t len = strlen(mstr);
universe@19 183
universe@19 184 /* evaluate check/checkmate flags */
universe@19 185 if (mstr[len-1] == '+') {
universe@19 186 len--; mstr[len] = '\0';
universe@19 187 move->check = 1;
universe@19 188 } else if (mstr[len-1] == '#') {
universe@19 189 len--; mstr[len] = '\0';
universe@19 190 move->checkmate = 1;
universe@19 191 }
universe@19 192
universe@19 193 /* evaluate promotion */
universe@19 194 if (len > 3 && mstr[len-2] == '=') {
universe@19 195 move->promotion = getpiece(mstr[len-1]);
universe@19 196 if (!move->promotion) {
universe@19 197 return INVALID_MOVE_SYNTAX;
universe@19 198 } else {
universe@19 199 move->promotion |= mycolor;
universe@19 200 len -= 2;
universe@19 201 mstr[len] = 0;
universe@19 202 }
universe@19 203 }
universe@19 204
universe@19 205 if (len == 2) {
universe@19 206 /* pawn move (e.g. "e4") */
universe@19 207 move->piece = PAWN;
universe@19 208 move->tofile = fileidx(mstr[0]);
universe@19 209 move->torow = rowidx(mstr[1]);
universe@19 210 } else if (len == 3) {
universe@19 211 if (strcmp(mstr, "O-O") == 0) {
universe@19 212 /* king side castling */
universe@19 213 move->piece = KING;
universe@19 214 move->fromfile = fileidx('e');
universe@19 215 move->tofile = fileidx('g');
universe@19 216 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
universe@19 217 } else {
universe@19 218 /* move (e.g. "Nf3") */
universe@19 219 move->piece = getpiece(mstr[0]);
universe@19 220 move->tofile = fileidx(mstr[1]);
universe@19 221 move->torow = rowidx(mstr[2]);
universe@19 222 }
universe@19 223
universe@19 224 } else if (len == 4) {
universe@19 225 move->piece = getpiece(mstr[0]);
universe@19 226 if (!move->piece) {
universe@19 227 move->piece = PAWN;
universe@19 228 move->fromfile = fileidx(mstr[0]);
universe@19 229 }
universe@19 230 if (mstr[1] == 'x') {
universe@19 231 /* capture (e.g. "Nxf3", "dxe5") */
universe@19 232 move->capture = 1;
universe@19 233 } else {
universe@19 234 /* move (e.g. "Ndf3", "N2c3", "e2e4") */
universe@19 235 if (isfile(mstr[1])) {
universe@19 236 move->fromfile = fileidx(mstr[1]);
universe@19 237 if (move->piece == PAWN) {
universe@19 238 move->piece = 0;
universe@19 239 }
universe@19 240 } else {
universe@19 241 move->fromrow = rowidx(mstr[1]);
universe@19 242 }
universe@19 243 }
universe@19 244 move->tofile = fileidx(mstr[2]);
universe@19 245 move->torow = rowidx(mstr[3]);
universe@19 246 } else if (len == 5) {
universe@19 247 if (strcmp(mstr, "O-O-O") == 0) {
universe@19 248 /* queen side castling "O-O-O" */
universe@19 249 move->piece = KING;
universe@19 250 move->fromfile = fileidx('e');
universe@19 251 move->tofile = fileidx('c');
universe@19 252 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
universe@19 253 } else {
universe@19 254 move->piece = getpiece(mstr[0]);
universe@19 255 if (mstr[2] == 'x') {
universe@19 256 move->capture = 1;
universe@19 257 if (move->piece) {
universe@19 258 /* capture (e.g. "Ndxf3") */
universe@19 259 move->fromfile = fileidx(mstr[1]);
universe@19 260 } else {
universe@19 261 /* long notation capture (e.g. "e5xf6") */
universe@19 262 move->piece = PAWN;
universe@19 263 move->fromfile = fileidx(mstr[0]);
universe@19 264 move->fromrow = rowidx(mstr[1]);
universe@19 265 }
universe@19 266 } else {
universe@19 267 /* long notation move (e.g. "Nc5a4") */
universe@19 268 move->fromfile = fileidx(mstr[1]);
universe@19 269 move->fromrow = rowidx(mstr[2]);
universe@19 270 }
universe@19 271 move->tofile = fileidx(mstr[3]);
universe@19 272 move->torow = rowidx(mstr[4]);
universe@19 273 }
universe@19 274 } else if (len == 6) {
universe@19 275 /* long notation capture (e.g. "Nc5xf3") */
universe@19 276 if (mstr[3] == 'x') {
universe@19 277 move->capture = 1;
universe@19 278 move->piece = getpiece(mstr[0]);
universe@19 279 move->fromfile = fileidx(mstr[1]);
universe@19 280 move->fromrow = rowidx(mstr[2]);
universe@19 281 move->tofile = fileidx(mstr[4]);
universe@19 282 move->torow = rowidx(mstr[5]);
universe@19 283 }
universe@19 284 }
universe@19 285
universe@19 286
universe@19 287 if (move->piece) {
universe@19 288 if (move->piece == PAWN && move->torow == (mycolor==WHITE?7:0)
universe@19 289 && !move->promotion) {
universe@19 290 return NEED_PROMOTION;
universe@19 291 }
universe@19 292
universe@19 293 move->piece |= mycolor;
universe@19 294 if (move->fromfile == POS_UNSPECIFIED
universe@19 295 || move->fromrow == POS_UNSPECIFIED) {
universe@19 296 return getlocation(board, move);
universe@19 297 } else {
universe@19 298 return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;
universe@19 299 }
universe@19 300 } else {
universe@19 301 return INVALID_MOVE_SYNTAX;
universe@19 302 }
universe@19 303 }

mercurial