-
Notifications
You must be signed in to change notification settings - Fork 0
/
TICTACTO.CPP
103 lines (98 loc) · 2.11 KB
/
TICTACTO.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
#include<iostream.h>
#include<conio.h>
char c[3][3];
void Input1(char c[3][3])
{
int x,y;
cout<<"Player 1's turn:: Enter coordinates: ";
cin>>x>>y;
if(c[x][y]!=' ')
{
cout<<"Illegal move\n";
Input1(c);
}
else c[x][y]='x';
}
void Input2(char c[3][3])
{
int x,y;
cout<<"Player 2's turn:: Enter coordinates: ";
cin>>x>>y;
if(c[x][y]!=' ')
{
cout<<"Illegal move\n";
Input2(c);
}
else c[x][y]='o';
}
int check(char g,char c[3][3])
{
if(c[0][0]==g&&c[0][1]==g&&c[0][2]==g) return 1;
else if(c[1][0]==g&&c[1][1]==g&&c[1][2]==g) return 1;
else if(c[2][0]==g&&c[2][1]==g&&c[2][2]==g) return 1;
else if(c[0][0]==g&&c[1][0]==g&&c[2][0]==g) return 1;
else if(c[0][1]==g&&c[1][1]==g&&c[2][1]==g) return 1;
else if(c[0][2]==g&&c[1][2]==g&&c[2][2]==g) return 1;
else if(c[0][0]==g&&c[1][1]==g&&c[2][2]==g) return 1;
else if(c[2][0]==g&&c[1][1]==g&&c[0][2]==g) return 1;
else return 0;
}
void main()
{
clrscr();
int k=0,m=0;
cout<<"TIC-TAC-TOE\n";
cout<<"Player 1 will give cross x\n";
cout<<"Player 2 will give circle o\n";
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
c[i][j]=' ';
cout<<"Game position coordinates:\n";
cout<<"(0,0) (0,1) (0,2)\n(1,0) (1,1) (1,2)\n(2,0) (2,1) (2,2)\n";
while(k==0)
{
clrscr();
cout<<"TIC-TAC-TOE\n";
cout<<"Player 1 will give cross x\n";
cout<<"Player 2 will give circle o\n";
cout<<"Game position coordinates:\n";
cout<<"(0,0) (0,1) (0,2)\n(1,0) (1,1) (1,2)\n(2,0) (2,1) (2,2)\n";
for(i=0;i<3;i++)
{
cout<<"---------------\n";
for(j=0;j<3;j++)
{
cout<<"| "<<c[i][j]<<" |";
}
cout<<"\n";
}
cout<<"---------------\n";
if(m%2==0) Input1(c);
else Input2(c);
if(check('x',c)||check('o',c))
{
k=1;
break;
}
if(m==8)
{
k=2;
break;
}
m++;
}
if(k==1&&m%2==0) cout<<"Player 1 wins\n";
if(k==1&&m%2!=0) cout<<"Player 2 wins\n";
if(k==2) cout<<"Match drawn\n";
for(i=0;i<3;i++)
{
cout<<"---------------\n";
for(j=0;j<3;j++)
{
cout<<"| "<<c[i][j]<<" |";
}
cout<<"\n";
}
cout<<"---------------\n";
getch();
}