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