-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.cpp
365 lines (292 loc) · 8.4 KB
/
util.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#include "chess.h"
// Utilsity functions for reading FEN String, EPD file, displaying board, etc
// used by the getPieceChar function
static char pieceCharMapping[] = {'.', 'P', 'N', 'B', 'R', 'Q', 'K'};
// gets the numeric code of the piece represented by a character
uint8 Utils::getPieceCode(char piece)
{
switch(piece) {
case 'p':
return COLOR_PIECE(BLACK, PAWN);
case 'n':
return COLOR_PIECE(BLACK, KNIGHT);
case 'b':
return COLOR_PIECE(BLACK, BISHOP);
case 'r':
return COLOR_PIECE(BLACK, ROOK);
case 'q':
return COLOR_PIECE(BLACK, QUEEN);
case 'k':
return COLOR_PIECE(BLACK, KING);
case 'P':
return COLOR_PIECE(WHITE, PAWN);
case 'N':
return COLOR_PIECE(WHITE, KNIGHT);
case 'B':
return COLOR_PIECE(WHITE, BISHOP);
case 'R':
return COLOR_PIECE(WHITE, ROOK);
case 'Q':
return COLOR_PIECE(WHITE, QUEEN);
case 'K':
return COLOR_PIECE(WHITE, KING);
default:
return EMPTY_SQUARE;
}
}
// Gets char representation of a piece code
char Utils::getPieceChar(uint8 code)
{
uint8 color = COLOR(code);
uint8 piece = PIECE(code);
char pieceChar = '.';
if (code != EMPTY_SQUARE)
{
assert((color == WHITE) || (color == BLACK));
assert((piece >= PAWN) && (piece <= KING));
pieceChar = pieceCharMapping[piece];
}
if (color == BLACK)
{
pieceChar += ('p' - 'P');
}
return pieceChar;
}
/*
Format of board in text file (e.g. Starting Position):
rnbqkbnr
pppppppp
........
........
........
........
PPPPPPPP
RNBQKBNR
*/
// methods to read a board from text file
void Utils::readBoardFromFile(char filename[], char board[8][8]) {
FILE * fp = fopen(filename, "r");
char buf[100];
for(int i=0;i<8;i++) {
fscanf(fp, "%s", buf);
for(int j=0;j<8;j++)
board[i][j] = buf[j];
}
fclose(fp);
}
// convert to char board
void Utils::boardCharTo088(BoardPosition *pos, char board[8][8])
{
int i, j;
int index088 = 0;
for (i = 7; i >= 0; i--)
{
for (j = 0; j < 8; j++)
{
char piece = board[i][j];
pos->board[index088] = getPieceCode(piece);
index088++;
}
// skip 8 cells of padding
index088 += 8;
}
}
void Utils::readBoardFromFile(char filename[], BoardPosition *pos)
{
char board[8][8];
readBoardFromFile(filename, board);
boardCharTo088(pos, board);
}
// methods to display the board (in the above form)
void Utils::dispBoard(char board[8][8])
{
int i,j;
for(i=0;i<8;i++) {
for(j=0;j<8;j++)
printf("%c", board[i][j]);
printf("\n");
}
}
// convert to char board
void Utils::board088ToChar(char board[8][8], BoardPosition *pos)
{
int i, j;
int index088 = 0;
for (i = 7; i >= 0; i--)
{
for (j = 0; j < 8; j++)
{
char piece = getPieceChar(pos->board[index088]);
board[i][j] = piece;
index088++;
}
// skip 8 cells of padding
index088 += 8;
}
}
void Utils::dispBoard(BoardPosition *pos)
{
char board[8][8];
board088ToChar(board, pos);
printf("\nBoard Position: \n");
dispBoard(board);
printf("\nGame State: \n");
if (pos->chance == WHITE)
{
printf("White to move\n");
}
else
{
printf("Black to move\n");
}
if (pos->enPassent)
{
printf("En passent allowed for file: %d\n", pos->enPassent);
}
printf("Allowed Castlings:\n");
if (pos->whiteCastle & CASTLE_FLAG_KING_SIDE)
printf("White King Side castle\n");
if (pos->whiteCastle & CASTLE_FLAG_QUEEN_SIDE)
printf("White Queen Side castle\n");
if (pos->blackCastle & CASTLE_FLAG_KING_SIDE)
printf("Black King Side castle\n");
if (pos->blackCastle & CASTLE_FLAG_QUEEN_SIDE)
printf("Black Queen Side castle\n");
}
void Utils::clearBoard(BoardPosition *pos)
{
for(int i=0;i<8;i++)
for (int j=0;j<8;j++)
pos->board[INDEX088(i, j)] = EMPTY_SQUARE;
}
// displays a move object
void Utils::displayMove(Move move)
{
char dispString[10];
uint8 r1, c1, r2, c2;
r1 = RANK(move.src)+1;
c1 = FILE(move.src);
r2 = RANK(move.dst)+1;
c2 = FILE(move.dst);
char sep = move.capturedPiece ? '*' : '-';
sprintf(dispString, "%c%d%c%c%d ",
c1+'a',
r1,
sep,
c2+'a',
r2);
printf(dispString);
}
// reads a FEN string into the given BoardPosition object
/*
Reference: (Wikipedia)
A FEN record contains 6 fields. The separator between fields is a space. The fields are:
1. Piece placement (from white's perspective). Each rank is described, starting with rank 8 and ending with rank 1; within each rank, the contents of each square are described from file a through file h. White pieces are designated using upper-case letters ("KQRBNP"), Black by lowercase ("kqrbnp"). Blank squares are noted using digits 1 through 8 (the number of blank squares), and "/" separate ranks.
2. Active color. "w" means white moves next, "b" means black.
3. Castling availability. If neither side can castle, this is "-". Otherwise, this has one or more letters: "K" (White can castle kingside), "Q" (White can castle queenside), "k" (Black can castle kingside), and/or "q" (Black can castle queenside).
4. En passant target square in algebraic notation. If there's no en passant target square, this is "-". If a pawn has just made a 2-square move, this is the position "behind" the pawn.
5. Halfmove clock: This is the number of halfmoves since the last pawn advance or capture. This is used to determine if a draw can be claimed under the fifty move rule.
6. Fullmove number: The number of the full move. It starts at 1, and is incremented after Black's move.
*/
void Utils::readFENString(char fen[], BoardPosition *pos)
{
int i, j;
char curChar;
int row = 0, col = 0;
memset(pos, 0, sizeof(BoardPosition));
// 1. read the board
for(i=0;fen[i];i++)
{
curChar = fen[i];
if(curChar=='/'||curChar=='\\')
{
row++; col=0;
}
else if(curChar >= '1' && curChar <= '8')
{ // blank squares
for(j = 0; j < curChar - '0'; j++)
{
pos->board[INDEX088(7-row, col)] = getPieceCode(curChar);
col++;
}
}
else if(curChar=='k'||curChar=='q'||curChar=='r'||curChar=='b'||curChar=='n'||curChar=='p' ||
curChar=='K'||curChar=='Q'||curChar=='R'||curChar=='B'||curChar=='N'||curChar=='P')
{
pos->board[INDEX088(7 - row, col)] = getPieceCode(curChar);
col++;
}
if(row >= 7 && col == 8) break; // done with the board, set the flags now
}
i++;
// 2. read the chance
while(fen[i]==' ')
i++;
if(fen[i]=='b' || fen[i]=='B')
{
pos->chance = BLACK;
}
else
{
pos->chance = WHITE;
}
i++;
// 3. read the castle flags
pos->whiteCastle = pos->blackCastle = 0;
while(fen[i]==' ')
i++;
while(fen[i]!= ' ') {
switch(fen[i]) {
case 'k':
pos->blackCastle |= CASTLE_FLAG_KING_SIDE;
break;
case 'q':
pos->blackCastle |= CASTLE_FLAG_QUEEN_SIDE;
break;
case 'K':
pos->whiteCastle |= CASTLE_FLAG_KING_SIDE;
break;
case 'Q':
pos->whiteCastle |= CASTLE_FLAG_QUEEN_SIDE;
break;
}
i++;
}
// 4. read en-passent flag
pos->enPassent = 0;
while(fen[i]==' ')
i++;
if(fen[i] >= 'a' && fen[i] <= 'h')
pos->enPassent = fen[i] - 'a' + 1;
while(fen[i]!=' ' && fen[i])
i++;
//TODO: 5. read the half-move and the full move clocks
}
// reads a epd file with perft values and compares the values with the ones generated by the move generator
/*
void Utils::testPerftFile() {
char buf[200][1000];
unsigned long long realNodes[10];
int n=0;
FILE * fp = fopen("c:\\deltachess\\reference\\perftsuite.epd", "r");
while(fgets(buf[n++], 995, fp));
fclose(fp);
for(maxDepth=1;maxDepth<7;maxDepth++) {
for(int i=0;i<n-1;i++) {
readFENString(buf[i]);
readNodeCounts(buf[i], realNodes);
dispBoard(board);
printf("%d %d %d %d\n\n", chance, castleflagB, castleflagW, enPassentFlag);
nodeCount=0;
minMax(0);
printf("\nNode Counts: Actual - %llu, Real - %llu", nodeCount, realNodes[maxDepth]);
if(nodeCount!=realNodes[maxDepth])
printf(" #### FAILURE #####\n");
else
printf(" SUCCESS \n");
printf("\n-------------------------------------------------------------------\n\n");
}
printf("\n*********************END OF DEPTH %d **********************\n\n", maxDepth);
}
}
*/