-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAmulyaGaurTICTACTOE.cpp
More file actions
156 lines (156 loc) · 3.24 KB
/
AmulyaGaurTICTACTOE.cpp
File metadata and controls
156 lines (156 loc) · 3.24 KB
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
/*
Author : Amulya Gaur
Run this code in c++ ide and follow the instructions.
Have fun!
*/
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
char a[3][3];
void print()
{
cout<<" --- --- --- "<<endl;
cout<<"| "<<a[0][0]<<" | "<<a[0][1]<<" | "<<a[0][2]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
cout<<"| "<<a[1][0]<<" | "<<a[1][1]<<" | "<<a[1][2]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
cout<<"| "<<a[2][0]<<" | "<<a[2][1]<<" | "<<a[2][2]<<" |"<<endl;
cout<<" --- --- --- "<<endl;
}
int main()
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
a[i][j]=' ';
cout<<"Welcome to Tic-Tac-Toe Game"<<endl;
print();
cout<<endl;
cout<<"This will be your playground"<<endl<<"Player 1 and 2 will enter the coordinate of the block(0 based indexing) which they want to fill"<<endl;
cout<<"Character for player 1 is X and for player 2 is O"<<endl;
cout<<"Initial state is "<<endl;
print();
cout<<endl<<"Lets play!"<<endl;
while(1)
{
int x1,y1,x2,y2,empty1=0,empty2=0,r1,c1,d11,d12,r2,c2,d21,d22;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(a[i][j]==' ')
empty1++;
if(empty1==0)
{
cout<<"Game Tie! See you again!"<<endl;
break;
}
else
{
while(1)
{
cout<<"Player 1"<<endl;
cin>>x1>>y1;
if(x1>=0 && x1<=2 && y1>=0 && y1<=2 && a[x1][y1]==' ')
break;
else
cout<<"Invalid coordinate! try again"<<endl;
}
a[x1][y1]='X';
cout<<"playground looks like:"<<endl;
print();
r1=0,c1=0,d11=0,d12=0;
for(int i=0;i<3;i++)
{
if(a[x1][i]=='X')
r1++;
if(a[i][y1]=='X')
c1++;
}
if(x1==y1)
{
for(int i=0,j=0;i<3 && j<3;i++,j++)
if(a[i][j]=='X')
d11++;
}
if((x1+y1)==2)
{
if(a[0][2]=='X')
d12++;
if(a[1][1]=='X')
d12++;
if(a[2][0]=='X')
d12++;
}
}
if(r1==3 || c1==3 || d11==3 || d12==3)
{
cout<<"Player 1 wins! Thanks for playing!"<<endl;
break;
}
else
{
empty2=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(a[i][j]==' ')
empty2++;
if(empty2==0)
{
cout<<"Game Tie! See you again!"<<endl;
break;
}
else
{
while(1)
{
cout<<"Player 2"<<endl;
cin>>x2>>y2;
if(x2>=0 && x2<=2 && y2>=0 && y2<=2 && a[x2][y2]==' ')
break;
else
cout<<"Invalid coordinate! try again"<<endl;
}
a[x2][y2]='O';
cout<<"playground looks like:"<<endl;
print();
r2=0,c2=0,d21=0,d22=0;
for(int i=0;i<3;i++)
{
if(a[x2][i]=='O')
r2++;
if(a[i][y2]=='O')
c2++;
}
if(x2==y2)
{
for(int i=0,j=0;i<3 && j<3;i++,j++)
if(a[i][j]=='O')
d21++;
}
if((x2+y2)==2)
{
if(a[0][2]=='O')
d22++;
if(a[1][1]=='O')
d22++;
if(a[2][0]=='O')
d22++;
}
}
if(r2==3 || c2==3 || d21==3 || d22==3)
{
cout<<"Player 2 wins!"<<endl;
break;
}
}
empty2=0;
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
if(a[i][j]==' ')
empty2++;
if(empty2==0)
{
cout<<"Game Tie! See you again!"<<endl;
break;
}
}
return 0;
}