-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGame.cpp
More file actions
295 lines (261 loc) · 9.24 KB
/
Copy pathGame.cpp
File metadata and controls
295 lines (261 loc) · 9.24 KB
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
/***************************************
Program Name: Final Project
Author: Robert Elsom
Date: 3/6/2019
Description: Game class, responsible for making the board and carrying out the actual gameplay.
**************************************/
#include <iostream>
#include <cmath>
#include "Game.hpp"
#include "Space.hpp"
#include "Creek.hpp"
#include "Tree.hpp"
#include "Cabin.hpp"
#include "validStr.hpp"
using std::cout;
using std::cin;
using std::endl;
using std::string;
//constructor creates character, fills backpack and also sets max amount of items in maxBackpackCapacity
Game::Game() {
backpack["wood"] = 0;
backpack["water"] = 100;
backpack["matches"] = 0;
backpack["keys"] = 0;
round();
}
//simulates rounds until time runs out or user wins
void Game::round() {
printMap();
while(!gameFinished) {
updateWater(-14);
//check if ran out of water if not moving to a water space
if ((board.getCharacterSpace())->getType() == "water") {
checkWaterLevel();
}
board.moveCharacter();
printMap();
interactWithSpace();
if (!gameFinished) {
checkWaterLevel();
updateTime();
}
}
printFinalResults();
}
//updates time and checks if need to go to next day or out of time
void Game::updateTime() {
timeCounter++;
//if MAX_TIME hours pass, return to shelter and increase dayCounter
if (timeCounter == MAX_TIME) {
dayCounter++;
timeCounter = 0;
if (dayCounter > MAX_DAYS) {
std::cout << "\n\nOh no! You ran out of time to exit the woods! A tornado just destroyed everything, including you!" << std::endl;
gameFinished = true;
}
else {
moveNearestShelter();
}
}
if (!gameFinished) {
printResultsHour();
}
}
//moves character to nearest shelter
void Game::moveNearestShelter() {
Space* nearestShelter = nullptr;
//if not already in a shelter
if (!((board.getCharacterSpace())->hasShelter())) {
std::cout << "\nYou ran out of daylight. You are about to return to your nearest shelter." << std::endl;
//pause before displaying new screen
std::cout << "Press enter to continue playing..." << std::endl;
std::cin.ignore();
std::cin.get();
double distance = 0.0;
double shortestDistance = 0.0;
Space* temp = board.getHead();
Space* headRow = board.getHead();
//test board to find nearest shelter
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
//finds shelters
if (temp->hasShelter()) {
distance = calculateDistance(j, i);
//set shortest distance to first distance found
if (shortestDistance == 0.0) {
shortestDistance = distance;
}
//compare next distances found to shortest distance and set as shortest distance if shorter
if (distance <= shortestDistance) {
shortestDistance = distance;
nearestShelter = temp;
}
}
//moves to next spot on board
temp = temp->getPointer("right");
}
//move to next row
headRow = headRow->getPointer("down");
temp = headRow;
}
}
else {
nearestShelter = board.getCharacterSpace();
}
//moves character to nearest shelter if there is one and test if user got hypothermia
if (nearestShelter != nullptr) {
board.moveCharacterShelter(nearestShelter);
board.printBoard();
std::cout << "You returned to a shelter for the night." << std::endl;
if (backpack.at("wood") > 0 && backpack.at("matches") > 0) {
std::string userChoice = validStr( "Would you like to build a fire to reduce hypothermia for 4 pieces of wood and 1 match? (Y/N)");
updateWood(-4);
backpack.at("matches") -= 1;
if (userChoice == "N" || userChoice == "n") {
gameFinished = (board.getCharacterSpace())->gotHypothermia();
}
}
else if (backpack.at("wood") <= 0) {
std::cout << "Unfortunately, you do not have enough wood for a fire to eliminate the chances of hypothermia."<<std::endl;
gameFinished = (board.getCharacterSpace())->gotHypothermia();
}
else {
std::cout << "Unfortunately, you do not have any way to start a fire to eliminate the chances of hypothermia."<<std::endl;
gameFinished = (board.getCharacterSpace())->gotHypothermia();
}
if (!gameFinished) {
std::cout << "\nYou dodged hypothermia and survived the night!" << std::endl;
}
}
}
//calculates distance from shelter to spot located at col, row
double Game::calculateDistance(int col, int row) {
return sqrt(pow((board.getCharacterCol() - col),2) + pow((board.getCharacterRow() - row),2));
}
//calls enterSpace from certain space types passing needed parameter
void Game::interactWithSpace() {
int tempInt = 0;
if (board.getCharacterSpace() == board.getExitSpace()) {
board.setPrintExit();
if (backpack.at("keys") == 1) {
std::string finalChoice = validStr("You found a car! Would you like to use your key to see if it starts? (Y/N)");
if (finalChoice == "Y" || finalChoice =="y") {
gameFinished = true;
wonGame = true;
std::cout << "\nThe car started! You used it to return to civilization!" << std::endl << std::endl;
}
else {
std::cout << "Ok. Continue searching on a way to leave the woods!" << std::endl << std::endl;
}
}
else {
std::cout << "You found a car! If only you had a key to start it up!" << std::endl;
board.setPrintExit();
std::cout << "After this turn, it will be marked on your map with a XX symbol" << std::endl;
}
}
else if (board.getCharacterSpace()->getType() == "creek") {
tempInt = (board.getCharacterSpace())->enterSpace(backpack.at("water"));
updateWater(tempInt);
}
else if (board.getCharacterSpace()->getType() == "cabin") {
tempInt = (board.getCharacterSpace())->enterSpace(backpack.at("matches"));
updateMatches(tempInt);
addToBackpack("keys", 1);
board.setPrintExit();
}
else {
tempInt = (board.getCharacterSpace())->enterSpace(backpack.at("wood"));
updateWood(tempInt);
}
}
void Game::addToBackpack(std::string key, int value) {
backpack.at(key) += value;
}
void Game::printMap() {
board.printBoard();
}
void Game::printResultsHour() {
std::cout << std::endl << std::endl << "---------Hourly Results---------" << std::endl;
std::cout << "Current Backpack contents: " << std::endl;
std::cout << "Wood: " << backpack.at("wood") << std::endl;
std::cout << "Water Remaining: " << backpack.at("water") << "%" << std::endl;
std::cout << "Number of matches: " << backpack.at("matches") << std::endl;
std::cout << "Number of keys: " << backpack.at("keys") << std::endl;
std::cout << std::endl << "Hours remaining in day: " << MAX_TIME - timeCounter << std::endl;
std::cout << "Days remaining: " << MAX_DAYS-dayCounter <<std::endl;
}
void Game::printFinalResults() {
if (wonGame) {
std::cout << "\n\nYOU WON! You survived and excelled in the woods!" << std::endl;
}
std::cout << std::endl << std::endl << "---------Final Results---------" << std::endl;
if (wonGame) {
std::cout << "YOU ARE A WINNER!!!!!"<<std::endl <<std::endl;
}
else {
std::cout << " YOU LOST! You did not survive the woods!" << std::endl << std::endl;
}
std::cout << "Current Backpack contents: " << std::endl;
std::cout << "Wood: " << backpack.at("wood") << std::endl;
std::cout << "Water Remaining: " << backpack.at("water") << "%" << std::endl;
std::cout << "Number of matches: " << backpack.at("matches") << std::endl;
std::cout << "Number of keys: " << backpack.at("keys") << std::endl;
std::cout << std::endl << "Days lasted: " << dayCounter-1 << std::endl;
std::cout << "Hours lasted: " << timeCounter <<std::endl <<std::endl << std::endl;
}
//updates water in backpack
void Game::updateWater(int newNum) {
//make sure not exceding max capacity of water
if (newNum + backpack.at("water") > maxCapacity.at("water")) {
newNum = maxCapacity.at("water")-backpack.at("water");
}
if (newNum + backpack.at("water") < 0) {
newNum = 0 - backpack.at("water");
}
backpack.at("water") += newNum;
}
//checks if water is empty
void Game::checkWaterLevel() {
if (backpack.at("water") <= 0) {
std::cout << std::endl << "Oh no, you ran out of water and died!" << std::endl;
gameFinished = true;
}
}
//udpates wood in backpack
void Game::updateWood(int newNum) {
std::string key = "wood";
if (newNum + backpack.at(key) > maxCapacity.at(key)) {
newNum = maxCapacity.at(key) - backpack.at(key);
if (newNum == 0) {
(board.getCharacterSpace())->setEntered();
std::cout << "Sorry, you have too much wood already. Nothing was added to your backpack." <<std::endl;
}
}
else if (newNum + backpack.at(key) < 0) {
newNum = 0 - backpack.at(key);
}
if ((board.getCharacterSpace())->getType() == "tree" && newNum != 0) {
std::cout << "You just entered the woods and found " << newNum;
std::cout << " pieces of wood to add to your backpack!" << std::endl;
}
backpack.at(key) += newNum;
}
//updates matches in backpack
void Game::updateMatches(int newNum) {
std::string key = "matches";
if (newNum + backpack.at(key) > maxCapacity.at(key)) {
newNum = maxCapacity.at(key) - backpack.at(key);
}
else if (newNum + backpack.at(key) < 0) {
newNum = 0 - backpack.at(key);
}
if ((board.getCharacterSpace())->getType() == "cabin" && newNum != 0) {
std::cout << "You just entered the cabin and found " << newNum;
std::cout << " matches to add to your backpack!" << std::endl;
}
backpack.at(key) += newNum;
std::cout << std::endl << "You also found car keys and a map to the car! It will be marked with XX on your next turn!" << std::endl;
board.setExit();
}