This repository has been archived by the owner on Oct 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
Copy pathTicTacToe_vs_cpu.cpp
235 lines (209 loc) · 3.74 KB
/
TicTacToe_vs_cpu.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
#include <bits/stdc++.h>
using namespace std;
char pl,cl;
char board[3][3];
struct Move
{
int row,col;
};
//function to check if player won or not
bool isWin(char pl,char board[3][3])
{
//check for rows
for(int i=0;i<3;i++)
{
if(board[i][0]==pl && board[i][1]==pl && board[i][2]==pl)
return true;
}
//check for columns
for(int i=0;i<3;i++)
{
if(board[0][i]==pl && board[1][i]==pl && board[2][i]==pl)
return true;
}
//check for diagonals
if(board[0][0]==pl && board[1][1]==pl && board[2][2]==pl)
return true;
if(board[0][2]==pl && board[1][1]==pl && board[2][0]==pl)
return true;
return false;
}
//function to check if board is over or not
bool isOver(char board[3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==' ')
return false;
}
}
return true;
}
//function to calculate score
int score(char p,int depth,char board[3][3])
{
if(p==pl && isWin(p,board)==true)
{
return 10-depth;
}
else if(p==cl && isWin(p,board)==true)
return depth-10;
else
return 0;
}
//function to apply minimax algorithm
int minimax(char board[3][3],int depth,char p) {
int sc=score(p,depth,board);
if(sc!=0)
return sc;
if(isOver(board)==true && sc==0)
return 0;
if(p==pl) {
int best=-100;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j]==' ')
{
board[i][j]=p;
best=max(best,minimax(board,depth+1,cl));
board[i][j]=' ';
}
}
}
return best;
}
else if(p==cl)
{
int best=100;
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
if(board[i][j]==' ')
{
board[i][j]=p;
best=min(best,minimax(board,depth+1,pl));
board[i][j]=' ';
}
}
}
return best;
}
}
//function to calculate best move
Move FindBestMove(char board[3][3],char p)
{
Move best;
int val=-1000;
best.row=-1;
best.col=-1;
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
if(board[i][j]==' ')
{
board[i][j]=p; //make the move
int value=minimax(board,0,cl); //start with the minimizing player
board[i][j]=' '; //undo move
//find the best move by calculating one giving highest positive return value
if(value>val)
{
best.row=i;
best.col=j;
val=value;
}
}
}
}
return best;
}
//function to print board
void Draw()
{
for(int i=0;i<3;i++) {
for(int j=0;j<3;j++) {
cout<<"|"<<board[i][j]<<"|";
}
cout<<endl;
cout<<"---------";
cout<<endl;
}
}
//function to print opponent's moves
void Opponent(char board[3][3],char p)
{
int x,y;
cout<<"Enter x coordinate of the row u want to insert\n";
cin>>x;
cout<<"Enter y coordinate of the column u want to insert\n";
cin>>y;
if(board[x][y]==' ')
board[x][y]=p;
else {
cout<<"Wrong Move! Exiting game !!!";
exit(0);
}
Draw();
}
//The main function
int main()
{
char p;
cout<<"Enter Your Choice: X or O"<<endl;
cin>>cl;
pl=(cl=='X')?'O':'X'; //pl is the computer player,cl is user
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
board[i][j]=' ';
}
}
Draw();
//Loop untill board is over
while(!isOver(board))
{
if(pl=='X')
{
Move best=FindBestMove(board,pl);
board[best.row][best.col]=pl;
cout<<"\n\n\n";
Draw();
if(isWin(pl,board))
break;
cout<<"\n\n\n";
if(!isOver(board))
Opponent(board,cl);
cout<<"\n\n\n";
if(isWin(cl,board))
break;
}
if(pl=='O')
{
cout<<"\n\n\n";
Opponent(board,cl);
if(isWin(cl,board))
break;
cout<<"\n\n\n";
if(!isOver(board)) {
Move best=FindBestMove(board,pl);
board[best.row][best.col]=pl;
Draw();
}
if(isWin(pl,board))
break;
cout<<"\n\n\n";
}
}
if(isWin(pl,board)==true)
cout<<"Computer Wins\n";
else if(isWin(cl,board)==true)
cout<<"You Win\n";
else
cout<<"Draw\n";
cout<<"GAME OVER\n";
}