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