-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNode.cpp
101 lines (100 loc) · 2.23 KB
/
Node.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
#include "Node.h"
Node::Node(Chessboard& boardIn, std::string& move) {
eval = 0;
currentBoard = new Chessboard(boardIn);
currentMove = move;
mate = 0;
return;
}
void Node::DeleteTree() {
for (unsigned int i=0; i<childNodes.size(); i++) {
childNodes[i]->DeleteTree();
}
delete currentBoard;
}
void Node::SetEval(float evalIn) {
eval = evalIn;
evalSet = true;
}
void Node::CreateChildren() {
Chessboard newBoard(*currentBoard);
newBoard.FlipMove();
std::vector<std::string> allMoves = newBoard.CheckValidMoves(currentMove);
allMoves = PinsAndChecks(allMoves, newBoard);
if (allMoves.size() == 0) {
if (CheckmateCheck(newBoard, currentMove)) {
newBoard.FlipMove();
if (newBoard.GetMove())
mate = 1;
else
mate = 2;
return;
}
mate = 3;
return;
}
for (unsigned int i=0; i<allMoves.size(); i++) {
Chessboard childBoard(newBoard);
childBoard.ProcessMove(allMoves[i]);
Node* newNode = new Node(childBoard, allMoves[i]);
childNodes.push_back(newNode);
}
return;
}
void Node::IncreaseDepth() {
for (unsigned int i=0; i<childNodes.size(); i++)
childNodes[i]->IncreaseDepth();
if (childNodes.size() == 0) {
CreateChildren();
return;
}
return;
}
int Node::TotalNodeCount() {
/* currentBoard->DisplayBoard(); */
int counter = 1;
for (unsigned int i=0; i<childNodes.size(); i++) {
counter += childNodes[i]->TotalNodeCount();
}
return counter;
}
void Node::CalculateEval() {
if (mate != 0) {
if (mate == 1) {
eval = -999999;
} else if (mate == 2) {
eval = 999999;
} else {
eval = 0;
}
evalSet = true;
return;
}
// Go down to all children
for (unsigned int i=0; i<childNodes.size(); i++) {
childNodes[i]->CalculateEval();
}
// If its a leaf node, calculate its eval
if (childNodes.size() == 0) {
eval = EvaluatePosition(currentBoard);
return;
}
// Calculate non leaf node eval
if (currentBoard->GetMove()) {
float min = 999999;
for (unsigned int i=0; i<childNodes.size(); i++) {
if (childNodes[i]->GetEval() < min)
min = childNodes[i]->GetEval();
}
eval = min;
} else {
float max = -999999;
for (unsigned int i=0; i<childNodes.size(); i++) {
if (childNodes[i]->GetEval() > max)
max = childNodes[i]->GetEval();
}
eval = max;
}
evalSet = true;
return;
}