-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.cpp
More file actions
369 lines (352 loc) · 13 KB
/
Copy pathTicTacToe.cpp
File metadata and controls
369 lines (352 loc) · 13 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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include<iostream>
#include<cstdlib>
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
using namespace std;
class board{
protected :
char a[3][3];/*Our board matrix of 3X3*/
public :
/*Default constructor for board giving us a empty 3*3 array */
board(){
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
a[i][j]=' ';
}
}
}
bool validity(int i,int j){
if(a[i][j]!=' '){
return true;
}else{
return false;
}
}
void showboard(){
/*To Print the first row of index*/
cout<<"\n\n0 1 2 3 ";
/*To print from row 2 to 4*/
cout<<"\n";
for(int i=0;i<3;i++){/*Loops 3 times for our matrix ,i.e. 3 rows*/
cout<<"\n\n";
cout<<" | |\n";
for(int j=0;j<4;j++){/*Loopps 4 times , this is for our column*/
if(j==0){/*First column , to print only indexes*/
cout<<i+1<<"\t";
}else if(j==3){
cout<<" "<<a[i][j-1]<<"\t";
}
else{
cout<<" "<<a[i][j-1]<<" |"<<" ";
}
}
cout<<"\n | |";
if(i==0||i==1){
cout<<"\n _____________________________";
}
}
cout<<"\n\n";
}
void update_array(int i, int j,char c){
a[i][j]=c;
}
bool check_status(){
int m=0;
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
if(a[i][j]!=' '){
m++;
}
}
}
if(m!=9){
return true;
}else{
return false;
}
}
char check_winner(){
/*First checking rows*/
if(a[0][0]==a[0][1] && a[0][1]==a[0][2] && a[0][0]!=' '){
return a[0][0];
}else if(a[1][0]==a[1][1] && a[1][1]==a[1][2] && a[1][0]!=' '){
return a[1][0];
}else if(a[2][0]==a[2][1] && a[2][1]==a[2][2] && a[2][0]!=' '){
return a[2][0];
}else{}
/*Checking column*/
if(a[0][0]==a[1][0] && a[1][0]==a[2][0] && a[0][0]!=' '){
return a[0][0];
}else if(a[0][1]==a[1][1] && a[1][1]==a[2][1] && a[1][1]!=' '){
return a[0][1];
}else if(a[0][2]==a[1][2] && a[1][2]==a[2][2] && a[2][2]!=' '){
return a[0][2];
}else{}
/*Checking diagonal*/
if(a[0][0]==a[1][1]&&a[1][1]==a[2][2]&&a[2][2]!=' '){
return a[2][2];
}else if(a[2][0]==a[1][1]&&a[1][1]==a[0][2]&&a[1][1]!=' '){
return a[1][1];
}else{
}
return ' ';
}
};
class symbol{
private :
char p1,p2;/*Symbols representing player*/
public :
/*Constructor player choosen symbols*/
symbol(char P1,char P2){
p1=P1;
p2=P2;
}
symbol(){
p1='O';
p2='X';
}
char getsymbol_p1(){
return p1;
}
char getsymbol_p2(){
return p2;
}
void update_symbol(char p ,char q){
p1=p;
p2=q;
}
void checksymbol(){
cout<< "---------------------------------";
cout<< "\n| Symbol of player 1 is : " << "[ " << p1 << " ] |";
cout<< "\n| Symbol of player 2 is : " << "[ " << p2 << " ] |";
cout<< "\n---------------------------------";
}
};
class score{/*Creating a score class to track player scores */
private :
int p1;
int p2;
public :
score(){
p1=0;
p2=0;
}
int get_score_p1(){
return p1;
}
int get_score_p2(){
return p2;
}
void update_p1(){
p1++;
}
void update_p2(){
p2++;
}
};
class turn{
private:
bool t;
public :
turn(){
t=true;
}
turn(bool b){
t=b;
}
void update_turn(){
if(t){
t=false;
}else{
t=true;
}
}
bool check_turn(){
return t;
}
};
int main()
{
symbol p;
score s;
turn t=true;
char state=' ';
bool run=true;
while (run){
while(state != 'p' && state != 'P' &&
state != 's' && state != 'S' &&
state != 'c' && state != 'C' &&
state != 'q' && state != 'Q'){
cout<<"\n\n****************Tic-Tac-Toe****************\n\n";
cout<<"1) Play ( Enter p to play )\n";
cout<<"2) Score ( Enter s to check scores )\n";
cout<<"3) Characters / Symbols ( Enter c to check \n or update symbol of players )\n";
cout<<"4) Quit ( Enter q to quit )\n\n";
cout<<"Enter your command : ";
cin>>state;
cout<<"\n";
if (state != 'p' && state != 'P' &&
state != 's' && state != 'S' &&
state != 'c' && state != 'C' &&
state != 'q' && state != 'Q') {
cout<<"\n\n The command u entered is invalid , please try again !!!! ";
}else{
break;
}
}
clearScreen();
if(state=='p'||state=='P'){
clearScreen();
board b;
turn t1=t.check_turn();
int i, j, n;
while (b.check_status()) {
if (b.check_status() && b.check_winner() == p.getsymbol_p1()) {
clearScreen();
cout<<"\n\n****************Tic-Tac-Toe****************\n\n";
b.showboard();
cout<<"\n--------------------------------";
cout<< "\n| Player 1 is the Winner !!!! |\n";
cout<<"--------------------------------\n\n";
s.update_p1();
break;
} else if (b.check_status() && b.check_winner() == p.getsymbol_p2()) {
clearScreen();
cout<<"\n\n****************Tic-Tac-Toe****************\n\n";
b.showboard();
cout<<"\n--------------------------------";
cout<< "\n| Player 2 is the Winner !!!! |\n";
cout<<"--------------------------------\n\n";
s.update_p2();
break;
} else if (b.check_status() == false && b.check_winner() == p.getsymbol_p1()) {
clearScreen();
cout<<"\n\n****************Tic-Tac-Toe****************\n\n";
b.showboard();
cout<<"\n--------------------------------";
cout<< "\n| Player 1 is the Winner !!!! |\n";
cout<<"--------------------------------\n\n";
s.update_p1();
break;
} else if (b.check_status() == false && b.check_winner() == p.getsymbol_p2()) {
clearScreen();
cout<<"\n\n****************Tic-Tac-Toe****************\n\n";
b.showboard();
cout<<"\n--------------------------------";
cout<< "\n| Player 1 is the Winner !!!! |\n";
cout<<"--------------------------------\n\n";
s.update_p2();
break;
}else {
}
if (t1.check_turn()) {
bool is_fine = true;
clearScreen();
cout<<"\n\n****************Tic-Tac-Toe****************\n\n";
p.checksymbol();
b.showboard();
cout<<"\n---------------------------";
cout<< "\n| It is turn of Player 1 |\n";
cout<<"---------------------------\n\n";
while (is_fine) {
cout<< "Enter your index pair (e.g 11 =(1,1)) : ";
cin>> n;
i = n / 10 - 1;
j = n % 10 - 1;
if ((n == 11 || n == 12 || n == 13 || n == 21 || n == 22 || n == 23 || n == 31 || n == 32 || n == 33) && b.validity(i, j) == false) {
is_fine = false;
b.update_array(i, j, p.getsymbol_p1());
break;
} else if (b.validity(i, j) == true) {
cout<< "\n\n Player 1 the index (" << i+1 << "," << j+1 << ")\nis already filled.Please input a valid input!!!!\n\n";
} else if ((n != 11 || n != 12 || n != 13 || n != 21 || n != 22 || n != 23 || n != 31 || n != 32 || n != 33)) {
cout<< "\n\n Player 1 your input is invalid \nPlease Input a valid input !!!!\n\n";
} else {
}
}
t1.update_turn();
} else {
bool is_fine_p2 = true;
clearScreen();
cout<<"\n\n****************Tic-Tac-Toe****************\n\n";
p.checksymbol();
b.showboard();
cout<<"\n---------------------------";
cout<< "\n| It is turn of Player 2 |\n";
cout<<"---------------------------\n\n";
while (is_fine_p2) {
cout<<"Enter your index pair (e.g 11 =(1,1)) : ";
cin>>n;
i = n / 10 - 1;
j = n % 10 - 1;
if ((n == 11 || n == 12 || n == 13 || n == 21 || n == 22 || n == 23 || n == 31 || n == 32 || n == 33) && b.validity(i, j) == false) {
is_fine_p2 = false;
b.update_array(i, j, p.getsymbol_p2());
break;
} else if (b.validity(i, j) == true) {
cout<<"\n\n Player 2 the index (" << i+1 << "," << j+1 << ")\nis already filled.Please input a valid input!!!!\n\n";
} else if ((n != 11 || n != 12 || n != 13 || n != 21 || n != 22 || n != 23 || n != 31 || n != 32 || n != 33)) {
cout<<"\n\n Player 2 your input is invalid \nPlease Input a valid input !!!!\n\n";
} else {
}
}
t1.update_turn();
}
}
if (b.check_status() == false && b.check_winner() == ' ') {
clearScreen();
cout<<"\n\n****************Tic-Tac-Toe****************\n\n";
cout<< "\n----------------------------------------";
cout<< "\n| Player 1 and Player 2 are Tied !!!! |";
cout<< "\n----------------------------------------\n\n";
}
t.update_turn();
}else if(state=='s'||state=='S'){
cout<<"\n\n*************Tic-Tac-Toe-Score************\n\n";
cout<<"--------------------\n";
cout<<"| Scores : |\n| |\n";
cout<<"| 1) Player 1 : "<<s.get_score_p1()<<" |";
cout<<"\n| 2) Player 2 : "<<s.get_score_p2()<<" |";
cout<<"\n--------------------\n";
}else if(state=='c'||state=='C'){
int i=1,j=0;
while(i!=0){
cout<<"\n\n*************Tic-Tac-Toe-Symbol************\n\n";
cout<<"1) Show (Enter 1 to show symbols assingned \n to each player)\n";
cout<<"2) Update (Enter 2 to update symbols \n assingned to each player)\n\n";
cout<<"Enter your command : ";
cin>>j;
cout<<"\n";
if (j!=1 && j!=2) {
cout<<"\n\n The command u entered is invalid , please try again !!!! ";
}else{
break;
}
}
if(j==1){
clearScreen();
cout<<"\n\n*************Tic-Tac-Toe-Symbol************\n\n";
p.checksymbol();
}else if(j==2){
clearScreen();
char p1,p2;
cout<<"\n\n*************Tic-Tac-Toe-Symbol************\n\n";
cout<<"Enter Symbol To Assing To Player 1 : ";
cin>>p1;
cout<<"Enter Symbol To Assing To Player 2 : ";
cin>>p2;
p.update_symbol(p1,p2);
}
}else if(state=='q'||state=='Q'){
return 0;
}else{
}
state=' ';
}
return 0;
}