-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTicTackToe.cpp
160 lines (147 loc) · 3.02 KB
/
TicTackToe.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
#include<iostream>
using namespace std;
char space[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
int row;
int column;
string n1="",n2="";
char token='x';
bool tie=false;
void function1()
{
cout<<" | | \n";
cout<<" "<<space[0][0]<<" | "<<space[0][1]<<" | "<<space[0][2]<<endl;
cout<<"____|____|_____\n";
cout<<" | | \n";
cout<<" "<<space[1][0]<<" | "<<space[1][1]<<" | "<<space[1][2]<<endl;
cout<<"____|____|_____\n";
cout<<" | | \n";
cout<<" "<<space[2][0]<<" | "<<space[2][1]<<" | "<<space[2][2]<<endl;
cout<<" | | \n";
}
void function2()
{
int digit;
if(token=='x')
{
cout<<n1<<" please enter";
cin>>digit;
}
if(token=='0')
{
cout<<n2<<" please enter";
cin>>digit;
}
if(digit==1)
{
row=0;
column=0;
}
if(digit==2)
{
row=0;
column=1;
}
if(digit==3)
{
row=0;
column=2;
}
if(digit==4)
{
row=1;
column=0;
}
if(digit==5)
{
row=1;
column=1;
}
if(digit==6)
{
row=1;
column=2;
}
if(digit==7)
{
row=2;
column=0;
}
if(digit==8)
{
row=2;
column=1;
}
if(digit==9)
{
row=2;
column=2;
}
else if(digit<1 || digit>9){
cout<<"INVALID !!!"<<endl;
}
if(token=='x'&& space[row][column]!='x' && space[row][column]!='0')
{
space[row][column]='x';
token='0';
}
else if(token=='0'&& space[row][column]!='x' && space[row][column]!='0')
{
space[row][column]='0';
token='x';
}
else
{
cout<<"THERE IS NO EMPTY SPACE!!!";
function2();
}
}
bool function3()
{
for(int i=0;i<3;i++)
{
if(space[i][0]==space[i][1]&&space[i][0]==space[i][2] || space[0][i]==space[1][i]&&space[0][i]==space[2][i])
{
return true;
}
}
if(space[0][0]== space[1][1]&&space[1][1]==space[2][2] || space[0][2]==space[1][1]&&space[1][1]==space[2][0])
{return true;}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if( space[i][j] !='x' && space[i][j]!='0')
{
return false;
}
}
}
tie=true;
}
int main()
{
cout<<"Welcome to TIC TACK TOE:\n";
cout<<"Enter the name of player 1 \n";
getline(cin,n1);
cout<<"Enter the name of player 2 \n";
getline(cin,n2);
while(!function3())
{
function1();
function2();
function3();
}
if(token=='x' && tie==false)
{
cout<<n2<<" WINS!!!"<<endl;
}
else if(token=='0' && tie==false)
{
cout<<n1<<" WINS!!!"<<endl;
}
else
{
cout<<"IT's A DRAW!!!!"<<endl;
}
return 0;
}