-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess_engine.cpp
78 lines (61 loc) · 2.14 KB
/
chess_engine.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
#include <iostream>
#include <chrono>
#include <ostream>
#include "tables.h"
#include "position.h"
#include "types.h"
//Computes the perft of the position for a given depth, using bulk-counting
//According to the https://www.chessprogramming.org/Perft site:
//Perft is a debugging function to walk the move generation tree of strictly legal moves to count
//all the leaf nodes of a certain depth, which can be compared to predetermined values and used to isolate bugs
template<Color Us>
unsigned long long perft(Position& p, unsigned int depth) {
int nmoves;
unsigned long long nodes = 0;
MoveList<Us> list(p);
if (depth == 1) return (unsigned long long) list.size();
for (Move move : list) {
p.play<Us>(move);
nodes += perft<~Us>(p, depth - 1);
p.undo<Us>(move);
}
return nodes;
}
//A variant of perft, listing all moves and for each move, the perft of the decremented depth
//It is used solely for debugging
template<Color Us>
void perftdiv(Position& p, unsigned int depth) {
unsigned long long nodes = 0, pf;
MoveList<Us> list(p);
for (Move move : list) {
std::cout << move;
p.play<Us>(move);
pf = perft<~Us>(p, depth - 1);
std::cout << ": " << pf << " moves\n";
nodes += pf;
p.undo<Us>(move);
}
std::cout << "\nTotal: " << nodes << " moves\n";
}
void test_perft() {
Position p;
Position::set("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", p);
std::cout << p;
std::chrono::steady_clock::time_point begin = std::chrono::steady_clock::now();
auto n = perft<WHITE>(p, );
std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
auto diff = end - begin;
std::cout << "Nodes: " << n << "\n";
std::cout << "NPS: "
<< int(n * 1000000.0 / std::chrono::duration_cast<std::chrono::microseconds>(diff).count())
<< "\n";
std::cout << "Time difference = "
<< std::chrono::duration_cast<std::chrono::microseconds>(diff).count() << " [microseconds]\n";
}
int main() {
//Make sure to initialise all databases before using the library!
initialise_all_databases();
zobrist::initialise_zobrist_keys();
test_perft();
return 0;
}