Skip to content

Commit 79c4f12

Browse files
Create Tic-Tac-Toe Game.cpp
1 parent 1a0973a commit 79c4f12

File tree

1 file changed

+248
-0
lines changed

1 file changed

+248
-0
lines changed

Tic-Tac-Toe Game.cpp

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
1+
#include<iostream.h>
2+
#include<conio.h>
3+
#include<dos.h>
4+
#include<process.h>
5+
6+
7+
8+
char mat[3][3];
9+
void table(void); //function to print the table
10+
void welcome(void); //function for welcome screen
11+
12+
void main()
13+
{
14+
welcome();
15+
A: clrscr();
16+
int i,j,m,n,sum=0;
17+
char ch;
18+
19+
for(m=0;m<3;++m)
20+
for(n=0;n<3;++n)
21+
mat[m][n]=’’;
22+
table();
23+
24+
25+
while(sum<10)
26+
{
27+
//for player 1
28+
cout<<“Player 1 is’x’nChoose the position:”;
29+
cout<<“nRow:”;
30+
cin>>i;
31+
cout<<“Coloumn:”;
32+
cin>>j;
33+
34+
//if position is wrong
35+
for(;i>3||i<1||j>3||j<1||(‘x’==mat[i-1][j-1]||’o’==mat[i-1][j-1]);)
36+
{
37+
cout<<“nSorry!!!! wrong position,Choose the position again”;
38+
cout<<“nRow:”;
39+
cin>>i;
40+
cout<<“Coloumn:”;
41+
cin>>j;
42+
}
43+
mat[i-1][j-1]=’x’;
44+
sum++;
45+
46+
//to check if player 1 wins or not
47+
if(mat[0][0]==’x’&&mat[0][0]==mat[0][1]&&mat[0][0]==mat[0][2])
48+
{
49+
table();
50+
cout<<“nPlayer 1 wins…….!!!” ;
51+
cout<<“nYou have played Great…..!!!”;
52+
sleep(5);
53+
break;
54+
}
55+
56+
if(mat[1][0]==’x’&&mat[1][0]==mat[1][1]&&mat[1][0]==mat[1][2])
57+
{
58+
table();
59+
cout<<“nPlayer 1 wins…….!!!”;
60+
cout<<“nYou have played Great…..!!!”;
61+
sleep(5);
62+
break;
63+
}
64+
65+
if(mat[2][0]==’x’&&mat[2][0]==mat[2][1]&&mat[2][0]==mat[2][2])
66+
{
67+
table();
68+
cout<<“nPlayer 1 wins…….!!!”;
69+
cout<<“nYou have played Great…..!!!”;
70+
sleep(5);
71+
break;
72+
}
73+
74+
if(mat[0][0]==’x’&&mat[0][0]==mat[1][0]&&mat[0][0]==mat[2][0])
75+
{
76+
table();
77+
cout<<“nPlayer 1 wins…….!!!”;
78+
cout<<“nYou have played Great…..!!!”;
79+
sleep(5);
80+
break;
81+
}
82+
83+
if(mat[0][1]==’x’&&mat[0][1]==mat[1][1]&&mat[0][1]==mat[2][1])
84+
{
85+
table();
86+
cout<<“nPlayer 1 wins…….!!!”;
87+
cout<<“nYou have played Great…..!!!”;
88+
sleep(5);
89+
break;
90+
}
91+
92+
if(mat[0][2]==’x’&&mat[0][2]==mat[1][2]&&mat[0][2]==mat[2][2])
93+
{
94+
table();
95+
cout<<“nPlayer 1 wins…….!!!”;
96+
cout<<“nYou have played Great…..!!!”;
97+
sleep(5);
98+
break;
99+
}
100+
101+
if(mat[0][0]==’x’&&mat[0][0]==mat[1][1]&&mat[0][0]==mat[2][2])
102+
{
103+
table();
104+
cout<<“nPlayer 1 wins…….!!!”;
105+
cout<<“nYou have played Great…..!!!”;
106+
sleep(5);
107+
break;
108+
}
109+
110+
if(mat[0][2]==’x’&&mat[0][2]==mat[1][1]&&mat[0][0]==mat[2][0])
111+
{
112+
table();
113+
cout<<“nPlayer 1 wins…….!!!”;
114+
cout<<“nYou have played Great…..!!!”;
115+
sleep(5);
116+
break;
117+
}
118+
119+
if(sum==9) //to check the chances
120+
{
121+
table();
122+
cout<<“ntThe game is over…….no one wins…HaHaHa…..!!!”;
123+
break;
124+
}
125+
//for player 2
126+
cout<<“nnPlayer 2 is’o’nChoose the position:”;
127+
cout<<“nRow:”;
128+
cin>>i;
129+
cout<<“Coloumn:”;
130+
cin>>j;
131+
132+
//if position is wrong
133+
for(;i>3||i<1||j>3||j<1||(‘x’==mat[i-1][j-1]||’o’==mat[i-1][j-1]);)
134+
{
135+
cout<<“nSorry!!!! wrong position,Choose the position again”;
136+
cout<<“nRow:”;
137+
cin>>i;
138+
cout<<“Coloumn:”;
139+
cin>>j;
140+
}
141+
mat[i-1][j-1]=’o’;
142+
sum++;
143+
table();
144+
145+
//to check player 2 wins or not
146+
if(mat[0][0]==’o’&&mat[0][0]==mat[0][1]&&mat[0][0]==mat[0][2])
147+
{
148+
cout<<“nPlayer 2 wins…….!!!”;
149+
cout<<“nYou have played Great…..!!!”;
150+
sleep(5);
151+
break;
152+
}
153+
154+
if(mat[1][0]==’o’&&mat[1][0]==mat[1][1]&&mat[1][0]==mat[1][2])
155+
{
156+
cout<<“nPlayer 2 wins…….!!!”;
157+
cout<<“nYou have played Great…..!!!”;
158+
sleep(5);
159+
break;
160+
}
161+
162+
if(mat[2][0]==’o’&&mat[2][0]==mat[2][1]&&mat[2][0]==mat[2][2])
163+
{
164+
cout<<“nPlayer 2 wins…….!!!”;
165+
cout<<“nYou have played Great…..!!!”;
166+
sleep(5);
167+
break;
168+
}
169+
170+
if(mat[0][0]==’o’&&mat[0][0]==mat[1][0]&&mat[0][0]==mat[2][0])
171+
{
172+
cout<<“nPlayer 2 wins…….!!!”;
173+
cout<<“nYou have played Great…..!!!”;
174+
sleep(5);
175+
break;
176+
}
177+
178+
if(mat[0][1]==’o’&&mat[0][1]==mat[1][1]&&mat[0][1]==mat[2][1])
179+
{
180+
cout<<“nPlayer 2 wins…….!!!”;
181+
cout<<“nYou have played Great…..!!!”;
182+
sleep(5);
183+
break;
184+
}
185+
186+
if(mat[0][2]==’o’&&mat[0][2]==mat[1][2]&&mat[0][2]==mat[2][2])
187+
{
188+
cout<<“nPlayer 2 wins…….!!!”;
189+
cout<<“nYou have played Great…..!!!”;
190+
sleep(5);
191+
break;
192+
}
193+
194+
if(mat[0][0]==’o’&&mat[0][0]==mat[1][1]&&mat[0][0]==mat[2][2])
195+
{
196+
cout<<“nPlayer 2 wins…….!!!”;
197+
cout<<“nYou have played Great…..!!!”;
198+
sleep(5);
199+
break;
200+
}
201+
202+
if(mat[0][2]==’o’&&mat[0][2]==mat[1][1]&&mat[0][0]==mat[2][0])
203+
{
204+
cout<<“nPlayer 2 wins…….!!!”;
205+
cout<<“nYou have played Great…..!!!”;
206+
sleep(5);
207+
break;
208+
}
209+
}
210+
cout<<“nntWould you like to play more….?(y/n):”;
211+
cin>>ch;
212+
if(ch==’y’||ch==’Y’)
213+
goto A;
214+
else
215+
{
216+
cout<<“nntThanks for Playing…….:)”;
217+
sleep(5);
218+
exit(0);
219+
}
220+
}
221+
222+
void table()
223+
{
224+
clrscr();
225+
cout<<“nntt 1 2 3n”;
226+
cout<<“tt1 “<<mat[0][0]<<“|”<<mat[0][1]<<“|”<<mat[0][2];
227+
cout<<“ntt –|-|–“;
228+
cout<<“ntt2 “<<mat[1][0]<<“|”<<mat[1][1]<<“|”<<mat[1][2];
229+
cout<<“ntt –|-|–“;
230+
cout<<“ntt3 “<<mat[2][0]<<“|”<<mat[2][1]<<“|”<<mat[2][2]<<“nn”;
231+
}
232+
233+
void welcome()
234+
{
235+
textmode(C80);
236+
textcolor(YELLOW);
237+
clrscr();
238+
239+
cout<<“nnnnnttttWelcome To”;
240+
sleep(2);
241+
cout<<“nnttttTic-Tac-Toe”;
242+
sleep(1);
243+
cout<<“nntttt Game”;
244+
sleep(1);
245+
cout<<“nnnnnnnnntttttPress any key to continue…..!!”;
246+
getch();
247+
}
248+

0 commit comments

Comments
 (0)