src/chess/rules.h

Thu, 10 Apr 2014 11:44:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 10 Apr 2014 11:44:55 +0200
changeset 36
ebe0c961e9a6
parent 33
866025982aa9
child 40
47162a7621da
permissions
-rw-r--r--

reduced awesome great nanosecond precision so we can compile on OS X

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@16 36 #define VALID_MOVE_SYNTAX 0
universe@16 37 #define INVALID_MOVE_SYNTAX 1
universe@16 38 #define INVALID_POSITION 2
universe@16 39 #define AMBIGUOUS_MOVE 3
universe@18 40 #define NEED_PROMOTION 4
universe@16 41
universe@19 42
universe@19 43 #define PIECE_MASK 0x0F
universe@19 44 #define COLOR_MASK 0x30
universe@19 45 #define ENPASSANT_THREAT 0x40
universe@19 46
universe@19 47 #define WHITE 0x10
universe@19 48 #define BLACK 0x20
universe@19 49
universe@19 50 #define PAWN 0x01
universe@19 51 #define ROOK 0x02
universe@19 52 #define KNIGHT 0x03
universe@19 53 #define BISHOP 0x04
universe@19 54 #define QUEEN 0x05
universe@19 55 #define KING 0x06
universe@19 56
universe@19 57 #define WPAWN (WHITE|PAWN)
universe@19 58 #define WROOK (WHITE|ROOK)
universe@19 59 #define WKNIGHT (WHITE|KNIGHT)
universe@19 60 #define WBISHOP (WHITE|BISHOP)
universe@19 61 #define WQUEEN (WHITE|QUEEN)
universe@19 62 #define WKING (WHITE|KING)
universe@19 63 #define BPAWN (BLACK|PAWN)
universe@19 64 #define BROOK (BLACK|ROOK)
universe@19 65 #define BKNIGHT (BLACK|KNIGHT)
universe@19 66 #define BBISHOP (BLACK|BISHOP)
universe@19 67 #define BQUEEN (BLACK|QUEEN)
universe@19 68 #define BKING (BLACK|KING)
universe@19 69
universe@19 70 typedef uint8_t Board[8][8];
universe@19 71
universe@19 72 typedef struct {
universe@19 73 uint8_t piece;
universe@19 74 uint8_t fromfile;
universe@19 75 uint8_t fromrow;
universe@19 76 uint8_t tofile;
universe@19 77 uint8_t torow;
universe@19 78 uint8_t promotion;
universe@36 79 struct timeval timestamp;
universe@36 80 struct timeval movetime;
universe@19 81 _Bool check;
universe@19 82 _Bool capture;
universe@19 83 } Move;
universe@19 84
universe@23 85 typedef struct MoveList MoveList;
universe@23 86
universe@23 87 struct MoveList {
universe@23 88 Move move;
universe@23 89 MoveList* next;
universe@23 90 };
universe@23 91
universe@30 92
universe@30 93 typedef struct {
universe@30 94 uint8_t servercolor;
universe@30 95 _Bool timecontrol;
universe@30 96 uint16_t time;
universe@30 97 uint16_t addtime;
universe@30 98 } GameInfo;
universe@30 99
universe@23 100 typedef struct {
universe@23 101 Board board;
universe@23 102 uint8_t mycolor;
universe@23 103 MoveList* movelist;
universe@23 104 MoveList* lastmove;
universe@27 105 _Bool checkmate;
universe@27 106 _Bool stalemate;
universe@23 107 } GameState;
universe@23 108
universe@25 109 #define opponent_color(color) ((color)==WHITE?BLACK:WHITE)
universe@25 110
universe@19 111 #define POS_UNSPECIFIED UINT8_MAX
universe@19 112 #define mdst(b,m) b[(m)->torow][(m)->tofile]
universe@19 113 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
universe@19 114
universe@28 115 #define isidx(idx) ((uint8_t)(idx) < 8)
universe@19 116
universe@19 117 #define isfile(file) (file >= 'a' && file <= 'h')
universe@19 118 #define isrow(row) (row >= '1' && row <= '8')
universe@19 119
universe@19 120 #define rowidx(row) (row-'1')
universe@19 121 #define fileidx(file) (file-'a')
universe@19 122
universe@19 123 #define rowchr(row) (row+'1')
universe@19 124 #define filechr(file) (file+'a')
universe@19 125
universe@19 126 #define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \
universe@19 127 isidx((move)->tofile) && isidx((move)->torow))
universe@19 128
universe@19 129 /* secure versions - use, if index is not checked with isidx() */
universe@19 130 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
universe@19 131 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)
universe@19 132
universe@23 133 void gamestate_cleanup(GameState *gamestate);
universe@23 134
universe@19 135 /**
universe@19 136 * Maps a character to a piece.
universe@19 137 *
universe@19 138 * Does not work for pawns, since they don't have a character.
universe@19 139 *
universe@19 140 * @param c one of R,N,B,Q,K
universe@19 141 * @return numeric value for the specified piece
universe@19 142 */
universe@19 143 uint8_t getpiece(char c);
universe@19 144
universe@19 145 /**
universe@19 146 * Maps a piece to a character.
universe@19 147 *
universe@19 148 * Does not work for pawns, scince they don't have a character.
universe@19 149 *
universe@19 150 * @param piece one of ROOK, KNIGHT, BISHOP, QUEEN, KING
universe@19 151 * @return character value for the specified piece
universe@19 152 */
universe@19 153 char getpiecechr(uint8_t piece);
universe@19 154
universe@19 155 /**
universe@25 156 * Checks, if a specified field is covered by a piece of a certain color.
universe@25 157 *
universe@29 158 * The out-parameters may both be NULL, but if any of them is set, the other
universe@29 159 * must be set, too.
universe@28 160 *
universe@28 161 * @param gamestate the current game state
universe@28 162 * @param row row of the field to check
universe@28 163 * @param file file of the field to check
universe@28 164 * @param color the color of the piece that should threaten the field
universe@29 165 * @param threats the array where to store the threats (should be able to the
universe@29 166 * rare maximum of 16 elements)
universe@29 167 * @param threatcount a pointer to an uint8_t where to store the amount of threats
universe@28 168 * @return TRUE, if any piece of the specified color threatens the specified
universe@28 169 * field (i.e. could capture an opponent piece)
universe@28 170 */
universe@29 171 _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file,
universe@29 172 uint8_t color, Move* threats, uint8_t* threatcount);
universe@29 173
universe@29 174 /**
universe@29 175 * Checks, if a specified field is covered by a piece of a certain color AND
universe@29 176 * if this piece is not pinned and therefore able to perform the move.
universe@29 177 *
universe@29 178 * The out-parameters may both be NULL, but if any of them is set, the other
universe@29 179 * must be set, too.
universe@29 180 *
universe@29 181 * @param gamestate the current game state
universe@29 182 * @param row row of the field to check
universe@29 183 * @param file file of the field to check
universe@29 184 * @param color the color of the piece that should threaten the field
universe@29 185 * @param threats the array where to store the threats (should be able to the
universe@29 186 * rare maximum of 16 elements)
universe@29 187 * @param threatcount a pointer to an uint8_t where to store the amount of threats
universe@29 188 * @return TRUE, if any piece of the specified color threatens the specified
universe@29 189 * field (i.e. could capture an opponent piece)
universe@29 190 */
universe@29 191 _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file,
universe@29 192 uint8_t color, Move* threats, uint8_t* threatcount);
universe@28 193
universe@28 194 /**
universe@28 195 * Checks, if a specified field is covered by a piece of a certain color.
universe@28 196 *
universe@25 197 * @param gamestate the current game state
universe@25 198 * @param row row of the field to check
universe@25 199 * @param file file of the field to check
universe@25 200 * @param color the color of the piece that should cover the field
universe@25 201 * @return TRUE, if any piece of the specified color threatens the specified
universe@29 202 * field
universe@25 203 */
universe@28 204 #define is_covered(gamestate, row, file, color) \
universe@29 205 get_threats(gamestate, row, file, color, NULL, NULL)
universe@29 206
universe@29 207 /**
universe@29 208 * Checks, if a specified field is attacked by a piece of a certain color.
universe@29 209 *
universe@29 210 * I.e. the field is covered by a piece AND this piece is not pinned and
universe@29 211 * therefore able to perform the move.
universe@29 212 *
universe@29 213 * @param gamestate the current game state
universe@29 214 * @param row row of the field to check
universe@29 215 * @param file file of the field to check
universe@29 216 * @param color the color of the piece that should cover the field
universe@29 217 * @return TRUE, if any piece of the specified color threatens the specified
universe@29 218 * field and could capture an opponent piece
universe@29 219 */
universe@29 220 #define is_attacked(gamestate, row, file, color) \
universe@29 221 get_threats(gamestate, row, file, color, NULL, NULL)
universe@29 222
universe@29 223 /**
universe@29 224 * Checks, if a specified field is protected by a piece of a certain color.
universe@29 225 *
universe@29 226 * I.e. the field is covered by a piece that is NOT the king AND this piece is
universe@29 227 * not pinned and therefore able to perform the move.
universe@29 228 *
universe@29 229 * @param gamestate the current game state
universe@29 230 * @param row row of the field to check
universe@29 231 * @param file file of the field to check
universe@29 232 * @param color the color of the piece that should cover the field
universe@29 233 * @return TRUE, if any piece (excluding the king) of the specified color
universe@29 234 * threatens the specified field and could capture an opponent piece
universe@29 235 */
universe@29 236 _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
universe@29 237 uint8_t color);
universe@25 238
universe@25 239 /**
universe@19 240 * Evaluates a move syntactically and stores the move data in the specified
universe@19 241 * object.
universe@19 242 *
universe@23 243 * @param gamestate the current game state
universe@19 244 * @param mstr the input string to parse
universe@19 245 * @param move a pointer to object where the move data shall be stored
universe@19 246 * @return status code (see rules/rules.h for the list of codes)
universe@19 247 */
universe@23 248 int eval_move(GameState *gamestate, char *mstr, Move *move);
universe@19 249
universe@19 250 /**
universe@19 251 * Validates move by applying chess rules.
universe@23 252 * @param gamestate the current game state
universe@19 253 * @param move the move to validate
universe@19 254 * @return TRUE, if the move complies to chess rules, FALSE otherwise
universe@19 255 */
universe@23 256 _Bool validate_move(GameState *gamestate, Move *move);
universe@19 257
universe@19 258 /**
universe@19 259 * Applies a move and deletes captured pieces.
universe@19 260 *
universe@23 261 * @param gamestate the current game state
universe@19 262 * @param move the move to apply
universe@19 263 */
universe@23 264 void apply_move(GameState *gamestate, Move *move);
universe@19 265
universe@33 266
universe@33 267 /**
universe@33 268 * Returns the remaining time on the clock for the specified player.
universe@33 269 *
universe@33 270 * @param gameinfo the information about the game
universe@33 271 * @param gamestate the current game state
universe@33 272 * @param color either BLACK or WHITE
universe@33 273 * @return the remaining time - if time control is disabled, this function
universe@33 274 * always returns zero
universe@33 275 */
universe@33 276 uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
universe@33 277 uint8_t color);
universe@33 278
universe@10 279 #endif /* RULES_H */
universe@10 280

mercurial