Repository navigation
Expand file tree
/
Copy pathTree.cpp
More file actions
218 lines (162 loc) · 4.64 KB
/
Copy pathTree.cpp
File metadata and controls
218 lines (162 loc) · 4.64 KB
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
#include "Tree.h"
#ifndef __TREE_H__
#define __TREE_H__
Tree:: Tree(Board* r, int AIPlayerNumber){
AIPlayer = AIPlayerNumber;
root = new item;
root->name = "root";
root->board = r;
root->player = 0;
lastMove = root;
}
Tree:: ~Tree(){
destructor(root);
lastMove = NULL;
}
void Tree:: destructor(item* board){
if(board->children.size() == 0){
delete board->board;
board = NULL;
}
else{
for(int i=board->children.size()-1;i>=0;i--){
destructor(board->children[i]);
}
}
return;
}
void Tree:: generateChildren(item* parent){
if(parent->children.size()!=0)
return;
int** ark = parent->board->getBoard();
char c1,c2;
string s1,s2;
for(int i=1;i<9;i++){
for(int j=1;j<9;j++){
if(ark[i][j]!=0)
continue;
Board* tempBoard = new Board(parent->board,i,j,nextPlayer(parent));
if(tempBoard->isBoardValid()){
item* newChild = new item;
c1 = i+'0'; c2 = j+'a'-1; s1=c1; s2=c2;
newChild->name = s1+s2;
newChild->board = tempBoard;
newChild->player = nextPlayer(parent);
parent->children.push_back(newChild);
}
else delete tempBoard;
}
}
}
string Tree:: suggestMovesToPlayer(){
if(lastMove->children.size()==0)
generateChildren(lastMove);
int n = lastMove->children.size();
if(n==0){
return "No Moves Available";
}
string s[n];
for(int i=0;i<n;i++){
s[i] = lastMove->children[i]->name;
}
string setOfMoves;
for(int i=0;i<n-1;i++)
setOfMoves += s[i] + " ";
setOfMoves += s[n-1];
return setOfMoves;
}
bool Tree:: acceptNextMove(string nextMove){
if(nextMove=="No Moves Available"){
Board* board = new Board(lastMove, 0, 0, 5);
lastMove = board;
board-
}
else{
for(int i=0;i<lastMove->children.size();i++){
if(lastMove->children[i]->name==nextMove){
lastMove = lastMove->children[i];
return true;
}
}
return false;
}
}
string Tree::makeMoveAsComputer(int depth){
generateChildren(lastMove);
if(lastMove->children.size()==0){
acceptNextMove("No Moves Available");
return "No Moves Available";
}
if(lastMove->children.size()==1){
acceptNextMove(lastMove->children[0]->name);
return lastMove->children[0]->name;
}
string maxMove; int maxVal=INT_MIN; int now;
// best already explored option along path to the root for MAXIMIZER
int alpha = INT_MIN;
// best already explored option along path to the root for MINIMIZER
int beta = INT_MAX;
for(int i=0;i<lastMove->children.size();i++){
if(maxVal > alpha)
alpha = maxVal;
now = heuRec(lastMove->children[i],depth-1, "MIN",alpha, beta);
if(now > maxVal){
maxVal = now;
maxMove = lastMove->children[i]->name;
}
}
acceptNextMove(maxMove);
return maxMove;
}
int Tree:: heuRec(item* now, int depth, string Type, int alpha, int beta){
if(depth==0)
return now->board->defineHeuristics(now->player);
generateChildren(now);
if(Type=="MIN"){
if(now->children.size()==0){
return heuRec(now,depth-1,"MAX",alpha, beta);
}
int minVal=INT_MAX; int nowVal;
for(int i=0;i<now->children.size();i++){
if(minVal < beta)
beta = minVal;
nowVal = heuRec(now->children[i],depth-1, "MAX", alpha,beta);
if(nowVal < minVal){
minVal = nowVal;
}
if(nowVal < alpha)
break; /** PRUNING **/
}
return minVal;
}
else{
if(now->children.size()==0){
return heuRec(now,depth-1,"MIN", alpha, beta);
}
int maxVal=INT_MIN; int nowVal;
for(int i=0;i<now->children.size();i++){
if(maxVal > alpha)
alpha = maxVal;
nowVal = heuRec(now->children[i],depth-1, "MIN", alpha, beta);
if(nowVal > maxVal){
maxVal = nowVal;
}
if(nowVal > beta)
break; /** PRUNING **/
}
return maxVal;
}
}
Board* Tree:: getCurrentBoard(){
return lastMove->board;
}
void Tree::printLastBoardPosition(){
lastMove->board->printBoard();
}
int Tree:: getCurrentPlayer(){
return nextPlayer(lastMove);
}
int Tree:: nextPlayer(item* board){
return (board->player % 2) + 1;
}
#endif //__TREE_H__