-
Notifications
You must be signed in to change notification settings - Fork 0
/
Map.cpp
273 lines (235 loc) · 6.92 KB
/
Map.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
#include "Map.h"
using namespace std;
Map::Map()
{
resetMap();
}
// resets player position, count values, and initializes values in position arrays to -1
void Map::resetMap() {
playerPosition[0] = 0;
playerPosition[1] = 0;
bestBuyPosition[0] = -1;
bestBuyPosition[1] = -1;
npc_count = 0;
hacker_count = 0;
best_buy_on_map = false;
for (int i = 0; i < num_npcs; i++) {
npcPositions[i][0] = -1;
npcPositions[i][1] = -1;
}
for (int i = 0; i < num_hackers; i++) {
hackerPositions[i][0] = -1;
hackerPositions[i][1] = -1;
}
for (int i = 0; i < num_rows; i++) {
for (int j = 0; j < num_cols; j++) {
mapData[i][j] = '-';
}
}
}
// return player's row position
int Map::getPlayerRowPosition() {
return playerPosition[0];
}
// return player's column position
int Map::getPlayerColPosition() {
return playerPosition[1];
}
int Map::getNPCCount() {
return npc_count;
}
int Map::getHackerCount() {
return hacker_count;
}
// set player's row position to parameter row
void Map::setPlayerRowPosition(int row) {
playerPosition[0] = row;
}
// set player's column position to parameter row
void Map::setPlayerColPosition(int col) {
playerPosition[1] = col;
}
void Map::setNPCCount(int count) {
npc_count = count;
}
void Map::setHackerCount(int count) {
hacker_count = count;
}
/* add Hacker to map
* Parameters: where to spawn Hacker -- row (int), col (int)
* Return: (bool) false if no more space in hackerPositions array
* or if (row, col) is an invalid position
* or if (row, col) is already populated; else true
*/
bool Map::spawnHacker(int row, int col) {
// out of map bounds
if (!(row >= 0 && row < num_rows && col >= 0 && col < num_cols)) {
return false;
}
if (hacker_count >= num_hackers) {
return false;
}
// location must be blank to spawn
if (mapData[row][col] != '-') {
return false;
}
if (hackerPositions[hacker_count][0] == -1 && hackerPositions[hacker_count][1] == -1) {
hackerPositions[hacker_count][0] = row;
hackerPositions[hacker_count][1] = col;
mapData[row][col] = 'H';
hacker_count++;
return true;
}
return false;
}
/* add NPC to map
* Parameters: where to spawn NPC -- row (int), col (int)
* Return: (bool) false if no more space in npcPositions array
* or if (row, col) is an invalid position
* or if (row, col) is already populated; else true
*/
bool Map::spawnNPC(int row, int col) {
// out of map bounds
if (!(row >= 0 && row < num_rows && col >= 0 && col < num_cols)) {
return false;
}
if (npc_count >= num_npcs) {
return false;
}
// location must be blank to spawn
if (mapData[row][col] != '-') {
return false;
}
if (npcPositions[npc_count][0] == -1 && npcPositions[npc_count][1] == -1) {
npcPositions[npc_count][0] = row;
npcPositions[npc_count][1] = col;
mapData[row][col] = 'N';
npc_count++;
return true;
}
return false;
}
/* add Best Buy to map
* Parameters: where to spawn Best Buy -- row (int), col (int)
* Return: (bool) false if (row, col) is an invalid location
* or if (row, col) is already populated
* or if there is a best buy already on the map; else true
*/
bool Map::spawnBestBuy(int row, int col) {
// out of map bounds
if (!(row >= 0 && row < num_rows && col >= 0 && col < num_cols)) {
return false;
}
// location must be blank to spawn
if (mapData[row][col] != '-') {
return false;
}
if (best_buy_on_map) {
return false;
}
if (bestBuyPosition[0] == -1 && bestBuyPosition[1] == -1) {
bestBuyPosition[0] = row;
bestBuyPosition[1] = col;
mapData[row][col] = 'B';
best_buy_on_map = true;
return true;
}
return false;
}
// return true if x, y position has a best buy there
bool Map::isBestBuyLocation(){
return bestBuyPosition[0] == playerPosition[0] && bestBuyPosition[1] == playerPosition[1];
}
// return true if x, y position has an npc there
bool Map::isNPCLocation(){
for(int i = 0; i < num_npcs; i++){
if(npcPositions[i][0] == playerPosition[0] && npcPositions[i][1] == playerPosition[1]){
return true;
}
}
return false;
}
// return true if x, y position has a hacker there
bool Map::isHackerLocation() {
for(int i = 0; i < num_hackers; i++){
if(hackerPositions[i][0] == playerPosition[0] && hackerPositions[i][1] == playerPosition[1]){
return true;
}
}
return false;
}
/*
* This function prints a menu of valid moves based on playerPosition
* Parameters: none
* Return: nothing (void)
*/
void Map::displayMoves(){
if(!(playerPosition[0] == 0)){
cout << "w (Up)" << endl;
}
if(!(playerPosition[0] == (num_rows - 1))){
cout << "s (Down)" << endl;
}
if(!(playerPosition[1] == 0)){
cout << "a (Left)" << endl;
}
if(!(playerPosition[1] == (num_cols - 1))){
cout << "d (Right)" << endl;
}
}
/*
* This function takes in user input
* and updates playerPosition on the map.
* Parameters: move (char) -- 'w' (up), 'a' (left), 's' (down), 'd' (right)
* Return: (bool) if move is valid, then true, else false
*/
bool Map::executeMove(char move){
// if user inputs w, move up if it is an allowed move
if(!(playerPosition[0] == 0) && (tolower(move) == 'w')){
playerPosition[0] -= 1;
return true;
}
// if user inputs s, move down if it is an allowed move
else if(!(playerPosition[0] == (num_rows - 1))&& (tolower(move) == 's')){
playerPosition[0] += 1;
return true;
}
// if user inputs a, move left if it is an allowed move
else if(!(playerPosition[1] == 0)&& (tolower(move) == 'a')){
playerPosition[1] -= 1;
return true;
}
// if user inputs d, move right if it is an allowed move
else if(!(playerPosition[1] == (num_cols - 1))&& (tolower(move) == 'd')){
playerPosition[1] += 1;
return true;
}
else{
cout << "Invalid Move" << endl;
return false;
}
}
/*
* This function prints a 2D map in the terminal.
* Parameters: none
* Return: nothing (void)
*/
void Map::displayMap() {
for (int i = 0; i < num_rows; i++) {
for (int j = 0; j < num_cols; j++) {
if (playerPosition[0] == i && playerPosition[1] == j) {
cout << "x";
} else if (mapData[i][j] == 'H') { // don't show hacker on the map
cout << "H";
}
else {
cout << mapData[i][j];
}
}
cout << endl;
}
}
// returns true if there is already a Best Buy on the map
bool Map::isBestBuyOnMap() {
return best_buy_on_map;
}