-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaGame.cpp
More file actions
345 lines (288 loc) · 9.8 KB
/
Copy pathaGame.cpp
File metadata and controls
345 lines (288 loc) · 9.8 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
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
344
345
#include <iostream>
#include "aGame.h"
#include <sstream>
#include <fstream>
#include "anExitList.h"
#include "anItemList.h"
#include "anItem.h"
#include "aRoom.h"
using namespace std;
aGame::aGame(){
currRoom = 0;
numRooms = 0;
winRoom = 0;
winText = "";
}
//--------------------------------------------------------------
// dispHelp
//--------------------------------------------------------------
void aGame::dispHelp() {
cout << "Discover a task to complete. Complete it by findin/taking \n";
cout << "items from places throughout the world, then dropping the items\n";
cout << "in the place instructed. Good Luck!\n";
cout << "Commands: \n";
cout << "- look: see the room description, objects and exits.\n";
cout << "- exa <object>: examine an object in the room or inventory.\n";
cout << "- inv: see a list of your inventory\n";
cout << "- drop <object>: drop inventory object in the roo\n";
cout << "- take <object>: take object from room into your inventory.\n";
cout << "- help: display this menu again.\n";
cout << "- quit: exit the game\n";
cout << "- print: for debugging: print internal data structures.\n";
cout << "ALSO: all exits are commands to take the exit.\n";
rooms[currRoom].enter();
cout << "==>";
string command;
string rest;
//cin >> command;
string aline;
cin.ignore();
getline(cin, aline);
splitFirst(aline, command, rest);
while (command.compare("quit") != 0) {
if (command.compare("look") == 0) {
rooms[currRoom].look();
}
else if (command.compare("exa") == 0) {
rooms[currRoom].examine(rest, playerInv);
}
else if (command.compare("inv") == 0) {
inv();
}
else if (command.compare("drop") == 0) {
bool gameWin = win(rest, playerInv);
}
else if (command.compare("take") == 0) {
rooms[currRoom].take(rest, playerInv);
}
else if (command.compare("dispHelp") == 0) {
dispHelp();
}
else if (command.compare("print") == 0) {
print();
}
else {
cout << "What?" << endl;
}
if (command.compare("quit") == 0) {
cout << "Thanks for playing! Bye!" << endl;
}
cout << "==>";
getline(cin, aline);
splitFirst(aline, command, rest);
}
} // dispHelp()
//------------------------------------------------------------------------------------------
// splitFirst
//------------------------------------------------------------------------------------------
// Given: aline: any string
// Modifies: first: the first word of aline
// rest: all words of aline with the first word removed
// multiple contiguous white spaces are reduced to one blank
// Returns: 0: aline was empty, nothing to split
// 1: aline only contained one word, so command is empty
// 2: alone containted two or more words and was split
// Splits a given string into the first word in the string and the rest of the string.
// Returns the number of resulting strings.
// Examples:
// splitFirst("", first, rest) sets first="", rest="" and returns 0
// splitFirst("look", first, rest) sets first="look", rest="" and returns 1
// splitFirst("look over there", first, rest) sets first="look", rest="over there" and
// returns 2
//-----------------------------------------------------------------------------------------
int aGame::splitFirst(string aline, string & first, string & rest) {
first = rest = ""; // init results to empty string
if (aline == "") return 0; // if nothing to split, we're done
string next; // next word in aline
istringstream iss(aline); // converts aline into a input string stream
iss >> first; // extract the first word, if any
iss >> rest; // extract the second word, if any
if (rest == "") return 1; // if the second word is empty, only 1 word in aline
iss >> next; // extract the third word, if any
while (iss && next != "") { // while there are more words in the stream
rest = rest + " " + next; // stick it on the end of rest
iss >> next; // get the next word, if any
}
return 2; // aline was split into two parts.
} // splitFirst()
//--------------------------------------------------------------
// print
//--------------------------------------------------------------
// note: may have to change the names of some things here.
void aGame::print() {
cout << "GAME: currRoom=" << currRoom << " winRoom=" << winRoom << " winText=" << winText << endl;
cout << "GAME WIN INVENTORY: \n";
winInv.print();
cout << "GAME PLAYER INVENTORY: \n";
playerInv.print();
cout << "GAME: numRooms = " << numRooms << endl;
for (int i = 0; i < numRooms; i++)
rooms[i].print();
} // print()
void aGame::readfile(string filename) {
ifstream f;
f.open(filename);
if (f.fail()) {
cout << "File is not valid" << endl;
}
bool j;
int n = 0;
if (f.is_open()) {
numRooms = 0;
/*tag.compare("quit") != 0*/
while (n != 1) {
string command;
string rest;
string aline;
//f.ignore();
getline(f, aline);
n = splitFirst(aline, command, rest);
if (command.compare("WIN_ROOM:") == 0) {
istringstream stoi(rest);
if (command == "WIN_ROOM:") {
stoi >> winRoom;
}
}
else if (command.compare("WIN_ITEM:") == 0) {
if (command == "WIN_ITEM:") {
winInv.addItem(rest, "", true);
}
}
else if (command.compare("WIN_TEXT") == 0) {
if (command == "WIN_TEXT:") {
winText = rest;
}
}
else if (command.compare("ROOM:") == 0) {
if (command == "ROOM:") {
rooms[numRooms].setShortDescription(rest);
getline(f, aline);
rooms[numRooms].setLongDescription(aline);
//rooms[numRooms].getShortDescription();
numRooms++;
}
}
else if (command.compare("ROOM_ITEM:") == 0) {
if (command == "ROOM_ITEM:") {
splitFirst(rest, command, rest);
if (command == "true") {
j = true;
}
else {
j = false;
}
getline(f, aline);
rooms[numRooms - 1].addItem(rest, aline, j);
}
//numItems++;
}
else if (command.compare("EXIT:") == 0) {
if (command == "EXIT:") {
splitFirst(rest, command, rest);
int i = stoi(command);
rooms[numRooms - 1].addExit(rest, i);
}
}
/*else {
tag = "quit"
}*/
}
}
f.close();
}
void aGame::play() {
string filename;
cout << "+-----------------------------------------------+" << endl;
cout << "| SimpleQuest |" << endl;
cout << "| by Blair Hall |" << endl;
cout << "+-----------------------------------------------+" << endl;
cout << "Enter game data file name: ";
cin >> filename;
readfile(filename);
cout << "Discover a task to complete.Complete it by findin / taking" << endl;
cout << "items from places throughout the world, then dropping the items" << endl;
cout << "in the place instructed.Good Luck!" << endl;
cout << "Commands:" << endl;
cout << "- look: see the room description, objects and exits." << endl;
cout << "- exa <object> : examine an object in the room or inventory. " << endl;
cout << "- inv : see a list of your inventory" << endl;
cout << "- drop <object> : drop inventory object in the room" << endl;
cout << "- take <object> : take object from room into your inventory." << endl;
cout << "- help : display this menu again." << endl;
cout << "- quit : exit the game" << endl;
cout << "- print : for debugging : print internal data structures." << endl;
cout << "ALSO : all exits are commands to take the exit." << endl;
system("pause");
system("cls");
cout << "You fall asleep and enter the world of dreams
" << endl;
rooms[currRoom].enter();
cout << "==>";
string command;
string rest;
//cin >> command;
string aline;
cin.ignore();
getline(cin, aline);
splitFirst(aline, command, rest);
while (command.compare("quit") != 0) {
if (command.compare("look") == 0) {
rooms[currRoom].look();
}
else if (command.compare("exa") == 0) {
rooms[currRoom].examine(rest, playerInv);
}
else if (command.compare("inv") == 0) {
inv();
}
else if (command.compare("drop") == 0) {
bool gameWin = win(rest, playerInv);
}
else if (command.compare("take") == 0) {
rooms[currRoom].take(rest, playerInv);
}
else if (command.compare("help") == 0) {
dispHelp();
}
else if (command.compare("print") == 0) {
print();
}
else {
cout << "What?" << endl;
}
if (command.compare("quit") == 0) {
cout << "Thanks for playing! Bye!" << endl;
}
cout << "==>";
getline(cin, aline);
splitFirst(aline, command, rest);
}
}
void aGame::look() {
rooms[currRoom].look();
}
void aGame::exa(string name) {
rooms[currRoom].examine(name, playerInv);
}
void aGame::inv() {
for (int i = 0; i < playerInv.getNumItems(); i++) {
playerInv.getItem(i).print();
}
}
void aGame::drop(string name) {
rooms[currRoom].drop(name, playerInv);
}
void aGame::take(string name) {
rooms[currRoom].take(name, playerInv);
}
bool aGame::win(string item, anItemList playerInv) {
rooms[currRoom].drop(item, playerInv);
if (currRoom == winRoom) {
for (int i = 0; i < winInv.getNumItems(); i++) {
if (rooms[currRoom].findItem(winInv.getItem(i).getName()) == NOT_FOUND) {
return false;
}
return true;
}
return false;
}
}