-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
245 lines (182 loc) · 6.8 KB
/
main.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include <iostream>
#include <ctime>
#include "MinmaxAB.h"
#include "TreeNodes.h"
#include "ConnectFourBoard.h"
#include "AlphaBeta.h"
using namespace std;
/*variable that can be accessed by files
TreeNodes.h and .cpp, and main function
*/
int num_nodes_generated, num_nodes_expanded,game_path_length,start_time,stop_time;
/*made as a global variable since it must be accessible to
Minmax() fn and also AlphaBeta() fn
*/
char evalfn_choiceX,evalfn_choiceO;
/* for calling minmaxAB function
creates a tree object,board object and minmaxab object
using a while loop keeps calling the minmaxAB function
repeatedly until the condition checkPlayerWon evaluates
to true. */
void MinMax(){
int moves_made = 0;
tic *board_ob = new tic;
cout<<endl;
cout<<"initial board state before starting game with(Initial Node config/Root Node) "<<moves_made<<" moves made"<<endl;
cout<<"---------------------------------------------------------"<<endl;
board_ob->display_board(board_ob);
board_ob->player = 'X';
start_time = clock();
while(!board_ob->checkPlayerWon()){
tree *head = new tree;
*(head->ob) = *board_ob;
if(head->ob->isBoardEmpty()){
int random_col_index = head->ob->makeFirstMove();
head->ob->board[0][random_col_index] = 'X';
head->ob->player = 'X';
moves_made++;
game_path_length++;
cout<<"After 1st move board state(Max's move/Root Node):"<<endl;
head->ob->display_board(head->ob);
}
minmaxA min_ob;
//assigns the users choice of evaluation function chosen
min_ob.eval_choice = evalfn_choiceX;
int x = min_ob.minmaxAB(head,0,1000,-1000,head->ob->player);
moves_made++;
game_path_length++;
//to get the optimal node - Move Gen function
head->move_gen(board_ob,moves_made);
}
stop_time = clock();
}
/* for calling minmaxAB function
creates a tree object,board object and alphabeta object
using a while loop keeps calling the alphabeta function
repeatedly until the condition checkPlayerWon evaluates
to true. */
void AlphaBeta(){
int moves_made = 0;
tic *board_ob = new tic;
cout<<endl;
cout<<"initial board state before starting game with(Initial Node config/Root Node) "<<moves_made<<" moves made"<<endl;
cout<<"---------------------------------------------------------"<<endl;
board_ob->display_board(board_ob);
board_ob->player = 'X';
start_time = clock();
while(!board_ob->checkPlayerWon()){
tree *head = new tree;
*(head->ob) = *board_ob;
if(head->ob->isBoardEmpty()){
int random_col_index = head->ob->makeFirstMove();
head->ob->board[0][random_col_index] = 'X';
head->ob->player = 'X';
moves_made++;
game_path_length++;
cout<<"After 1st move board state(Max's move/Root Node):"<<endl;
head->ob->display_board(head->ob);
}
alphabeta ab_ob;
//assigns the users choice of evaluation function chosen
ab_ob.eval_choice = evalfn_choiceO;
int x = ab_ob.alpha_beta(head,0,head->ob->player,1000,-1000);
moves_made++;
game_path_length++;
//to get the optimal node - Move Gen function
head->move_gen(board_ob,moves_made);
}
stop_time = clock();
}
int main()
{
int algo_choiceX,algo_choiceO;
cout<<"Select which Algorithm X has to play with ?"<<endl;
cout<<endl;
cout<<" 1 - MinmaxAB"<<endl;
cout<<" 2 - AlphaBeta"<<endl;
cout<<endl;
cin>>algo_choiceX;
cout<<endl;
cout<<"Select which Algorithm O has to play with ?"<<endl;
cout<<endl;
cout<<" 1 - MinmaxAB"<<endl;
cout<<" 2 - AlphaBeta"<<endl;
cout<<endl;
cin>>algo_choiceO;
cout<<endl;
cout<<"Choose the val function for player X "<<endl;
cout<<" M/m - Madhusudhan's eval function"<<endl;
cout<<" H/h - Harsha's eval function"<<endl;
cout<<" S/s - Santosh's eval function"<<endl;
cout<<endl;
cin>>evalfn_choiceX;
cout<<endl;
cout<<"Choose the val function for player O "<<endl;
cout<<" M/m - Madhusudhan's eval function"<<endl;
cout<<" H/h - Harsha's eval function"<<endl;
cout<<" S/s - Santosh's eval function"<<endl;
cout<<endl;
cin>>evalfn_choiceO;
cout<<endl;
int moves_made = 0;
tic *board_ob = new tic;
cout<<endl;
cout<<"initial board state before starting game with(Initial Node config/Root Node) "<<moves_made<<" moves made"<<endl;
cout<<"---------------------------------------------------------"<<endl;
board_ob->display_board(board_ob);
board_ob->player = 'X';
start_time = clock();
while(!board_ob->checkPlayerWon()){
tree *head = new tree;
*(head->ob) = *board_ob;
if(head->ob->isBoardEmpty()){
int random_col_index = head->ob->makeFirstMove();
head->ob->board[0][random_col_index] = 'X';
head->ob->player = 'X';
moves_made++;
game_path_length++;
cout<<"After 1st move board state(Max's move/Root Node):"<<endl;
head->ob->display_board(head->ob);
}
minmaxA min_ob;
alphabeta ab_ob;
//assigns the users choice of evaluation function chosen
if(head->ob->player == 'X' && algo_choiceX == 1){
min_ob.eval_choice = evalfn_choiceX;
int x = min_ob.minmaxAB(head,0,1000,-1000,head->ob->player);
moves_made++;
game_path_length++;
//to get the optimal node - Move Gen function
//head->move_gen(board_ob,moves_made);
}
if(head->ob->player == 'O' && algo_choiceX == 1){
min_ob.eval_choice = evalfn_choiceX;
int x = min_ob.minmaxAB(head,0,1000,-1000,head->ob->player);
moves_made++;
game_path_length++;
//to get the optimal node - Move Gen function
//head->move_gen(board_ob,moves_made);
}
if(head->ob->player == 'X' && algo_choiceX == 2){
ab_ob.eval_choice = evalfn_choiceX;
//assigns the users choice of evaluation function chosen
int x = ab_ob.alpha_beta(head,0,head->ob->player,1000,-1000);
moves_made++;
game_path_length++;
//to get the optimal node - Move Gen function
//head->move_gen(board_ob,moves_made);
}
if(head->ob->player == 'O' && algo_choiceX == 2){
ab_ob.eval_choice = evalfn_choiceX;
//assigns the users choice of evaluation function chosen
int x = ab_ob.alpha_beta(head,0,head->ob->player,1000,-1000);
moves_made++;
game_path_length++;
//to get the optimal node - Move Gen function
//head->move_gen(board_ob,moves_made);
}
head->move_gen(board_ob,moves_made);
}
stop_time = clock();
return 0;
}