-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
343 lines (291 loc) · 8.54 KB
/
Board.cpp
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include <stdio.h>
#include "defs.h"
bool CheckBoard(const S_BOARD *pos)
{
int t_pceNum[13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int t_bigPce[2] = {0, 0};
int t_majPce[2] = {0, 0};
int t_minPce[2] = {0, 0};
int t_material[2] = {0, 0};
U64 t_pawns[3] = {0ULL, 0ULL, 0ULL};
t_pawns[WHITE] = pos->pawns[WHITE];
t_pawns[BLACK] = pos->pawns[BLACK];
t_pawns[BOTH] = pos->pawns[BOTH];
for (int t_piece = wP; t_piece <= bK; t_piece++)
{
for (int t_piece_num = 0; t_piece_num < pos->pceNum[t_piece]; t_piece_num++)
{
int sq120 = pos->pList[t_piece][t_piece_num];
ASSERT(pos->board[sq120] == t_piece);
}
}
// Go through the board, check piece count and other counters;
for (int sq64 = 0; sq64 < 64; sq64++)
{
int sq120 = Sq64ToSq120[sq64];
int t_piece = pos->board[sq120];
int color = PieceCol[t_piece];
t_pceNum[t_piece] += 1;
if (PieceBig[t_piece] == TRUE)
t_bigPce[color]++;
if (PieceMin[t_piece] == TRUE)
t_minPce[color]++;
if (PieceMaj[t_piece] == TRUE)
t_majPce[color]++;
t_material[color] += PieceVal[t_piece];
}
for (int t_piece = wP; t_piece <= bK; t_piece++)
{
ASSERT(t_pceNum[t_piece] == pos->pceNum[t_piece]);
}
// Check the Bitboard count
int pCount = CountBits(t_pawns[WHITE]);
ASSERT(pCount == pos->pceNum[wP]);
pCount = CountBits(t_pawns[BLACK]);
ASSERT(pCount == pos->pceNum[bP]);
pCount = CountBits(t_pawns[BOTH]);
ASSERT(pCount == (pos->pceNum[bP] + pos->pceNum[wP]));
// check bitboard squares
while (t_pawns[WHITE])
{
int sq64 = PopBit(&t_pawns[WHITE]);
ASSERT(pos->board[Sq64ToSq120[sq64]] == wP);
}
while (t_pawns[BLACK])
{
int sq64 = PopBit(&t_pawns[BLACK]);
ASSERT(pos->board[Sq64ToSq120[sq64]] == bP);
}
while (t_pawns[BOTH])
{
int sq64 = PopBit(&t_pawns[BOTH]);
ASSERT((pos->board[Sq64ToSq120[sq64]] == bP) || (pos->board[Sq64ToSq120[sq64]] == wP));
}
ASSERT(t_material[WHITE] == pos->material[WHITE] && t_material[BLACK] == pos->material[BLACK]);
ASSERT(t_minPce[WHITE] == pos->minPce[WHITE] && t_minPce[BLACK] == pos->minPce[BLACK]);
ASSERT(t_majPce[WHITE] == pos->majPce[WHITE] && t_majPce[BLACK] == pos->majPce[BLACK]);
ASSERT(t_bigPce[WHITE] == pos->bigPce[WHITE] && t_bigPce[BLACK] == pos->bigPce[BLACK]);
ASSERT(pos->side == WHITE || pos->side == BLACK);
ASSERT(GeneratePosKey(pos) == pos->posKey);
ASSERT(pos->enPas == NO_SQ || (RanksBrd[pos->enPas] == RANK_6 && pos->side == WHITE) || (RanksBrd[pos->enPas] == RANK_3 && pos->side == BLACK));
ASSERT(pos->board[pos->KingSq[WHITE]] == wK);
ASSERT(pos->board[pos->KingSq[BLACK]] == bK);
return true;
}
void UpdateListsMaterial(S_BOARD *pos)
{
for (int index = 0; index < BRD_SQ_NUM; index++)
{
int sq = index;
int piece = pos->board[sq];
if (piece != 120 && piece != EMPTY)
{
int color = PieceCol[piece];
if (PieceBig[piece] == TRUE)
pos->bigPce[color]++;
if (PieceMin[piece] == TRUE)
pos->minPce[color]++;
if (PieceMaj[piece] == TRUE)
pos->majPce[color]++;
pos->material[color] += PieceVal[piece];
pos->pList[piece][pos->pceNum[piece]] = sq;
pos->pceNum[piece]++;
if (piece == wK)
pos->KingSq[WHITE] = sq;
if (piece == bK)
pos->KingSq[BLACK] = sq;
if (piece == wP)
{
SETBIT(pos->pawns[WHITE], Sq120ToSq64[sq]);
SETBIT(pos->pawns[BOTH], Sq120ToSq64[sq]);
}
else if (piece == bP)
{
SETBIT(pos->pawns[BLACK], Sq120ToSq64[sq]);
SETBIT(pos->pawns[BOTH], Sq120ToSq64[sq]);
}
}
}
}
int ParseFen(const char *fen, S_BOARD *pos)
{
ASSERT(fen != NULL);
ASSERT(pos != NULL);
ResetBoard(pos);
int rank = RANK_8;
int file = FILE_A;
int piece = 0;
int count = 0; // To count the number of empty squares
int i = 0;
int sq64 = 0;
int sq120 = 0;
while ((rank >= RANK_1) && *fen)
{
count = 1;
switch (*fen)
{
case 'p':
piece = bP;
break;
case 'r':
piece = bR;
break;
case 'n':
piece = bN;
break;
case 'b':
piece = bB;
break;
case 'k':
piece = bK;
break;
case 'q':
piece = bQ;
break;
case 'P':
piece = wP;
break;
case 'R':
piece = wR;
break;
case 'N':
piece = wN;
break;
case 'B':
piece = wB;
break;
case 'K':
piece = wK;
break;
case 'Q':
piece = wQ;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
piece = EMPTY;
count = *fen - '0'; // Converts the char to an int
break;
case '/':
case ' ':
rank--;
file = FILE_A;
fen++;
continue;
default:
std::cout << "FEN error" << std::endl;
return -1;
}
for (i = 0; i < count; i++)
{
sq64 = rank * 8 + file;
sq120 = Sq64ToSq120[sq64];
if (piece != EMPTY)
{
pos->board[sq120] = piece;
}
file++;
}
fen++;
}
ASSERT(*fen == 'w' || *fen == 'b');
pos->side = (*fen == 'w') ? WHITE : BLACK;
fen += 2;
while (*fen != ' ')
{
switch (*fen)
{
case 'K':
pos->castlePerm |= WKCA;
break;
case 'Q':
pos->castlePerm |= WQCA;
break;
case 'k':
pos->castlePerm |= BKCA;
break;
case 'q':
pos->castlePerm |= BQCA;
break;
}
fen++;
}
fen++;
ASSERT(pos->castlePerm >= 0 && pos->castlePerm <= 15);
if (*fen != '-')
{
file = fen[0] - 'a';
rank = fen[1] - '1'; // Subtracting '1' from the char gives us the int value of the char
ASSERT(file >= FILE_A && file <= FILE_H);
ASSERT(rank >= RANK_1 && rank <= RANK_8);
pos->enPas = FR2SQ(file, rank);
}
pos->posKey = GeneratePosKey(pos);
UpdateListsMaterial(pos);
return 0;
}
void ResetBoard(S_BOARD *pos)
{
for (int i = 0; i < BRD_SQ_NUM; i++)
{
pos->board[i] = 120; // Set them to offboard
}
for (int i = 0; i < 64; i++)
{
pos->board[Sq64ToSq120[i]] = EMPTY;
}
for (int i = 0; i < 2; i++)
{
pos->bigPce[i] = 0;
pos->majPce[i] = 0;
pos->minPce[i] = 0;
pos->material[i] = 0;
pos->pawns[i] = 0ULL;
}
for (int i = 0; i < 13; i++)
{
pos->pceNum[i] = 0;
}
pos->side = BOTH;
pos->enPas = NO_SQ;
pos->fiftyMove = 0;
pos->ply = 0;
pos->hisPly = 0;
pos->castlePerm = 0;
pos->posKey = 0ULL;
}
void PrintBoard(const S_BOARD *pos)
{
for (int rank = RANK_8; rank >= RANK_1; rank--)
{
std::cout << rank + 1 << " ";
for (int file = FILE_A; file <= FILE_H; file++)
{
int sq120 = FR2SQ(file, rank);
int piece = pos->board[sq120];
std::cout << PceChar[piece] << " ";
}
std::cout << std::endl;
}
std::cout << std::endl
<< " ";
for (int file = FILE_A; file <= FILE_H; file++)
{
std::cout << " " << (char)('a' + file);
}
std::cout << std::endl
<< std::endl;
std::cout << "side : " << SideChar[pos->side] << std::endl;
std::cout << "enPas : " << pos->enPas << std::endl;
std::cout << "castle : " << pos->castlePerm && WKCA ? 'K' : '-' << pos->castlePerm && WQCA ? 'Q'
: '-' << pos->castlePerm && BKCA ? 'k'
: '-' << pos->castlePerm && BQCA ? 'q'
: '-';
std::cout << std::endl;
std::cout << "PosKey : " << pos->posKey << std::endl;
}