-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextBasedGame.cpp
209 lines (178 loc) · 5.67 KB
/
TextBasedGame.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
#include<iostream>
#include <vector>
#include "Room.cpp"
#include <map>
#include "Item.cpp"
#include "Events.cpp"
#include <fstream>
#include<windows.h>
#include<string>
#include<sstream>
//using std::string;
//using std::vector;
//using std::pair;
//using std::cout;
static Room parseRoom(string rawRoom);
static vector<string> getRawMap();
/*
BOAT_FRAME1:
| | |
)_) )_) )_)
)____) )____) )____)
)______)______)______)
/\________|______|______|__________/\
| |
---^^~-^---^~-^------~-^\ O o O o O /----~----~~~~~---^^-^-^---~\n
^~~^ ^~^~^~^~ ^^ ~~^~^^~\n
^~~~^~~~~^^ ^^^^^\n"
BOAT_FRAME2:
| | |
)_) )_) )_)
)____) )____) )____)
)______)______)______)
/\________|______|______|________/\
--^--^----^^----^-~~----` ~~ ^^^~~^ '----~--~~^^--^---^---^~-
^^^^^^ ~^~^^^~ ^^ ^ ~~~ ~^ ^^
-----~^~^^~^~ ^^^^
WELCOME:
_ _ _ _ __
| | | |___| |___ ___ _____ ___| |
| | | | -_| | _| . | | -_|__|
|_____|___|_|___|___|_|_|_|___|__|
*/
int main()
{
const string BOAT_FRAME1 = " | | |\n )_) )_) )_)\n )____) )____) )____)\n )______)______)______)\n /\\________|______|______|________/\\\n | |\n---^^~-^---^~-^------~-^\\ ~~ O ^o O o O ^^ /----~----~~~~~---^^-^-^---~\n ^~~^ ^~^~^~^~ ^^ ~~^~^^~\n ^~~~^~~~~^^ ^^^^^\n\n";
const string BOAT_FRAME2 = "\n | | |\n )_) )_) )_)\n )____) )____) )____)\n )______)______)______)\n /\\________|______|______|________/\\\n--^--^----^^----^-~~----` ~~ ^^^~~^ '----~--~~^^--^---^---^~-\n ^^^^^^ ~^~^^^~ ^^ ^ ~~~ ~^ ^^\n -----~^~^^~^~ ^^^^\n\n";
const string WELCOME = " _ _ _\t\t __\n| | | |___| |___ ___ _____ ___| |\n| | | | -_| | _| . | | -_|__|\n|_____|___|_|___|___|_|_|_|___|__|\n";
//Shop testShop("Hello, this is a shop, buy something.", vector<Item>{Item(20, 1, 1, "Healing Potion", "Lets you heal, what did you expect?"), Item(200, 1, 1, "Boomerang", "Always comes back, unlike those 5 gold coins you lent to Brent all those years ago.")});
Shop testShop("Hello, this is a shop, buy something.");
vector<string> rawMap = getRawMap();
const int MAP_SIZE = rawMap.size();
pair<int, int> playerPos(0, 0);
pair<pair<int, int>, Room> insertion;
Room outOfBounds = Room();
std::map<pair<int, int>, Room> rooms;
for (int i = 0; i < MAP_SIZE; i++) {
Room currentParsedRoom = parseRoom(rawMap.at(i));
insertion.first = currentParsedRoom.getCoords();
insertion.second = currentParsedRoom;
rooms.insert(insertion);
}
pair<pair<int, int>, Room> testEventRoom;
testEventRoom.first = { 0, -2 };
//EventRoom *erm = new EventRoom("You're at the base of a staircase up to the deck", 0, -2, false, true, false, true, &testShop);
Room roomIn;
pair<int, int> adjacentCoords;
string adjacentRoomDescriptions[4];
for (int i = 0; i < 12; i++) {
cout << WELCOME;
switch (i % 2)
{
case 0:
cout << BOAT_FRAME1 << std::flush;
break;
default:
cout << BOAT_FRAME2 << std::flush;
break;
}
Sleep(800);
}
//-----------------------------------------------------------------------------------------------------------------------------------+
//Game Loop
while (true)
{
roomIn = rooms[playerPos];
roomIn.describe();
cout << "You are at: (" << playerPos.first << ", " << playerPos.second << ")" << std::endl;
for (int i = 0; i < 4; i++) {
adjacentCoords = playerPos;
switch (i) {
case 0:
adjacentCoords.second = playerPos.second + 1;
break;
case 1:
adjacentCoords.first = playerPos.first + 1;
break;
case 2:
adjacentCoords.second = playerPos.second - 1;
break;
case 3:
adjacentCoords.first = playerPos.first - 1;
break;
}
try {
adjacentRoomDescriptions[i] = rooms[adjacentCoords].getShortDescription();
}
catch (std::exception)
{
adjacentRoomDescriptions[i] = "null";
}
}
switch (roomIn.move(adjacentRoomDescriptions))
{
case 0:
playerPos.second++;
break;
case 1:
playerPos.first++;
break;
case 2:
playerPos.second--;
break;
case 3:
playerPos.first--;
break;
}
}
}
static Room parseRoom(string rawRoom) {
string segment[7];
string delimeter = "|";
for (int i = 0; i < 4; i++) {
segment[i] = rawRoom.substr(0, rawRoom.find(delimeter));
rawRoom.erase(0, rawRoom.find(delimeter) + delimeter.length());
}
Room parsedRoom;
try {
parsedRoom = Room(segment[0], segment[1], (stoi(segment[2])), stoi(segment[3]));
}
catch (std::invalid_argument const&) {
}
return parsedRoom;
}
static vector<string> getRawMap() {
std::fstream newFile;
vector<string> rawMap;
newFile.open("Map.txt", std::ios::in);
if (newFile.is_open()) {
std::string tp;
while (getline(newFile, tp)) {
rawMap.push_back(tp);
}
}
return rawMap;
}
/*
To do:
add more actions:
check GPS (display coords)
check inventory:
Item object w/ inheritence for certain types (weapons, armor, etc.)
Wouldn't need many functions, just variables.
name
damage
durability
quantity
price
better directional descriptions
add events:
Shops
Fights
Misc Encounters
NPCs
Plan the story...
Add rooms in
event could be an object, with inheritence.
"EventRoom" could be a subclass of Room.
*/