-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperft.cpp
More file actions
244 lines (207 loc) · 7.11 KB
/
perft.cpp
File metadata and controls
244 lines (207 loc) · 7.11 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
// a simple 0x88 move generater / perft tool
#include "chess.h"
// routines to make a move on the board and to undo it
void makeMove(BoardPosition *pos, Move move)
{
uint8 piece = PIECE(pos->board[move.src]);
uint32 chance = pos->chance;
pos->board[move.dst] = pos->board[move.src];
pos->board[move.src] = EMPTY_SQUARE;
if (move.flags)
{
// special moves
// 1. Castling: update the rook position too
if(move.flags == CASTLE_KING_SIDE)
{
if (chance == BLACK)
{
pos->board[0x77] = EMPTY_SQUARE;
pos->board[0x75] = COLOR_PIECE(BLACK, ROOK);
}
else
{
pos->board[0x07] = EMPTY_SQUARE;
pos->board[0x05] = COLOR_PIECE(WHITE, ROOK);
}
}
else if (move.flags == CASTLE_QUEEN_SIDE)
{
if (chance == BLACK)
{
pos->board[0x70] = EMPTY_SQUARE;
pos->board[0x73] = COLOR_PIECE(BLACK, ROOK);
}
else
{
pos->board[0x00] = EMPTY_SQUARE;
pos->board[0x03] = COLOR_PIECE(WHITE, ROOK);
}
}
// 2. en-passent: clear the captured piece
else if (move.flags == EN_PASSENT)
{
pos->board[INDEX088(RANK(move.src), pos->enPassent - 1)] = EMPTY_SQUARE;
}
// 3. promotion: update the pawn to promoted piece
else if (move.flags == PROMOTION_QUEEN)
{
pos->board[move.dst] = COLOR_PIECE(chance, QUEEN);
}
else if (move.flags == PROMOTION_ROOK)
{
pos->board[move.dst] = COLOR_PIECE(chance, ROOK);
}
else if (move.flags == PROMOTION_KNIGHT)
{
pos->board[move.dst] = COLOR_PIECE(chance, KNIGHT);
}
else if (move.flags == PROMOTION_BISHOP)
{
pos->board[move.dst] = COLOR_PIECE(chance, BISHOP);
}
}
// update game state variables
pos->enPassent = 0;
if (piece == KING)
{
if (chance == BLACK)
{
pos->blackCastle = 0;
}
else
{
pos->whiteCastle = 0;
}
}
else if (piece == ROOK)
{
if (chance == BLACK)
{
if (move.src == 0x77)
pos->blackCastle &= ~CASTLE_FLAG_KING_SIDE;
else if (move.src == 0x70)
pos->blackCastle &= ~CASTLE_FLAG_QUEEN_SIDE;
}
else
{
if (move.src == 0x7)
pos->whiteCastle &= ~CASTLE_FLAG_KING_SIDE;
else if (move.src == 0x0)
pos->whiteCastle &= ~CASTLE_FLAG_QUEEN_SIDE;
}
}
else if ((piece == PAWN) && (abs(RANK(move.dst) - RANK(move.src)) == 2))
{
pos->enPassent = FILE(move.src) + 1;
}
// clear the appriopiate castle flag if a rook is captured
if (PIECE(move.capturedPiece) == ROOK)
{
if (chance == BLACK)
{
if (move.dst == 0x7)
pos->whiteCastle &= ~CASTLE_FLAG_KING_SIDE;
else if (move.dst == 0x0)
pos->whiteCastle &= ~CASTLE_FLAG_QUEEN_SIDE;
}
else
{
if (move.dst == 0x77)
pos->blackCastle &= ~CASTLE_FLAG_KING_SIDE;
else if (move.dst == 0x70)
pos->blackCastle &= ~CASTLE_FLAG_QUEEN_SIDE;
}
}
// flip the chance
pos->chance = !chance;
}
// this has a bug - doesn't handle special moves like castling, etc correctly!
void undoMove(BoardPosition *pos, Move move, uint8 bc, uint8 wc, uint8 enPassent)
{
pos->board[move.src] = pos->board[move.dst];
pos->board[move.dst] = move.capturedPiece;
pos->blackCastle = bc;
pos->whiteCastle = wc;
pos->enPassent = enPassent;
pos->chance = !pos->chance;
}
// perft search
template <class Generator>
uint64 perft(BoardPosition *pos, int depth)
{
// TODO: check if keeping this local variable is ok
Move moves[MAX_MOVES];
uint64 childPerft = 0;
uint32 nMoves = Generator::generateMoves(pos, moves);
if (depth == 1)
{
return nMoves;
}
for (uint32 i = 0; i < nMoves; i++)
{
//uint8 blackCastle = pos->blackCastle;
//uint8 whiteCastle = pos->whiteCastle;
//uint8 enPassent = pos->enPassent;
BoardPosition newPos = *pos;
makeMove(&newPos, moves[i]);
uint32 count = perft<Generator>(&newPos, depth - 1);
/*
if (depth == 3)
{
Utils::displayMove(moves[i]);
printf(" : %d\n", count);
}
*/
childPerft += count;
//undoMove(pos, moves[i], blackCastle, whiteCastle, enPassent);
}
return childPerft;
}
// for timing CPU code : start
double gTime;
#define START_TIMER { \
LARGE_INTEGER count1, count2, freq; \
QueryPerformanceFrequency (&freq); \
QueryPerformanceCounter(&count1);
#define STOP_TIMER \
QueryPerformanceCounter(&count2); \
gTime = ((double)(count2.QuadPart-count1.QuadPart)*1000.0)/freq.QuadPart; \
}
// for timing CPU code : end
int main()
{
BoardPosition testBoard;
// some test board positions from http://chessprogramming.wikispaces.com/Perft+Results
//Utils::readFENString("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", &testBoard); // start.. 20 positions
Utils::readFENString("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -", &testBoard); // position 2 (caught max bugs for me)
//Utils::readFENString("8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - -", &testBoard); // position 3
//Utils::readFENString("r2q1rk1/pP1p2pp/Q4n2/bbp1p3/Np6/1B3NBn/pPPP1PPP/R3K2R b KQ - 0 1", &testBoard); // position 4
//Utils::readFENString("r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1", &testBoard); // mirror of position 4
//Utils::readFENString("rnbqkb1r/pp1p1ppp/2p5/4P3/2B5/8/PPP1NnPP/RNBQK2R w KQkq - 0 6", &testBoard); // position 5
//Utils::readFENString("3Q4/1Q4Q1/4Q3/2Q4R/Q4Q2/3Q4/1Q4Rp/1K1BBNNk w - - 0 1", &testBoard); // - 218 positions.. correct!
Utils::dispBoard(&testBoard);
//Move moves[MAX_MOVES];
//uint32 nMoves = MoveGenerator::generateMoves(&testBoard, moves);
//printf("\nMoves generated: %d\n", nMoves);
//MoveGeneratorLUT::init();
//nMoves = MoveGeneratorLUT::generateMoves(&testBoard, moves);
//printf("\nMoves generated by LUT: %d\n", nMoves);
for (int depth=1;depth<8;depth++)
{
uint64 leafNodes;
START_TIMER
leafNodes = perft<MoveGenerator>(&testBoard, depth);
STOP_TIMER
printf("\nPerft %d: %llu, ", depth, leafNodes);
printf("Time taken: %g seconds, nps: %llu\n", gTime/1000.0, (uint64) ((leafNodes/gTime)*1000.0));
}
/*
START_TIMER
leafNodes = perft<MoveGeneratorLUT>(&testBoard, depth);
STOP_TIMER
printf("\nLookup table based Move Generator: ");
printf("\nPerft %d: %llu", depth, leafNodes);
printf("\nTime taken: %g seconds, nps: %llu\n", gTime/1000.0, (uint64) ((leafNodes/gTime)*1000.0));
*/
return 0;
}