Chess Engine
C++ chess engine with movegen, bitboards, and Arduino-friendly docs
Loading...
Searching...
No Matches
perft.cpp
Go to the documentation of this file.
1#include <cstdio>
2#include "../defs.h"
3#include "../board/board.h"
5#include "../move/make_move.h"
6#include "../util/notation.h"
7#include "../util/misc.h" // for GetTimeMs()
8
9long leafNodes = 0;
10
11void Perft(int depth, S_BOARD* pos) {
13
14 if (depth == 0) {
15 ++leafNodes;
16 return;
17 }
18
19 S_MOVELIST list[1];
20 generateAllMoves(pos, list);
21
22 for (int i = 0; i < list->count; ++i) {
23 const int move = list->moves[i].move;
24 if (!makeMove(pos, move)) {
25 continue;
26 }
27 Perft(depth - 1, pos);
28 takeMove(pos);
29 }
30}
31
32void PerftTest(int depth, S_BOARD* pos) {
34
35 printBoardState(pos);
36 std::printf("\nStarting Test To Depth:%d\n", depth);
37
38 leafNodes = 0;
39 const int startMs = GetTimeMs();
40
41 S_MOVELIST list[1];
42 generateAllMoves(pos, list);
43
44 for (int i = 0; i < list->count; ++i) {
45 const int move = list->moves[i].move;
46 if (!makeMove(pos, move)) {
47 continue;
48 }
49 const long before = leafNodes;
50 Perft(depth - 1, pos);
51 takeMove(pos);
52 const long nodesForMove = leafNodes - before;
53 std::printf("move %d : %s : %ld\n", i + 1, moveToString(move), nodesForMove);
54 }
55
56 std::printf("\nTest Complete : %ld nodes visited in %dms\n",
57 leafNodes, GetTimeMs() - startMs);
58}
bool isBoardStateValid(const S_BOARD *pos)
Performs a full internal consistency check of the board state.
Definition board.cpp:28
void printBoardState(const S_BOARD *pos)
Print the board to stdout in a human-readable format.
Definition board.cpp:272
#define ASSERT(n)
Definition defs.h:14
int makeMove(S_BOARD *board, int move)
Make a move; returns non-zero if legal (king not left in check).
void takeMove(S_BOARD *board)
Undo the last made move.
int GetTimeMs()
Definition misc.cpp:17
void generateAllMoves(const S_BOARD *board, S_MOVELIST *moveList)
Generate all pseudo-legal moves for the side to move.
char * moveToString(const int move)
Convert an encoded move to coordinate notation.
Definition notation.cpp:17
void PerftTest(int depth, S_BOARD *pos)
Executes a perft test and prints move breakdown and timing.
Definition perft.cpp:32
void Perft(int depth, S_BOARD *pos)
Runs a perft search from a given board state.
Definition perft.cpp:11
long leafNodes
Global counter for leaf nodes visited during perft.
Definition perft.cpp:9
int move
Definition defs.h:62
int count
Definition defs.h:68
S_MOVE moves[MAXPOSITIONMOVES]
Definition defs.h:67