-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.cpp
343 lines (296 loc) · 14.2 KB
/
Player.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*******************************************************************************
* Filename: Player.cpp *
* Author: Daniel Wilson *
* E-Mail: [email protected] *
* Date: 10 June 2017 *
* *
* Description: Provides the implementation of the player class which is able to*
* control the player and its movements/interactions in the game. *
* *
*******************************************************************************/
#include "Player.hpp" // Player::, PlayerActions
#include "Location.hpp" // Location
#include "ioutil.hpp" // Menu
#include "Item.hpp" // enum Item
#include <string> // std::string
#include <vector> // std::vector
#include <iostream> // std::cout
#include "map.hpp"
using std::string;
using std::vector;
using std::cout;
using std::endl;
PlayerActions actionToEnum(const vector<string>& actions, int actionSelection);
void doAction(PlayerActions doThis);
string enumToString(Item item);
/*******************************************************************************
* Function: Player::Player *
* *
* Description: Initializes the player's current location to the arguement. *
* *
* Inputs: startingLocation - Location* *
* *
* Outputs: void *
*******************************************************************************/
Player::Player(Location* startingLocation)
{
currentLocation = startingLocation;
}
/*******************************************************************************
* Function: Player::~Player *
* *
* Description: Sets current location to nullptr. *
* *
* Inputs: startingLocation - Location* *
* *
* Outputs: void *
*******************************************************************************/
Player::~Player()
{
currentLocation = nullptr;
inventory.clear();
}
/*******************************************************************************
* Function: Player::takeAction *
* *
* Description: Displays a list of actions the player can take based on their *
* current location and their inventory items. *
* *
* Inputs: void *
* *
* Outputs: bool - returns true if the player has won the game. *
*******************************************************************************/
bool Player::chooseAction()
{
string title = "Available Actions\n";
title += "=================";
vector<string> availableOptions;
if (currentLocation->getNorth() != nullptr)
availableOptions.push_back("Move North");
if (currentLocation->getEast() != nullptr)
availableOptions.push_back("Move East");
if (currentLocation->getSouth() != nullptr &&
currentLocation->getLocationType() != LocationType::LAKESHORE)
availableOptions.push_back("Move South");
if (currentLocation->getWest() != nullptr)
availableOptions.push_back("Move West");
if (currentLocation->getItem() == Item::KAYAK)
availableOptions.push_back("Take Kayak");
if (currentLocation->getItem() == Item::HOSE)
availableOptions.push_back("Take Garden Hose");
if (currentLocation->getItem() == Item::CAN)
availableOptions.push_back("Take Gas Can");
if (currentLocation->getItem() == Item::GAS && hasCan() && hasHose())
availableOptions.push_back("Siphon Gas");
if (currentLocation->getItem() == Item::TAPE)
availableOptions.push_back("Take Duct Tape");
if (currentLocation->getLocationType() == LocationType::LAKESHORE &&
hasKayak())
availableOptions.push_back("Kayak out to middle of lake");
if (currentLocation->getLocationType() == LocationType::CAR &&
hasTape())
availableOptions.push_back("Patch car with duct tape");
if (currentLocation->getLocationType() == LocationType::TAPED_CAR &&
hasGas())
availableOptions.push_back("Gas up the car and get out of here!");
availableOptions.push_back("Show Inventory");
string prompt = "Take action: ";
Menu actionMenu(title, availableOptions, prompt);
int actionSelection;
cout << "\n\n";
actionMenu.print();
actionSelection = actionMenu.getSelection();
PlayerActions choice;
if (actionSelection != -1)
{
choice = actionToEnum(availableOptions, actionSelection);
return doAction(choice);
}
return false;
}
/*******************************************************************************
* Function: actionToEnum *
* *
* Description: Converts the action selected by the user to the PlayerActions *
* type. *
* *
* Inputs: actions - vector<string>& *
* actionSelection - int *
* *
* Outputs: PlayerActions *
*******************************************************************************/
enum PlayerActions actionToEnum(const vector<string>& actions,
int actionSelection)
{
string actionName = actions.at(actionSelection - 1);
if (actionName == "Move North")
return PlayerActions::MOVE_NORTH;
else if (actionName == "Move East")
return PlayerActions::MOVE_EAST;
else if (actionName == "Move South")
return PlayerActions::MOVE_SOUTH;
else if (actionName == "Move West")
return PlayerActions::MOVE_WEST;
else if (actionName == "Show Inventory")
return PlayerActions::SHOW_INVENTORY;
else if (actionName == "Take Kayak")
return PlayerActions::TAKE_ITEM;
else if (actionName == "Take Garden Hose")
return PlayerActions::TAKE_ITEM;
else if (actionName == "Take Gas Can")
return PlayerActions::TAKE_ITEM;
else if (actionName == "Siphon Gas")
return PlayerActions::TAKE_ITEM;
else if (actionName == "Take Duct Tape")
return PlayerActions::TAKE_ITEM;
else if (actionName == "Kayak out to middle of lake")
return PlayerActions::MOVE_SOUTH;
else if (actionName == "Patch car with duct tape")
return PlayerActions::USE_TAPE;
else if (actionName == "Gas up the car and get out of here!")
return PlayerActions::ESCAPE;
else
return PlayerActions::DO_NOTHING;
}
/*******************************************************************************
* Function: Player::doAction *
* *
* Description: Cause the player to take an action based on their previous *
* action selection. *
* *
* Inputs: doThis - PlayerActions *
* *
* Outputs: bool - returns true if the player has won the game. *
*******************************************************************************/
bool Player::doAction(PlayerActions doThis)
{
if (doThis == PlayerActions::MOVE_NORTH)
currentLocation = currentLocation->getNorth();
else if (doThis == PlayerActions::MOVE_EAST)
currentLocation = currentLocation->getEast();
else if (doThis == PlayerActions::MOVE_SOUTH)
currentLocation = currentLocation->getSouth();
else if (doThis == PlayerActions::MOVE_WEST)
currentLocation = currentLocation->getWest();
else if (doThis == PlayerActions::SHOW_INVENTORY)
printInventory();
else if (doThis == PlayerActions::TAKE_ITEM)
inventory.push_back(currentLocation->takeItem());
else if (doThis == PlayerActions::USE_TAPE)
{
string description;
description = "There's nothing duct tape can't fix! Now all this car ";
description += "needs is some gasoline and a whole lot of luck!";
currentLocation->setDescription(description);
currentLocation->setLocationType(LocationType::TAPED_CAR);
}
else if (doThis == PlayerActions::ESCAPE)
{
return true;
}
return false;
}
/*******************************************************************************
* Function: Player::printLocationDescription() const *
* *
* Description: Wrapper to the current location's printDescription menthod. *
* *
* Inputs: void *
* *
* Outputs: void *
*******************************************************************************/
void Player::printLocationDescription() const
{
currentLocation->printDescription();
}
/*******************************************************************************
* Function: Player::printInventory() const *
* *
* Description: Prints all of the player's inventory items. *
* *
* Inputs: void *
* *
* Outputs: void *
*******************************************************************************/
void Player::printInventory() const
{
ValidatedInput input;
terminalClear();
if (inventory.size() == 0)
cout << "Your pack is empty. Things are not looking good.";
else
{
for (unsigned int i = 0; i < inventory.size(); ++i)
cout << i + 1 << ". " << enumToString(inventory.at(i)) << endl;
}
cout << "\n\nPress ENTER to return";
input.fromKeyboard();
}
/*******************************************************************************
* Function: enumToString *
* *
* Description: Takes an enum of an item and converts it to a string. *
* *
* Inputs: item - Item (enum) *
* *
* Outputs: string *
*******************************************************************************/
string enumToString(Item item)
{
if (item == Item::KAYAK)
return "kayak";
else if (item == Item::HOSE)
return "garden hose";
else if (item == Item::CAN)
return "gas can";
else if (item == Item::GAS)
return "gas (in can)";
else if (item == Item::TAPE)
return "duct tape";
else
return "invalid item";
}
/*******************************************************************************
* Function: Player::hasCan/Gas/Hose/Tape/Kayak const *
* *
* Description: Returns if the player has a particular item in their inventory. *
* *
* Inputs: void *
* *
* Outputs: bool - returns true if the user has the named item. *
*******************************************************************************/
bool Player::hasCan() const
{
for (unsigned int i = 0; i < inventory.size(); ++i)
if (inventory.at(i) == Item::CAN)
return true;
return false;
}
bool Player::hasGas() const
{
for (unsigned int i = 0; i < inventory.size(); ++i)
if (inventory.at(i) == Item::GAS)
return true;
return false;
}
bool Player::hasHose() const
{
for (unsigned int i = 0; i < inventory.size(); ++i)
if (inventory.at(i) == Item::HOSE)
return true;
return false;
}
bool Player::hasTape() const
{
for (unsigned int i = 0; i < inventory.size(); ++i)
if (inventory.at(i) == Item::TAPE)
return true;
return false;
}
bool Player::hasKayak() const
{
for (unsigned int i = 0; i < inventory.size(); ++i)
if (inventory.at(i) == Item::KAYAK)
return true;
return false;
}