-
Notifications
You must be signed in to change notification settings - Fork 0
/
TicTac.cpp
146 lines (128 loc) · 2.92 KB
/
TicTac.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
#include <iostream>
#include <string>
using namespace std;
char space[3][3]= {{'1','2','3'},{'4','5','6'},{'7','8','9' }};
int row;
int column;
char token= 'x';
string n1="";
string n2="";
bool tie= false;
void Pattern()
{
cout<<" | | "<<endl;
cout<<" "<<space[0][0]<<" | "<<space[0][1]<<" | "<<space[0][2]<<" "<<endl;
cout<<"___|_____|___"<<endl;
cout<<" | | "<<endl;
cout<<" "<<space[1][0]<<" | "<<space[1][1]<<" | "<<space[1][2]<<" "<<endl;
cout<<"___|_____|___"<<endl;
cout<<" | | "<<endl;
cout<<" "<<space[2][0]<<" | "<<space[2][1]<<" | "<<space[2][2]<<" "<<endl;
cout<<" | | "<<endl;
cout<<endl;
}
void Condition(){
int digit;
if(token=='x'){
cout<<n1<<" ITS YOUR TURN: "<<endl;
cin>>digit;
}
if(token=='0'){
cout<<n2<<" ITS YOUR TURN: "<<endl;
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<<"NO Empty Space Left!!!"<<endl;
Condition();
}
Pattern();
}
bool Final()
{
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;
return false;
}
int main(){
cout<<"Enter the name of the player one: "<<endl;
getline(cin,n1);
cout<<"Enter the name of the player two: "<<endl;
getline(cin,n2);
cout<<n1<<" WILL PLAY FIRST"<<endl;
cout<<n2<<" WILL PLAY AFTER PLAYER ONE"<<endl;
while(! Final()){
Pattern();
Condition();
Final();
}
if(token=='x' ){
cout<<n2<<" WINS !!!!"<<endl;
}
else if(token=='0'){
cout<<n1<<" WINS !!!!"<<endl;
}
else{
cout<<"WELL! its a DRAW..."<<endl;
}
}