-
Notifications
You must be signed in to change notification settings - Fork 0
/
TTT.cpp
258 lines (245 loc) · 4.54 KB
/
TTT.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
//preprocessor directives
#include <iostream>
#include<iomanip>
#include "TTT.h"
using namespace std;
TicTacToe::TicTacToe()
{
//Intializing with zero
square[3][3] = 0; //initializing the whole 2D 3x3 array with 0, every entry is 0
int n = 10;//number to initialize. First box, row 0 column 0 is named 10, and like that row and column change and boxes are named from 10 to 18
//nested loop used because we are accessing 2D array
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
square[i][j] = n++;
}
}
//Box Print
//function calling
display();
//checkwin
checkwin();
data();
}
void TicTacToe::display()
{
cout << "\t --- --- ---\n\t|";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << setw(2) << setfill('0') << square[i][j] << " | ";
}
cout << "\n\t --- --- ---\n\t|";
}
}
void TicTacToe::data()
{
for (int i = 1; i < 5; i++)
{
//Player 1
challenger();
display();
result = checkwin();
if (result == 1) {
find();
exit(0);
}
//Player 2
opponent();
display();
result = checkwin();
if (result == 2) {
find();
exit(0);
}
}
find();
}
void TicTacToe::challenger() {
cout << "\n Player " << player1 << " Enter the Box: ";
cin >> box;
check();//giving arguments to mark()
mark(player1, row, col);
}
void TicTacToe::opponent() {
cout << "\n Player " << player2 << " Enter the Box: ";
cin >> box;
check();//giving paramters to mark()
mark(player2, row, col);
}
void TicTacToe::check() {
check:
while (1) //infinite loop
{
if (box == 10)
{
row = 0;
col = 0;
goto exit;
}
else if (box == 11)
{
row = 0;
col = 1;
goto exit;
}
else if (box == 12)
{
row = 0;
col = 2;
goto exit;
}
else if (box == 13)
{
row = 1;
col = 0;
goto exit;
}
else if (box == 14)
{
row = 1;
col = 1;
goto exit;
}
else if (box == 15)
{
row = 1;
col = 2;
goto exit;
}
else if (box == 16)
{
row = 2;
col = 0;
goto exit;
}
else if (box == 17)
{
row = 2;
col = 1;
goto exit;
}
else if (box == 18)
{
row = 2;
col = 2;
goto exit;
}
else
{
cout << "Wrong Entry!\n";
cout << "Enter box# again: ";
cin >> box;
goto check;
}
}
exit:
if (1) {};//null statement
}
void TicTacToe::find() {
if (result == 1)
{
cout << "\n Congratualtions! player " << player1 << " has Won \n";
flag = 1;
}
else if (result == 2)
{
cout << "\n Congratualtions! player " << player2 << " has Won \n";
flag = 1;
}
else if (flag == 0) {
cout << " \n Sorry, The game is a draw \n";
}
else {
//null statement, doesnot matter if you write else or not
}
}
void TicTacToe::mark(int player, int row, int col)
{
if (player == 1)
{
//Player1
if (square[row][col] == 1 || square[row][col] == 2) {
cout << " !!!Wrong Input!!!Enter Box# agian: ";
challenger();
}
else {
square[row][col] = 1;
}
}
else {
//Player2
if (square[row][col] == 1 || square[row][col] == 2) {
cout << " !!!Wrong Input!!!Enter Box# agian: ";
opponent();
}
else {
square[row][col] = 2;
}
}
}
int TicTacToe::checkwin()
{
if (square[0][0] == square[0][1] && square[0][1] == square[0][2]) //1st row
{
if (square[0][0] == 1)
return 1;
else
return 2;
}
else if (square[1][0] == square[1][1] && square[1][1] == square[1][2]) //2nd row
{
if (square[1][0] == 1)
return 1;
else
return 2;
}
else if (square[2][0] == square[2][1] && square[2][1] == square[2][2]) //3rd row
{
if (square[2][0] == 1)
return 1;
else
return 2;
}
else if (square[0][0] == square[1][0] && square[1][0] == square[2][0]) //1st column
{
if (square[0][0] == 1)
return 1;
else
return 2;
}
else if (square[0][1] == square[1][1] && square[1][1] == square[2][1]) //2nd column
{
if (square[0][1] == 1)
return 1;
else
return 2;
}
else if (square[0][2] == square[1][2] && square[1][2] == square[2][2]) //3rd column
{
if (square[0][2] == 1)
return 1;
else
return 2;
}
else if (square[0][0] == square[1][1] && square[1][1] == square[2][2]) //primary diagonal
{
if (square[0][0] == 1)
return 1;
else
return 2;
}
else if (square[0][2] == square[1][1] && square[1][1] == square[2][0]) //secondary diagonal
{
if (square[0][2] == 1)
return 1;
else
return 2;
}
else {
return 0;
}
}