Mon, 16 Jun 2014 13:45:31 +0200
added pgn parser and writer (without comment support yet) + minor refactorings
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 char string[8];
94 } Move;
96 typedef struct MoveList MoveList;
98 struct MoveList {
99 Move move;
100 MoveList* next;
101 };
104 typedef struct {
105 uint8_t servercolor;
106 uint8_t timecontrol;
107 uint16_t time;
108 uint16_t addtime;
109 } GameInfo;
111 typedef struct {
112 Board board;
113 MoveList* movelist;
114 MoveList* lastmove;
115 _Bool checkmate;
116 _Bool stalemate;
117 _Bool remis;
118 _Bool resign;
119 } GameState;
121 #define opponent_color(color) ((color)==WHITE?BLACK:WHITE)
123 #define POS_UNSPECIFIED UINT8_MAX
124 #define mdst(b,m) b[(m)->torow][(m)->tofile]
125 #define msrc(b,m) b[(m)->fromrow][(m)->fromfile]
127 #define isidx(idx) ((uint8_t)(idx) < 8)
129 #define isfile(file) (file >= 'a' && file <= 'h')
130 #define isrow(row) (row >= '1' && row <= '8')
132 #define rowidx(row) (row-'1')
133 #define fileidx(file) (file-'a')
135 #define rowchr(row) (row+'1')
136 #define filechr(file) (file+'a')
138 #define chkidx(move) (isidx((move)->fromfile) && isidx((move)->fromrow) && \
139 isidx((move)->tofile) && isidx((move)->torow))
141 /* secure versions - use, if index is not checked with isidx() */
142 #define fileidx_s(c) (isfile(c)?fileidx(c):POS_UNSPECIFIED)
143 #define rowidx_s(c) (isrow(c)?rowidx(c):POS_UNSPECIFIED)
145 #define is_game_running(gamestate) !((gamestate)->checkmate || \
146 (gamestate)->resign || (gamestate)->stalemate || (gamestate)->remis)
149 /**
150 * Initializes a game state and prepares the chess board.
151 * @param gamestate the game state to initialize
152 */
153 void gamestate_init(GameState *gamestate);
155 /**
156 * Cleans up a game state and frees the memory for the movement list.
157 * @param gamestate the game state to clean up
158 */
159 void gamestate_cleanup(GameState *gamestate);
161 /**
162 * Maps a character to a piece.
163 *
164 * Does not work for pawns, since they don't have a character.
165 *
166 * @param c one of R,N,B,Q,K
167 * @return numeric value for the specified piece
168 */
169 uint8_t getpiece(char c);
171 /**
172 * Maps a piece to a character.
173 *
174 * Does not work for pawns, scince they don't have a character.
175 *
176 * @param piece one of ROOK, KNIGHT, BISHOP, QUEEN, KING
177 * @return character value for the specified piece
178 */
179 char getpiecechr(uint8_t piece);
181 /**
182 * Checks, if a specified field is covered by a piece of a certain color.
183 *
184 * The out-parameters may both be NULL, but if any of them is set, the other
185 * must be set, too.
186 *
187 * @param gamestate the current game state
188 * @param row row of the field to check
189 * @param file file of the field to check
190 * @param color the color of the piece that should threaten the field
191 * @param threats the array where to store the threats (should be able to hold
192 * the rare maximum of 16 elements)
193 * @param threatcount a pointer to an uint8_t where the count of threats is
194 * stored
195 * @return TRUE, if any piece of the specified color threatens the specified
196 * field (i.e. could capture an opponent piece)
197 */
198 _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file,
199 uint8_t color, Move* threats, uint8_t* threatcount);
201 /**
202 * Checks, if a specified field is covered by a piece of a certain color AND
203 * if this piece is not pinned and therefore able to perform the move.
204 *
205 * The out-parameters may both be NULL, but if any of them is set, the other
206 * must be set, too.
207 *
208 * @param gamestate the current game state
209 * @param row row of the field to check
210 * @param file file of the field to check
211 * @param color the color of the piece that should threaten the field
212 * @param threats the array where to store the threats (should be able to hold
213 * the rare maximum of 16 elements)
214 * @param threatcount a pointer to an uint8_t where the count of threats is
215 * stored
216 * @return TRUE, if any piece of the specified color threatens the specified
217 * field (i.e. could capture an opponent piece)
218 */
219 _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file,
220 uint8_t color, Move* threats, uint8_t* threatcount);
222 /**
223 * Checks, if a specified field is covered by a piece of a certain color.
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
231 */
232 #define is_covered(gamestate, row, file, color) \
233 get_threats(gamestate, row, file, color, NULL, NULL)
235 /**
236 * Checks, if a specified field is attacked by a piece of a certain color.
237 *
238 * I.e. the field is covered by a piece AND this piece is not pinned and
239 * 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 of the specified color threatens the specified
246 * field and could capture an opponent piece
247 */
248 #define is_attacked(gamestate, row, file, color) \
249 get_threats(gamestate, row, file, color, NULL, NULL)
251 /**
252 * Checks, if a specified field is protected by a piece of a certain color.
253 *
254 * I.e. the field is covered by a piece that is NOT the king AND this piece is
255 * not pinned and therefore able to perform the move.
256 *
257 * @param gamestate the current game state
258 * @param row row of the field to check
259 * @param file file of the field to check
260 * @param color the color of the piece that should cover the field
261 * @return TRUE, if any piece (excluding the king) of the specified color
262 * threatens the specified field and could capture an opponent piece
263 */
264 _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file,
265 uint8_t color);
267 /**
268 * Checks, if the specified move cannot be performed, because the piece is
269 * either pinned or cannot remove the check.
270 *
271 * Note: in chess a piece is pinned, when it can't be moved because the move
272 * would result in a check position. But this function <u>also</u> returns true,
273 * if the king is already in check position and the specified move does not
274 * protect the king.
275 *
276 * @param gamestate the current game state
277 * @param move the move to check
278 * @return TRUE, if the move cannot be performed because the king would be in
279 * check after the move
280 */
281 _Bool is_pinned(GameState *gamestate, Move *move);
283 /**
284 * Evaluates a move syntactically and stores the move data in the specified
285 * object.
286 *
287 * @param gamestate the current game state
288 * @param mstr the input string to parse
289 * @param move a pointer to object where the move data shall be stored
290 * @param color the color of the player to evaluate the move for
291 * @return status code (see macros in this file for the list of codes)
292 */
293 int eval_move(GameState *gamestate, char *mstr, Move *move, uint8_t color);
295 /**
296 * Validates move by applying chess rules.
297 * @param gamestate the current game state
298 * @param move the move to validate
299 * @return status code (see macros in this file for the list of codes)
300 */
301 int validate_move(GameState *gamestate, Move *move);
303 /**
304 * Applies a move and deletes captured pieces.
305 *
306 * @param gamestate the current game state
307 * @param move the move to apply
308 */
309 void apply_move(GameState *gamestate, Move *move);
312 /**
313 * Returns the remaining time on the clock for the specified player.
314 *
315 * @param gameinfo the information about the game
316 * @param gamestate the current game state
317 * @param color either BLACK or WHITE
318 * @return the remaining time - if time control is disabled, this function
319 * always returns zero
320 */
321 uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate,
322 uint8_t color);
324 #endif /* RULES_H */