-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
407 lines (373 loc) · 10.2 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <GL/freeglut.h>
#include <math.h>
#define maxx 4
#define maxy 4
#define dx 100
#define dy 100
using namespace std;
struct Move
{
int row, col;
};
void display();
bool isMovesLeft(char board[3][3]);
int evaluate(char b[3][3]);
Move findBestMove(char board[3][3]);
GLfloat x[maxx]={0.0};
GLfloat x0=100,y0=100;
GLint i,j;
char player = 'x', opponent = 'o';
char WinState='X';
int WinStateCol=999,WinStateRow=999;
char board[3][3] =
{
{ '_', '_', '_' },
{ '_', '_', '_' },
{ '_', '_', '_' }
};
void ShowStatus(){
glLineWidth(30);
glColor3ub(0x54,0x54,0x54);
if(WinState == '\\' )
{
glBegin(GL_LINES);
glVertex2f(x[3],x[0]);
glVertex2f(x[0],x[3]);
glEnd();
cout<<WinState<<endl;
}
else if(WinState=='/'){
glBegin(GL_LINES);
glVertex2f(x[0],x[0]);
glVertex2f(x[3],x[3]);
glEnd();
cout<<WinState<<endl;
}
else if(WinState=='-'){
glBegin(GL_LINES);
glVertex2f(x[0],x[WinStateRow]+50);
glVertex2f(x[3],x[WinStateRow]+50);
glEnd();
cout<<WinState<<endl;
}
else if(WinState=='|'){
glBegin(GL_LINES);
glVertex2f(x[WinStateCol]+50,x[0]);
glVertex2f(x[WinStateCol]+50,x[3]);
glEnd();
cout<<WinState<<endl;
}
}
void gamePlay()
{
if(isMovesLeft(board) && evaluate(board)==0){
Move bestMove = findBestMove(board);
cout<<"The Optimal Move is :n \n";
cout<<"ROW: "<<bestMove.row<<endl<<"COL: "<<bestMove.col<<endl;
board[bestMove.row][bestMove.col]=player;
if(evaluate(board)>0){
cout<<"player wins!\n";
}
}
else{
if(evaluate(board)<0){
cout<<"opponent wins!\n";
}
else if(evaluate(board)>0){
cout<<"player wins!\n";
}
else if(evaluate(board)==0){
cout<<"draw!\n";
}
}
}
void myinit()
{
glLineWidth(30);
glClearColor(0.078125,0.73828125,0.671875,1.0);
glColor3f(1.0,0.0,0.0);
glPointSize(5.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0,499.0,0.0,499.0);
glutPostRedisplay();
}
void reshape(int w,int h)
{
glutReshapeWindow(500,500);
}
bool mouseBtnTest(int btn,int state){
if(btn==GLUT_LEFT_BUTTON && state==GLUT_DOWN && evaluate(board)==0)
return true;
return false;
}
void mouse(int btn,int state,int ox,int oy)
{
int xc=ox,yc=500-oy;
if(mouseBtnTest(btn,state) && xc > x[0] && xc < x[1] && yc > x[0] && yc < x[1] && board[0][0]=='_' ){
board[0][0]=opponent;gamePlay();
}
else if(mouseBtnTest(btn,state) && xc > x[1] && xc < x[2] && yc > x[0] && yc < x[1] && board[0][1]=='_' ){
board[0][1]=opponent;gamePlay();
}
else if(mouseBtnTest(btn,state) && xc > x[2] && xc < x[3] && yc > x[0] && yc < x[1] && board[0][2]=='_' ){
board[0][2]=opponent;gamePlay();
}
else if(mouseBtnTest(btn,state) && xc > x[0] && xc < x[1] && yc > x[1] && yc < x[2] && board[1][0]=='_' ){
board[1][0]=opponent;gamePlay();
}
else if(mouseBtnTest(btn,state) && xc > x[1] && xc < x[2] && yc > x[1] && yc < x[2] && board[1][1]=='_' ){
board[1][1]=opponent;gamePlay();
}
else if(mouseBtnTest(btn,state) && xc > x[2] && xc < x[3] && yc > x[1] && yc < x[2] && board[1][2]=='_' ){
board[1][2]=opponent;gamePlay();
}
else if(mouseBtnTest(btn,state) && xc > x[0] && xc < x[1] && yc > x[2] && yc < x[3] && board[2][0]=='_' ){
board[2][0]=opponent;gamePlay();
}
else if(mouseBtnTest(btn,state) && xc > x[1] && xc < x[2] && yc > x[2] && yc < x[3] && board[2][1]=='_' ){
board[2][1]=opponent;gamePlay();
}
else if(mouseBtnTest(btn,state) && xc > x[2] && xc < x[3] && yc > x[2] && yc < x[3] && board[2][2]=='_' ){
board[2][2]=opponent;gamePlay();
}
glutPostRedisplay();
}
void keyboard(unsigned char key,int x, int y)
{
if (key == 27 || key=='q'|| key=='Q') // escape key
exit(0);
else if(key=='R' || key=='r'){
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
board[i][j]='_';
glutPostRedisplay();
}
}
void drawX(int i,int j){
glLineWidth(7);
glColor3ub(0x54,0x54,0x54);
glBegin(GL_LINES);
glVertex2f(x[j]+25,x[i]+25);
glVertex2f(x[j+1]-25,x[i+1]-25);
glVertex2f(x[j+1]-25,x[i]+25);
glVertex2f(x[j]+25,x[i+1]-25);
glEnd();
}
void drawO(int i,int j){
float xc=x[j]+dx/2,yc=x[i]+dx/2,r=dx/4,theta=0;
glLineWidth(3);
glColor3ub(0xf2,0xeb,0xd3);
glBegin(GL_LINE_LOOP);
for(int k=0;k<360;k++,theta+=(1*(3.14/180))){
glVertex2f(xc+r*cos(theta),yc+r*sin(theta));
}
glEnd();
}
void display()
{
glClear(GL_COLOR_BUFFER_BIT);
glLineWidth(30);
glColor3f(0.0,0.0,1.0);
for(i=0;i<maxx;i++)
x[i]=x0+i*dx;
glColor3ub(0x0d,0xa1,0x92);
glBegin(GL_LINES);
glVertex2f(x[0],x[2]);
glVertex2f(x[3],x[2]);
glVertex2f(x[0],x[1]);
glVertex2f(x[3],x[1]);
glVertex2f(x[1],x[0]);
glVertex2f(x[1],x[3]);
glVertex2f(x[2],x[0]);
glVertex2f(x[2],x[3]);
glEnd();
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(board[i][j]==opponent){
drawO(i,j);
}
else if(board[i][j]==player){
drawX(i,j);
}
}
}
if(evaluate(board)>0){
ShowStatus();
}
glFlush();
}
bool isMovesLeft(char board[3][3])
{
for (int i = 0; i<3; i++)
for (int j = 0; j<3; j++)
if (board[i][j]=='_')
return true;
return false;
}
int evaluate(char b[3][3])
{
// Checking Rows for X or O victory
for (int row = 0; row<3; row++)
{
if (b[row][0]==b[row][1] &&
b[row][1]==b[row][2])
{
if (b[row][0]==player){
WinState='-';
WinStateRow=row;
return +10;
}
else if (b[row][0]==opponent)
return -10;
}
}
// Checking Columns for X or O victory
for (int col = 0; col<3; col++)
{
if (b[0][col]==b[1][col] &&
b[1][col]==b[2][col])
{
if (b[0][col]==player){
WinState='|';
WinStateCol=col;
return +10;
}
else if (b[0][col]==opponent)
return -10;
}
}
// Checking Diagonals for X or O victory
if (b[0][0]==b[1][1] && b[1][1]==b[2][2])
{
if (b[0][0]==player){
WinState='/';
return +10;
}
else if (b[0][0]==opponent)
return -10;
}
if (b[0][2]==b[1][1] && b[1][1]==b[2][0])
{
if (b[0][2]==player){
WinState='\\';
return +10;
}
else if (b[0][2]==opponent)
return -10;
}
// Else if none of them have won then return 0
return 0;
}
// This is the minimax function.
//It considers all the possible ways the game can go and returns value of the board
int minimax(char board[3][3], int depth, bool isMax)
{
int score = evaluate(board);
// If Maximizer won the game return his/her evaluated score
if (score == 10)
return score-depth;
// If Minimizer has won game return his/her evaluated score
if (score == -10)
return score+depth;
// If there are no more moves and no winner then it is a tie
if (isMovesLeft(board)==false)
return 0;
// If maximizer's move
if (isMax)
{
int best = -1000;
// Traverse all cells
for (int i = 0; i<3; i++)
{
for (int j = 0; j<3; j++)
{
// Check if cell is empty
if (board[i][j]=='_')
{
// Make the move
board[i][j] = player;
// Call minimax recursively and choose the maximum value
best = max( best,minimax(board, depth+1, !isMax) );
// Undo the move
board[i][j] = '_';
}
}
}
return best;
}
// If minimizer's move
else
{
int best = 1000;
// Traverse all cells
for (int i = 0; i<3; i++)
{
for (int j = 0; j<3; j++)
{
// Check if cell is empty
if (board[i][j]=='_')
{
// Make the move
board[i][j] = opponent;
// Call minimax recursively and choose the minimum value
best = min(best,minimax(board, depth+1, !isMax));
// Undo the move
board[i][j] = '_';
}
}
}
return best;
}
}
// Returns the best possible move for the player
Move findBestMove(char board[3][3])
{
int bestVal = -1000;
Move bestMove;
bestMove.row = -1;
bestMove.col = -1;
// Traverse all cells, evalutae minimax function for all empty cells. And return the cell with optimal value
for (int i = 0; i<3; i++)
{
for (int j = 0; j<3; j++)
{
// Check if celll is empty
if (board[i][j]=='_')
{
// Make the move
board[i][j] = player;
// compute evaluation function for this move
int moveVal = minimax(board, 0, false);
// Undo the move
board[i][j] = '_';
//update the best
if (moveVal > bestVal)
{
bestMove.row = i;
bestMove.col = j;
bestVal = moveVal;
}
}
}
}
cout<<"The value of the best Move is :"<<bestVal<<endl;
return bestMove;
}
int main(int argc, char **argv)
{
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
glutInitWindowSize(500,500);
glutInitWindowPosition(100,100);
glutCreateWindow("TicTacToe");
myinit();
glutMouseFunc(mouse);
glutKeyboardFunc(keyboard);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMainLoop();
}