-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathCPerft.cc
105 lines (93 loc) · 2.69 KB
/
CPerft.cc
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
#include <iomanip>
#include <sstream>
#include <assert.h>
#include "CHashTable.h"
#include "CPerft.h"
#include "trace.h"
/***************************************************************
* clear
***************************************************************/
void CPerft::clear()
{
m_nodes = 0;
m_leafnodes = 0;
m_hashEntry.set(m_board);
} // end of clear
/***************************************************************
* search
***************************************************************/
void CPerft::search(int level)
{
TRACE(m_moveList << std::endl);
m_nodes++;
if (level == 0)
{
m_leafnodes++;
return;
}
CMoveList moves;
m_board.find_legal_moves(moves);
for (unsigned int i=0; i<moves.size(); ++i)
{
CMove move = moves[i];
#ifdef DEBUG_HASH
uint32_t oldHash = m_board.calcHash();
CHashEntry hashCopy(m_hashEntry);
#endif
m_hashEntry.update(m_board, move);
m_board.make_move(move);
m_moveList.push_back(move);
#ifdef DEBUG_HASH
{
CHashEntry m_newHash;
m_newHash.set(m_board);
if (m_newHash != m_hashEntry)
{
TRACE(m_moveList.ToLongString() << std::endl);
TRACE("New hash failure(2):" << std::hex << (m_newHash ^ m_hashEntry) << std::endl);
TRACE(m_board);
assert(false);
}
}
#endif
if (!m_board.isOtherKingInCheck())
{
search(level-1);
}
m_board.undo_move(move);
m_hashEntry.update(m_board, move);
m_moveList.pop_back();
#ifdef DEBUG_HASH
uint32_t newHash = m_board.calcHash();
if (oldHash != newHash)
{
TRACE("Hash failure" << std::endl);
TRACE(m_board);
assert(false);
}
if (hashCopy != m_hashEntry)
{
TRACE("New hash failure" << std::endl);
TRACE(m_board);
assert(false);
}
#endif
}
} // end of search
/***************************************************************
* ToString
***************************************************************/
std::string CPerft::ToString() const
{
std::stringstream ss;
ss << "nodes = " << std::setw(8) << m_nodes;
ss << " leafnodes = " << std::setw(8) << m_leafnodes;
return ss.str();
} // end of ToString
/***************************************************************
* operator <<
***************************************************************/
std::ostream& operator <<(std::ostream &os, const CPerft &rhs)
{
return os << rhs.ToString();
} // end of std::ostream& operator <<(std::ostream &os, const CPerft &rhs)