-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
404 lines (300 loc) · 11.5 KB
/
Game.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#include "Game.h"
#include "Board.h"
#include <stdio.h>
#include <iostream>
#include <cctype>
// Declaring a new board
Board* board;
// Empty constructor to set default values of attributes
Game::Game() {
}
// Getter for the current player
bool Game::getPlayer() {
return this->currentPlayer;
}
// Getter for the Y-axis (row) of the player's move
char Game::getChoiceRow() {
return this->userMove[0];
}
// Getter for the X-axis (column) of the player's move
char Game::getChoiceColumn() {
return this->userMove[1];
}
// Setter for the player's move updating 'userMove' attribute
void Game::setUserMove(char input[3]){
this->userMove[0] = input[0];
this->userMove[1] = input[1];
}
// Setter for the current player playing updating 'currentPlayer' attribute
void Game::setPlayer(bool newPlayer) {
this->currentPlayer = newPlayer;
}
int Game::requestSize() {
// Declaring a variable to store the size of the board.
int size;
// Requesting input from user
std::cout << "Enter board size (between 3 and 10): ";
std::cin >> size;
// If board size chosen is not between 3 and 10
if (!(size >= 3 && size <= 10)) {
do {
// Clear error flags
std::cin.clear();
// Emptying buffer
while (getchar() != '\n');
// Requesting user input again
printf("Enter a correct choice:\n");
std::cin >> size;
} while (!(size >= 3 && size <= 10)); // Loop until size is not between 3 and 10
}
// Emptying buffer
while (getchar() != '\n');
return size;
}
char Game::requestChoice(){
// Declaring a variable to store player 1's symbol.
char choice;
// Requesting input from user
printf("Player 1, select symbol: 'X' or 'O' \n");
std::cin >> choice;
// Changing the input to lowercase
choice = tolower(choice);
// If the symbol is not an 'x' or an 'o' (upper or lowercase)
if (!(choice == 'x' || choice == 'o')) {
do {
// Emptying buffer
while (getchar() != '\n');
// Requesting input from user
printf("Enter a correct choice:\n");
std::cin >> choice;
// Changing the input to lowercase
choice = tolower(choice);
// Keep looping until the symbol is not an 'x' or an 'o' (upper or lowercase)
} while (!(tolower(choice) == 'x' || tolower(choice) == 'o'));
}
// Emptying buffer
while (getchar() != '\n');
if (choice == 'x') {
return 1; // Returning 1 corresponding to cell type 1 ('X')
}else if(choice == 'o') {
return 2; // Returning 2 corresponding to cell type 2 ('O')
}
}
void Game::requestMove() {
// Creating a string to store a player's move
char move[3];
// Requesting input from user
printf("Player %d's move: ",this->getPlayer()+1);
std::cin.get(move, sizeof(move));
// Converting the alphabetic part from an ASCII value to its corresponding numeric index
move[0] = tolower(move[0]) - 'a';
// Converting the numeric part from an ASCII value to its corresponding numeric index
move[1] = move[1] - '0';
// If it is not true that: alphabetic part is between 0 and the board size, the numeric part is also between 0 and the board size and the cell is unfilled
if (!((move[0] >= 0 && move[0] < 0 + board->getSize()) && (move[1] >= 0 && move[1] < 0 + board->getSize()) && board->getEntry(move[0],move[1]).getType() == 0)) {
do {
// Clear error flags
std::cin.clear();
// Emptying buffer
while (getchar() != '\n');
// If the move chosen is incorrect
if (!((move[0] >= 0 && move[0] < 0 + board->getSize()) && (move[1] >= 0 && move[1] < 0 + board->getSize()))) {
printf("Enter a correct choice: ");
}else{
printf("Cell already filled. Try again: ");
}
// Requesting input from user
std::cin.get(move, sizeof(move));
// Converting the alphabetic part from an ASCII value to its corresponding numeric index
move[0] = tolower(move[0]) - 'a';
// Converting the numeric part from an ASCII value to its corresponding numeric index
move[1] = move[1] - '0';
// Keep looping while it is not true that: alphabetic part is between 0 and the board size, the numeric part is also between 0 and the board size and the cell is unfilled
} while (!((move[0] >= 0 && move[0] < 0 + board->getSize()) && (move[1] >= 0 && move[1] < 0 + board->getSize()) && board->getEntry(move[0], move[1]).getType() == 0));
}
// Emptying buffer
while (getchar() != '\n');
// Updating 'userMove' attribute with the player's coordinates
this->setUserMove(move);
}
void Game::updateGameState(int* currPlayerRows, int* currPlayerCols, int* currPlayerDiag, int* p2Rows, int* p2Cols, int* p2Diag) {
// If opponent hasn't already played in the row
if (*(p2Rows + this->getChoiceRow()) == 0) {
(*(currPlayerRows + this->getChoiceRow()))++; // Increment the current player's number of symbols in that row
}else{
// Set both players' number of symbols in that row to -1 (unwinnable row)
*(currPlayerRows + this->getChoiceRow()) = -1;
*(p2Rows + this->getChoiceRow()) = -1;
}
// If player has played on the leading diagonal
if (this->getChoiceRow() == this->getChoiceColumn()) {
// If opponent hasn't already played in it
if (*p2Diag == 0)
(*currPlayerDiag)++; // Increment the current player's number of symbols in the diagonal
else {
// Set both players' number of symbols in that diagonal to -1 (unwinnable diagonal)
*currPlayerDiag = -1;
*p2Diag = -1;
}
}
// If player has played on the antidiagonal
if (this->getChoiceRow() == board->getSize() - this->getChoiceColumn() - 1) {
// If opponent hasn't already played in it
if (*(p2Diag + 1) == 0)
(*(currPlayerDiag + 1))++; // Increment the current player's number of symbols in the diagonal
else {
// Set both players' number of symbols in that diagonal to -1 (unwinnable diagonal)
*(currPlayerDiag + 1) = -1;
*(p2Diag + 1) = -1;
}
}
// If opponent hasn't already played in the column
if (*(p2Cols + this->getChoiceColumn()) == 0) {
(*(currPlayerCols + this->getChoiceColumn()))++; // Increment the current player's number of symbols in that column
}else{
// Set both players' number of symbols in that column to -1 (unwinnable diagonal)
*(currPlayerCols + this->getChoiceColumn()) = -1;
*(p2Cols + this->getChoiceColumn()) = -1;
}
}
void Game::makeMove(int type, int* currPlayerRows, int* currPlayerCols, int* currPlayerDiag, int* p2Rows, int* p2Cols, int* p2Diag) {
// Updating the entry chosen by the player to the player's corresponding symbol
board->setEntry(this->getChoiceRow(), this->getChoiceColumn(), type);
// Updating the state of the game
this->updateGameState(currPlayerRows, currPlayerCols, currPlayerDiag, p2Rows, p2Cols, p2Diag);
// Displaying the updated board to the user
board->display();
}
int Game::checkWin(int* currPlayerRows, int* currPlayerCols, int* currPlayerDiag){
int isWon = 0; // Declaring a variable to keep track if a player has won or tied
int tie = 0; // Declaring a variable to keep track if game has tied
// If current player has the same number of symbols in either diagonal as the board size
if (*currPlayerDiag == board->getSize() || *(currPlayerDiag + 1) == board->getSize()) {
isWon = 1; // Updating isWon variable to represent that the game has been won
return isWon;
}
// Looping through the board length
for (int i = 0; i < board->getSize(); i++) {
// If current player has any row or any column with the same number of symbols as the board size
if (*(currPlayerRows + i) == board->getSize() || *(currPlayerCols + i) == board->getSize()) {
isWon = 1; // Updating isWon variable to represent that the game has been won
break;
}
// If current row is unwinnable (-1);
if (*(currPlayerRows + i) == -1) {
tie++;
}
// If current column is unwinnable (-1);
if (*(currPlayerCols + i) == -1) {
tie++;
}
// If tie counter is equal to number of rows + columns (meaning all columns and rows are unwinnable)
// and both diagonals are unwinnable
if (tie == board->getSize() * 2 && *currPlayerDiag == -1 && *(currPlayerDiag + 1) == -1) {
isWon = 2; // Updating isWon variable to represent that the game has tied
break;
}
}
return isWon;
}
int Game::run() {
std::cout << "\t\t\t**********************************************************\n";
std::cout << "\t\t\t* TIC TAC TOE *\n";
std::cout << "\t\t\t* *\n";
std::cout << "\t\t\t**********************************************************\n\n\n\n";
// Initialising a board to the size specified by the user
board = new Board(requestSize());
int* p1Rows = (int*)malloc(sizeof(int) * board->getSize()); // Allocating memory for an array counting player 1's symbol in each row
int* p1Cols = (int*)malloc(sizeof(int) * board->getSize()); // Allocating memory for an array counting player 1's symbol in each column
int* p2Rows = (int*)malloc(sizeof(int) * board->getSize()); // Allocating memory for an array counting player 2's symbol in each row
int* p2Cols = (int*)malloc(sizeof(int) * board->getSize()); // Allocating memory for an array counting player 2's symbol in each column
// If memory allocation fails
if (p1Rows == NULL || p1Cols == NULL || p2Rows == NULL || p2Cols == NULL)
{
// Freeing all allocated memory for the board
board->~Board();
free(p1Rows);
free(p1Cols);
free(p2Rows);
free(p2Cols);
return EXIT_FAILURE;
}
// Initialising all arrays to 0
for (int i = 0; i < board->getSize(); i++) {
*(p1Rows + i) = 0;
*(p1Cols + i) = 0;
*(p2Rows + i) = 0;
*(p2Cols + i) = 0;
}
int diagonalPlayer1[2] = { 0,0 }; // Creating an array to count player 1's symbols in each diagonal
int diagonalPlayer2[2] = { 0,0 }; // Creating an array to count player 2's symbols in each diagonal
// Obtaining player 1's symbol
char player1Symbol = requestChoice();
// Creating a variable to set player 2's symbol accordingly
char player2Symbol = 0;
if (player1Symbol == 1) {
player2Symbol = 2;
}else if(player1Symbol == 2) {
player2Symbol = 1;
}
// Displaying the board to the user for the first time
board->display();
do{
// Setting player 1 as current player
this->setPlayer(0);
// Requesting a move
this->requestMove();
// Applying the move with player 1's symbol
this->makeMove(player1Symbol, p1Rows, p1Cols, diagonalPlayer1, p2Rows, p2Cols, diagonalPlayer2);
// Checking if player 1 has won or game is tied
int win = this->checkWin(p1Rows, p1Cols, diagonalPlayer1);
if (win == 1) {
printf("Player 1 has won!");
// Freeing all allocated memory for the board
board->~Board();
free(p1Rows);
free(p1Cols);
free(p2Rows);
free(p2Cols);
return EXIT_SUCCESS;
}
else if (win == 2) {
printf("It's a tie!");
// Freeing all allocated memory for the board
board->~Board();
free(p1Rows);
free(p1Cols);
free(p2Rows);
free(p2Cols);
return EXIT_SUCCESS;
}
// Setting player 2 as current player
this->setPlayer(1);
// Requesting a move
this->requestMove();
// Applying the move with player 2's symbol
this->makeMove(player2Symbol, p2Rows, p2Cols, diagonalPlayer2, p1Rows, p1Cols, diagonalPlayer1); //change 7
// Checking if player 2 has won or game is tied
win = this->checkWin(p2Rows, p2Cols, diagonalPlayer2);
if (win == 1) {
printf("Player 2 has won!");
// Freeing all allocated memory for the board
board->~Board();
free(p1Rows);
free(p1Cols);
free(p2Rows);
free(p2Cols);
return EXIT_SUCCESS;
}
else if (win == 2) {
printf("It's a tie!");
// Freeing all allocated memory for the board
board->~Board();
free(p1Rows);
free(p1Cols);
free(p2Rows);
free(p2Cols);
return EXIT_SUCCESS;
}
} while (true); // Infinite loop until an if statement breaks the loop
}