-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathtic-tac-toe.cpp
204 lines (191 loc) · 5.25 KB
/
tic-tac-toe.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
#include<bits/stdc++.h>
using namespace std;
struct chaal{
int row,column;
};
char b[4][4];
char player,comp; //player is you and comp is your bot
int evaluate(int depth) //This function calculates the score of board at given time
{
int i;
//Check if there are three consecutive O's or X's in rows
for(i=1;i<=3;i++)
{
if(b[i][1]==b[i][2]&&b[i][2]==b[i][3])
{
if(b[i][1]==comp)
{return 20-depth;}
else if(b[i][1]==player)
{return -20+depth;}
}
}
//Check if there are three consecutive O's or X's in Columns
for(i=1;i<=3;i++)
{
if(b[1][i]==b[2][i]&&b[2][i]==b[3][i])
{
if(b[1][i]==comp)
{return 20-depth;}
else if(b[1][i]==player)
{return -20+depth;}
}
}
//Check if there are three consecutive O's or X's in Diagonals
if(b[1][1]==b[2][2]&&b[2][2]==b[3][3])
{
if(b[1][1]==comp)
{return 20-depth;}
else if(b[1][1]==player)
{return -20+depth;}
}
if(b[1][3]==b[2][2]&&b[2][2]==b[3][1])
{
if(b[1][3]==comp)
{return 20-depth;}
else if(b[1][3]==player)
{return -20+depth;}
}
return 0;
}
//Checks if there is any move left on the board
bool moveleft()
{
int i,j;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
if(b[i][j]=='_')
return true;
return false;
}
int minimax(int depth,bool ismaxturn)
{
int i,j,val;
int score=evaluate(depth);
if(score==20-depth)
return score;
if(score==-20+depth)
return score;
if(moveleft()==false)
return 0;
if(ismaxturn)
{
int best=-10000;
// Iterate over all 9 positions
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(b[i][j]=='_') //If the position is blank
{
b[i][j]=comp;
best=max(minimax(depth+1,!ismaxturn),best); //Updates the best value
b[i][j]='_';
}
if(alpha>beta) return best;
}
}
return best;
}
else
{
int best=10000;
// Iterate over all 9 positions
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(b[i][j]=='_') //If the position is blank
{
b[i][j]=player; //set step
best=min(minimax(depth+1,!ismaxturn),best); //Updates the best value
b[i][j]='_'; //Unset step...changes board back to its original form
}
}
}
return best;
}
}
//This function returns the best possible move to the main function
chaal findbestmove(int depth,bool ismaxturn)
{
int i,j,val;
struct chaal bestmove;
//Initialization steps
int bestval=-10000;
bestmove.row=-1;
bestmove.column=-1;
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if(b[i][j]=='_')
{
b[i][j]=comp;
val=minimax(depth,!ismaxturn);
// If we found a better move ,just update bestmove with that move
if(val>bestval)
{
bestval=val;
bestmove.row=i;
bestmove.column=j;
}
b[i][j]='_';
}
}
}
return bestmove;
}
void print()
{
printf("\n\n");
printf("\t\t\t %c | %c | %c \n", b[1][1],b[1][2], b[1][3]);
printf("\t\t\t--------------\n");
printf("\t\t\t %c | %c | %c \n", b[2][1],b[2][2], b[2][3]);
printf("\t\t\t--------------\n");
printf("\t\t\t %c | %c | %c \n\n", b[3][1],b[3][2], b[3][3]);
}
int main()
{
int p,i,j,r,c;
const int INF=10000000;
struct chaal cur_move;
for(i=1;i<=3;i++)
for(j=1;j<=3;j++)
b[i][j]='_';
printf("WELCOME TO THE GAME OF TIC-TAC-TOE\n");
printf("PRESS 1 FOR TAKING O AND 2 FOR TAKING X\n");
scanf("%d",&p);
if(p==1)
{player='O';comp='X';}
else
{player='X';comp='O';}
printf("I GIVE YOU THE CHANCE OF MAKING FIRST TURN \n");
int depth=1;
while(moveleft())
{
printf("ENTER THE ROW AND COLUMN OF YOUR INPUT\n");
cin>>r>>c;
b[r][c]=player;
int x=evaluate(depth);
depth++;
print();
printf("\n\n");
if(x==-20+depth) // You have won the game
{
printf("CONGRATULATIONS\n");
return 0;
}
cur_move=findbestmove(depth,true);
b[cur_move.row][cur_move.column]=comp;
print();
x=evaluate(depth);
printf("\n\n");
if(x==20-depth)
{
printf("SORRY TO SAY BUT YOU NEED A LOT OF PRACTICE\n");
return 0;
}
}
printf("IT IS A DRAW\n");
return 0;
}