-
Notifications
You must be signed in to change notification settings - Fork 0
/
perft.c
61 lines (45 loc) · 1.43 KB
/
perft.c
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
#include "defs.h"
#include "stdio.h"
long leafNodes;
void Perft(int depth, S_BOARD *pos) { // Recursive function that loops to all possible leafnodes at given depth
ASSERT(CheckBoard(pos));
if (depth == 0) { // Count all leafnodes
leafNodes++;
return;
}
S_MOVELIST list[1];
GenerateAllMoves(pos, list);
int MoveNum;
for (MoveNum = 0; MoveNum < list->count; ++MoveNum) {
if (!MakeMove(pos, list->moves[MoveNum].move)) {
continue;
}
Perft(depth - 1, pos); // Call recursive function
TakeMove(pos);
}
return;
}
void PerftTest(int depth, S_BOARD *pos) {
ASSERT(CheckBoard(pos));
PrintBoard(pos);
printf("\nStarting Test to Depth %d\n", depth);
leafNodes = 0;
int start = GetTimeMs();
S_MOVELIST list[1];
GenerateAllMoves(pos, list);
int move;
int MoveNum = 0;
for (MoveNum = 0; MoveNum < list->count; ++MoveNum) {
move = list->moves[MoveNum].move;
if (!MakeMove(pos, move)) {
continue;
}
long cumNodes = leafNodes;
Perft(depth - 1 , pos);
TakeMove(pos);
long oldNodes = leafNodes - cumNodes;
printf("Move %d : %s : %ld\n", MoveNum+1, PrMove(move), oldNodes);
}
printf("\nTest Complete: %ld nodes visited in %dms.\n", leafNodes, GetTimeMs() - start);
return;
}