-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.cpp
227 lines (215 loc) · 5.03 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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#include<bits/stdc++.h>
using namespace std;
class game
{
public:
char pc, ph;
char gboard[3][3];
game(char);
void display_board();
void display_co_board();
bool check_moves(char [3][3]);
int board_value(char [3][3]);
int minimax(char [3][3], bool);
void play_best_move();
};
game :: game(char ch = 'x')
{
pc = ( ch == 'x'? 'o' : 'x' );
ph = ch;
printf( "\nYou are playing as '%c' and computer as '%c' !!\n\n", ph, pc );
cout<<"INITAIL BOARD :\n";
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
gboard[i][j] = '_'; cout<<'_'<<" ";
}
cout<<endl;
}
cout<<endl;
}
void game :: display_co_board()
{
for(int i=1;i<4;i++)
{
for(int j=1;j<4;j++)
{
printf( "(%d,%d) ",i ,j );
}
cout<<endl;
}
}
void game :: display_board()
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<gboard[i][j]<<" ";
}
cout<<endl;
}
cout<<endl;
}
bool game :: check_moves(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 game :: board_value(char board[3][3])
{
for(int i=0;i<3;i++)
{
if(board[0][i] == board[1][i] && board[1][i] == board[2][i])
{
if(board[0][i] == pc) return 10;
else if(board[0][i] == ph) return -10;
}
if(board[i][0] == board[i][1] && board[i][1] == board[i][2])
{
if(board[i][0] == pc) return 10;
else if(board[i][0] == ph) return -10;
}
}
if(board[0][0] == board[1][1] && board[1][1] == board[2][2])
{
if(board[0][0] == pc) return 10;
else if(board[0][0] == ph) return -10;
}
if(board[0][2] == board[1][1] && board[1][1] == board[2][0])
{
if(board[0][2] == pc) return 10;
else if(board[0][2] == ph) return -10;
}
return 0;
}
int game :: minimax(char board[3][3], bool turn)
{
int value = board_value(board);
if(value == 10) return 10;
else if(value == -10) return -10;
else if(!check_moves(board)) return 0;
int res;
if(turn)
{
res = -999;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j] == '_')
{
board[i][j] = pc;
res = max(res, minimax(board, !turn));
board[i][j] = '_';
}
}
}
}
else
{
res = 999;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(board[i][j] == '_')
{
board[i][j] = ph;
res = min(res, minimax(board, !turn));
board[i][j] = '_';
}
}
}
}
return res;
}
void game :: play_best_move()
{
int reward = -1000, temp;
int x,y;
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(gboard[i][j] == '_')
{
gboard[i][j] = pc;
temp = minimax(gboard, false);
gboard[i][j] = '_';
if(temp>reward)
{
reward = temp;
x = i;
y = j;
}
}
}
}
gboard[x][y] = pc;
}
int main()
{
char ch;
int x, y;
int counter = 1;
cout<<"Enter the player you want to play as (x,o) !\n";
while(true)
{
cin>>ch;
if(ch != 'x' && ch != 'o') cout<<"Invalid Choice! Enter Again\n";
else break;
}
game g(ch);
cout<<"Co-ordinates of the board are as follows :\n\n";
g.display_co_board();
cout<<endl;
while(true)
{
cout<<"Enter the position you want to play ( eg: 1 2 )\n";
cin>>x>>y;
--x, --y;
if(x<0 || y<0 || x>2 || y>2)
{
cout<<"Out of bounds! Enter again\n";
continue;
}
else if(g.gboard[x][y] != '_')
{
cout<<"Already taken!! Enter again\n";
continue;
}
cout<<endl;
g.display_co_board();
cout<<endl;
g.gboard[x][y] = g.ph;
g.play_best_move();
if(g.check_moves(g.gboard) == false)
{
g.display_board();
cout<<"TIE!\n";
break;
}
else if(g.board_value(g.gboard) == 10)
{
g.display_board();
cout<<"YOU LOST!\n";
break;
}
else if(g.board_value(g.gboard) == -10)
{
g.display_board();
cout<<"YOU WON\n";
break;
}
g.display_board();
}
return 0;
}