-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperformance.c
111 lines (90 loc) · 2.31 KB
/
performance.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
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
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "main.h"
static void perft(Board *b, int depth, PerftResult *result)
{
if (depth == 0) return;
Move move_list[MAX_MOVES];
int move_count;
Generate(b, b->turn, GEN_ALL, move_list, &move_count);
for (int i = 0; i < move_count; i++)
{
MakeMove(b, move_list[i]);
if (!IsKingAttacked(b, !b->turn))
{
if (depth == 1)
{
result->total++;
switch (GET_TYPE(move_list[i]))
{
case MOVE_TYPE_EP:
result->eps++;
result->captures++;
break;
case MOVE_TYPE_CASTLE_K:
result->castles++;
break;
case MOVE_TYPE_CASTLE_Q:
result->castles++;
break;
case MOVE_TYPE_CAPTURE:
result->captures++;
break;
case MOVE_TYPE_PROMOTION:
result->promotions++;
break;
case MOVE_TYPE_PROMOTION_WITH_CAPTURE:
result->promotions++;
result->captures++;
break;
}
}
perft(b, depth - 1, result);
}
UnmakeMove(b);
}
}
void RunPerft(Board *b, int depth)
{
PerftResult result;
memset(&result, 0, sizeof(PerftResult));
clock_t start = clock();
perft(b, depth, &result);
printf(
"Perft(%d):\ntotal:%d\neps:%d\ncaptures:%d\ncastles:%d\npromotions:%d\n\n\n",
depth,
result.total,
result.eps,
result.captures,
result.castles,
result.promotions
);
clock_t end = clock();
double time = (double)(end - start) / CLOCKS_PER_SEC;
long nps = result.total / time;
printf("Nodes per second: %ld\n\n\n", nps);
}
void RunPerftDiv(Board *b, int depth)
{
Move move_list[MAX_MOVES];
int move_count;
Generate(b, b->turn, GEN_ALL, move_list, &move_count);
int total = 0;
for (int i = 0; i < move_count; i++)
{
MakeMove(b, move_list[i]);
if (!SquareAttackedBy(b, b->turn, ffsll(b->piece[!b->turn][KING]) - 1))
{
PerftResult result;
memset(&result, 0, sizeof(PerftResult));
perft(b, depth - 1, &result);
total += result.total;
char buff[6];
PrintMoveStr(buff, move_list[i]);
printf("%d - %s: %d\n", i, buff, result.total);
}
UnmakeMove(b);
}
printf("\nNodes searched: %d\n", total);
}