-
Notifications
You must be signed in to change notification settings - Fork 5
/
tic-tac-toe.cpp
263 lines (256 loc) · 4.29 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
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
#include<iostream>
#include<windows.h>
using namespace std;
class print;
class fill1;
class win1;
int flag=0;
class body
{
protected:
char mat[5][11];
public:
body()
{
for(int i=0;i<5;i++)
{
for(int j=0;j<11;j++)
{
if(i==1 || i==3)
mat[i][j]='-';
else if(j==3 || j==7)
mat[i][j]='|';
else
mat[i][j]=' ';
}
}
}
friend class print;
friend class fill1;
friend class win1;
};
class fill1
{
public:
void filldat(body &ob11,int a,int b)
{
if(a==1)
{
if(b==1)
ob11.mat[0][1]='0';
if(b==2)
ob11.mat[0][5]='0';
if(b==3)
ob11.mat[0][9]='0';
if(b==4)
ob11.mat[2][1]='0';
if(b==5)
ob11.mat[2][5]='0';
if(b==6)
ob11.mat[2][9]='0';
if(b==7)
ob11.mat[4][1]='0';
if(b==8)
ob11.mat[4][5]='0';
if(b==9)
ob11.mat[4][9]='0';
}
else if(a==2)
{
if(b==1)
ob11.mat[0][1]='x';
if(b==2)
ob11.mat[0][5]='x';
if(b==3)
ob11.mat[0][9]='x';
if(b==4)
ob11.mat[2][1]='x';
if(b==5)
ob11.mat[2][5]='x';
if(b==6)
ob11.mat[2][9]='x';
if(b==7)
ob11.mat[4][1]='x';
if(b==8)
ob11.mat[4][5]='x';
if(b==9)
ob11.mat[4][9]='x';
}
}
};
class win1
{
public:
void check1(body &ob12)
{
if(ob12.mat[0][1]=='0' && ob12.mat[2][1]=='0')
{
if(ob12.mat[4][1]=='0')
flag=1;
}
if(ob12.mat[0][1]=='0' && ob12.mat[0][5]=='0')
{
if(ob12.mat[0][9]=='0')
flag=1;
}
if(ob12.mat[0][1]=='0' && ob12.mat[2][5]=='0')
{
if(ob12.mat[4][9]=='0')
flag=1;
}
if(ob12.mat[0][5]=='0' && ob12.mat[2][5]=='0')
{
if(ob12.mat[4][5]=='0')
flag=1;
}
if(ob12.mat[0][9]=='0' && ob12.mat[2][9]=='0')
{
if(ob12.mat[4][9]=='0')
flag=1;
}
if(ob12.mat[0][9]=='0' && ob12.mat[2][5]=='0')
{
if(ob12.mat[4][1]=='0')
flag=1;
}
if(ob12.mat[2][1]=='0' && ob12.mat[2][5]=='0')
{
if(ob12.mat[2][9]=='0')
flag=1;
}
if(ob12.mat[4][1]=='0' && ob12.mat[4][5]=='0')
{
if(ob12.mat[4][9]=='0')
flag=1;
}
//second player
if(ob12.mat[0][1]=='x' && ob12.mat[2][1]=='x')
{
if(ob12.mat[4][1]=='x')
flag=2;
}
if(ob12.mat[0][1]=='x' && ob12.mat[0][5]=='x')
{
if(ob12.mat[0][9]=='x')
flag=2;
}
if(ob12.mat[0][1]=='x' && ob12.mat[2][5]=='x')
{
if(ob12.mat[4][9]=='x')
flag=2;
}
if(ob12.mat[0][5]=='x' && ob12.mat[2][5]=='x')
{
if(ob12.mat[4][5]=='x')
flag=2;
}
if(ob12.mat[0][9]=='x' && ob12.mat[2][9]=='x')
{
if(ob12.mat[4][9]=='x')
flag=2;
}
if(ob12.mat[0][9]=='x' && ob12.mat[2][5]=='x')
{
if(ob12.mat[4][1]=='x')
flag=2;
}
if(ob12.mat[2][1]=='x' && ob12.mat[2][5]=='x')
{
if(ob12.mat[2][9]=='x')
flag=2;
}
if(ob12.mat[4][1]=='x' && ob12.mat[4][5]=='x')
{
if(ob12.mat[4][9]=='x')
flag=2;
}
}
};
class print
{
public:
void printdata(body &ob10)
{
system("CLS");
for(int i=0;i<5;i++)
{
for(int j=0;j<11;j++)
cout<<ob10.mat[i][j];
cout<<endl;
}
}
};
int main()
{
start:
int no1,no2;
char choice;
string name1,name2;
body ob1;
fill1 ob5;
print ob4;
win1 ob8;
cout<<"PLAYER 1 ENTER YOUR NAME = ";
cin>>name1;
cout<<"PLAYER 2 ENTER YOUR NAME = ";
cin>>name2;
ob4.printdata(ob1);
while(1)
{
cout<<"PLAYER "<<name1<<" ENTER CHOICE : ";
cin>>no1;
ob5.filldat(ob1,1,no1);
ob4.printdata(ob1);
ob8.check1(ob1);
if(flag==1)
{
cout<<"PLAYER "<<name1<<" WON THE GAME"<<endl;
cout<<"DO YOU WANT TO PLAY AGAIN(y/n) : ";
cin>>choice;
if(choice=='y')
system("CLS");
goto start;
break;
}
if(flag==2)
{
cout<<"PLAYER "<<name2<<" WON THE GAME"<<endl;
cout<<"DO YOU WANT TO PLAY AGAIN(y/n) : ";
cin>>choice;
if(choice=='y')
{
system("CLS");
goto start;
}
else
cout<<"GAME OVER";
break;
}
cout<<"PLAYER "<<name2<<" ENTER CHOICE : ";
cin>>no2;
ob5.filldat(ob1,2,no2);
ob4.printdata(ob1);
ob8.check1(ob1);
if(flag==1)
{
cout<<"PLAYER "<<name1<<" WON THE GAME"<<endl;
cout<<"DO YOU WANT TO PLAY AGAIN(y/n) : ";
cin>>choice;
if(choice=='y')
system("CLS");
goto start;
break;
}
if(flag==2)
{
cout<<"PLAYER "<<name2<<" WON THE GAME"<<endl;
cout<<"DO YOU WANT TO PLAY AGAIN(y/n) : ";
cin>>choice;
if(choice=='y')
system("CLS");
goto start;
break;
}
}
return 0;
}