-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.cpp
More file actions
273 lines (220 loc) · 7.44 KB
/
Copy pathboard.cpp
File metadata and controls
273 lines (220 loc) · 7.44 KB
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
#include "board.h"
#include <string.h>
void ClearBoard(Board *board) {
memset(board->pieces, 0, sizeof(board->pieces));
memset(board->occupancies, 0, sizeof(board->occupancies));
memset(board->history, 0, sizeof(board->history));
for (int i = 0; i < 64; i++) {
board->squares[i] = NO_PIECE;
}
board->piecesCounts = 0ULL;
board->zobrist = 0ULL;
board->pawnZobrist = 0ULL;
board->stm = WHITE;
board->xstm = BLACK;
board->epSquare = 0;
board->castling = 0;
board->histPly = 0;
board->moveNo = 1;
board->fmr = 0;
board->nullply = 0;
}
void ParseFen(char *fen, Board *board) {
ClearBoard(board);
for (int i = 0; i < 64; i++) {
if ((*fen >= 'a' && *fen <= 'z') || (*fen >= 'A' && *fen <= 'Z')) {
int piece = CHAR_TO_PIECE[(int) *fen];
board->pieces[piece] |= (1ULL << (i)); // Set it on the bitboard
board->squares[i] = piece;
if (*fen != 'K' && *fen != 'k') {
board->piecesCounts += (1ULL << (4 * piece));
}
} else if (*fen >= '0' && *fen <= '9') {
int offset = *fen - '1';
i += offset;
} else if (*fen == '/') {
i--;
}
fen++;
}
fen++;
board->stm = (*fen++ == 'w' ? WHITE : BLACK);
board->xstm = !board->stm;
board->castling = 0;
for (int i = 0; i < 4; i++) {
board->cr[i] = -1;
}
int whiteKing = LSB(PieceBB(KING, WHITE));
int blackKing = LSB(PieceBB(KING, BLACK));
Bitboard whiteRooks = PieceBB(ROOK, WHITE) & RANK_1;
Bitboard blackRooks = PieceBB(ROOK, BLACK) & RANK_8;
while (*(++fen) != ' ') {
if (*fen == 'K') {
board->castling |= WHITE_KS;
board->cr[0] = MSB(whiteRooks);
} else if (*fen == 'Q') {
board->castling |= WHITE_QS;
board->cr[1] = LSB(whiteRooks);
} else if (*fen >= 'A' && *fen <= 'H') {
board->castling |= ((*fen - 'A') > File(whiteKing) ? WHITE_KS : WHITE_QS);
board->cr[(*fen - 'A') > File(whiteKing) ? 0 : 1] = A1 + (*fen - 'A');
} else if (*fen == 'k') {
board->castling |= BLACK_KS;
board->cr[2] = MSB(blackRooks);
} else if (*fen == 'q') {
board->castling |= BLACK_QS;
board->cr[3] = LSB(blackRooks);
} else if (*fen >= 'a' && *fen <= 'h') {
board->castling |= ((*fen - 'a') > File(blackKing) ? BLACK_KS : BLACK_QS);
board->cr[(*fen - 'a') > File(blackKing) ? 2 : 3] = A8 + (*fen - 'a');
}
}
for (int i = 0; i < 64; i++) {
board->castlingRights[i] = board->castling;
if (i == whiteKing) {
board->castlingRights[i] ^= (WHITE_KS | WHITE_QS);
} else if (i == blackKing) {
board->castlingRights[i] ^= (BLACK_KS | BLACK_QS);
} else if (i == board->cr[0]) {
board->castlingRights[i] ^= WHITE_KS;
} else if (i == board->cr[1]) {
board->castlingRights[i] ^= WHITE_QS;
} else if (i == board->cr[2]) {
board->castlingRights[i] ^= BLACK_KS;
} else if (i == board->cr[3]) {
board->castlingRights[i] ^= BLACK_QS;
}
}
fen++;
if (*fen != '-') {
int f = fen[0] - 'a';
int r = 8 - (fen[1] - '0');
board->epSquare = r * 8 + f;
} else {
board->epSquare = 0;
}
while (*fen && *fen != ' ') {
fen++;
}
sscanf(fen, " %d %d", &board->fmr, &board->moveNo);
OccBB(WHITE) = OccBB(BLACK) = OccBB(BOTH) = 0;
for (int i = WHITE_PAWN; i <= BLACK_KING; i++) {
OccBB(i & 1) |= board->pieces[i];
}
OccBB(BOTH) = OccBB(WHITE) | OccBB(BLACK);
SetSpecialPieces(board);
SetThreats(board);
board->zobrist = Zobrist(board);
board->pawnZobrist = PawnZobrist(board);
}
void BoardToFen(char *fen, Board *board) {
for (int r = 0; r < 8; r++) {
int c = 0;
for (int f = 0; f < 8; f++) {
int sq = 8 * r + f;
int piece = board->squares[sq];
if (piece != NO_PIECE) {
if (c) {
*fen++ = c + '0';
}
*fen++ = PIECE_TO_CHAR[piece];
c = 0;
} else {
c++;
}
}
if (c) {
*fen++ = c + '0';
}
*fen++ = (r == 7) ? ' ' : '/';
}
*fen++ = board->stm ? 'b' : 'w';
*fen++ = ' ';
if (board->castling) {
if (board->castling & 0x8) {
*fen++ = 'K';
}
if (board->castling & 0x4) {
*fen++ = 'Q';
}
if (board->castling & 0x2) {
*fen++ = 'k';
}
if (board->castling & 0x1) {
*fen++ = 'q';
}
} else {
*fen++ = '-';
}
*fen++ = ' ';
sprintf(fen, "%s %d %d", board->epSquare ? SQ_TO_COMBO[board->epSquare] : "-", board->fmr, board->moveNo);
}
void PrintBoard(Board *board) {
static char fenBuffer[120];
for (int r = 0; r < 8; r++) {
printf("+-------+-------+-------+-------+-------+-------+-------+-------+\n");
printf("|");
for (int f = 0; f < 16; f++) {
if (f == 8) {
printf("\n|");
}
int sq = r * 8 + (f > 7 ? f - 8 : f);
if (f < 8) {
if (board->squares[sq] == NO_PIECE) {
printf(" |");
} else {
printf(" %c |", PIECE_TO_CHAR[board->squares[sq]]);
}
} else {
printf(" |");
}
}
printf("\n");
}
printf("+-------+-------+-------+-------+-------+-------+-------+-------+\n");
BoardToFen(fenBuffer, board);
printf("\n FEN: %s\n\n", fenBuffer);
}
// Special pieces are those giving check, and those that are pinned
inline void SetSpecialPieces(Board *board) {
}
void SetThreats(Board *board);
int DoesMoveCheck(Move move, Board *board);
int IsDraw(Board *board, int ply);
int IsRepetition(Board *board, int ply);
int IsMaterialDraw(Board *board);
int IsFiftyMoveRule(Board *board);
void MakeNullMove(Board *board);
void UndoNullMove(Board *board);
void MakeMove(Move move, Board *board) {
MakeMoveUpdate(move, board, 1);
}
void MakeMoveUpdate(Move move, Board *board, int update) {
int from = From(move);
int to = To(move);
int piece = Moving(move);
int captured = IsEP(move) ? Piece(PAWN, board->xstm) : board->squares[to];
memcpy(&board->history[board->histPly], board, offsetof(Board, stm));
board->history[board->histPly].capture = captured;
board->fmr++;
board->nullply++;
FlipBits(board->pieces[piece], from, to);
FlipBits(OccBB(board->stm), from, to);
FlipBits(OccBB(BOTH), from, to);
board->squares[from] = NO_PIECE;
board->squares[to] = piece;
board->zobrist ^= ZOBRIST_PIECES[piece][from] ^ ZOBRIST_PIECES[piece][to];
if (PieceType(piece) == PAWN) {
board->pawnZobrist ^= ZOBRIST_PIECES[piece][from] ^ ZOBRIST_PIECES[piece][to];
}
if (IsCas(move)) {
int rookFrom = board->cr[CASTLING_ROOK[to]];
int rookTo = CASTLE_ROOK_DEST[to];
int rook = Piece(ROOK, board->stm);
}
int captured = IsEP(move) ? Piece(PAWN, board->xstm) : board->squares[to];
// memcpy(&board->history,)
}
void UndoMove(Move move, Board *board);
int IsPseudoLegal(Move move, Board *board);
int IsLegal(Move move, Board *board);