-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2.cpp
More file actions
225 lines (221 loc) · 6.43 KB
/
2.cpp
File metadata and controls
225 lines (221 loc) · 6.43 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;
bool GetWinner(char symbol);
bool CheckRows(char symbol);
bool CheckColumns(char symbol);
bool CheckDiagonals(char symbol);
char array[3][3] = {{'1', '2', '3'}, {'4', '5', '6'}, {'7', '8', '9'}};
int main()
{
int i = 1, count = 0, cool = 0;
char symbol, value, taken;
while (true)
{
for (int z = 0; z < 3; z++)
{
for (int x = 0; x < 3; x++)
{
if (taken == array[z][x])
{
array[z][x] = symbol;
}
}
}
if (count > 3)
{
char sym = 'X';
// check who wins.
for (int y = 0; y < 2; y++)
{
int x;
int player = GetWinner(sym);
if (player == 1)
{
char opt;
if (sym == 'X')
{
x = 1;
}
if (sym == 'O')
{
x = 2;
}
cout << " ---------Congratulation-------------" << endl;
cout << " Player " << x << " Wins...";
cout << " \n\n";
cout << " Play Again (y/n) : ";
cin >> opt;
if (opt == 'y')
{
cool = 1;
}
if (opt == 'n')
{
exit(0);
}
}
sym = 'O';
}
if (cool == 1)
{
int i=0;
char line[9]={'1','2','3','4','5','6','7','8','9'};
for (int z = 0; z < 3; z++)
{
for (int x = 0; x < 3; x++)
{
array[z][x] = line[i];
i++;
}
}
i = 1, count = 0, cool = 0;
}
}
system("cls");
cout << " ----------------------------------------------------------------------" << endl;
cout << " ---------------------- Tic Tak Toe -----------------------" << endl;
cout << " ----------------------------------------------------------------------" << endl;
cout << " " << endl
<< endl;
system("cls");
cout << " _____________________" << endl;
cout << " | " << array[0][0] << " | " << array[0][1] << " | " << array[0][2] << " | " << endl;
cout << " |______|______|______|" << endl;
cout << " | " << array[1][0] << " | " << array[1][1] << " | " << array[1][2] << " | " << endl;
cout << " |______|______|______| " << endl;
cout << " | " << array[2][0] << " | " << array[2][1] << " | " << array[2][2] << " | " << endl;
cout << " |______|______|______|" << endl;
if (count % 2 == 0)
{
i = 1;
symbol = 'X';
}
if (count % 2 != 0)
{
symbol = 'O';
}
while (true)
{
cout << " player " << i << "[" << symbol << "]"
<< " Turn: ";
cin >> value;
int cont = 0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
if (value == array[i][j])
{
cont = 1;
taken = array[i][j];
break;
}
}
if (cont == 1)
{
break;
}
}
if (cont == 0)
{
cout << " Already Taken :) ";
cout << " Press Any Key: ";
getch();
}
if (value != '1' && value != '2' && value != '3' && value != '4' && value != '5' && value != '6' && value != '7' && value != '8' && value != '9')
{
cout << " InCorrect Input!!! TRY AGAIN :) " << endl
<< endl;
cout << " Press Any Key: ";
getch();
}
else
{
break;
}
}
count++;
i++;
}
}
bool GetWinner(char symbol)
{
if (CheckRows(symbol) || CheckColumns(symbol) || CheckDiagonals(symbol))
{
return 1;
}
else
{
return 0;
}
}
bool CheckRows(char symbol)
{
int count = 0;
for (int row = 0; row < 3; row++)
{
count = 0;
for (int col = 0; col < 3; col++)
{
if (array[row][col] == symbol)
{
count = count + 1;
}
}
if (count == 3)
{
return 1;
}
}
return 0;
}
bool CheckColumns(char symbol)
{
int count = 0;
for (int col = 0; col < 3; col++)
{
count = 0;
for (int row = 0; row < 3; row++)
{
if (array[row][col] == symbol)
{
count = count + 1;
}
}
if (count == 3)
{
return 1;
}
}
return 0;
}
bool CheckDiagonals(char symbol)
{
int count = 0;
for (int idx = 0; idx < 3; idx++)
{
if (array[idx][idx] == symbol)
{
count++;
}
}
if (count == 3)
{
return 1;
}
count = 0;
for (int row = 0, col = 2; row < 3; row++, col--)
{
if (array[row][col] == symbol)
{
count++;
}
}
if (count == 3)
{
return 1;
}
return 0;
}