-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.cpp
303 lines (207 loc) · 6.39 KB
/
Board.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
#include "Board.h"
/*----------------------------------------------------------------------------------------------*/
void Board:: ClearLegendArea() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 20; j++) {
board[i + legendPos.getY()][j + legendPos.getX()] = ' ';
}
}
}
/*----------------------------------------------------------------------------------------------*/
Board::Board(string fileName) {
bool only1Pacman = false;
bool moreThan1Pacman = false;
// Ghost vector.size
bool only1Legend = false;
bool moreThan1Legend = false;
// ilegal tunnels -> do not check
// a piece missing in the board -> function to verify that the board is a fixed shape.
// ilegal board, continue to next board + proper message
// ilegal board, with a fixable problem, do you wish to continue to play? bugs could be found
// ilegal legend pos
ifstream myFile(fileName);
string line;
int i = 0;
int j = 0;
while (getline(myFile, line)) {
j = 0;
for (auto c : line) {
if (c == ' ') {
c = FOOD;
maxFood++;
}
else if (c == '%') {
c = ' ';
}
else if (c == '@') {
if (only1Pacman) {
moreThan1Pacman = true;
}
else {
only1Pacman = true;
}
if (moreThan1Pacman) { // illegal board, more then 1 Pacman
throw PacmanError();
}
else {
playerStartPos = GameObject(j, i);
c = ' ';
}
}
else if (c == '$') {
ghostsStartPos.push_back(GameObject(j, i));
c = ' ';
}
else if (c == '&') { // LEGENDDDDD
if (only1Legend) {
moreThan1Legend = true;
}
else {
only1Legend = true;
}
if (moreThan1Legend) { // illegal board, more then 1 Legend
throw LegendError();
}
else {
legendPos = GameObject(j, i);
c = ' ';
ClearLegendArea();
}
}
// else , c == '#' => Do nothing
board[i][j] = c;
j++;
}
BoardCols = std::max((int)line.length(), BoardCols);
// cout << line <<endl;
i++;
}
// Legal Board Checks:
if (!only1Pacman) {
throw Pacman0Error();
}
if (!only1Legend) {
throw Legend0Error();
}
if (ghostsStartPos.size() > 4) {
throw GhostError();
}
// Tunnel / Board Checks??
// Board was Legal, continue KENNYYY
BoardRows = i;
myFile.close();
//This loops over the board and sends every ' ' character to be checked if
//it's actually a secret tunnel, if it is push into vector of SecretPathWays
for (int i = 0; i < BoardRows; i++)
{
for (int j = 0; j < BoardCols; j++)
{
if (board[i][j] == ' ' || board[i][j] == FOOD || board[i][j]== '%')
{
if (checkBlankSpaceForTunnel(i, j))
{
this->SecretPathWays.push_back(GameObject(j, i));
}
}
}
}
//This loops over the secret pathways and removes the food from them
//also reduces the maxfood as needed
for (int i = 0; i < SecretPathWays.size(); i++)
{
if (board[SecretPathWays[i].getY()][SecretPathWays[i].getX()] == FOOD)
{
board[SecretPathWays[i].getY()][SecretPathWays[i].getX()] = ' ';
maxFood--;
}
}
}
/*----------------------------------------------------------------------------------------------*/
char Board::getChar(int i, int j) {
return board[i][j];
}
/*----------------------------------------------------------------------------------------------*/
bool Board::checkBlankSpaceForTunnel(int i, int j) {
if (j == 0 && i == 0)
{
return false; // no situation where _# is a tunnel
} // #
if ( (board[i][j + 1] == '#' && board[i][j - 1] == '#')) { // if situation like #_#
if (i == 0 || i == BoardRows - 1) // if it's the first line or last line and no legend interruption
{
return true;
}
if (board[i - 1][j] == '\0' || board[i + 1][j] == '\0') { //if situation like #_# and the above is blank
return true;
}
}
else if ((i > 0 && i < terminal_Size_Y - 1) && (board[i + 1][j] == '#' || board[i - 1][j] == '#')) {
if (j == 0 || i == BoardRows - 1) // if it's the first line or last line and no legend interruption
{
return true;
}
if (board[i][j - 1] == '\0' || board[i][j + 1] == '\0') { //if situation like #_# and the above is blank
return true;
}
}
return false;
}
/*----------------------------------------------------------------------------------------------*/
void Board::printBoard() {
for (int row = 0; row < BoardRows; row++) {
for (int col = 0; col < BoardCols; col++) {
cout << board[row][col];
}
cout << endl;
}
}
/*----------------------------------------------------------------------------------------------*/
// GETTERS:
/*----------------------------------------------------------------------------------------------*/
char** Board:: getBoard() {
return (char**)board;
}
/*----------------------------------------------------------------------------------------------*/
int Board:: getBoardRows() {
return BoardRows;
}
/*----------------------------------------------------------------------------------------------*/
int Board:: getBoardCols() {
return BoardCols;
}
/*----------------------------------------------------------------------------------------------*/
int Board:: getGhostNum() {
return ghostsStartPos.size();
}
/*----------------------------------------------------------------------------------------------*/
GameObject Board::getLegendPos() {
GameObject ret = legendPos;
return ret;
}
/*----------------------------------------------------------------------------------------------*/
vector<GameObject> Board::getGhostsStartPos() {
vector <GameObject> ret = ghostsStartPos;
return ret;
}
/*----------------------------------------------------------------------------------------------*/
GameObject Board:: getPlayerStartPos() {
GameObject ret = playerStartPos;
return ret;
}
/*----------------------------------------------------------------------------------------------*/
vector<GameObject> Board:: getSecretPathWays() {
vector <GameObject> ret = SecretPathWays;
return ret;
}
/*----------------------------------------------------------------------------------------------*/
int Board::getMaxFood()
{
return maxFood;
}
/*----------------------------------------------------------------------------------------------*/
// SETTER:
/*----------------------------------------------------------------------------------------------*/
void Board::setBoard(GameObject pos, char ch) {
board[pos.getY()][pos.getX()] = ch;
}
/*----------------------------------------------------------------------------------------------*/