-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgame.cpp
More file actions
371 lines (332 loc) · 10.4 KB
/
game.cpp
File metadata and controls
371 lines (332 loc) · 10.4 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
370
371
#include<iostream>
#include<conio.h>
using namespace std;
#define PLAYER1 1
#define PLAYER2 2
#define PLAYER1MOVE '0'
#define PLAYER2MOVE 'X'
class Root{
public:
virtual void showBoard() = 0; //pure virtual function
};
class Board: public Root
{
private :
int side;
public :
char** gameBoard;
Board(int side)
{
this->side=side; // This pointer is used as data member and variable have same name.
gameBoard = new char*[side];
for(int i =0;i<side;i++)
{
gameBoard[i] = new char[side];
}
for(int i=0;i<side;i++)
{
for(int j=0;j<side;j++)
{
gameBoard[i][j]=' ';
}
}
}
void showBoard()
{
for(int i =0;i<=side*2;i++)
{
cout<<"\t\t\t\t ";
for(int j=0;j<side;j++)
{
if(i%2==0)
{
for(int k =0;k<side;k++)cout<<"----";
break;
}
else
{
if(j==0)cout<<"| ";
cout<<gameBoard[i/2][j]<<" | ";
}
}
cout<<"\n";
}
return;
}
};
class Play : public Board
{
private :
int side;
int moveIndex,x,y;
int place;
int count,cnt;
public:
Play(int side) : Board(side)
{
this->side = side;
moveIndex = 1;
count=0;
cnt=0;
}
int turn = PLAYER1;
bool rowCrossed()
{
for(int i = 0; i < side; i++)
{
if(gameBoard[i][0]==gameBoard[i][1] && gameBoard[i][1]==gameBoard[i][2] && gameBoard[i][0] !=' ')
return true;
}
return false;
}
bool columnCrossed()
{
for(int i = 0; i < side; i++)
{
if(gameBoard[0][i]==gameBoard[1][i] && gameBoard[1][i]==gameBoard[2][i] && gameBoard[0][i] !=' ')
return true;
}
return false;
}
bool diagonalCrossed()
{
if(gameBoard[0][0]==gameBoard[1][1] && gameBoard[1][1]==gameBoard[2][2] && gameBoard[0][0] !=' ')
return true;
if(gameBoard[0][2]==gameBoard[1][1] && gameBoard[1][1]==gameBoard[2][0] && gameBoard[0][2] !=' ')
return true;
return false;
}
bool gameOver()
{
return (this->rowCrossed() || this->columnCrossed() || this->diagonalCrossed());
}
int getData(int turn)
{
int val;
if(turn==PLAYER1)cout<<"\t\tPlayer 1 Turn\n";
else
cout<<"\t\tPlayer 2 Turn\n";
cout<<"Enter the place where you want to mark : ";
cin>>val;
if(val>side*side)
{
cnt++;//if invalid attempts more then 3 return end game and
if(cnt<3)
{
cout<<"\t\t You Entered Invalid Place \n";
cout<<"\t\t Enter Again...\n";
return getData(turn);
}
else
{
cout<<"You have tried Invalid place for three times \n\n";
cout<<"You have to start the game again\n";
cout<<"Press ANY KEY for menu\n";
return -1;
}
}
cnt=0;
return val;
}
void winner(int turn)
{
showBoard();
if(turn == PLAYER1)
{
cout<<"\t\t\tCongratualtions Player 1 !!!\n\t\t\tYou have won the match\n\n";
}
else
{
cout<<"\t\t\tCongratualtions Player 2 !!!\n\t\t\tYou have won the match\n\n";
}
return;
}
void gameStart(int turn)
{
while(this->gameOver()==false && moveIndex != side*side)
{
if(turn == PLAYER1)
{
showBoard();
place = this->getData(turn);
if(place == -1)return;
x=(place-1)/side;
y=(place-1)%side;
if(gameBoard[x][y]!=' ')
{
count++;
if(count<3)
{
cout<<"This place is already marked\n";
cout<<"Try Again\n";
}
else
{
cout<<"You have tried 3 times in a wrong place\n\n";
cout<<"You have to again start the game\n";
cout<<"Press ANY KEY for menu\n";
return;
}
}
else
{
count=0;
gameBoard[x][y] = PLAYER1MOVE;
cout<<"Player 1 has put "<<PLAYER1MOVE<<" in a cell "<<place<<"\n";
moveIndex++;
turn = PLAYER2;
}
}
else if(turn == PLAYER2)
{
showBoard();
place = this->getData(turn);
x=(place-1)/side;
y=(place-1)%side;
if(gameBoard[x][y]!=' ')
{
count++;
if(count<3)
{
showBoard();
cout<<"This place is already marked\n";
cout<<"Try Again\n";
}
else
{
showBoard();
cout<<"You have tried 3 times in a wrong place\n\n";
cout<<"You have to again start the game\n";
cout<<"Press ENTER for menu\n";
return;
}
}
else
{
count=0;
gameBoard[x][y] = PLAYER2MOVE;
cout<<"Player 2 has put "<<PLAYER2MOVE<<" in a cell "<<place<<"\n";
moveIndex++;
turn = PLAYER1;
}
}
}
if(this->gameOver()==false && moveIndex == side*side)
{
showBoard();
cout<<"It's a draw match \n\n";
}
else
{
cout<<moveIndex<<endl;
if(turn==PLAYER1)turn=PLAYER2;
else if(turn == PLAYER2)turn = PLAYER1;
winner(turn);
}
cout<<"\n\n\t\t\tPress ENTER for menu\n";
return ;
}
};
class Game : public Play
{
private:
int side;
public :
Game(int side) : Play(side)
{
this->side=side;
}
void Menu()
{
int choice;
do
{
cout<<"\n\n";
cout<<"\t\t--||-- TIC-TAC-TOE -- ||-- "<<"\n\n\n";
cout<<"\t\t - - - - - MENU - - - - -\n\n\n";
cout<<"\t\t 1. INSTRUCTION\n\n";
cout<<"\t\t 2. PLAY\n\n";
cout<<"\t\t 3. BACK\n\n";
cout<<"\n\n\t\t Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
{
this->instruction();
getch();
getch();
}
break;
case 2:
gameStart(PLAYER1);
getch();
getch();
return;
break;
case 3:
return;
break;
default:
cout<<" \t---\tInvalid Choice\t---\t\n";
cout<<"\t--- Press ENTER for menu ---\n";
getch();
getch();
break;
}
}while(choice!=3);
}
void instruction()
{
cout<<"\n\n";
cout<<"\t\t\t\t Tic-Tac_Toe \n\n";
cout<<"\t\tChoose a cell no. from 1-"<<side*side<<" as below and play\n\n";
int p=1;
for(int i =0;i<=side*2;i++)
{
cout<<"\t\t\t\t ";
for(int j=0;j<side;j++)
{
if(i%2==0)
{
for(int k =0;k<side;k++)cout<<"----";
break;
}
else
{
if(j==0)cout<<"| ";
cout<<p<<" | ";
p++;
}
}
cout<<"\n";
}
cout<<"\n\n\t\t\tPress ENTER to continue. . . \n";
return;
}
};
int main()
{
int gridSize;
while(1)
{
cout<<"\n\n";
cout<<"\t\t\t\t\t\t\t Press 1 for EXIT"<<"\n\n\n";
cout<<"\t\t-- -- -- TIC-TAC-TOE -- -- -- "<<"\n\n\n";
cout<<" (Note : Grid Size should be Odd no. and greater than 2)\n\n";
cout<<"\n\nEnter the Grid Size : ";
cin>>gridSize;
if(gridSize==1 || gridSize%2==0)
{
cout<<"\n\n";
cout<<"\t\t-- -- -- TIC-TAC-TOE -- -- -- "<<"\n\n\n";
cout<<"\t\tThank you for your precious time.\n";
cout<<"--\t--\t--\t BYE\t --\t--\t--\t\n";
break;
}
Game* g1 = new Game(gridSize);
g1->Menu();
delete g1;
}
return 0;
}