forked from stevemaughan/maverick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathperft.cpp
106 lines (85 loc) · 2.77 KB
/
perft.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
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
//===========================================================//
//
// Maverick Chess Engine
// Copyright 2013 Steve Maughan
//
//===========================================================//
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include "defs.h"
#include "data.h"
#include "procs.h"
#include "bittwiddle.h"
t_nodes perft(struct t_board *board, int depth) {
struct t_move_list move_list[1];
struct t_undo undo[1];
t_nodes total_nodes = 0;
t_nodes move_nodes = 0;
unsigned long start = time_now();
int i;
if (board->in_check)
generate_evade_check(board, move_list);
else
generate_moves(board, move_list);
for (i = move_list->count - 1; i >= 0; i--) {
if (make_move(board, move_list->pinned_pieces, move_list->move[i], undo)) {
move_nodes = 0;
if (depth > 1)
move_nodes += do_perft(board, depth - 1);
printf(move_as_str(move_list->move[i]));
printf(" = %u\n", move_nodes);
unmake_move(board, undo);
total_nodes += move_nodes;
}
}
unsigned long finish = time_now();
if (finish == start)
printf("Total Nodes: %I64d\n", total_nodes);
else
printf("Total Nodes: %I64d in %d milliseconds = nps %I64d\n", total_nodes, finish - start, 1000 * total_nodes / (finish - start));
return total_nodes;
}
t_nodes do_perft(struct t_board *board, int depth)
{
struct t_move_list move_list[1];
struct t_move_list bad_move_list[1];
bad_move_list->count = 0;
bad_move_list->imove = 0;
struct t_undo undo[1];
t_nodes nodes = 0;
int i;
assert(integrity(board));
if (board->in_check) {
generate_evade_check(board, move_list);
if (depth == 1) return move_list->count;
}
else {
//generate_captures(board, move_list);
//generate_quiet_moves(board, move_list);
generate_moves(board, move_list);
//if (!equal_move_lists(move_list, xmove_list)){
// write_board(board, "board.txt");
// write_move_list(xmove_list, "all.txt");
// write_move_list(move_list, "inc.txt");
//}
if (depth == 1) return legal_move_count(board, move_list);
}
for (i = move_list->count - 1; i >= 0; i--) {
assert(lookup_move(board, move_as_str(move_list->move[i])) == move_list->move[i]);
if (make_next_move(board, move_list, bad_move_list, undo)) {
assert(integrity(board));
//nodes++;
if (depth > 1)
nodes += do_perft(board, depth - 1);
else
nodes++;
unmake_move(board, undo);
assert(integrity(board));
}
else
assert(integrity(board));
}
return nodes;
}