-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTttBoard.cpp
More file actions
230 lines (153 loc) · 4.06 KB
/
TttBoard.cpp
File metadata and controls
230 lines (153 loc) · 4.06 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
#include <cstddef>
#include <stdexcept>
#include <iostream>
#include "TttBoard.h"
using namespace std;
namespace TicTacToe {
TttBoard::TttBoard(int dimension) : mDimension(dimension) {
mCells = new TttCell* [dimension];
for (int i = 0; i < dimension; ++i) {
mCells[i] = new TttCell[dimension];
}
}
TttBoard::~TttBoard() {
for (int i = 0; i < mDimension; ++i) {
delete [] mCells[i];
}
delete [] mCells;
mCells = NULL;
}
TttBoard::TttBoard(const TttBoard& cpy) {
}
TttBoard& TttBoard::operator=(const TttBoard& rhs) {
}
char TttBoard::getPeiceAt(int row, int column) const {
boundsCheck(row, column);
return mCells[row][column].getPeice();
}
void TttBoard::setPeiceAt(int row, int column, char peice) {
boundsCheck(row, column);
if (!isMoveLegal(row, column)) {
throw invalid_argument("There is already a peice in the specified cell");
}
mCells[row][column].setPeice(peice);
}
char TttBoard::declareWinner() const {
char winnerIs = isHorizonalWin();
winnerIs = winnerIs == ' ' ? isVerticalWin() : winnerIs;
winnerIs = winnerIs == ' ' ? isDiaganlWin() : winnerIs;
return winnerIs;
}
char TttBoard::isHorizonalWin() const {
for (int i = 0; i < mDimension; ++i) {
char atLeftOfBoard = mCells[i][0].getPeice();
if (atLeftOfBoard != ' ') {
int inARow = 1;
for (int j = 1; j < mDimension; ++j) {
//cout << i << " " << j << endl;
if (mCells[i][j].getPeice() == atLeftOfBoard) {
inARow++;
}
}
if (inARow == mDimension) {
return atLeftOfBoard;
}
}
}
return ' ';
}
char TttBoard::isVerticalWin() const {
for (int i = 0; i < mDimension; ++i) {
char atTopOfBoard = mCells[0][i].getPeice();
if (atTopOfBoard != ' ') {
int inARow = 1;
for (int j = 1; j < mDimension; ++j) {
if (mCells[j][i].getPeice() == atTopOfBoard) {
inARow++;
}
}
if (inARow == mDimension) {
return atTopOfBoard;
}
}
}
return ' ';
}
char TttBoard::isDiaganlWin() const {
char atTopLeft = mCells[0][0].getPeice();
if (atTopLeft != ' ') {
int inARow = 1;
for (int i = 1, j = 1; i < mDimension; i++, j++) {
if (mCells[i][j].getPeice() == atTopLeft) {
inARow++;
}
}
if (inARow == mDimension) {
return atTopLeft;
}
}
int d = mDimension - 1;
char atTopRight = mCells[0][d].getPeice();
if (atTopRight != ' ') {
int inARow = 1;
for (int i = 1, j = d - 1; i < mDimension; i++, j--) {
cout << i << " " << j << endl;
if (mCells[i][j].getPeice() == atTopRight) {
inARow++;
}
}
cout << inARow << endl;
if (inARow == mDimension) {
return atTopRight;
}
}
return ' ';
}
bool TttBoard::isBoardFull() const {
int numCellsFilled = 0;
for (int i = 0; i < mDimension; ++i) {
for (int j = 0; j < mDimension; ++j) {
if (mCells[i][j].getPeice() != ' ') {
++numCellsFilled;
}
}
}
return numCellsFilled == mDimension * mDimension;
}
bool TttBoard::isMoveLegal(int row, int column) const {
try {
boundsCheck(row, column);
}
catch (const out_of_range& e) {
return false;
}
return mCells[row][column].getPeice() == ' ';
}
char** TttBoard::boardRepresentation() const {
char** board = new char* [mDimension];
for (int i = 0; i < mDimension; i++) {
board[i] = new char [mDimension];
for (int j = 0; j < mDimension; j++) {
board[i][j] = mCells[i][j].getPeice();
}
}
return board;
}
void TttBoard::boundsCheck(int row, int column) const {
if (row >= mDimension || column >= mDimension) {
throw out_of_range("Out of Play area");
}
}
//oid isWin() {
// for each row
// for each column
// if the row index is 0 and the col index is 0
// todo figure out how to do this
// else if the row is 0 and the col is 1 less than the bounds
// todo figure out how to do this
// else if the row is 0 and the col is not on the boundry
// todo figure out how to do this
// else if the col is 0 and the row in not on the boundry
// todo figure out how to do this
//}
}