1 /*  DreamChess
2 **
3 **  DreamChess is the legal property of its developers, whose names are too
4 **  numerous to list here. Please refer to the AUTHORS.txt file distributed
5 **  with this source distribution.
6 **
7 **  This program is free software: you can redistribute it and/or modify
8 **  it under the terms of the GNU General Public License as published by
9 **  the Free Software Foundation, either version 3 of the License, or
10 **  (at your option) any later version.
11 **
12 **  This program is distributed in the hope that it will be useful,
13 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
14 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 **  GNU General Public License for more details.
16 **
17 **  You should have received a copy of the GNU General Public License
18 **  along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef DREAMER_BOARD_H
22 #define DREAMER_BOARD_H
23 
24 /* Chess pieces. Also used for indexing the bitboard array. */
25 #define PAWN 0
26 #define WHITE_PAWN 0
27 #define BLACK_PAWN 1
28 #define KNIGHT 2
29 #define WHITE_KNIGHT 2
30 #define BLACK_KNIGHT 3
31 #define BISHOP 4
32 #define WHITE_BISHOP 4
33 #define BLACK_BISHOP 5
34 #define ROOK 6
35 #define WHITE_ROOK 6
36 #define BLACK_ROOK 7
37 #define QUEEN 8
38 #define WHITE_QUEEN 8
39 #define BLACK_QUEEN 9
40 #define KING 10
41 #define WHITE_KING 10
42 #define BLACK_KING 11
43 #define ALL 12
44 #define WHITE_ALL 12
45 #define BLACK_ALL 13
46 
47 #define SQUARE_BIT(A) (1LL << (A))
48 
49 /* Empty squares required for kingside castle. */
50 #define WHITE_EMPTY_KINGSIDE (SQUARE_BIT(SQUARE_F1) | SQUARE_BIT(SQUARE_G1))
51 #define BLACK_EMPTY_KINGSIDE (SQUARE_BIT(SQUARE_F8) | SQUARE_BIT(SQUARE_G8))
52 
53 /* Empty squares required for queenside castle. */
54 #define WHITE_EMPTY_QUEENSIDE (SQUARE_BIT(SQUARE_B1) | SQUARE_BIT(SQUARE_C1) | SQUARE_BIT(SQUARE_D1))
55 #define BLACK_EMPTY_QUEENSIDE (SQUARE_BIT(SQUARE_B8) | SQUARE_BIT(SQUARE_C8) | SQUARE_BIT(SQUARE_D8))
56 
57 /* Squares where phantom kings are placed during a kingside castle to detect an
58 ** illegal move.
59 */
60 #define WHITE_PHANTOM_KINGSIDE (SQUARE_BIT(SQUARE_E1) | SQUARE_BIT(SQUARE_F1))
61 #define BLACK_PHANTOM_KINGSIDE (SQUARE_BIT(SQUARE_E8) | SQUARE_BIT(SQUARE_F8))
62 
63 /* Squares where phantom kings are placed during a queenside castle to detect an
64 ** illegal move.
65 */
66 #define WHITE_PHANTOM_QUEENSIDE (SQUARE_BIT(SQUARE_E1) | SQUARE_BIT(SQUARE_D1))
67 #define BLACK_PHANTOM_QUEENSIDE (SQUARE_BIT(SQUARE_E8) | SQUARE_BIT(SQUARE_D8))
68 
69 /* Sides.*/
70 #define SIDE_WHITE 0
71 #define SIDE_BLACK 1
72 #define OPPONENT(P) (((P) + 1) % 2) /*(P % 1) */
73 
74 /* Chess piece mask. */
75 #define PIECE_MASK 14
76 #define PIECE_IS_WHITE(A) (!((A)&1)) /* A % 1 */
77 #define PIECE_IS_BLACK(A) ((A)&1)
78 
79 /* Castling. */
80 
81 #define CASTLE_KINGSIDE 0
82 #define CASTLE_QUEENSIDE 2
83 
84 #define WHITE_CAN_CASTLE_KINGSIDE (1 << 0)
85 #define BLACK_CAN_CASTLE_KINGSIDE (1 << 1)
86 #define WHITE_CAN_CASTLE_QUEENSIDE (1 << 2)
87 #define BLACK_CAN_CASTLE_QUEENSIDE (1 << 3)
88 #define WHITE_HAS_CASTLED (1 << 4)
89 #define BLACK_HAS_CASTLED (1 << 5)
90 #define WHITE_PHANTOM_KINGS_KINGSIDE (1 << 6)
91 #define WHITE_PHANTOM_KINGS_QUEENSIDE (1 << 7)
92 #define BLACK_PHANTOM_KINGS_KINGSIDE (1 << 8)
93 #define BLACK_PHANTOM_KINGS_QUEENSIDE (1 << 9)
94 
95 #define PHANTOM_FLAGS (15 << 6)
96 
97 /* Squares on the board. */
98 #define SQUARE_A1 0
99 #define SQUARE_B1 1
100 #define SQUARE_C1 2
101 #define SQUARE_D1 3
102 #define SQUARE_E1 4
103 #define SQUARE_F1 5
104 #define SQUARE_G1 6
105 #define SQUARE_H1 7
106 #define SQUARE_A2 8
107 #define SQUARE_B2 9
108 #define SQUARE_C2 10
109 #define SQUARE_D2 11
110 #define SQUARE_E2 12
111 #define SQUARE_F2 13
112 #define SQUARE_G2 14
113 #define SQUARE_H2 15
114 #define SQUARE_A3 16
115 #define SQUARE_B3 17
116 #define SQUARE_C3 18
117 #define SQUARE_D3 19
118 #define SQUARE_E3 20
119 #define SQUARE_F3 21
120 #define SQUARE_G3 22
121 #define SQUARE_H3 23
122 #define SQUARE_A4 24
123 #define SQUARE_B4 25
124 #define SQUARE_C4 26
125 #define SQUARE_D4 27
126 #define SQUARE_E4 28
127 #define SQUARE_F4 29
128 #define SQUARE_G4 30
129 #define SQUARE_H4 31
130 #define SQUARE_A5 32
131 #define SQUARE_B5 33
132 #define SQUARE_C5 34
133 #define SQUARE_D5 35
134 #define SQUARE_E5 36
135 #define SQUARE_F5 37
136 #define SQUARE_G5 38
137 #define SQUARE_H5 39
138 #define SQUARE_A6 40
139 #define SQUARE_B6 41
140 #define SQUARE_C6 42
141 #define SQUARE_D6 43
142 #define SQUARE_E6 44
143 #define SQUARE_F6 45
144 #define SQUARE_G6 46
145 #define SQUARE_H6 47
146 #define SQUARE_A7 48
147 #define SQUARE_B7 49
148 #define SQUARE_C7 50
149 #define SQUARE_D7 51
150 #define SQUARE_E7 52
151 #define SQUARE_F7 53
152 #define SQUARE_G7 54
153 #define SQUARE_H7 55
154 #define SQUARE_A8 56
155 #define SQUARE_B8 57
156 #define SQUARE_C8 58
157 #define SQUARE_D8 59
158 #define SQUARE_E8 60
159 #define SQUARE_F8 61
160 #define SQUARE_G8 62
161 #define SQUARE_H8 63
162 
163 /* Total number of types of pieces. */
164 #define NR_PIECES 12
165 
166 /* Total number of bitboards. */
167 #define NR_BITBOARDS 14
168 
169 /* Empty square. */
170 #define NONE 12
171 
172 /* 64-bit bitboard. Bit 0 = A1, bit 1 = A2 etc. */
173 typedef unsigned long long bitboard_t;
174 
175 /* Struct describing the current state of the board. */
176 typedef struct board {
177 	bitboard_t bitboard[NR_BITBOARDS];
178 
179 	/* Hash key of the current board. */
180 	long long hash_key;
181 
182 	/* 0-3 can_castle flags
183 	** 4-5 has_castled flags
184 	** 6-9 phantom kings flags
185 	*/
186 	int castle_flags;
187 
188 	/* bitboard containing the current en_passant flags, if any. */
189 	bitboard_t en_passant;
190 
191 	/* Current player. 0 = white, 1 = black. */
192 	int current_player;
193 
194 	/* Current total material value for both black and white. */
195 	int material_value[2];
196 
197 	/* Number of pawns on the board for both black and white. */
198 	int num_pawns[2];
199 
200 	/* 50-move counter. */
201 	int fifty_moves;
202 } board_t;
203 
204 typedef int move_t;
205 #if 0
206 /* Structure describing a move on the board. */
207 typedef struct move
208 {
209     /* Move type. Constants are defined in chess_move.h. */
210     int type;
211 
212     /* The moving piece. Constants are defined above. */
213     int piece;
214 
215     /* The captured piece, if this is a capture move. */
216     int captured_piece;
217 
218     /* The source square. */
219     int source;
220 
221     /* The destination square. */
222     int destination;
223 }
224 move_t;
225 #endif
226 extern bitboard_t square_bit[64];
227 
228 extern board_t chess_board;
229 
230 int find_black_piece(board_t *board, int square);
231 /* Looks for a black piece on the board at a specified location.
232 ** Parameters: (board_t *) board: Pointer to the board to search.
233 **             (int) square: The square to search.
234 ** Returns   : (int): The black piece located at the square on the board, or
235 **                 NONE if no black piece was found. If both a fake king
236 **                 and a rook are on the square, the king is found instead
237 **                 of the rook.
238 */
239 
240 int find_white_piece(board_t *board, int square);
241 /* Looks for a white piece on the board at a specified location.
242 ** Parameters: (board_t *) board: Pointer to the board to search.
243 **             (int) square: The square to search.
244 ** Returns   : (int): The white piece located at the square on the board, or
245 **                 NONE if no white piece was found. If both a fake king
246 **                 and a rook are on the square, the king is found instead
247 **                 of the rook.
248 */
249 
250 void board_init(void);
251 /* Initialises the global array square_bit.
252 ** Parameters: (void)
253 ** Returns   : (void)
254 */
255 
256 void clear_board(board_t *board);
257 /* Clears a board.
258 ** Parameters: (board_t *) board: Pointer to the board to clear.
259 ** Returns   : (void)
260 */
261 
262 void setup_board(board_t *board);
263 /* Sets up a board to the starting position.
264 ** Parameters: (board_t *) board: Pointer to the board to set up.
265 ** Returns   : (void)
266 */
267 
268 void execute_move(board_t *board, move_t move);
269 /* Makes a move on a board.
270 ** Parameters: (board_t *) board: Board to make the move on.
271 **             (move_t) move: The move to make.
272 ** Returns   : (void)
273 */
274 
275 void unmake_move(board_t *board, move_t move, bitboard_t old_en_passant, int old_castle_flags, int old_fifty_moves);
276 /* Unmakes a move on a board.
277 ** Parameters: (board_t *) board: Board to unmake the move on.
278 **             (move_t) move: The move to unmake.
279 **             (bitboard_t) old_en_passant: The en-passant flags before the
280 **                 last move.
281 **             (int) old_castle_flags: The castling flags before the last
282 **                 move.
283 **             (int) old_fifty_moves: The 50 moves value before the last
284 **                 move.
285 ** Returns   : (void)
286 */
287 
288 int setup_board_fen(board_t *board, char *fen);
289 
290 #endif
291