-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.cpp
222 lines (205 loc) · 5.18 KB
/
search.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "search.h"
#include <iomanip>
#include <sstream>
int nodeCounter;
int search(Board b, int maxDepth, int m, int n)
{
searchNode* head = new searchNode(b);
nodeCounter = 0;
std::cout << "\n** R.Reti Endgame Board **\n";
head->printBoardImage();
int topHeurisic = _search(head,maxDepth,0,UNSET);
printTree(head,m,n);
printBest(head);
return topHeurisic;
}
int _search(searchNode* thisNode, const int maxDepth, int depth, int parentHeuristic)
{
thisNode->setNodeCount(nodeCounter++);
if (thisNode->anyKingsMissing())
{
return simpleHeuristic(thisNode->getBoard());
}
int currentPlayer = (depth % 2);
if (depth == maxDepth)
{
return simpleHeuristic(thisNode->getBoard());
}
Board currentBoard = thisNode->getBoard();
std::vector<Board> possibles = generateMoves(currentBoard.getPieces(currentPlayer), currentBoard);
if (possibles.size() == 0)
{
return simpleHeuristic(thisNode->getBoard());
}
int bestHeuristic = UNSET;
for (int p = 0; p < possibles.size(); ++p)
{
//std::cout << "depth " << depth << " trying possibles[" << p << "] whose move is " << possibles[p].getMoveName() << "\n";
searchNode* newNode = new searchNode(possibles[p]);
thisNode->addChild(newNode);
//std::cout << "newNode should match possibles[p] move name: " << newNode->getMoveName() << "\n";
int childHeuristic = _search(newNode,maxDepth,depth+1,bestHeuristic);
newNode->setHeuristic(childHeuristic);
updateBestHeuristic(currentPlayer, bestHeuristic, childHeuristic);
if (parentHeuristic != UNSET)
{
if (
(currentPlayer == 1 && bestHeuristic < parentHeuristic) ||
(currentPlayer == 0 && bestHeuristic > parentHeuristic)
)
{
thisNode->setCutoff(true);
break;
}
}
}
thisNode->setHeuristic(bestHeuristic);
return bestHeuristic;
}
int simpleHeuristic(Board b)
{
int whiteHeuristic = 0, blackHeuristic = 0;
std::vector<Piece *> white = b.getPieces(0);
std::vector<Piece *> black = b.getPieces(1);
for (int p = 0; p < white.size(); ++p)
{
calculatePieceValue(whiteHeuristic,(int) white[p]->getType());
}
for (int p = 0; p < black.size(); ++p)
{
calculatePieceValue(blackHeuristic,(int) black[p]->getType());
}
return whiteHeuristic - blackHeuristic;
}
void calculatePieceValue(int& sum, int type)
{
int pieceValue = 1;
for (int n = 0; n < type; ++n)
pieceValue *= 2;
sum += pieceValue;
}
void updateBestHeuristic(int pl, int& bestH, int newH)
{
if (bestH == UNSET)
{
bestH = newH;
}
else if (pl == 0)
{
if (newH > bestH)
bestH = newH;
}
else if (pl == 1)
{
if (newH < bestH)
bestH = newH;
}
}
void _printTree(searchNode* node, int depth, int maxDepth)
{
if (depth <= maxDepth)
{
std::ostringstream output;
output << std::left;
output << std::setw(6) << node->getNodeCount();
if (depth == 0)
output << "start ";
else if ((depth % 2) == 0)
output << "black ";
else
output << "white ";
output << std::setw(7) << depth;
std::string heuristicOutput;
heuristicOutput += std::to_string(node->getHeuristic());
if (node->hasCutoff())
heuristicOutput += " -cutoff-";
output << std::setw(12) << heuristicOutput << "|";
std::string moveText;
for (int indent = 0; indent < depth; ++indent)
{
moveText += " ";
}
moveText += node->getMoveName();
output << std::setw(20) << moveText;
output << node->prettyPrintBoard() << "\n";
std::cout << output.str();
for (int i = 0; i < node->numChildren(); ++i)
{
_printTree(node->getChild(i), depth+1, maxDepth);
}
}
}
void printBest(searchNode* head)
{
std::cout << "best moves based on search results\n";
_printBest(head,0);
}
void _printBest(searchNode* node, int depth)
{
searchNode* best = new searchNode(Board());
for (int i = 0; i < node->numChildren(); ++i)
{
determineBestChild(best,node->getChild(i),depth);
}
if (best->getHeuristic() == UNSET)
{
std::cout << "\n";
}
else
{
if ((depth % 2) == 0)
{
std::cout << ((depth / 2) + 1) << ". " << best->getMoveName() << ", ";
}
else
{
std::cout << best->getMoveName() << "\n";
node->printBoardImage();
best->printBoardImage();
}
_printBest(best,depth+1);
}
}
void printTree(searchNode* head, int m, int n)
{
searchNode* cursor = head;
std::cout << "Total Moves In Search Tree: " << nodeCounter + 1 << "\n";
std::cout << "\nindex player depth heuristic move board\n";
std::cout << "----------------------------------------------------------\n";
if (m > 0)
cursor = _traverseBest(cursor,0,m);
_printTree(cursor,m,n);
}
searchNode* _traverseBest(searchNode* node, int depth, int n)
{
if (depth == n)
{
return node;
}
searchNode* best = new searchNode(Board());
for (int i = 0; i < node->numChildren(); ++i)
{
determineBestChild(best,node->getChild(i),depth);
}
if (!(best->getHeuristic() == UNSET))
{
return _traverseBest(best,depth+1,n);
}
return node;
}
void determineBestChild(searchNode*& bestNode, searchNode* thisChild, int depth)
{
int player = depth % 2;
if (bestNode->getHeuristic() == UNSET)
{
bestNode = thisChild;
}
else if (player == 0 && bestNode->getHeuristic() < thisChild->getHeuristic())
{
bestNode = thisChild;
}
else if (player == 1 && bestNode->getHeuristic() > thisChild->getHeuristic())
{
bestNode = thisChild;
}
}