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