-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSingleBoard.cpp
322 lines (269 loc) · 7.24 KB
/
SingleBoard.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
//
// SingleBoard.cpp
// Ultimate Tic Tac Toe
//
// Created by Anuj Parakh on 4/15/18.
// Copyright © 2018 Anuj Parakh. All rights reserved.
//
#include <iostream>
#include "SingleBoard.hpp"
using namespace std;
std::string convertPlayerToString (Player player)
{
if (player == Player::Ax)
{
return "X";
}
else if (player == Player::Oh)
{
return "O";
}
else if (player == Player::No)
{
return "-";
}
return " ";
}
// Constructor for Single Board
SingleBoard::SingleBoard ()
{
// Set all the players as No (Empty)
for (int i = 0; i < MAX_ROWS; ++i)
{
for (int j = 0; j < MAX_COLS; ++j)
{
board [i] [j] = Player::No;
}
}
}
// Function to check if player exists at given row and colum
bool SingleBoard::playerExistsAt(int row, int col)
{
// if out of range, return false
if (row >= MAX_ROWS || col >= MAX_COLS)
return false;
// otherwise just return true
return true;
}
// Function to get player at given row and column
Player SingleBoard::getPlayerAt(int row, int col)
{
return board [row] [col];
}
// Function to set player at given row and column
bool SingleBoard::setPlayerAt(int row, int col, Player player)
{
// if player doesnt exist at row and col return false
if (!playerExistsAt(row, col))
{
return false;
}
// if position isn't empty return false
if (board [row] [col] != Player::No)
{
return false;
}
// set the new player at that position
board [row] [col] = player;
// return true (successful)
return true;
}
// Function to check if the board is full
bool SingleBoard::isFull ()
{
// Go through the board
for (int i = 0; i < MAX_ROWS; ++i)
{
for (int j = 0; j < MAX_COLS; ++j)
{
// If position is empty return false (not full)
if (board [i] [j] == Player::No)
{
return false;
}
}
}
// Return true (means board is full)
return true;
}
// Function to check if the board is empty
bool SingleBoard::isEmpty()
{
// Go through the board
for (int i = 0; i < MAX_ROWS; ++i)
{
for (int j = 0; j < MAX_COLS; ++j)
{
// If position is full return false (not empty)
if (board [i] [j] != Player::No)
{
return false;
}
}
}
// Return true (means board is empty)
return true;
}
// Function to print the board to cout
void SingleBoard::printBoard()
{
// Go through the board
for (int i = 0; i < MAX_ROWS; ++i)
{
for (int j = 0; j < MAX_COLS; ++j)
{
cout << convertPlayerToString (board [i] [j]) << " ";
}
cout << endl;
}
}
Player SingleBoard::checkDiagonalWinner ()
{
// go through all the players
// Assume both \ and / diagonals have a winner
bool foundWinnerOne = true;
bool foundWinnerTwo = true;
for (int rowIndex = 0; rowIndex < MAX_ROWS; ++rowIndex)
{
for (int colIndex = 0; colIndex < MAX_COLS; ++colIndex)
{
// for '\' diagonal
if (rowIndex == colIndex)
{
if (board [rowIndex] [colIndex] == Player::No)
{
foundWinnerOne = false;
}
if (board [rowIndex] [colIndex] != board [0] [0])
{
foundWinnerOne = false;
}
}
// for '/' diagonal
if ((rowIndex + colIndex) == (MAX_ROWS - 1))
{
if (board [rowIndex] [colIndex] == Player::No)
{
foundWinnerTwo = false;
}
if (board [rowIndex] [colIndex] != board [0] [MAX_ROWS - 1])
{
foundWinnerTwo = false;
}
}
}
}
// found winner for diagonal '\'
if (foundWinnerOne)
{
return board [0] [0];
}
// found winner for diagonal '\'
if (foundWinnerTwo)
{
return board [0] [MAX_ROWS - 1];
}
// if no winner return empty player
return Player::No;
}
Player SingleBoard::checkRowWinner()
{
// Go through all the rows
for (int rowIndex = 0; rowIndex < MAX_ROWS; ++rowIndex)
{
// Assume this row has a winner
bool foundWinner = true;
// Go through all the columns
for (int colIndex = 0; colIndex < MAX_COLS; ++colIndex)
{
// If found empty, no winner in that column
if (board [rowIndex] [colIndex] == Player::No)
{
foundWinner = false;
break;
}
// If not same as the first row, no one wins that row
if (board [rowIndex] [colIndex] != board [rowIndex] [0])
{
foundWinner = false;
break;
}
}
// If found a winner
if (foundWinner)
{
// return the player in the first column of the current row
return board [rowIndex] [0];
}
}
// No winner :(
return Player::No;
}
Player SingleBoard::checkColWinner()
{
// Go through all the rows
for (int colIndex = 0; colIndex < MAX_ROWS; ++colIndex)
{
// Assume this row has a winner
bool foundWinner = true;
// Go through all the columns
for (int rowIndex = 0; rowIndex < MAX_COLS; ++rowIndex)
{
// If found empty, no winner in that column
if (board [rowIndex] [colIndex] == Player::No)
{
foundWinner = false;
break;
}
// If not same as the first row, no one wins that row
if (board [rowIndex] [colIndex] != board [0] [colIndex])
{
foundWinner = false;
break;
}
}
// If found a winner
if (foundWinner)
{
// return the player in the first column of the current row
return board [0] [colIndex];
}
}
// No winner :(
return Player::No;
}
// Function to get the winner of the board
Player SingleBoard::getWinner()
{
// Find the winner in the row
Player rowWinner = checkRowWinner();
// Check if there actually is a winner, then return
if (rowWinner != Player::No)
{
return rowWinner;
}
// Find the winner in the col
Player colWinner = checkColWinner ();
// Check if there actually is a winner, then return
if (colWinner != Player::No)
{
return colWinner;
}
// Find the winner in the diagonals
Player diagWinner = checkDiagonalWinner();
// Check if there actually is a winner, then return
if (diagWinner != Player::No)
{
return diagWinner;
}
// If board is full, no winner
if (isFull())
{
return Player::No;
}
// otherwise no winner yet (DNE)
else
{
return Player::DNE;
}
}