-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
246 lines (229 loc) · 5.69 KB
/
Main.cpp
File metadata and controls
246 lines (229 loc) · 5.69 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
/*
Alex Pacheco
COMP 340 - Project 1
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include "Goblin.hpp"
#include "Orc.hpp"
#include "Player.hpp"
#include "Dungeon.hpp"
//DECLARE VARIABLES AND FUNCTIONS USED IN Main.cpp
Dungeon* dungeon;
Player* player;
bool continueGame = true;
int invalidInputCounter = 0;
void doAction(std::string userInput);
void printMenu();
int main()
{
srand(time(NULL));
std::string str_userInput;
std::cout << "Enter name for player: ";
getline(std::cin, str_userInput);
player = new Player(str_userInput);
std::cout << std::endl;
std::cout << "Welcome " << str_userInput << "!" << std::endl;
std::cout << "Health " << player->getCurrentHealth() << std::endl;
std::cout << "Attack " << player->getBaseAttackStat() << std::endl;
std::cout << "Defense " << player->getBaseDefenseStat() << std::endl;
std::cout << "Speed " << player->getBaseSpeedStat() << std::endl;
std::cout << std::endl;
printMenu();
std::cout << std::endl;
std::cout << "You have been kidnapped and brought to a dungeon by your" << std:: endl
<< "captors and left to die. You want to escape, but you only" << std::endl
<< "have a simple map and are trapped in a small room. There is" << std::endl
<< "one door in front of you, so you decide to walk through it." << std::endl
<< std::endl;
dungeon = new Dungeon(player);
do
{
Room* activeRoom = dungeon->getActiveRoom();
if(activeRoom->getEnemy())
{
Character* enemy = activeRoom->getEnemy();
if(enemy->getCurrentHealth() <= 0)
{
activeRoom->destroyEnemy();
}
else
{
enemy->setAttackTarget(player);
enemy->update();
}
player->setAttackTarget(activeRoom->getEnemy());
}
if(player->getCurrentHealth() <= 0)
{
std::cout << std::endl << "GAME OVER!" << std::endl << std::endl;
continueGame = false;
}
else
{
doAction(player->update());
}
} while(continueGame);
}
void doAction(std::string userInput)
{
std::transform(
userInput.begin(), userInput.end(), userInput.begin(), ::tolower);
if(userInput == "inventory")
{
player->printInventory();
}
else if(userInput == "search")
{
Item* item = dungeon->getActiveRoom()->getItem(false);
if(item)
{
std::cout << "After searching the room, you find a "
<< item->getName() << std::endl;
}
else
{
std::cout << "There do not appear to be any items in the room"
<< std::endl;
}
}
else if(userInput == "take item")
{
player->addItem(dungeon->getActiveRoom()->getItem(true));
}
else if(userInput == "use potion")
{
Item* item = player->removeItem("Potion");
if(item)
{
item->use(player);
}
}
else if(userInput == "use grenade")
{
Item* item = player->removeItem("Grenade");
if(item)
{
if(dungeon->getActiveRoom()->getEnemy())
{
item->use(dungeon->getActiveRoom()->getEnemy());
}
else
{
std::cout << "There is no enemy around, and the "
<< "grenade blows up on you." << std::endl;
item->use(player);
}
}
}
else if(userInput == "attack")
{
player->executeAttack();
}
else if(userInput == "walk north")
{
if(dungeon->getActiveRoom()->getEnemy())
{
std::cout << "You can't leave! There is an enemy nearby!" << std::endl;
}
else
{
dungeon->movePlayer(Dungeon::NORTH);
}
}
else if(userInput == "walk south")
{
if(dungeon->getActiveRoom()->getEnemy())
{
std::cout << "You can't leave! There is an enemy nearby!" << std::endl;
}
else
{
dungeon->movePlayer(Dungeon::SOUTH);
}
}
else if(userInput == "walk east")
{
if(dungeon->getActiveRoom()->getEnemy())
{
std::cout << "You can't leave! There is an enemy nearby!" << std::endl;
}
else
{
dungeon->movePlayer(Dungeon::EAST);
}
}
else if(userInput == "walk west")
{
if(dungeon->getActiveRoom()->getEnemy())
{
std::cout << "You can't leave! There is an enemy nearby!" << std::endl;
}
else
{
dungeon->movePlayer(Dungeon::WEST);
}
}
else if(userInput == "view map")
{
std::cout << std::endl
<< " [ ] N" << std::endl
<< " | W + E" << std::endl
<< " [ ] [ ]--[ ]--[ ] S" << std::endl
<< " | | |" << std::endl
<< " [ ]--[ ] [ ]" << std::endl
<< " | |" << std::endl
<< " [ ] [ ]--[ ]--[ ]" << std::endl
<< " |" << std::endl
<< " [ ]" << std::endl
<< std::endl;
}
else if(userInput == "help")
{
printMenu();
}
else if(userInput == "quit")
{
continueGame = false;
}
else
{
std::cout << "Invalid input!" << std::endl;
invalidInputCounter++;
if(invalidInputCounter == 2)
{
std::cout << "Please type 'help' for a list of valid commands."
<< std::endl;
invalidInputCounter = 0;
}
}
}
void printMenu()
{
std::cout << "'view map' "
<< "Look at a simple map of the dungeon" << std::endl;
std::cout << "'walk north' "
<< "Move player north" << std::endl;
std::cout << "'walk south' "
<< "Move player south" << std::endl;
std::cout << "'walk east' "
<< "Move player east" << std::endl;
std::cout << "'walk west' "
<< "Move player west" << std::endl;
std::cout << "'attack' "
<< "Attack enemy" << std::endl;
std::cout << "'search' "
<< "Search the room for items" << std::endl;
std::cout << "'inventory' "
<< "List items in player inventory" << std::endl;
std::cout << "'take item' "
<< "Take the item in the room" << std::endl;
std::cout << "'use [item]' "
<< "Use the item specified" << std::endl;
std::cout << "'help' "
<< "Reprint menu options" << std::endl;
std::cout << "'quit' "
<< "Quit the game" << std::endl;
}