Repository navigation
Expand file tree
/
Copy pathProject2.cpp
More file actions
499 lines (468 loc) · 16.5 KB
/
Copy pathProject2.cpp
File metadata and controls
499 lines (468 loc) · 16.5 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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
//----------------------------------------------------------------------------
// CS 215 - Project 2 - MiniMud
//----------------------------------------------------------------------------
// Author: Ethan Gamble
// Date: 3/19/2021
// Description: A MiniMud game where the player has to find the correct item and place them in the win room
//----------------------------------------------------------------------------
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include "gameutils.h"
using namespace std;
//----------------------------------------------------------------------------
// printLogo
//----------------------------------------------------------------------------
void printLogo() {
cout << "+--------------------+\n";
cout << "| MiniMUD game |\n";
cout << "| by: Ethan Gamble |\n";
cout << "+--------------------+\n";
}//printLogo
//----------------------------------------------------------------------------
// getInputFileName
//----------------------------------------------------------------------------
string getInputFileName() {
string fileName;
cout << "Enter the name of game to play: ";
getline(cin, fileName);
fileName = fileName + ".txt";
return fileName;
}//getInputFileName
//----------------------------------------------------------------------------
// initItems
//----------------------------------------------------------------------------
void initItems(item a[], int & numItems) {
for (int i = 0; i < MAX_ITEMS; i++) {
a[i].shortDesc = " ";
a[i].longDesc = " ";
}
numItems = 0;
}//initItems
//----------------------------------------------------------------------------
// initRooms
//----------------------------------------------------------------------------
void initRooms(room a[]) {
for (int i = 0; i < MAX_ROOMS; i++) {
a[i].shortDesc = " ";
a[i].longDesc = " ";
a[i].north = NO_EXIT;
a[i].south = NO_EXIT;
a[i].west = NO_EXIT;
a[i].east = NO_EXIT;
initItems(a[i].items, a[i].numItems);
}
}//initRooms
//----------------------------------------------------------------------------
// initWinData
//----------------------------------------------------------------------------
void initWinData(winDataStruct &w) {
w.winMessage = " ";
w.winRoom = NO_EXIT;
initItems(w.winItem, w.numWinItems);
}//initWinData
//----------------------------------------------------------------------------
// addItem
//----------------------------------------------------------------------------
void addItem(item newItem, item a[], int& numItems) {
string message = "addItem: maximum number of items exceeded!";
if (numItems < MAX_ITEMS) {
numItems++;
a[numItems - 1] = newItem;
}
else {
gameAbort(message);
}
}//addItem
//----------------------------------------------------------------------------
// addItemOverload
//----------------------------------------------------------------------------
void addItem(string shortDesc, string longDesc, item a[], int& numItems) {
item newItem;
newItem.shortDesc = shortDesc;
newItem.longDesc = longDesc;
addItem(newItem, a, numItems);
}// addItemOverload
//----------------------------------------------------------------------------
// doLook
//----------------------------------------------------------------------------
void doLook(room a, bool doShort) {
if (doShort) {
cout << a.shortDesc << endl;
}
else {
cout << a.longDesc << endl;
}
if (a.numItems == 1) {
cout << "You notice the following items: " << a.items[0].shortDesc << "." << endl;
}
else if (a.numItems > 1) {
cout << "You notice the following: ";
for (int i = 0; i < a.numItems - 1; i++) {
cout << a.items[i].shortDesc << ", ";
}
cout << a.items[a.numItems - 1].shortDesc << "." << endl;
}
}//doLook
//----------------------------------------------------------------------------
// doLookOverload
//----------------------------------------------------------------------------
void doLook(room a) {
doLook(a, false);
}
//----------------------------------------------------------------------------
// makeExitList
//----------------------------------------------------------------------------
string makeExitList(room thisRoom) {
string exitList = "You can go:";
if (thisRoom.north != NO_EXIT) exitList += " north,";
if (thisRoom.south != NO_EXIT) exitList += " south,";
if (thisRoom.east != NO_EXIT) exitList += " east,";
if (thisRoom.west != NO_EXIT) exitList += " west.";
exitList[exitList.length() - 1] = '.'; // change last comma to a period
return exitList;
} // makeExitList()
//----------------------------------------------------------------------------
// loadData
//----------------------------------------------------------------------------
void loadData(room rooms[], int& numRooms, winDataStruct& winData) {
// variable declarations
ifstream f; // input file handle
string tag; // first word extracted from each line
string filename; // name of input file
string data; // data read from file using cleanGetline()
int roomNdx = -1; // index of the "current room" being read/populated
// this should always be rumRooms-1
// initialize the rooms and winData
initRooms(rooms);
initWinData(winData);
// get input file name and open the input file; abort on failure
filename = getInputFileName();
f.open(filename);
if (f.fail()) {
gameAbort("Unable to open: ");
cout << filename;
}
// read line-by-line and store data in the game data structures
f >> tag;
while (tag != "END:") {
if (tag == "ROOM:") {
numRooms++; // starting a new room
roomNdx++; // increment every time numRooms is incremented
cleanGetline(f, rooms[roomNdx].shortDesc); // short desc on remainder of ROOM: line
cleanGetline(f, rooms[roomNdx].longDesc); // long desc on next line by itself (no tag)
}
else if (tag == "ITEM:") {
string shortDesc, longDesc;
cleanGetline(f, shortDesc);
cleanGetline(f, longDesc);
addItem(shortDesc, longDesc, rooms[roomNdx].items, rooms[roomNdx].numItems);
}
else if (tag == "WIN_ITEM:") {
cleanGetline(f, data);
addItem(data, "", winData.winItem, winData.numWinItems);
}
else if (tag == "NORTH:")
f >> rooms[roomNdx].north;
else if (tag == "SOUTH:")
f >> rooms[roomNdx].south;
else if (tag == "EAST:")
f >> rooms[roomNdx].east;
else if (tag == "WEST:")
f >> rooms[roomNdx].west;
else if (tag == "WIN_ROOM:")
f >> winData.winRoom;
else if (tag == "REMARK:")
cleanGetline(f, data); // data is junk, throw it away
else if (tag == "WIN_TEXT:")
cleanGetline(f, winData.winMessage);
else if (tag == "END:")
f.close();
else
gameAbort("Unknown tag in " + filename + ": " + tag);
// check for read failure and read the next tag
if (f.fail())
gameAbort("Failure while reading input file " + filename);
f >> tag;
}
} // loadData()
//----------------------------------------------------------------------------
// startUp
//----------------------------------------------------------------------------
void startUp(room rooms[], int& numRooms, item items[], int& numItems, winDataStruct& w) {
printLogo();
printHelp();
loadData(rooms, numRooms, w);
initItems(items, numItems);
cout << "You fall asleep and dream of a place far away...\n";
doLook(rooms[START_ROOM], false);
cout << makeExitList(rooms[START_ROOM]) << endl;
}//startUp
//----------------------------------------------------------------------------
// removeItem
//----------------------------------------------------------------------------
item removeItem(int idx, item a[], int & numItems) {
string message = "removeItem: invalid index = " + idx;
item copy;
if (idx >= 0 && idx < numItems) {
copy = a[idx];
numItems--;
for (int i = idx; i < numItems; i++) {
a[i] = a[i + 1];
}
}
else {
gameAbort(message);
}
return copy;
}//removeItem
//----------------------------------------------------------------------------
// findItem
//----------------------------------------------------------------------------
int findItem(string itemName, item a[], int numItems) {
int idx = NOT_FOUND;
for (int i = 0; i < numItems; i++) {
if (a[i].shortDesc == itemName) {
return i;
}
}
return idx;
}//findItem
//----------------------------------------------------------------------------
// doExamine
//----------------------------------------------------------------------------
void doExamine(string name, item roomInventory[], int numRoomItems, item player[], int numItems) {
int idx;
idx = findItem(name, roomInventory, numRoomItems);
if (idx > -1) {
cout << roomInventory[idx].longDesc << endl;
}
else{
idx = findItem(name, player, numItems);
if (idx > -1) {
cout << player[idx].longDesc << endl;
}
else {
cout << "You see no " << name << endl;
}
}
}//doExamine
//----------------------------------------------------------------------------
// doTake
//----------------------------------------------------------------------------
void doTake(string itemName, item roomInventory[], int & numRoomItems, item player[], int & numItems) {
int idx;
item item;
idx = findItem(itemName, roomInventory, numRoomItems);
if (idx > -1) {
if (numItems < MAX_ITEMS) {
item = removeItem(idx, roomInventory, numRoomItems);
addItem(item, player, numItems);
cout << "You take the " << itemName << endl;
}
else {
cout << "Your hands are full, you can't take that." << endl;
}
}
else {
idx = findItem(itemName, player, numItems);
if (idx > -1) {
cout << "You already have the " << itemName << endl;
}
else {
cout << "You see no " << itemName << endl;
}
}
}//doTake
//----------------------------------------------------------------------------
// doDrop
//----------------------------------------------------------------------------
void doDrop(string itemName, item roomInventory[], int& numRoomItems, item player[], int& numItems) {
int idx;
item item;
idx = findItem(itemName, player, numItems);
if (idx > -1) {
if (numItems <= MAX_ITEMS) {
item = removeItem(idx, player, numItems);
addItem(item.shortDesc,item.longDesc, roomInventory, numRoomItems);
cout << "You drop the " << itemName << endl;
}
else {
cout << "The room is full of junk; you can't drop that.";
}
}
else {
idx = findItem(itemName, roomInventory, numRoomItems);
if (idx != -1) {
cout << "You don't have the " << itemName << endl;
}
else {
cout << "You have no " << itemName << endl;
}
}
}//doDrop
//----------------------------------------------------------------------------
// getCmd
//----------------------------------------------------------------------------
string getCmd(string & remainder) {
string input;
cout << "===> ";
if (cin.peek() == '\n') cin.ignore();
getline(cin, input);
string cmd = split(input, remainder);
return cmd;
}//getCmd
//----------------------------------------------------------------------------
// doInventory
//----------------------------------------------------------------------------
// Given: the player's inventory (partial array of items)
// This is the game's response to the "inv"/"i" (inventory) command.
// Prints a message telling the player what he/she is holding.
//----------------------------------------------------------------------------
void doInventory(item item[], int numItems) {
if (numItems == 0)
cout << "You don't have anything.\n";
else {
cout << "You are holding: ";
for (int i = 0; i < (numItems - 1); i++) // for all but last item
cout << item[i].shortDesc << ", "; // print short desc with comma
cout << item[numItems - 1].shortDesc << ".\n"; // for last item, use period and newline
}
} // doInventory()
//----------------------------------------------------------------------------
// goNorth
//----------------------------------------------------------------------------
// Given: partial array of rooms
// Modifies: index of current room
// This is the game's response to the player entering the "north"/"n" command.
// When the north exit exists, changes the current room index and performs
// a "look" command with a short room description
void goNorth(int& currentRoom, room rooms[]) {
if (rooms[currentRoom].north == NO_EXIT)
cout << "You can't go north.\n";
else {
currentRoom = rooms[currentRoom].north;
doLook(rooms[currentRoom], true); // true = show short room desc.
}
} // goNorth()
//----------------------------------------------------------------------------
// goSouth
//----------------------------------------------------------------------------
void goSouth(int& currentRoom, room rooms[]) {
if (rooms[currentRoom].south == NO_EXIT) {
cout << "You can't go south.\n";
}
else {
currentRoom = rooms[currentRoom].south;
doLook(rooms[currentRoom], true);
}
}//goSouth
//----------------------------------------------------------------------------
// goEast
//----------------------------------------------------------------------------
void goEast(int& currentRoom, room rooms[]) {
if (rooms[currentRoom].east == NO_EXIT) {
cout << "You can't go east.\n";
}
else {
currentRoom = rooms[currentRoom].east;
doLook(rooms[currentRoom], true);
}
}//goEast
//----------------------------------------------------------------------------
// goWest
//----------------------------------------------------------------------------
void goWest(int& currentRoom, room rooms[]) {
if (rooms[currentRoom].west == NO_EXIT) {
cout << "You can't go west.\n";
}
else {
currentRoom = rooms[currentRoom].west;
doLook(rooms[currentRoom], true);
}
}//goWest
//----------------------------------------------------------------------------
// gameWon
//----------------------------------------------------------------------------
bool gameWon(winDataStruct data, room rooms[]) {
int idx;
int item = 0;
bool win = false;
for (int i = 0; i < data.numWinItems; i++) {
idx = findItem(data.winItem[i].shortDesc, rooms[data.winRoom].items, rooms[data.winRoom].numItems);
if (idx != -1) {
item++;
}
}
if (item == data.numWinItems) {
win = true;
}
return win;
}//gameWon
//----------------------------------------------------------------------------
// main
//----------------------------------------------------------------------------
int main() {
// declare data structures
room rooms[MAX_ROOMS];
int numRooms = 0, numItems, roomidx = 0;
item items[MAX_ITEMS];
winDataStruct winData;
int currentRoom = START_ROOM;
// declare other variables
string cmd; // command entered by user (first word of input)
string remainder; // remainder of command entered by user
bool gameWasWon = gameWon(winData,rooms); // check with gameWon() as needed
// start up: load game data from file and initialize player inventory
startUp(rooms,numRooms,items, numItems, winData);
// get first command input
cmd = getCmd(remainder);
// repeat until "exit" entered or the game is won
while (cmd != "exit" && !gameWasWon) {
if (cmd == "help")
printHelp();
// add cases for other commands and invocations
else if (cmd == "look" || cmd == "l") {
doLook(rooms[roomidx], false);
}
else if (cmd == "inv" || cmd == "i") {
doInventory(items, numItems);
}
else if (cmd == "exa" || cmd == "x") {
doExamine(remainder, rooms[roomidx].items, rooms[roomidx].numItems, items, numItems);
}
else if (cmd == "take" || cmd == "t") {
doTake(remainder, rooms[roomidx].items, rooms[roomidx].numItems, items, numItems);
}
else if (cmd == "drop" || cmd == "d") {
doDrop(remainder, rooms[roomidx].items, rooms[roomidx].numItems, items, numItems);
}
else if (cmd == "north" || cmd == "n") {
goNorth(roomidx, rooms);
cout << makeExitList(rooms[roomidx]) << endl;
}
else if (cmd == "south" || cmd == "s") {
goSouth(roomidx, rooms);
cout << makeExitList(rooms[roomidx]) << endl;
}
else if (cmd == "east" || cmd == "e") {
goEast(roomidx, rooms);
cout << makeExitList(rooms[roomidx]) << endl;
}
else if (cmd == "west" || cmd == "w") {
goWest(roomidx, rooms);
cout << makeExitList(rooms[roomidx]) << endl;
}
else // the user entered an unrecognized command
cout << "What??\n";
// check if game was won and print win message or get next user input
gameWasWon = gameWon(winData, rooms);
if (gameWasWon)
cout << winData.winMessage << endl;
else
cmd = getCmd(remainder);
}
system("pause");
return 0;
}