-
Notifications
You must be signed in to change notification settings - Fork 0
/
board.cpp
235 lines (201 loc) · 5.83 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
/*********************************************************************
** Program name: board.cpp
** Author: Jesse McKenna
** Date: 12/5/2017
** Description: Manages the creation and printing of a game board consisting of
** linked spaces, each of which contains pointers to its adjacent spaces
*********************************************************************/
#include "board.hpp"
#include "space.hpp"
#include "emptySpace.hpp"
#include "exitSpace.hpp"
#include "holeSpace.hpp"
#include "wallSpace.hpp"
#include "symbols.hpp" // constant chars for board symbols
#include <iostream>
#include <fstream>
#include <string>
// --- Constructor ---
Board::Board()
{
readMap();
origin = createSpace(map.at(0));
// Create board rows as linked structure
Space* current = origin;
Space* next;
// Create first row, left to right
for (int j = 1; j < boardCols; j++)
{
next = createSpace(map.at(j)); // create new space based on map textfile
current->setAdjacent(Space::RIGHT, next); // link to existing spaces
next->setAdjacent(Space::LEFT, current);
current = next; // move to next space
}
// Create subsequent rows, left to right, top to bottom
Space* rowStart = origin;
Space* above;
for (int i = 1; i < boardRows; i++)
{
// Create new row
next = createSpace(map.at(i * boardCols));
rowStart->setAdjacent(Space::DOWN, next); // link to previous row
next->setAdjacent(Space::UP, rowStart);
// Move to new row
above = rowStart; // set above to start of previous row
rowStart = next; // move to start of new row
current = rowStart; // set current to start of new row
// Create spaces in new row
for (int j = 1; j < boardCols; j++)
{
next = createSpace(map.at(i * boardCols + j)); // create new space
above = above->getAdjacent(Space::RIGHT); // set above to space above next
next->setAdjacent(Space::UP, above); // link to existing spaces
above->setAdjacent(Space::DOWN, next);
next->setAdjacent(Space::LEFT, current);
current->setAdjacent(Space::RIGHT, next);
current = next; // move to next space
}
}
// Put player at space below and to the right of the origin, i.e. [1,1]
playerSpace = origin->getAdjacent(Space::DOWN)->getAdjacent(Space::RIGHT);
playerSpace->setPlayer(true);
}
// --- Destructor ---
Board::~Board()
{
Space* rowStart = origin;
Space* current;
Space* garbage;
for (int i = 0; i < boardRows; i++)
{
current = rowStart;
rowStart = rowStart->getAdjacent(Space::DOWN); // prepare for next loop
for (int j = 0; j < boardCols; j++)
{
// Select node to be deleted
garbage = current;
current = current->getAdjacent(Space::RIGHT); // prepare for next loop
// Delete node
garbage->setAdjacent(Space::UP, nullptr);
garbage->setAdjacent(Space::DOWN, nullptr);
garbage->setAdjacent(Space::LEFT, nullptr);
garbage->setAdjacent(Space::DOWN, nullptr);
delete garbage;
}
}
}
// --- getPlayerSpace ---
// Return pointer to player's current location
Space* Board::getPlayerSpace()
{
return playerSpace;
}
// --- print ---
// Prints the board
void Board::print()
{
std::cout << "\n";
Space* rowStart = origin;
Space* current;
for (int i = 0; i < boardRows; i++)
{
current = rowStart;
rowStart = rowStart->getAdjacent(Space::DOWN); // prepare to print next row
for (int j = 0; j < boardCols; j++)
{
std::cout << current->getSymbol() << " ";
current = current->getAdjacent(Space::RIGHT); // move right to next space
}
std::cout << "\n";
}
}
// --- playerMove ---
// Moves player in given direction; returns true if player was moved
bool Board::playerMove(char direction)
{
Space* target;
if (direction == 'W' || direction == 'w') // up
{
target = playerSpace->getAdjacent(Space::UP);
}
else if (direction == 'A' || direction == 'a') // left
{
target = playerSpace->getAdjacent(Space::LEFT);
}
else if (direction == 'S' || direction == 's') // down
{
target = playerSpace->getAdjacent(Space::DOWN);
}
else if (direction == 'D' || direction == 'd') // right
{
target = playerSpace->getAdjacent(Space::RIGHT);
}
else
{
return false; // invalid input
}
if (target != nullptr && target->isWalkable()) // if target is valid
{
playerSpace->setPlayer(false); // remove player from old location
playerSpace = target;
target->setPlayer(true); // assign player to new location
return true;
}
return false; // return false if target was nullptr or not walkable
}
// --- readMap ---
// Gets the map data from .txt file to prepare for board creation
void Board::readMap()
{
std::ifstream mapFile;
mapFile.open(mapFilename, std::ios::in);
if (!mapFile)
{
map = "######## @ ^## ^# ##P# ###^# @@###^@@E########"; // default map
boardRows = 7;
boardCols = 7;
return;
}
boardRows = 0;
boardCols = 0;
std::string line = "";
getline(mapFile, line); // read first line from mapFile
boardCols = line.length(); // number of columns = length of first line
map += line;
boardRows++;
while (getline(mapFile, line)) // read each subsequent line from mapFile
{
map += line;
boardRows++; // number of rows = number of lines read in from file
}
mapFile.close();
}
// --- createSpace ---
// Accepts a char read from the 'map' in readMap, then allocates and returns a
// pointer to a Space of the type that corresponds to the given char symbol
Space* Board::createSpace(char type)
{
Space* newSpace;
if (type == WALL)
{
newSpace = new WallSpace;
}
else if (type == HOLE)
{
newSpace = new HoleSpace;
}
else if (type == EXIT)
{
newSpace = new ExitSpace;
}
else if (type == ROCK || type == PICK)
{
newSpace = new EmptySpace;
newSpace->setSymbol(type);
}
else
{
newSpace = new EmptySpace;
}
return newSpace;
}