-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.hpp
211 lines (190 loc) · 5.82 KB
/
board.hpp
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
#ifndef PROJECT_BOARD_HPP
#define PROJECT_BOARD_HPP
/*/////////////////////////////////////////////////////////////////////////////
//
// FILENAME: board.hpp
//
// DESCRIPTION:
// Provides template for board of tic tac toe
// Two primary templates are defined
// class TTTBoard<T>
// class TTTBoard<TTTBoard<T>>
// Data is stored in vector
//
// PUBILC FUNCTIONS:
// TTTBoard<T>:
// TTTBoard();
// TTTBoard(T init);
// T getPos(int x, int y);
// void setPos(int x, int y, T val);
// bool checkWin(T player);
// void printBoard();
//
// TTTBoard<TTTBoard<T>>:
// TTTBoard(T init);
// TTTBoard<T> getPos(int x, int y);
// void setPos(int x, int y, TTTBoard<T> val);
// bool checkWin();
// void printBoard();
//
// NOTES:
//
/////////////////////////////////////////////////////////////////////////////*/
#include <iostream>
#include "except.hpp"
using std::endl;
using std::cout;
template <typename T>
class TTTBoard {
private:
bool won;
T info[3][3];
void checkPos(int x, int y) {
if (x < 0 || x > 2)
throw BadCordsExcept();
if (y < 0 || y > 2)
throw BadCordsExcept();
}
public:
TTTBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
info[i][j] = (T)NULL;
}
}
won = false;
}
TTTBoard(T init) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
info[i][j] = init;
}
}
won = false;
}
const T getPos(const int x, const int y) const {
checkPos(x, y);
return info[x][y];
}
void setPos(const int x, const int y, const T val) {
checkPos(x, y);
if (won)
throw BoardWonExcept();
info[x][y] = val;
}
const bool checkWin(const T player) {
if(won) {
return true;
}
if ((info[0][0] == player) && (info[1][0] == player) && (info[2][0] == player)) {
won = true;
return true;
} else if ((info[0][1] == player) && (info[1][1] == player) && (info[2][1] == player)) {
won = true;
return true;
} else if ((info[0][2] == player) && (info[1][2] == player) && (info[2][2] == player)) {
won = true;
return true;
} else if ((info[0][0] == player) && (info[0][1] == player) && (info[0][2] == player)) {
won = true;
return true;
} else if ((info[1][0] == player) && (info[1][1] == player) && (info[1][2] == player)) {
won = true;
return true;
} else if ((info[2][0] == player) && (info[2][1] == player) && (info[2][2] == player)) {
won = true;
return true;
} else if ((info[0][0] == player) && (info[1][1] == player) && (info[2][2] == player)) {
won = true;
return true;
} else if ((info[0][2] == player) && (info[1][1] == player) && (info[2][0] == player)) {
won = true;
return true;
} else
return false;
}
void printBoard() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << info[i][j];
if(j < 2)
cout << " ";
}
cout << endl;
}
}
void printLine(const int line) const {
if ((line < 0) || (line > 2))
throw BadCordsExcept();
for (int i = 0; i < 3; i++) {
cout << info[line][i];
if (i < 2)
cout << " ";
}
}
};
/*
// Board implementation for Upper Level Board
*/
template <typename T>
class TTTBoard<TTTBoard<T>> {
private:
TTTBoard<T> info[3][3];
void checkPos(const int x, const int y) {
if (x < 0 || x > 2)
throw BadCordsExcept();
if (y < 0 || y > 2)
throw BadCordsExcept();
}
public:
TTTBoard(const T init) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
info[i][j] = TTTBoard<T>(init);
}
}
}
TTTBoard<T>& getPos(const int x, const int y) {
checkPos(x, y);
return info[x][y];
}
void setPos(const int x, const int y, const TTTBoard<T> val) {
checkPos(x, y);
info[x][y] = val;
}
const bool checkWin(const T player) {
if (info[0][0].checkWin(player) && info[1][0].checkWin(player) && info[2][0].checkWin(player)) {
return true;
} else if (info[0][1].checkWin(player) && info[1][1].checkWin(player) && info[2][1].checkWin(player)) {
return true;
} else if (info[0][2].checkWin(player) && info[1][2].checkWin(player) && info[2][2].checkWin(player)) {
return true;
} else if (info[0][0].checkWin(player) && info[0][1].checkWin(player) && info[0][2].checkWin(player)) {
return true;
} else if (info[1][0].checkWin(player) && info[1][1].checkWin(player) && info[1][2].checkWin(player)) {
return true;
} else if (info[2][0].checkWin(player) && info[2][1].checkWin(player) && info[2][2].checkWin(player)) {
return true;
} else if (info[0][0].checkWin(player) && info[1][1].checkWin(player) && info[2][2].checkWin(player)) {
return true;
} else if (info[0][2].checkWin(player) && info[1][1].checkWin(player) && info[2][0].checkWin(player)) {
return true;
} else
return false;
}
void printBoard() {
for (int i = 0; i < 3; i++) { // Row
for (int j = 0; j < 3; j++) { // Line
for (int k = 0; k < 3; k++) { // Column
info[i][k].printLine(j);
if (k < 2)
cout << " ";
}
cout << endl;
}
if (i < 2)
cout << endl;
}
}
};
#endif