-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboard.c
More file actions
460 lines (384 loc) · 10.7 KB
/
board.c
File metadata and controls
460 lines (384 loc) · 10.7 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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#include "ark.h"
int getPiecefromChar(char ch)
{
int piece;
switch (ch)
{
case 'P':
piece = wP;
break;
case 'N':
piece = wN;
break;
case 'B':
piece = wB;
break;
case 'R':
piece = wR;
break;
case 'Q':
piece = wQ;
break;
case 'K':
piece = wK;
break;
case 'p':
piece = bP;
break;
case 'n':
piece = bN;
break;
case 'b':
piece = bB;
break;
case 'r':
piece = bR;
break;
case 'q':
piece = bQ;
break;
case 'k':
piece = bK;
break;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
piece = EMPTY;
break;
default:
piece = -1;
break;
}
return piece;
}
// Double chechking whether the pieces on the board are setup correctly
int CheckBoard(const S_BOARD *board)
{
int t_pceNum[13] = {0};
int t_bigPce[2] = {0};
int t_majorPce[2] = {0};
int t_minorPce[2] = {0};
int t_material[2] = {0};
U64 t_pawns[3] = {0ULL, 0ULL, 0ULL};
t_pawns[WHITE] = board->pawns[WHITE];
t_pawns[BLACK] = board->pawns[BLACK];
t_pawns[BOTH] = board->pawns[BOTH];
// Check the position of the pieces
for (int piece = wP; piece <= bK; piece++)
{
for (int count = 0; count < board->pceNum[piece]; count++)
{
int sq = board->pList[piece][count];
ASSERT(piece == board->pieces[sq]);
}
}
// Check the count
for (int sq64 = 0; sq64 < 64; sq64++)
{
int sq120 = SQ120(sq64);
int piece = board->pieces[sq120];
t_pceNum[piece]++;
int color = PieceCol[piece];
if (PieceBig[piece] == TRUE)
t_bigPce[color]++;
if (PieceMaj[piece] == TRUE)
t_majorPce[color]++;
if (PieceMin[piece] == TRUE)
t_minorPce[color]++;
t_material[color] += PieceVal[piece];
}
for (int p = wP; p <= bK; p++)
{
ASSERT(board->pceNum[p] == t_pceNum[p]);
}
// Check the pawn bitmaps
int pcount;
pcount = CNT(t_pawns[WHITE]);
ASSERT(pcount == board->pceNum[wP]);
pcount = CNT(t_pawns[BLACK]);
ASSERT(pcount == board->pceNum[bP]);
pcount = CNT(t_pawns[BOTH]);
ASSERT(pcount == (board->pceNum[wP] + board->pceNum[bP]));
// Check pawn bitmaps and position
while (t_pawns[WHITE])
{
int sq64 = POP(&t_pawns[WHITE]);
ASSERT(board->pieces[SQ120(sq64)] == wP);
}
while (t_pawns[BLACK])
{
int sq64 = POP(&t_pawns[BLACK]);
ASSERT(board->pieces[SQ120(sq64)] == bP);
}
while (t_pawns[BOTH])
{
int sq64 = POP(&t_pawns[BOTH]);
ASSERT(board->pieces[SQ120(sq64)] == wP || board->pieces[SQ120(sq64)] == bP);
}
// Check the count of each category
ASSERT(t_material[WHITE] == board->material[WHITE] && t_material[BLACK] == board->material[BLACK]);
ASSERT(t_bigPce[WHITE] == board->bigPce[WHITE] && t_bigPce[BLACK] == board->bigPce[BLACK]);
ASSERT(t_majorPce[WHITE] == board->majorPce[WHITE] && t_majorPce[BLACK] == board->majorPce[BLACK]);
ASSERT(t_minorPce[WHITE] == board->minorPce[WHITE] && t_minorPce[BLACK] == board->minorPce[BLACK]);
ASSERT(board->side == WHITE || board->side == BLACK);
// Check the poskey
ASSERT(generatePosKey(board) == board->posKey);
ASSERT(board->enPass == NO_SQ || (RanksBrd[board->enPass] == RANK_6 && board->side == WHITE) || (RanksBrd[board->enPass] == RANK_3 && board->side == BLACK));
// Check the king's position (This Also includes that a position might have no kings)
ASSERT(board->pieces[board->kingSq[WHITE]] == wK || board->pieces[board->kingSq[WHITE]] == OFF_BOARD);
ASSERT(board->pieces[board->kingSq[BLACK]] == bK || board->pieces[board->kingSq[BLACK]] == OFF_BOARD);
return TRUE;
}
// Update the values of the each category
void UpdateListsMaterial(S_BOARD *board)
{
for (int index = 0; index < BRD_SQ_NUM; index++)
{
int piece = board->pieces[index];
if (piece != OFF_BOARD && piece != EMPTY)
{
int colour = PieceCol[piece];
if (PieceBig[piece] == TRUE)
board->bigPce[colour]++;
if (PieceMin[piece] == TRUE)
board->minorPce[colour]++;
if (PieceMaj[piece] == TRUE)
board->majorPce[colour]++;
board->material[colour] += PieceVal[piece];
board->pList[piece][board->pceNum[piece]] = index;
board->pceNum[piece]++;
if (piece == wK)
board->kingSq[WHITE] = index;
if (piece == bK)
board->kingSq[BLACK] = index;
if (piece == wP || piece == bP)
{
SETBIT(board->pawns[colour], SQ64(index));
SETBIT(board->pawns[BOTH], SQ64(index));
}
}
}
}
// Parse a FEN in char * passed, and setup the board accordingly
int parseFen(const char *fen, S_BOARD *board)
{
ASSERT(fen != NULL);
ASSERT(board != NULL);
int rank = RANK_8;
int file = FILE_A;
int frstSpace = FALSE;
int index = 0;
resetBoard(board);
for (; index < strlen(fen); index++)
{
if (fen[index] == ' ')
{
break;
}
if (fen[index] != '/')
{
int fenPiece = getPiecefromChar(fen[index]);
if (fenPiece == EMPTY)
{
file += (fen[index] - '0') - 1;
}
else if (fenPiece != -1)
{
board->pieces[FR2SQ(file, rank)] = fenPiece;
}
else
{
printf("FEN ERROR!");
return 1;
}
file++;
}
else
{
if (file != FILE_NONE)
{
printf("FEN ERROR!");
return 1;
}
rank--;
file = FILE_A;
}
}
index++;
ASSERT(fen[index] == 'w' || fen[index] == 'b');
board->side = (fen[index] == 'w') ? WHITE : BLACK;
index += 2;
while (fen[index] != ' ')
{
switch (fen[index])
{
case 'K':
board->castlePerm |= WKCA;
break;
case 'Q':
board->castlePerm |= WQCA;
break;
case 'k':
board->castlePerm |= BKCA;
break;
case 'q':
board->castlePerm |= BQCA;
break;
}
index++;
}
index++;
if (fen[index] != '-')
{
int enPassFile = fen[index] - 'a';
int enPassRank = fen[index + 1] - '1';
ASSERT(enPassFile >= FILE_A && enPassFile <= FILE_H);
ASSERT(enPassFile >= RANK_1 && enPassFile <= RANK_8);
board->enPass = FR2SQ(enPassFile, enPassRank);
index += 2;
}
index++;
board->posKey = generatePosKey(board);
UpdateListsMaterial(board);
ASSERT(CheckBoard(board));
return 0;
}
// Revert the board to its original state
void resetBoard(S_BOARD *board)
{
// Set all board squares to NO_SQ
for (int i = 0; i < BRD_SQ_NUM; i++)
{
board->pieces[i] = OFF_BOARD;
}
// Set all the 64 squares to EMPTY
for (int i = 0; i < 64; i++)
{
board->pieces[SQ120(i)] = EMPTY;
}
for (int i = WHITE; i <= BOTH; i++)
{
board->pawns[i] = 0ULL;
}
for (int i = WHITE; i <= BLACK; i++)
{
board->bigPce[i] = 0;
board->majorPce[i] = 0;
board->minorPce[i] = 0;
board->material[i] = 0;
}
for (int i = wP; i <= bK; i++)
{
board->pceNum[i] = 0;
}
board->kingSq[WHITE] = board->kingSq[BLACK] = NO_SQ;
board->side = BOTH;
board->enPass = NO_SQ;
board->fiftyMoves = 0;
board->ply = 0;
board->hisply = 0;
board->castlePerm = 0;
board->posKey = 0ULL;
}
void printBoardFen(const S_BOARD *board)
{
int totalEmpty = 0;
printf("Current Fen : ");
for (int rank = RANK_8; rank >= RANK_1; rank--)
{
for (int file = FILE_A; file <= FILE_H; file++)
{
int piece = board->pieces[FR2SQ(file, rank)];
if (!piece)
{
totalEmpty += 1;
}
else
{
if (totalEmpty > 0)
{
printf("%d", totalEmpty);
totalEmpty = 0;
}
printf("%c", PieceChars[piece]);
}
}
if (totalEmpty > 0)
{
printf("%d", totalEmpty);
totalEmpty = 0;
}
if (rank != RANK_1)
printf("/");
}
printf(" %c", tolower(SideChars[board->side]));
printf(" %c%c%c%c",
board->castlePerm & WKCA ? 'K' : '-',
board->castlePerm & WQCA ? 'Q' : '-',
board->castlePerm & BKCA ? 'k' : '-',
board->castlePerm & BQCA ? 'q' : '-');
if (board->enPass != NO_SQ && board->enPass != OFF_BOARD)
{
printf(" %s", PrSq(board->enPass));
}
else
{
printf(" -", PrSq(board->enPass));
}
printf(" %d", board->ply / 2);
printf(" %d", board->ply);
printf(" \n");
}
// Prints the board to the console
void printBoard(const S_BOARD *board)
{
printf("GAME BOARD \n\n");
for (int rank = RANK_8; rank >= RANK_1; rank--)
{
printf("%8c", RankChars[rank]);
for (int file = FILE_A; file <= FILE_H; file++)
{
int piece = board->pieces[FR2SQ(file, rank)];
printf("%5c", PieceChars[piece]);
}
printf("\n\n");
}
printf("\n%8c", ' ');
for (int file = FILE_A; file <= FILE_H; file++)
printf("%5c", FileChars[file]);
printf("\n\n");
printf("Side to Play : %c\n", SideChars[board->side]);
printf("EnPass : %d\n", board->enPass);
printf("Castle Perm : %c%c%c%c\n",
board->castlePerm & WKCA ? 'K' : '-',
board->castlePerm & WQCA ? 'Q' : '-',
board->castlePerm & BKCA ? 'k' : '-',
board->castlePerm & BQCA ? 'q' : '-');
printf("PosKey : %llX\n", board->posKey);
printBoardFen(board);
printf("\n\n");
}
// Prints the board to the console
void printBoardOnly(const S_BOARD *board)
{
printf("GAME BOARD \n\n");
for (int rank = RANK_8; rank >= RANK_1; rank--)
{
printf("%8c", RankChars[rank]);
for (int file = FILE_A; file <= FILE_H; file++)
{
int piece = board->pieces[FR2SQ(file, rank)];
printf("%5c", PieceChars[piece]);
}
printf("\n\n");
}
printf("\n%8c", ' ');
for (int file = FILE_A; file <= FILE_H; file++)
printf("%5c", FileChars[file]);
printf("\n");
}