-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathboard.h
322 lines (279 loc) · 10 KB
/
board.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/***************************************************************************************
* Copyright (c) 2014, Antonio Garro. *
* All rights reserved *
* *
* Redistribution and use in source and binary forms, with or without modification, are *
* permitted provided that the following conditions are met: *
* *
* 1. Redistributions of source code must retain the above copyright notice, this list *
* of conditions and the following disclaimer. *
* *
* 2. Redistributions in binary form must reproduce the above copyright notice, this *
* list of conditions and the following disclaimer in the documentation and/or other *
* materials provided with the distribution. *
* *
* 3. Neither the name of the copyright holder nor the names of its contributors may be *
* used to endorse or promote products derived from this software without specific *
* prior written permission. *
* *
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" *
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE *
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE *
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE *
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL *
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR *
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER *
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR *
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF *
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
***************************************************************************************/
#ifndef BOARDH
#define BOARDH
/*Type to store moves: ORIG:bits 0-7, DEST:bits 8-15, PROMOTED and CASTLE RIGHTS:bits 16-19,
CAPTURED:bits 20-23, PAWN_EP:bits 24-31 -> 0xEECPDDOO*/
typedef unsigned int MOVE;
typedef unsigned char SQUARE;
typedef unsigned char PIECE;
typedef unsigned char COLOR;
/*Zobrist keys*/
typedef unsigned long long KEY;
#define SQSMASK(move) (move & 0xFFFF)
#define ORIGMASK(move) (move & 0xFF)
#define DESTMASK(move) ((move & 0xFFFF) >> 8)
#define PROMMASK(move) ((move & 0xFFFFF) >> 16)
#define CAPTMASK(move) ((move & 0xFFFFFF) >> 20)
#define EPMASK(move) (move >> 24)
/* white pieces have least significant bit = 1; black = 0.*/
/* K_CASTLE_RIGHT | Q_CASTLE_RIGHT = BOTH_CASTLES */
enum PIECES { EMPTY,
K_CASTLE_RIGHT,
B_PAWN,
W_PAWN,
B_KNIGHT,
W_KNIGHT,
B_BISHOP,
W_BISHOP,
B_ROOK,
W_ROOK,
B_QUEEN,
W_QUEEN,
B_KING,
W_KING,
Q_CASTLE_RIGHT,
BOTH_CASTLES
};
#define WHITE 1
#define BLACK 0
#define TURN_BLACK(piece) (piece & 0xE)
#define TURN_WHITE(piece) (piece | 0x1)
/* #define BLACK_TO_COLOR(blackpiece, piece) ((blackpiece)|(piece & 0x1)) */
#define GET_COLOR(piece) (piece & 0x1)
#define FIRST_ROW 0x00
#define SECOND_ROW 0x10
#define THIRD_ROW 0x20
#define FOURTH_ROW 0x30
#define FIFTH_ROW 0x40
#define SIXTH_ROW 0x50
#define SEVENTH_ROW 0x60
#define EIGHT_ROW 0x70
#define A_COLUMN 0x00
#define B_COLUMN 0x01
#define C_COLUMN 0x02
#define D_COLUMN 0x03
#define E_COLUMN 0x04
#define F_COLUMN 0x05
#define G_COLUMN 0x06
#define H_COLUMN 0x07
#define COLUMN(square) ((square) & 0x0F)
#define ROW(square) ((square) & 0xF0)
#define SQUARE(row, column) (row + column)
#define IN_BOARD(square) !((square) & 0x88)
#define INVALID_SQ 0xFF
#define NULL_MOVE 0xFFFF
#define ROW8(sq) ((sq) >> 4)
#define a1 0x00
#define b1 0x01
#define c1 0x02
#define d1 0x03
#define e1 0x04
#define f1 0x05
#define g1 0x06
#define h1 0x07
#define a8 0x70
#define b8 0x71
#define c8 0x72
#define d8 0x73
#define e8 0x74
#define f8 0x75
#define g8 0x76
#define h8 0x77
#define STARTPOS "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1\n"
#define ROW_UP 0x10
#define ROW_DOWN -0x10
#define HISTLEN 500
#define BOARDSIZE 0x80
#define PAWN_VALUE 100
#define KNIGHT_VALUE 300
#define BISHOP_VALUE 310
#define ROOK_VALUE 500
#define QUEEN_VALUE 900
#define STARTPAWNS 1600
#define STARTMATERIAL 6240
#include "hashtable.h"
#include "pawns.h"
typedef struct BOARD {
PIECE squares[BOARDSIZE];
BITBOARD pawns[2];
COLOR white_to_move;
/*Coordinates "behind" the pawn that can be captured en passant, as in FEN:*/
SQUARE en_passant;
unsigned char wk_castle, wq_castle, bk_castle, bq_castle;
unsigned char w_castled, b_castled;
SQUARE wking_pos, bking_pos;
int ply;
int rev_plies[HISTLEN];
KEY zobrist_key;
KEY zobrist_history[HISTLEN];
int piece_material[2];
int pawn_material[2];
int bishops[2];
} BOARD;
typedef struct ZOBKEYS {
KEY zob_pieces[16][BOARDSIZE];
KEY zob_enpass[BOARDSIZE];
KEY zob_castle[4];
KEY zob_side;
} ZOBKEYS;
extern ZOBKEYS zobkeys;
KEY PolyglotKey(const BOARD*);
void InitBoard(BOARD*);
void InitZobrist(BOARD*);
void InitMaterial(BOARD*);
void PrintBoard(const BOARD*);
int ReadFEN(const char*, BOARD*);
int MoveGen(const BOARD*, MOVE*, char);
int CaptureGen(const BOARD*, MOVE*);
int GenerateWhiteCastle(const BOARD*, MOVE*, int);
int GenerateBlackCastle(const BOARD*, MOVE*, int);
typedef int (PIECEMOVES)(const BOARD*, SQUARE, MOVE*, int, char);
PIECEMOVES WhitePawnMoves, BlackPawnMoves, NonSlidingMoves, SlidingMoves;
static PIECEMOVES *const piece_moves[] = {
0,
0,
&BlackPawnMoves,
&WhitePawnMoves,
&NonSlidingMoves,
&NonSlidingMoves,
&SlidingMoves,
&SlidingMoves,
&SlidingMoves,
&SlidingMoves,
&SlidingMoves,
&SlidingMoves,
&NonSlidingMoves,
&NonSlidingMoves,
};
#ifndef DELTAS
#define DELTAS
static const SQUARE w_pawn_capture[] = {0x0F, 0x11, 0};
static const SQUARE b_pawn_capture[] = {-0x0F, -0x11, 0};
static const SQUARE knight_delta[] = {0x21, 0x12, 0x1F, 0x0E, -0x12, -0x0E, -0x21, -0x1F, 0};
static const SQUARE bishop_delta[] = {0x11, 0x0F, -0x11, -0x0F, 0};
static const SQUARE rook_delta[] = {0x01, 0x10, -0x01, -0x10, 0};
static const SQUARE king_delta[] = {0x11, 0x0F, -0x11, -0x0F, 0x01, 0x10, -0x01, -0x10, 0};
#ifdef POINTDELTAS
static const SQUARE *const deltas[] = {
0,
0,
b_pawn_capture,
w_pawn_capture,
knight_delta,
knight_delta,
bishop_delta,
bishop_delta,
rook_delta,
rook_delta,
king_delta,
king_delta,
king_delta,
king_delta,
};
#else
static const SQUARE deltas[][9] = {
{0,0,0,0,0,0,0,0,0,},
{0,0,0,0,0,0,0,0,0,},
{-0x0F, -0x11,0,0,0,0,0,0,0},
{0x0F, 0x11,0,0,0,0,0,0,0},
{0x21, 0x12, 0x1F, 0x0E, -0x12, -0x0E, -0x21, -0x1F, 0},
{0x21, 0x12, 0x1F, 0x0E, -0x12, -0x0E, -0x21, -0x1F, 0},
{0x11, 0x0F, -0x11, -0x0F,0,0,0,0,0},
{0x11, 0x0F, -0x11, -0x0F,0,0,0,0,0},
{0x01, 0x10, -0x01, -0x10,0,0,0,0,0},
{0x01, 0x10, -0x01, -0x10,0,0,0,0,0},
{0x11, 0x0F, -0x11, -0x0F, 0x01, 0x10, -0x01, -0x10, 0},
{0x11, 0x0F, -0x11, -0x0F, 0x01, 0x10, -0x01, -0x10, 0},
{0x11, 0x0F, -0x11, -0x0F, 0x01, 0x10, -0x01, -0x10, 0},
{0x11, 0x0F, -0x11, -0x0F, 0x01, 0x10, -0x01, -0x10, 0}
};
#endif
static const SQUARE distance_to_center[] = {
6, 5, 4, 3, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0,
5, 4, 3, 2, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0,
4, 3, 2, 1, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0,
3, 2, 1, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0,
3, 2, 1, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0,
4, 3, 2, 1, 1, 2, 3, 4, 0, 0, 0, 0, 0, 0, 0, 0,
5, 4, 3, 2, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0,
6, 5, 4, 3, 3, 4, 5, 6, 0, 0, 0, 0, 0, 0, 0, 0
};
#endif
void MakeMove(BOARD*, MOVE*);
void Takeback(BOARD*, const MOVE);
char IsLegal(BOARD*, MOVE*);
int Perft(BOARD*, int);
int SortMoves(BOARD*, MOVE*, int, MOVE[]);
int FilterWinning(BOARD*, MOVE*, int);
int LazyEval(const BOARD*);
int StaticEval(const BOARD*);
int Value(PIECE);
int AttackingPieces(const BOARD*, SQUARE, COLOR, SQUARE*);
inline int IsAttacked(const BOARD *board, SQUARE square, COLOR attacking_color)
{
return AttackingPieces(board, square, attacking_color, 0);
}
inline int WhiteInCheck(const BOARD *board) {
return IsAttacked(board, board->wking_pos, BLACK);
}
inline int BlackInCheck(const BOARD *board) {
return IsAttacked(board, board->bking_pos, WHITE);
}
inline int InCheck(const BOARD *board, SQUARE *attacking_sqs) {
if (board->white_to_move) return AttackingPieces(board, board->wking_pos, BLACK, attacking_sqs);
else return AttackingPieces(board, board->bking_pos, WHITE, attacking_sqs);
}
inline int LeftInCheck(const BOARD *board) {
if (board->white_to_move) return IsAttacked(board, board->bking_pos, WHITE);
else return IsAttacked(board, board->wking_pos, BLACK);
}
inline MOVE Move(PIECE piece, SQUARE dest, SQUARE orig)
{
return (piece << 16) | (dest << 8) | orig;
}
/*Game stage according to material present; 0 -> 1 as game progresses.*/
inline float PawnStage(const BOARD *board)
{
return 1.0f - (float)(board->pawn_material[0] + board->pawn_material[1])/STARTPAWNS;
}
inline float GameStage(const BOARD *board)
{
return 1.0f - (float)(board->piece_material[0] + board->piece_material[1])/STARTMATERIAL;
}
/* Some methods to convert coordinates to algebraic notation, and the other way round.*/
PIECE CharToPiece(const char);
char PieceToChar(PIECE);
char CharToCoordinate(const char);
char RowCoordinateToChar(const char);
char ColumnCoordinateToChar(const char);
MOVE AlgebToMove(const char*);
void MoveToAlgeb(const MOVE, char*);
#endif