-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperft.cpp
More file actions
157 lines (126 loc) · 4.98 KB
/
perft.cpp
File metadata and controls
157 lines (126 loc) · 4.98 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
#include <iostream>
#include <chrono>
#include <cassert>
#include "position.h"
#include "movegen.h"
using namespace std;
#define copy_board(pos) \
int board_copy[128], king_squares_copy[2], side_copy, enpassant_copy, castle_copy; \
copy(pos.board, pos.board + 128, board_copy); \
copy(pos.king_squares, pos.king_squares + 2, king_squares_copy); \
side_copy = pos.side; \
enpassant_copy = pos.enpassant; \
castle_copy = pos.castle;
#define restore_board(pos) \
copy(board_copy, board_copy + 128, pos.board); \
copy(king_squares_copy, king_squares_copy + 2, pos.king_squares); \
pos.side = side_copy; \
pos.enpassant = enpassant_copy; \
pos.castle = castle_copy;
// perft test
uint64_t perft(Position &pos, int depth, bool divide) {
if (depth == 0) return 1;
Movelist moves;
//int n_moves;
uint64_t nodes = 0;
//n_moves =
generate_pseudo_moves(pos, moves);
//generate_moves(moves);
// // define board state copies
// int board_copy[128], king_squares_copy[2], side_copy, enpassant_copy, castle_copy;
// // copy board state
// copy(pos.board, pos.board + 128, board_copy);
// copy(pos.king_squares, pos.king_squares + 2, king_squares_copy);
// side_copy = pos.side;
// enpassant_copy = pos.enpassant;
// castle_copy = pos.castle;
//copy_board(pos);
// copy board state
//Position copy = pos;
for (int i = 0; i < moves.count; i++) {
//print_board();
if (pos.make_move(moves.moves[i], depth)) {
// pos.print_board();
// getchar();
uint64_t result = perft(pos, depth - 1, false);
nodes += result;
if (divide) {
cout << Position::square_to_coord[decode_source(moves.moves[i])];
cout << Position::square_to_coord[decode_target(moves.moves[i])];
cout << " " << result << endl;
}
// restore board state
// copy(board_copy, board_copy + 128, pos.board);
// copy(king_squares_copy, king_squares_copy + 2, pos.king_squares);
// pos.side = side_copy;
// pos.enpassant = enpassant_copy;
// pos.castle = castle_copy;
//restore_board(pos);
// restore board state
//pos = copy;
pos.unmake_move(moves.moves[i], depth);
//pos.print_board();
//getchar();
}
else pos.unmake_move(moves.moves[i], depth); //pos = copy;
}
return nodes;
}
void run_perft(Position &pos, int depth) {
auto start = chrono::high_resolution_clock::now();
uint64_t result = perft(pos, depth, true);
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
cout << endl;
cout << "Nodes visited: " << result << endl;
cout << "Total time: " << duration.count() << " ms" << endl;
cout << fixed << "Nodes per second: " << int(1000.0 * result / duration.count()) << endl;
}
void run_perft_tests() {
Position pos;
uint64_t result;
uint64_t total_nodes = 0;
auto start = chrono::high_resolution_clock::now();
// test 1
cout << "Test 1: Start position" << endl;
pos.parse_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
result = perft(pos, 5, true);
assert(result == 4865609);
total_nodes += result;
// test 2
cout << endl << "Test 2: Kiwipete" << endl;
pos.parse_fen("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1");
result = perft(pos, 4, true);
assert(result == 4085603);
total_nodes += result;
// test 3
cout << endl << "Test 3: Position 3" << endl;
pos.parse_fen("8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - ");
result = perft(pos, 5, true);
assert(result == 674624);
total_nodes += result;
// test 4
cout << endl << "Test 4: Position 4" << endl;
pos.parse_fen("r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1");
result = perft(pos, 4, true);
assert(result == 422333);
total_nodes += result;
// test 5
cout << endl << "Test 5: Position 5" << endl;
pos.parse_fen("rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8");
result = perft(pos, 4, true);
assert(result == 2103487);
total_nodes += result;
// test 6
cout << endl << "Test 6: Position 6" << endl;
pos.parse_fen("r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10");
result = perft(pos, 4, true);
assert(result == 3894594);
total_nodes += result;
auto stop = chrono::high_resolution_clock::now();
auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
cout << endl << "Perft tests passed" << endl;
cout << "Nodes visited: " << total_nodes << endl;
cout << "Total time: " << duration.count() << " ms" << endl;
cout << fixed << "Nodes per second: " << int(1000.0 * total_nodes / duration.count()) << endl;
}