Repository navigation
Expand file tree
/
Copy pathttt.cpp
More file actions
148 lines (145 loc) · 3.34 KB
/
Copy pathttt.cpp
File metadata and controls
148 lines (145 loc) · 3.34 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
#include<iostream>
#include<limits>
using namespace std;
//Checks for win and loss
bool win(int t[3][3])
{
for(int i = 0; i<=2; i++)
{
if(t[i][0] == t[i][1] && t[i][1] == t[i][2] && t[i][1] == 2)return true;
if(t[i][0] == t[i][1] && t[i][1] == t[i][2] && t[i][1] == 3)return true;
if(t[0][i] == t[1][i] && t[1][i] == t[2][i] && t[2][i] == 2)return true;
if(t[0][i] == t[1][i] && t[1][i] == t[2][i] && t[2][i] == 3)return true;
}
if(t[1][1] == t[2][2] && t[2][2] == t[0][0] && t[2][2] == 2)return true;
if(t[1][1] == t[2][2] && t[2][2] == t[0][0] && t[2][2] == 3)return true;
if(t[0][2] == t[1][1] && t[1][1] == t[2][0] && t[1][1] == 2)return true;
if(t[0][2] == t[1][1] && t[1][1] == t[2][0] && t[1][1] == 3)return true;
return false;
}
//Check if the element is empty or not
bool check(const int t[3][3], int row, int column)
{
if(t[row][column] == 1)
{
return true;
}
return false;
}
//Takes input and verifies it
int input()
{
int n;
while(true)
{
if(cin>>n)
{
if(n > 3 || n < 1)
{
cout<<"Please enter either 1, 2 or 3";
continue;
}
}
else if(cin.fail())
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout<<"Please enter a valid number";
continue;
}
return n;
}
}
//This function shows the grid to the user
void show(int t[3][3])
{
int r = 0, c = 0;
while(r<3)
{
if(t[r][c] == 1)cout<<" ";
else if(t[r][c] == 2)cout<<"X";
else if(t[r][c] == 3)cout<<"O";
if(c<2)
{
cout<<"|";
c++;
continue;
}
else
{
cout<<"\n";
c = 0;
r++;
}
}
}
//The part where i am displaying text for input
void ttt()
{
//1 is empty space, 2 is X and 3 is O
int t[3][3] = {{1,1,1},{1,1,1},{1,1,1}};
int i = 1;
bool x = false;
while(i<=9)
{
if(!x)
{
cout<<"Select which row you want to play 'X'";
int row = input();
row--;
cout<<"Select which column you want to play 'X'";
int column = input();
column--;
if(!check(t, row, column))
{
cout<<"This space is already played";
continue;
}
else
{
t[row][column] = 2;
x = true;
i++;
show(t);
if(win(t))
{
cout<<"X wins!";
return;
}
}
}
else if(x)
{
cout<<"Select which row you want to play 'O'";
int row = input();
row--;
cout<<"Select which column you want to play 'O'";
int column = input();
column--;
if(!check(t, row, column))
{
cout<<"This space is already played\n";
continue;
}
else
{
t[row][column] = 3;
i++;
x = false;
show(t);
if(win(t))
{
cout<<"O Wins!";
return;
}
}
}
}
cout<<"It is a draw";
}
//main function
int main()
{
cout<<"--------Tic-Tac-Toe--------\n";
ttt();
}