Mon, 16 Jun 2014 13:45:31 +0200
added pgn parser and writer (without comment support yet) + minor refactorings
universe@10 | 1 | /* |
universe@10 | 2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
universe@10 | 3 | * |
universe@10 | 4 | * Copyright 2014 Mike Becker. All rights reserved. |
universe@10 | 5 | * |
universe@10 | 6 | * Redistribution and use in source and binary forms, with or without |
universe@10 | 7 | * modification, are permitted provided that the following conditions are met: |
universe@10 | 8 | * |
universe@10 | 9 | * 1. Redistributions of source code must retain the above copyright |
universe@10 | 10 | * notice, this list of conditions and the following disclaimer. |
universe@10 | 11 | * |
universe@10 | 12 | * 2. Redistributions in binary form must reproduce the above copyright |
universe@10 | 13 | * notice, this list of conditions and the following disclaimer in the |
universe@10 | 14 | * documentation and/or other materials provided with the distribution. |
universe@10 | 15 | * |
universe@10 | 16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
universe@10 | 17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
universe@10 | 18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
universe@10 | 19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
universe@10 | 20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
universe@10 | 21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
universe@10 | 22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
universe@10 | 23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
universe@10 | 24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
universe@10 | 25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
universe@10 | 26 | * POSSIBILITY OF SUCH DAMAGE. |
universe@10 | 27 | * |
universe@10 | 28 | */ |
universe@10 | 29 | |
universe@10 | 30 | #ifndef RULES_H |
universe@10 | 31 | #define RULES_H |
universe@10 | 32 | |
universe@19 | 33 | #include <stdint.h> |
universe@36 | 34 | #include <sys/time.h> |
universe@10 | 35 | |
universe@48 | 36 | #define VALID_MOVE_SYNTAX 0 |
universe@48 | 37 | #define VALID_MOVE_SEMANTICS 0 /* use same code for a success */ |
universe@48 | 38 | #define INVALID_MOVE_SYNTAX 1 |
universe@48 | 39 | #define INVALID_POSITION 2 |
universe@48 | 40 | #define AMBIGUOUS_MOVE 3 |
universe@48 | 41 | #define NEED_PROMOTION 4 |
universe@48 | 42 | #define PIECE_PINNED 5 |
universe@48 | 43 | #define KING_IN_CHECK 6 |
universe@48 | 44 | #define KING_MOVES_INTO_CHECK 7 |
universe@48 | 45 | #define RULES_VIOLATED 10 |
universe@16 | 46 | |
universe@19 | 47 | |
universe@19 | 48 | #define PIECE_MASK 0x0F |
universe@19 | 49 | #define COLOR_MASK 0x30 |
universe@19 | 50 | #define ENPASSANT_THREAT 0x40 |
universe@19 | 51 | |
universe@19 | 52 | #define WHITE 0x10 |
universe@19 | 53 | #define BLACK 0x20 |
universe@19 | 54 | |
universe@19 | 55 | #define PAWN 0x01 |
universe@19 | 56 | #define ROOK 0x02 |
universe@19 | 57 | #define KNIGHT 0x03 |
universe@19 | 58 | #define BISHOP 0x04 |
universe@19 | 59 | #define QUEEN 0x05 |
universe@19 | 60 | #define KING 0x06 |
universe@19 | 61 | |
universe@19 | 62 | #define WPAWN (WHITE|PAWN) |
universe@19 | 63 | #define WROOK (WHITE|ROOK) |
universe@19 | 64 | #define WKNIGHT (WHITE|KNIGHT) |
universe@19 | 65 | #define WBISHOP (WHITE|BISHOP) |
universe@19 | 66 | #define WQUEEN (WHITE|QUEEN) |
universe@19 | 67 | #define WKING (WHITE|KING) |
universe@19 | 68 | #define BPAWN (BLACK|PAWN) |
universe@19 | 69 | #define BROOK (BLACK|ROOK) |
universe@19 | 70 | #define BKNIGHT (BLACK|KNIGHT) |
universe@19 | 71 | #define BBISHOP (BLACK|BISHOP) |
universe@19 | 72 | #define BQUEEN (BLACK|QUEEN) |
universe@19 | 73 | #define BKING (BLACK|KING) |
universe@19 | 74 | |
universe@19 | 75 | typedef uint8_t Board[8][8]; |
universe@19 | 76 | |
universe@40 | 77 | struct movetimeval { |
universe@40 | 78 | uint64_t tv_sec; |
universe@40 | 79 | int32_t tv_usec; |
universe@40 | 80 | }; |
universe@40 | 81 | |
universe@19 | 82 | typedef struct { |
universe@19 | 83 | uint8_t piece; |
universe@19 | 84 | uint8_t fromfile; |
universe@19 | 85 | uint8_t fromrow; |
universe@19 | 86 | uint8_t tofile; |
universe@19 | 87 | uint8_t torow; |
universe@19 | 88 | uint8_t promotion; |
universe@41 | 89 | uint8_t check; |
universe@41 | 90 | uint8_t capture; |
universe@40 | 91 | struct movetimeval timestamp; |
universe@40 | 92 | struct movetimeval movetime; |
universe@49 | 93 | char string[8]; |
universe@19 | 94 | } Move; |
universe@19 | 95 | |
universe@23 | 96 | typedef struct MoveList MoveList; |
universe@23 | 97 | |
universe@23 | 98 | struct MoveList { |
universe@23 | 99 | Move move; |
universe@23 | 100 | MoveList* next; |
universe@23 | 101 | }; |
universe@23 | 102 | |
universe@30 | 103 | |
universe@30 | 104 | typedef struct { |
universe@30 | 105 | uint8_t servercolor; |
universe@40 | 106 | uint8_t timecontrol; |
universe@30 | 107 | uint16_t time; |
universe@30 | 108 | uint16_t addtime; |
universe@30 | 109 | } GameInfo; |
universe@30 | 110 | |
universe@23 | 111 | typedef struct { |
universe@23 | 112 | Board board; |
universe@23 | 113 | MoveList* movelist; |
universe@23 | 114 | MoveList* lastmove; |
universe@27 | 115 | _Bool checkmate; |
universe@27 | 116 | _Bool stalemate; |
universe@50 | 117 | _Bool remis; |
universe@50 | 118 | _Bool resign; |
universe@23 | 119 | } GameState; |
universe@23 | 120 | |
universe@25 | 121 | #define opponent_color(color) ((color)==WHITE?BLACK:WHITE) |
universe@25 | 122 | |
universe@19 | 123 | #define POS_UNSPECIFIED UINT8_MAX |
universe@19 | 124 | #define mdst(b,m) b[(m)->torow][(m)->tofile] |
universe@19 | 125 | #define msrc(b,m) b[(m)->fromrow][(m)->fromfile] |
universe@19 | 126 | |
universe@28 | 127 | #define isidx(idx) ((uint8_t)(idx) < 8) |
universe@19 | 128 | |
universe@19 | 129 | #define isfile(file) (file >= 'a' && file <= 'h') |
universe@19 | 130 | #define isrow(row) (row >= '1' && row <= '8') |
universe@19 | 131 | |
universe@19 | 132 | #define rowidx(row) (row-'1') |
universe@19 | 133 | #define fileidx(file) (file-'a') |
universe@19 | 134 | |
universe@19 | 135 | #define rowchr(row) (row+'1') |
universe@19 | 136 | #define filechr(file) (file+'a') |
universe@19 | 137 | |
universe@19 | 138 | #define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \ |
universe@19 | 139 | isidx((move)->tofile) && isidx((move)->torow)) |
universe@19 | 140 | |
universe@19 | 141 | /* secure versions - use, if index is not checked with isidx() */ |
universe@19 | 142 | #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED) |
universe@19 | 143 | #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED) |
universe@19 | 144 | |
universe@50 | 145 | #define is_game_running(gamestate) !((gamestate)->checkmate || \ |
universe@50 | 146 | (gamestate)->resign || (gamestate)->stalemate || (gamestate)->remis) |
universe@50 | 147 | |
universe@50 | 148 | |
universe@50 | 149 | /** |
universe@50 | 150 | * Initializes a game state and prepares the chess board. |
universe@50 | 151 | * @param gamestate the game state to initialize |
universe@50 | 152 | */ |
universe@50 | 153 | void gamestate_init(GameState *gamestate); |
universe@50 | 154 | |
universe@49 | 155 | /** |
universe@49 | 156 | * Cleans up a game state and frees the memory for the movement list. |
universe@49 | 157 | * @param gamestate the game state to clean up |
universe@49 | 158 | */ |
universe@23 | 159 | void gamestate_cleanup(GameState *gamestate); |
universe@23 | 160 | |
universe@19 | 161 | /** |
universe@19 | 162 | * Maps a character to a piece. |
universe@19 | 163 | * |
universe@19 | 164 | * Does not work for pawns, since they don't have a character. |
universe@19 | 165 | * |
universe@19 | 166 | * @param c one of R,N,B,Q,K |
universe@19 | 167 | * @return numeric value for the specified piece |
universe@19 | 168 | */ |
universe@19 | 169 | uint8_t getpiece(char c); |
universe@19 | 170 | |
universe@19 | 171 | /** |
universe@19 | 172 | * Maps a piece to a character. |
universe@19 | 173 | * |
universe@19 | 174 | * Does not work for pawns, scince they don't have a character. |
universe@19 | 175 | * |
universe@19 | 176 | * @param piece one of ROOK, KNIGHT, BISHOP, QUEEN, KING |
universe@19 | 177 | * @return character value for the specified piece |
universe@19 | 178 | */ |
universe@19 | 179 | char getpiecechr(uint8_t piece); |
universe@19 | 180 | |
universe@19 | 181 | /** |
universe@25 | 182 | * Checks, if a specified field is covered by a piece of a certain color. |
universe@25 | 183 | * |
universe@29 | 184 | * The out-parameters may both be NULL, but if any of them is set, the other |
universe@29 | 185 | * must be set, too. |
universe@28 | 186 | * |
universe@28 | 187 | * @param gamestate the current game state |
universe@28 | 188 | * @param row row of the field to check |
universe@28 | 189 | * @param file file of the field to check |
universe@28 | 190 | * @param color the color of the piece that should threaten the field |
universe@47 | 191 | * @param threats the array where to store the threats (should be able to hold |
universe@47 | 192 | * the rare maximum of 16 elements) |
universe@47 | 193 | * @param threatcount a pointer to an uint8_t where the count of threats is |
universe@47 | 194 | * stored |
universe@28 | 195 | * @return TRUE, if any piece of the specified color threatens the specified |
universe@28 | 196 | * field (i.e. could capture an opponent piece) |
universe@28 | 197 | */ |
universe@29 | 198 | _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file, |
universe@29 | 199 | uint8_t color, Move* threats, uint8_t* threatcount); |
universe@29 | 200 | |
universe@29 | 201 | /** |
universe@29 | 202 | * Checks, if a specified field is covered by a piece of a certain color AND |
universe@29 | 203 | * if this piece is not pinned and therefore able to perform the move. |
universe@29 | 204 | * |
universe@29 | 205 | * The out-parameters may both be NULL, but if any of them is set, the other |
universe@29 | 206 | * must be set, too. |
universe@29 | 207 | * |
universe@29 | 208 | * @param gamestate the current game state |
universe@29 | 209 | * @param row row of the field to check |
universe@29 | 210 | * @param file file of the field to check |
universe@29 | 211 | * @param color the color of the piece that should threaten the field |
universe@47 | 212 | * @param threats the array where to store the threats (should be able to hold |
universe@47 | 213 | * the rare maximum of 16 elements) |
universe@47 | 214 | * @param threatcount a pointer to an uint8_t where the count of threats is |
universe@47 | 215 | * stored |
universe@29 | 216 | * @return TRUE, if any piece of the specified color threatens the specified |
universe@29 | 217 | * field (i.e. could capture an opponent piece) |
universe@29 | 218 | */ |
universe@29 | 219 | _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file, |
universe@29 | 220 | uint8_t color, Move* threats, uint8_t* threatcount); |
universe@28 | 221 | |
universe@28 | 222 | /** |
universe@28 | 223 | * Checks, if a specified field is covered by a piece of a certain color. |
universe@28 | 224 | * |
universe@25 | 225 | * @param gamestate the current game state |
universe@25 | 226 | * @param row row of the field to check |
universe@25 | 227 | * @param file file of the field to check |
universe@25 | 228 | * @param color the color of the piece that should cover the field |
universe@25 | 229 | * @return TRUE, if any piece of the specified color threatens the specified |
universe@29 | 230 | * field |
universe@25 | 231 | */ |
universe@28 | 232 | #define is_covered(gamestate, row, file, color) \ |
universe@29 | 233 | get_threats(gamestate, row, file, color, NULL, NULL) |
universe@29 | 234 | |
universe@29 | 235 | /** |
universe@29 | 236 | * Checks, if a specified field is attacked by a piece of a certain color. |
universe@29 | 237 | * |
universe@29 | 238 | * I.e. the field is covered by a piece AND this piece is not pinned and |
universe@29 | 239 | * therefore able to perform the move. |
universe@29 | 240 | * |
universe@29 | 241 | * @param gamestate the current game state |
universe@29 | 242 | * @param row row of the field to check |
universe@29 | 243 | * @param file file of the field to check |
universe@29 | 244 | * @param color the color of the piece that should cover the field |
universe@29 | 245 | * @return TRUE, if any piece of the specified color threatens the specified |
universe@29 | 246 | * field and could capture an opponent piece |
universe@29 | 247 | */ |
universe@29 | 248 | #define is_attacked(gamestate, row, file, color) \ |
universe@29 | 249 | get_threats(gamestate, row, file, color, NULL, NULL) |
universe@29 | 250 | |
universe@29 | 251 | /** |
universe@29 | 252 | * Checks, if a specified field is protected by a piece of a certain color. |
universe@29 | 253 | * |
universe@29 | 254 | * I.e. the field is covered by a piece that is NOT the king AND this piece is |
universe@29 | 255 | * not pinned and therefore able to perform the move. |
universe@29 | 256 | * |
universe@29 | 257 | * @param gamestate the current game state |
universe@29 | 258 | * @param row row of the field to check |
universe@29 | 259 | * @param file file of the field to check |
universe@29 | 260 | * @param color the color of the piece that should cover the field |
universe@29 | 261 | * @return TRUE, if any piece (excluding the king) of the specified color |
universe@29 | 262 | * threatens the specified field and could capture an opponent piece |
universe@29 | 263 | */ |
universe@29 | 264 | _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file, |
universe@29 | 265 | uint8_t color); |
universe@25 | 266 | |
universe@25 | 267 | /** |
universe@47 | 268 | * Checks, if the specified move cannot be performed, because the piece is |
universe@47 | 269 | * either pinned or cannot remove the check. |
universe@47 | 270 | * |
universe@47 | 271 | * Note: in chess a piece is pinned, when it can't be moved because the move |
universe@47 | 272 | * would result in a check position. But this function <u>also</u> returns true, |
universe@47 | 273 | * if the king is already in check position and the specified move does not |
universe@47 | 274 | * protect the king. |
universe@47 | 275 | * |
universe@47 | 276 | * @param gamestate the current game state |
universe@47 | 277 | * @param move the move to check |
universe@47 | 278 | * @return TRUE, if the move cannot be performed because the king would be in |
universe@47 | 279 | * check after the move |
universe@47 | 280 | */ |
universe@47 | 281 | _Bool is_pinned(GameState *gamestate, Move *move); |
universe@47 | 282 | |
universe@47 | 283 | /** |
universe@19 | 284 | * Evaluates a move syntactically and stores the move data in the specified |
universe@19 | 285 | * object. |
universe@19 | 286 | * |
universe@23 | 287 | * @param gamestate the current game state |
universe@19 | 288 | * @param mstr the input string to parse |
universe@19 | 289 | * @param move a pointer to object where the move data shall be stored |
universe@50 | 290 | * @param color the color of the player to evaluate the move for |
universe@48 | 291 | * @return status code (see macros in this file for the list of codes) |
universe@19 | 292 | */ |
universe@50 | 293 | int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color); |
universe@19 | 294 | |
universe@19 | 295 | /** |
universe@19 | 296 | * Validates move by applying chess rules. |
universe@23 | 297 | * @param gamestate the current game state |
universe@19 | 298 | * @param move the move to validate |
universe@48 | 299 | * @return status code (see macros in this file for the list of codes) |
universe@19 | 300 | */ |
universe@48 | 301 | int validate_move(GameState *gamestate, Move *move); |
universe@19 | 302 | |
universe@19 | 303 | /** |
universe@19 | 304 | * Applies a move and deletes captured pieces. |
universe@19 | 305 | * |
universe@23 | 306 | * @param gamestate the current game state |
universe@19 | 307 | * @param move the move to apply |
universe@19 | 308 | */ |
universe@23 | 309 | void apply_move(GameState *gamestate, Move *move); |
universe@19 | 310 | |
universe@33 | 311 | |
universe@33 | 312 | /** |
universe@33 | 313 | * Returns the remaining time on the clock for the specified player. |
universe@33 | 314 | * |
universe@33 | 315 | * @param gameinfo the information about the game |
universe@33 | 316 | * @param gamestate the current game state |
universe@33 | 317 | * @param color either BLACK or WHITE |
universe@33 | 318 | * @return the remaining time - if time control is disabled, this function |
universe@33 | 319 | * always returns zero |
universe@33 | 320 | */ |
universe@33 | 321 | uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate, |
universe@33 | 322 | uint8_t color); |
universe@33 | 323 | |
universe@10 | 324 | #endif /* RULES_H */ |
universe@10 | 325 |