This repository was archived by the owner on Jul 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicGame.cpp
More file actions
229 lines (207 loc) · 5.09 KB
/
Copy pathTicGame.cpp
File metadata and controls
229 lines (207 loc) · 5.09 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
#include "TicGame.h"
#include "stdafx.h"
constexpr int BOARD_SIZE = 3;
constexpr char BOARD_DEFAULT_CHAR = '_';
// Initialise all data members
TicGame::TicGame()
{
// Set board to NULL
for (int i = 0; i < BOARD_SIZE; ++i)
{
for (int j = 0; j < BOARD_SIZE; ++j)
{
Game[i][j] = BOARD_DEFAULT_CHAR;
}
}
InputCoords.Row = 0;
InputCoords.Column = 0;
ScoreA = ScoreB = 0;
PlayerTurn = 'X';
}
// Get functions
int TicGame::GetScoreA() const { return ScoreA; }
int TicGame::GetScoreB() const { return ScoreB; }
char TicGame::GetPlayerTurn() const { return PlayerTurn; }
bool TicGame::bIsGameOver() const { return GameOver; }
// Returns TRUE on valid input
bool TicGame::PlayTurn()
{
// Input Turn string
char InputValue[5];
cout << endl << PlayerTurn << "'s turn, ";
cout << "make your move: "; //TODO: Dear lord, learn English!
fgets(InputValue, 5, stdin);
SplitInput(InputValue);
switch (ValidatedInput(InputValue))
{
case EInputErrorType::BadBoth:
case EInputErrorType::BadColumn:
case EInputErrorType::BadRow:
case EInputErrorType::TooLong:
cout << "Input should be of format <Row><Column> eg. \"A3\"";
return false;
case EInputErrorType::Occupied:
cout << "Cell is occupied.";
return false;
case EInputErrorType::OK:
cout << "Valid Input.\n";
break;
}
return true;
}
// Splits input into InputCoords
void TicGame::SplitInput(char *InputValue)
{
InputCoords.Row = (int)(toupper(*InputValue) - 'A') + 1;
InputValue++;
InputCoords.Column = atoi(InputValue);
}
// Adds entry to Board
void TicGame::AddTurn()
{
Game[InputCoords.Row][InputCoords.Column] = PlayerTurn;
if (EndGame() == EEndGameType::False)
SwapPlayer();
else
{
GameOver = true;
cout << PlayerTurn << " wins!\n"; // TODO: Improve with subroutine
}
}
// Checks win conditions
EEndGameType TicGame::EndGame()
{
/*
int StreakCount = 1;
// Check row-wise
for (int i = 0; i < BOARD_SIZE; ++i)
{
char temp = Game[i][0];
StreakCount = 1;
for (int j = 0; j < BOARD_SIZE; ++j)
{
if (Game[i][j] == temp && j != 0)
{
StreakCount++;
}
}
if (StreakCount == 3)
return EEndGameType::RowWin;
else
StreakCount = 0;
}
// Check column-wise
for (int i = 0; i < BOARD_SIZE; ++i)
{
char temp = Game[0][i];
StreakCount = 1;
for (int j = 0; j < BOARD_SIZE; ++j)
{
if (Game[j][i] == temp && j != 0)
{
StreakCount++;
}
}
}
if (StreakCount == 3)
return EEndGameType::ColumnWin;
else
StreakCount = 0;
// Check primary diagonal
char temp = Game[1][1];
StreakCount = 1;
for (int i = 0; i < BOARD_SIZE; ++i)
{
if (Game[i][i] == temp && i != 1)
StreakCount++;
}
if (StreakCount == 3)
return EEndGameType::DiagonalWin;
else
StreakCount = 0;
// Check secondary diagonal
StreakCount = 1;
for (int i = 0; i < BOARD_SIZE; ++i)
{
if (Game[i][BOARD_SIZE - i-1] == temp && i != 1)
StreakCount++;
}
if (StreakCount == 3)
return EEndGameType::DiagonalWin;
else
StreakCount = 0;
*/
if (Game[0][0] == Game[0][1] && Game[0][0] == Game[0][2]) // Row 1
return EEndGameType::RowWin;
if (Game[1][0] == Game[1][1] && Game[1][0] == Game[1][2]) // Row 2
return EEndGameType::RowWin;
if (Game[2][0] == Game[2][1] && Game[2][0] == Game[2][2]) // Row 3
return EEndGameType::RowWin;
if (Game[0][0] == Game[1][0] && Game[0][0] == Game[2][0]) // Column 1
return EEndGameType::ColumnWin;
if (Game[0][1] == Game[1][1] && Game[0][1] == Game[2][1]) // Column 2
return EEndGameType::ColumnWin;
if (Game[0][2] == Game[1][2] && Game[0][2] == Game[2][2]) // Column 3
return EEndGameType::ColumnWin;
if (Game[0][0] == Game[1][1] && Game[0][0] == Game[2][2]) // Primary Diagonal
return EEndGameType::DiagonalWin;
if (Game[0][2] == Game[1][1] && Game[0][2] == Game[2][0]) // Secondary Diagonal
return EEndGameType::DiagonalWin;
return EEndGameType::False;
}
// Returns Error state of InputCoords
EInputErrorType TicGame::ValidatedInput(char InputValue[])
{
if(strlen(InputValue) > 3)
return EInputErrorType::TooLong;
if (InputCoords.Row < 1 || InputCoords.Row > BOARD_SIZE)
{
if (InputCoords.Column < 1 || InputCoords.Column > BOARD_SIZE)
{
return EInputErrorType::BadBoth;
}
else
{
return EInputErrorType::BadRow;
}
}
else
{
if (InputCoords.Column < 1 || InputCoords.Column > BOARD_SIZE)
{
return EInputErrorType::BadColumn;
}
}
// Match array indices
InputCoords.Column--;
InputCoords.Row--;
// Checks array for unoccupied cell
if (Game[InputCoords.Row][InputCoords.Column] != BOARD_DEFAULT_CHAR)
{
return EInputErrorType::Occupied;
}
return EInputErrorType::OK;
}
// Prints the current game state
void TicGame::DisplayBoard() const
{
int i = 0, j = 0;
printf(" A B C \n");
printf(" ___________ \n");
printf(" 1 | %c | %c | %c | \n", Game[0][0], Game[0][1], Game[0][2]);
printf(" |___|___|___| \n");
printf(" 2 | %c | %c | %c | \n", Game[1][0], Game[1][1], Game[1][2]);
printf(" |___|___|___| \n");
printf(" 3 | %c | %c | %c | \n", Game[2][0], Game[2][1], Game[2][2]);
printf(" |___|___|___| \n");
}
void TicGame::SwapPlayer()
{
if (PlayerTurn == 'X')
PlayerTurn = 'O';
else
PlayerTurn = 'X';
}
TicGame::~TicGame()
{
}