-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.cpp
More file actions
685 lines (620 loc) · 19.5 KB
/
Game.cpp
File metadata and controls
685 lines (620 loc) · 19.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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
#include "Game.h"
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
#include <vector> // For vector class
#include "RoomInfo.h"
using namespace std; // So "std::cout" may be abbreviated to "cout"
void Game::createMap()
{
// Map
// n
// ^
// |
// w <--ROOM--> e
// |
// ;
// s
//
// [B0 Kitchen] [D0 Backyard][Exit]
// | |
// [Start] [A1 Entrance] <==> [B1 Living Room] <==> [C1 Dining Room] <==> [D1 Great Room]<==>[E1 Garage]
// | |
// [B2 Bathroom] [C2 Pantry]
//
//
Monster* Mahnoor = new Monster(50, 5); // creating first monster
Monster* Dora = new Monster(15, 4); // creating second monster
Monster* Azra = new Monster(65,6); // creating 3rd monster
Item* HalalBurger = new Item(0, 15); // creating item to increase hp
Item* TrashCan = new Item(1, 1); // creating item to increase defense
Item* PepperSpray = new Item(1, 2); // creating item to increase defense
// Creating a map with 9 rooms, each connected without a cycle and one exit room and entrance.
Room* A1 = new Room("Start Room", nullptr, nullptr, nullptr, nullptr, false);
Room* B1 = new Room("Living Room", nullptr, A1, nullptr, nullptr, false);
A1->setEastRoom(B1);
Room* B0 = new Room("Kitchen", nullptr, nullptr, B1, nullptr, TrashCan);
B1->setNorthRoom(B0);
Room* B2 = new Room("Bathroom", nullptr, nullptr, nullptr, B1, Dora, PepperSpray);
B1->setSouthRoom(B2);
Room* C1 = new Room("Dining Room", B1, nullptr, nullptr, nullptr, Mahnoor);
B1->setEastRoom(C1);
C1->setWestRoom(B1);
Room* C2 = new Room("Pantry", nullptr, nullptr, nullptr, C1, HalalBurger);
C1->setSouthRoom(C2);
Room* D1 = new Room("Great Room", C1, nullptr, nullptr, nullptr, false);
C1->setEastRoom(D1);
D1->setWestRoom(C1);
Room* D0 = new Room("Backyard", nullptr, nullptr, nullptr, D1, true);
D1->setNorthRoom(D0);
Room* E1 = new Room("Garage", nullptr, D1, nullptr, nullptr, Azra);
D1->setEastRoom(E1);
currentRoom = A1;
previousRoom = nullptr;
}
void Game::run()
{
int userInput; string heroNameInput;
cout << "Welcome to Hero Escape!" // Application header w game guide
<< "\n-----------------------" << endl
<< "\nMap Name: Azra's Haunted House" << endl
<< "\nGame's Guide: Move your hero through multiple rooms of the map in order to"
<< "\nreach the MYSTERY exit room. Be AWARE, the rooms throughout the map contain DANGEROUS monsters"
<< "\nsuch as Azra and her SCARY friends, Mahnoor and Dora, who will HURT you at all costs! Look out"
<< "\nfor items to aid in your battle and use your ability to retreat from a room wisely!" << endl;
cout << "\nWhat is your Hero's Name? ";
cin >> heroNameInput;
while (!currentRoom->isExit())
{
/* LAYOUT of WHILE LOOP
// Show room infos
// if Retreating options
// Move to previous room
while(p-> next != null)
{
previous = p;
p = ->next;
}
//Decrease retreat count
// if Hero has items options
// use an item process
// if Hero is alive
// does current room have items if
// picking up items
// execute move process
// if current room is an exit
// display congrats
// else
// game is over
*/
// Show room infos
cout << "\n\nRoom Information" << endl
<< "----------------" << endl;
//Output of map
if (currentRoom->getName() == "Start Room")
{
cout << "\t\t\t [ Kitchen]\t\t\t\t [ Backyard]"
<< "\n\t\t\t\t|\t\t \t\t\t|\n"
<< "[ *HERE* Entrance] <==> [ Living Room] <==> [ Dining Room] <==> [ Great Room]<==>[ Garage]"
<< "\n\t\t\t\t|\t\t |\n"
<< "\t\t\t [ Bathroom]\t\t[ Pantry]\n\n";
}
else if (currentRoom->getName() == "Living Room")
{
cout << "\t\t\t [ Kitchen]\t\t\t\t [ Backyard]"
<< "\n\t\t\t\t|\t\t \t\t\t|\n"
<< "[ Entrance] <==> [ *HERE* Living Room] <==> [ Dining Room] <==> [ Great Room]<==>[ Garage]"
<< "\n\t\t\t\t|\t\t |\n"
<< "\t\t\t [ Bathroom]\t\t[ Pantry]\n\n";
}
else if (currentRoom->getName() == "Kitchen")
{
cout << "\t\t [ *HERE* Kitchen]\t\t\t [ Backyard]"
<< "\n\t\t\t|\t\t \t\t\t|\n"
<< "[ Entrance] <==> [ Living Room] <==> [ Dining Room] <==> [ Great Room]<==>[ Garage]"
<< "\n\t\t\t|\t\t |\n"
<< "\t\t [ Bathroom]\t\t[ Pantry]\n\n";
}
else if (currentRoom->getName() == "Bathroom")
{
cout << "\t\t [ Kitchen]\t\t\t\t [ Backyard]"
<< "\n\t\t\t|\t\t \t\t\t|\n"
<< "[ Entrance] <==> [ Living Room] <==> [ Dining Room] <==> [ Great Room]<==>[ Garage]"
<< "\n\t\t\t|\t\t |\n"
<< "\t\t [ *HERE* Bathroom]\t [ Pantry]\n\n";
}
else if (currentRoom->getName() == "Dining Room")
{
cout << "\t\t [ Kitchen]\t\t\t\t\t [ Backyard]"
<< "\n\t\t\t|\t\t \t\t\t\t|\n"
<< "[ Entrance] <==> [ Living Room] <==> [ *HERE* Dining Room] <==> [ Great Room]<==>[ Garage]"
<< "\n\t\t\t|\t\t |\n"
<< "\t\t [ Bathroom]\t\t [ Pantry]\n\n";
}
else if (currentRoom->getName() == "Pantry")
{
cout << "\t\t [ Kitchen]\t\t\t\t [ Backyard]"
<< "\n\t\t\t|\t\t \t\t\t|\n"
<< "[ Entrance] <==> [ Living Room] <==> [ Dining Room] <==> [ Great Room]<==>[ Garage]"
<< "\n\t\t\t|\t\t |\n"
<< "\t\t [ Bathroom]\t\t[ *HERE* Pantry]\n\n";
}
else if (currentRoom->getName() == "Great Room")
{
cout << "\t\t [ Kitchen]\t\t\t\t [ Backyard]"
<< "\n\t\t\t|\t\t \t\t\t|\n"
<< "[ Entrance] <==> [ Living Room] <==> [ Dining Room] <==> [ *HERE* Great Room]<==>[ Garage]"
<< "\n\t\t\t|\t\t |\n"
<< "\t\t [ Bathroom]\t\t[ Pantry]\n\n";
}
else if (currentRoom->getName() == "Garage")
{
cout << "\t\t [ Kitchen]\t\t\t\t [ Backyard]"
<< "\n\t\t\t|\t\t \t\t\t|\n"
<< "[ Entrance] <==> [ Living Room] <==> [ Dining Room] <==> [ Great Room]<==>[ *HERE* Garage]"
<< "\n\t\t\t|\t\t |\n"
<< "\t\t [ Bathroom]\t\t[ Pantry]\n\n";
}
else
{
continue;
}
// Display items, monster, and hero characteristics
cout << "Name: " << currentRoom->getName() << endl
<< "Monster: ";
if (currentRoom->getRoomMonster() == nullptr)
{
cout << "None\n";
}
else
{
if (currentRoom->getRoomMonster()->getHP() > 20 && currentRoom->getRoomMonster()->getHP() < 55)
{
cout << "Mahnoor ~ ";
}
else if (currentRoom->getRoomMonster()->getHP() < 20)
{
cout << "Dora ~ ";
}
else
{
cout << "Azra ~ ";
}
cout << "HP " << currentRoom->getRoomMonster()->getHP() <<
" Attack Power: " << currentRoom->getRoomMonster()->getAttackPower() << endl;
}
cout << "Item: ";
if (!currentRoom->getRoomItem())
{
cout << "None\n";
}
else
{
if (currentRoom->getRoomItem()->isHP())
{
if (currentRoom->getRoomItem()->getValue() == 15)
{
cout << "Halal Burger" << " HP: " << currentRoom->getRoomItem()->getValue() << endl;
}
}
else
{
if (currentRoom->getRoomItem()->getValue() == 1)
{
cout << "Trash Can" << " Defense Power: " << currentRoom->getRoomItem()->getValue() << endl;
}
else
{
cout << "Pepper Spray " << "Defense Power: " << currentRoom->getRoomItem()->getValue() << endl;
}
}
}
cout << "----------------" << endl;
cout << "\n" << heroNameInput <<": HP: " << HeroName.getHP() << " Attack Power: " << HeroName.getAttackPower()
<< " Defense Power: " << HeroName.getDefensePower() << " Retreat Count: " << HeroName.getRetreatCount() << endl;
// if Retreating options
if (previousRoom != nullptr)
{
if (HeroName.getRetreatCount() > 0) {
cout << "\nWould you like to retreat? [yes/no]: ";
string optionChoice = "";
cin >> optionChoice;
if (optionChoice == "yes" || optionChoice == "YES" ||
optionChoice == "Yes" || optionChoice == "YeS" ||
optionChoice == "yEs" || optionChoice == "yeS" ||
optionChoice == "YEs" || optionChoice == "yES" ||
optionChoice == "y" || optionChoice == "Y") {
//Move to previous room using swap
Room* temp;
temp = currentRoom;
currentRoom = previousRoom;
previousRoom = temp;
//decrease retreat count
HeroName.decreaseRetreatCount();
continue;
}
}
}
// if Hero has items options
if (HeroName.anyItems()) {
cout << "\nWould you like to use an item? [yes/no] ";
string itemChoice = "";
cin >> itemChoice;
if (itemChoice == "yes" || itemChoice == "YES" ||
itemChoice == "Yes" || itemChoice == "YeS" ||
itemChoice == "yEs" || itemChoice == "yeS" ||
itemChoice == "YEs" || itemChoice == "yES" ||
itemChoice == "y" || itemChoice == "Y") {
// use an item process
/* display dynamically, not hardcoded
1. Create the menu based on the information of the bag
2. NULL values can be shown as empty spaces.
3. If user select an empty SPACE, just let the user know that that cannot be used, and continue the game.
4. If use selects a non-empty SPACE:
4a. Apply the item on the hero
4b. Remove the item from memory.
*/
for (int i = 0; i < 3; i++)
{
if (HeroName.bag[i] == nullptr)
{
cout << i + 1 << ". Empty" << endl;
}
else
{
if (HeroName.bag[i]->isHP())
{
if (HeroName.bag[i]->getValue() == 15)
{
cout << i + 1 << ". Halal Burger" << endl;
}
}
else
{
if (HeroName.bag[i]->getValue() == 1)
{
cout << i + 1 << ". Trash Can" << endl;
}
else
{
cout << i + 1 << ". Pepper Spray" << endl;
}
}
}
}
int choice;
std::cout << "choice: ";
std::cin >> choice;
std::cout << "\n";
while (HeroName.bag[choice - 1] == nullptr)
{
cout << "There is no item in " << choice << endl;
std::cout << "choice: ";
std::cin >> choice;
std::cout << "\n";
}
switch (choice) {
case 1:
if (HeroName.bag[choice - 1]->isHP())
{
if (HeroName.bag[choice - 1]->getValue() == 15)
{
cout << "You ate Halal Burger! HP increased by 15" << endl;
HeroName.setHP(HeroName.getHP() + 15);
HeroName.bag[choice - 1] = nullptr;
break;
}
}
else
{
if (HeroName.bag[choice - 1]->getValue() == 1)
{
cout << "You equipped Trash Can! Defense increased by 1" << endl;
HeroName.setDefensePower(HeroName.getDefensePower() + 1);
HeroName.bag[choice - 1] = nullptr;
break;
}
else
{
cout << "You equipped Pepper Spray! Defense increased by 2" << endl;
HeroName.setDefensePower(HeroName.getDefensePower() + 2);
HeroName.bag[choice - 1] = nullptr;
break;
}
}
case 2:
if (currentRoom->getRoomItem()->isHP())
{
if (currentRoom->getRoomItem()->getValue() == 15)
{
cout << "You ate Halal Burger! HP increased by 15";
HeroName.setHP(HeroName.getHP() + 15);
HeroName.bag[choice - 1] = nullptr;
break;
}
}
else
{
if (currentRoom->getRoomItem()->getValue() == 1)
{
cout << "You equipped Trash Can! Defense increased by 1";
HeroName.setDefensePower(HeroName.getDefensePower() + 1);
HeroName.bag[choice - 1] = nullptr;
break;
}
else
{
cout << "You equipped Pepper Spray! Defense increased by 2";
HeroName.setDefensePower(HeroName.getDefensePower() + 2);
HeroName.bag[choice - 1] = nullptr;
break;
}
}
case 3:
if (HeroName.bag[choice - 1]->isHP())
{
if (HeroName.bag[choice - 1]->getValue() == 15)
{
cout << "You ate Halal Burger! HP increased by 15" << endl;
HeroName.setHP(HeroName.getHP() + 15);
HeroName.bag[choice - 1] = nullptr;
break;
}
}
else
{
if (HeroName.bag[choice - 1]->getValue() == 1)
{
cout << "You equipped Trash Can! Defense increased by 1" << endl;
HeroName.setDefensePower(HeroName.getDefensePower() + 1);
HeroName.bag[choice - 1] = nullptr;
break;
}
else
{
cout << "You equipped Pepper Spray! Defense increased by 2" << endl;
HeroName.setDefensePower(HeroName.getDefensePower() + 2);
HeroName.bag[choice - 1] = nullptr;
break;
}
}
default:
break;
}
}
}
// Hero vs Monster battle
if (currentRoom->getRoomMonster() != nullptr)
{
int status = 0;
if (currentRoom->getRoomMonster()->getHP() > 20 && currentRoom->getRoomMonster()->getHP() < 55)
{
cout << "\nMonster Mahnoor";
}
if (currentRoom->getRoomMonster()->getHP() < 20 && currentRoom->getRoomMonster()->getHP() > 0)
{
cout << "\nMonster Dora";
}
if (currentRoom->getRoomMonster()->getHP() > 50)
{
cout << "\nMonster Azra";
}
cout << " Approached, Battle Begins!\n" << endl;
while (status == 0)
{
status = HeroName * (*(currentRoom->getRoomMonster())); // Hero and Monster battle
cout << "Pow!!" << "\tBam!" << endl << "\t\tBoom!" << "\t\tKaboom!!!" << endl; // battle action words
// output of hero hp and dp & Monster hp and ap
cout << "\n" << heroNameInput << ": HP: " << HeroName.getHP() << " Defense Power: " << HeroName.getDefensePower() << endl;
cout << "Monster: HP " << currentRoom->getRoomMonster()->getHP() <<
" Attack Power: " << currentRoom->getRoomMonster()->getAttackPower() << endl << endl;
cout << "Wham!" << "\t\tBang!!!" << endl << "\tWow!" << "\t\t\tCrash!!" << endl; //battle action words
}
if (status == 1)
{
cout << "\nThe Monster has been defeated!" << endl;
currentRoom->setRoomMonster(nullptr);
}
if (status == -1)
{
cout << "\nThe Monster has won the battle!" << endl;
currentRoom->setRoomMonster(nullptr);
break;
}
}
// if Hero is alive
if (HeroName.isAlive()) {
// does current room have any items
if (currentRoom->getRoomItem() != nullptr) {
// want to pick it up
cout << "\nWould you like to pick up ";
if (currentRoom->getRoomItem()->isHP())
{
if (currentRoom->getRoomItem()->getValue() == 15)
{
cout << "Halal Burger";
}
}
else
{
if (currentRoom->getRoomItem()->getValue() == 1)
{
cout << "Trash Can";
}
else
{
cout << "Pepper Spray";
}
}
// execute pick up item
cout << " [yes/no]: ";
string pickUpChoice;
cin >> pickUpChoice;
if (pickUpChoice == "yes" || pickUpChoice == "YES" ||
pickUpChoice == "Yes" || pickUpChoice == "YeS" ||
pickUpChoice == "yEs" || pickUpChoice == "yeS" ||
pickUpChoice == "YEs" || pickUpChoice == "yES" ||
pickUpChoice == "y" || pickUpChoice == "Y") {
cout << "\nBag: " << endl;
for (int i = 0; i < 3; i++)
{
if (HeroName.bag[i] == nullptr)
{
cout << i + 1 << ". Empty" << endl;
}
else
{
if (HeroName.bag[i]->isHP())
{
if (HeroName.bag[i]->getValue() == 15)
{
cout << i + 1 << ". Halal Burger" << endl;
}
}
else
{
if (HeroName.bag[i]->getValue() == 1)
{
cout << i + 1 << ". Trash Can" << endl;
}
else
{
cout << i + 1 << ". Pepper Spray" << endl;
}
}
}
}
cout << "\nWhich position would you like to store the item? ";
int position;
std::cin >> position;
switch (position) {
/*
assume valid input
User selects option X
if bag[X] is null
bag[X] = currentRoom.getItem();
currentRoom.setItem(nullptr);
else bag[X] is not null
delete bag[X];
bag[X] = currentRoom.getItem();
currentRoom.setItem(nullptr);
*/
case 1:
if (HeroName.bag[0] == nullptr)
{
HeroName.bag[0] = currentRoom->getRoomItem();
currentRoom->setRoomItem(nullptr);
}
else
{
delete HeroName.bag[0];
HeroName.bag[0] = currentRoom->getRoomItem();
currentRoom->setRoomItem(nullptr);
}
break;
case 2:
if (HeroName.bag[1] == nullptr)
{
HeroName.bag[1] = currentRoom->getRoomItem();
currentRoom->setRoomItem(nullptr);
}
else
{
delete HeroName.bag[1];
HeroName.bag[1] = currentRoom->getRoomItem();
currentRoom->setRoomItem(nullptr);
}
break;
case 3:
if (HeroName.bag[2] == nullptr)
{
HeroName.bag[2] = currentRoom->getRoomItem();
currentRoom->setRoomItem(nullptr);
}
else
{
delete HeroName.bag[2];
HeroName.bag[2] = currentRoom->getRoomItem();
currentRoom->setRoomItem(nullptr);
}
break;
}
}
}
}
// execute move process
//For each option: include direction, name of room, and memory address
vector<RoomInfo> menuObj;
RoomInfo Ntemp, Stemp, Etemp, Wtemp; // To store in vector
int countTemp = 0;
// Creating a menu
cout << "\nRoom: " << currentRoom->getName() << endl;
if (currentRoom->getNorthRoom() != nullptr)
{
Ntemp.direction = "North";
Ntemp.roomName = currentRoom->getNorthRoom()->getName();
Ntemp.roomAdress = currentRoom->getNorthRoom();
menuObj.push_back(Ntemp);
countTemp++;
}
if (currentRoom->getSouthRoom() != nullptr)
{
Stemp.direction = "South";
Stemp.roomName = currentRoom->getSouthRoom()->getName();
Stemp.roomAdress = currentRoom->getSouthRoom();
menuObj.push_back(Stemp);
countTemp++;
}
if (currentRoom->getEastRoom() != nullptr)
{
Etemp.direction = "East";
Etemp.roomName = currentRoom->getEastRoom()->getName();
Etemp.roomAdress = currentRoom->getEastRoom();
menuObj.push_back(Etemp);
countTemp++;
}
if (currentRoom->getWestRoom() != nullptr)
{
Wtemp.direction = "West";
Wtemp.roomName = currentRoom->getWestRoom()->getName();
Wtemp.roomAdress = currentRoom->getWestRoom();
menuObj.push_back(Wtemp);
countTemp++;
}
// Displaying a menu
for (int i = 0; i < menuObj.size(); i++)
{
cout << i + 1 << ") "; //option num
cout << menuObj.at(i).direction << ": "; // direction
cout << menuObj.at(i).roomName << endl; // room name
}
if (countTemp == 1)
{
cout << "Select a direction [Enter 1 only]: ";
cin >> userInput;
}
else
{
cout << "Select a direction [Enter between 1-" << countTemp << "]: ";
cin >> userInput;
}
previousRoom = currentRoom; // store current room in prev room before update
currentRoom = menuObj.at(userInput - 1).roomAdress; // Assume a valid option is entered
}
// if current room is an exit
if (currentRoom->isExit()) {
//display congrats
std::cout << "\nCongratulations you found the mystery exit room of Backyard!\n";
}
// else
//game is over
else {
std::cout << "\nGame over ~ You Lost!\n";
}
};
Game::Game() : HeroName(49, 5, 2, 1)
{
createMap();
}