-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextAdventure(Project).cpp
1111 lines (1029 loc) · 36.1 KB
/
TextAdventure(Project).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
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
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Name: Donald Tran
// Date: 03/20/17
// AU User ID: DZT0021
// File Name: Project1.cpp
//
// Instructions to compile:
// g++ -o main -std=c++11 -Wall Project1.cpp
//
// Read Me:
// This program was created using "CLion" IDE and
// compiled using CMake version 3.6 and the g++ compiler on
// Tux051 via SSH protocol
//
#include <iostream>
#include <fstream>
#include <iomanip>
#include <limits>
#undef max
using namespace std;
// Global Class Declarations
class character;
class main_menu;
class game_menu;
class encounter;
class high_scores;
// Global constant
const int MAX_SIZE = 10;
/** Character class containing all data about individual players */
class character{
private:
int intelligence = 15;
int time = 25;
int money = 10;
int position = 0;
public:
char name[21];
int getIntel();
int getTime();
int getMoney();
int getPosition();
int getScore();
void setTime(int newTime);
void setPosition(int newPosition);
void setIntel(int newIntel);
void setMoney(int newMoney);
string getName();
};
void character::setTime(int newTime) {
time = newTime;
}
void character::setPosition(int newPosition) {
position = newPosition;
}
void character::setIntel(int newIntel) {
intelligence = newIntel;
}
void character::setMoney(int newMoney) {
money = newMoney;
}
int character::getIntel() {
return intelligence;
}
int character::getMoney() {
return money;
}
int character::getTime() {
return time;
}
int character::getPosition() {
return position;
}
int character::getScore() {
return intelligence * time * money;
}
string character::getName() {
return name;
}
// Global class instance
character p;
/** Class for high scores which manages all aspects of maintaining
* scores and updating new scores to the "scores.txt" file */
class high_scores{
private:
ifstream fileIn;
string lines;
public:
int scoresArr[MAX_SIZE];
string namesArr[MAX_SIZE];
void addScore();
void readCurrentScores();
void sortScores();
void writeScores();
void printScores();
int getNumberOfScores();
};
int high_scores::getNumberOfScores() {
int numberOfScores = 0;
fileIn.open("scores.txt");
if (fileIn.fail()) {
fileIn.close();
}
while (getline(fileIn, lines)) {
if (numberOfScores >= 10) {
break;
}
if (lines == "") {
break;
}
else {
++numberOfScores;
}
}
fileIn.close();
return numberOfScores;
}
void high_scores::readCurrentScores() {
fileIn.open("scores.txt");
if (fileIn.fail()) {
cout << "\nError during file opening\n\n";
fileIn.close();
}
else {
for (int i = 0; i < MAX_SIZE; i++) {
fileIn >> namesArr[i];
fileIn >> scoresArr[i];
}
}
fileIn.close();
}
void high_scores::addScore() {
int i, j;
int pos = 0;
for (i = 0; i < MAX_SIZE - 1; ++i) {
if (scoresArr[i] <= p.getScore() && p.getScore() < scoresArr[i + 1]) {
pos = i + 1;
break;
}
}
for (j = MAX_SIZE - 1; j > pos; --j) {
scoresArr[j] = scoresArr[j - 1];
namesArr[j] = namesArr[j - 1];
}
scoresArr[pos]= p.getScore();
namesArr[pos] = p.getName();
}
void high_scores::sortScores() {
int i, j, temp1;
string temp2;
for (i = 0; i < MAX_SIZE - 1; ++i)
{
for (j = i + 1; j < MAX_SIZE - 1; ++j)
{
if (scoresArr[i] < scoresArr[j])
{
temp1 = scoresArr[i];
scoresArr[i] = scoresArr[j];
scoresArr[j] = temp1;
temp2 = namesArr[i];
namesArr[i] = namesArr[j];
namesArr[j] = temp2;
}
}
}
}
void high_scores::writeScores() {
ofstream outFile("scores.txt");
if (outFile.is_open()) {
for (int i = 0; i < MAX_SIZE; i++) {
outFile << namesArr[i] << " " << scoresArr[i] << endl;
if (scoresArr[i + 1] == 0) {
break;
}
}
outFile.close();
} else {
cout << "GAME ERROR: There was a problem writing to the score board.\n" << endl;
}
}
void high_scores::printScores() {
string name; //name output to file
int score; //score output to file
int count1 = 0;
int count2 = 0;
int scoreCount = getNumberOfScores();
if (scoreCount == 0) {
cout << "\nNo new high score added.\n\n";
}
else if (scoreCount == 1) {
fileIn.open("scores.txt");
if (fileIn.fail()) {
cout << "\nThere was an error reading high scores.\n\n";
exit(1);
}
cout << "\nThe New top High Score is:\n\n";
while ((fileIn >> name >> score) && count1 < 10) {
cout << "\t";
cout << left << setw(25) << setfill(' ') << name;
cout << left << setw(20) << setfill(' ') << score << endl;
++count1;
}
cout << "\n\t--NO MORE SCORES TO SHOW--\n\n\n";
fileIn.close();
}
else {
fileIn.open("scores.txt");
if (fileIn.fail()) {
cout << "\nThere was an error reading high scores.\n\n";
exit(1);
}
cout << "\nThe New top " << scoreCount << " High Scores are:\n\n";
while ((fileIn >> name >> score) && count2 < 10) {
cout << "\t";
cout << left << setw(25) << setfill(' ') << name;
cout << left << setw(20) << setfill(' ') << score << endl;
++count2;
}
cout << "\n\t--NO MORE SCORES TO SHOW--\n\n\n";
fileIn.close();
}
}
// Global class instance
high_scores hs;
/** Main menu class handles all initial menu options */
class main_menu{
private:
int choice;
ifstream inFile;
string lines;
public:
void getCharName();
void removeNameSpaces(char* s);
void selectMainOption();
void viewTopScores();
int getNumberOfScores();
void startGame();
};
void main_menu::startGame() {
p.setIntel(p.getIntel() + (rand() % 6));
p.setTime(p.getTime() + (rand() % 6));
p.setMoney(p.getMoney() + (rand() % 6));
cout << "\nEntering the game...\n" << endl;
cout << "You have: \n\n";
cout << "Intelligence: " << p.getIntel() << endl;
cout << "Time: " << p.getTime() << endl;
cout << "Money: " << "$" << p.getMoney() << ".00\n\n";
cout << "You are " << (20 - p.getPosition()) << " steps from the goal. ";
cout << "Time left: " << p.getTime() << ".\n\n";
}
int main_menu::getNumberOfScores() {
int numberOfScores = 0;
inFile.open("scores.txt");
if (inFile.fail()) {
inFile.close();
}
while (getline(inFile, lines)) {
if (numberOfScores >= 10) {
break;
}
if (lines == "") {
break;
}
else {
++numberOfScores;
}
}
inFile.close();
return numberOfScores;
}
void main_menu::viewTopScores() {
string name; //name output to file
int score; //score output to file
int count1 = 0;
int count2 = 0;
int scoreCount = getNumberOfScores();
if (scoreCount == 0) {
cout << "\nThere are currently no high scores. Try your luck and be the first.";
}
else if (scoreCount == 1) {
inFile.open("scores.txt");
if (inFile.fail()) {
inFile.close();
cout << "\nThere was an error reading high scores.\n\n";
exit(1);
}
cout << "\nThe top High Score is:\n\n";
while ((inFile >> name >> score) && count1 < 10) {
cout << "\t";
cout << left << setw(25) << setfill(' ') << name;
cout << left << setw(20) << setfill(' ') << score << endl;
++count1;
}
cout << "\n\t--NO MORE SCORES TO SHOW--\n";
inFile.close();
}
else {
inFile.open("scores.txt");
if (inFile.fail()) {
inFile.close();
cout << "\nThere was an error reading high scores.\n\n";
exit(1);
}
cout << "\nThe top " << scoreCount << " High Scores are:\n\n";
while ((inFile >> name >> score) && count2 < 10) {
cout << "\t";
cout << left << setw(25) << setfill(' ') << name;
cout << left << setw(20) << setfill(' ') << score << endl;
++count2;
}
cout << "\n\t--NO MORE SCORES TO SHOW--\n";
inFile.close();
}
}
void main_menu::removeNameSpaces(char* s) {
char* cpy = s; // an alias to iterate through s without moving s
char* temp = s;
while (*cpy)
{
if (*cpy != ' ')
*temp++ = *cpy;
cpy++;
}
*temp = 0;
}
void main_menu::getCharName() {
cout << "What's your name? \n"
"(limit 20 characters, no spaces): ";
cin.getline(p.name, 21);
removeNameSpaces(p.name);
cout << "\n===========================================================================" << endl;
cout << " Welcome, " << p.getName() << "!" << endl;
cout << "===========================================================================";
}
void main_menu::selectMainOption() {
bool valid = false;
int loopCount = 0;
while (!valid) {
cout << "\n\n1) Start a New Game of Shelby Center and Dragons!" << endl;
cout << "2) View top 10 High Scores" << endl;
cout << "3) Quit" << endl;
cout << "\n\tPlease choose an option: ";
cin >> choice;
/** Catches condition where when the input name was longer than 20 characters
choice defaults to zero, causing switch statement to jump to default.
Counts the number of loops this condition is met
and then loops back to the beginning of the while loop if
the condition has been met more than once. Continues otherwise. */
if (choice == 0) {
while (choice == 0) {
if (loopCount == 1)
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\n" << "\nInvalid response. Try again: ";
cin >> choice;
if (choice != 0)
{
break;
}
}
else
{
loopCount++;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin >> choice;
if (choice != 0)
{
break;
}
}
}
}
switch (choice) {
case 1:
startGame();
valid = true;
break;
case 2:
getNumberOfScores();
viewTopScores();
break;
case 3:
cout << "\nYou quit before you even started. How sad and tragic your life must be.\n\n";
exit(1);
default:
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cout << "\n" << "\nInvalid response. Try again.";
continue;
}
continue;
}
}
/** Encounter class handles all possible encounters
* including puzzles */
class encounter{
public:
void faceRandPuzzles();
void faceProfessor();
void faceGradStud();
void faceGruntWork();
void faceGrading();
void faceNothing();
};
void encounter::faceRandPuzzles() {
int rand_answer4 = (rand() % 10 + 1);
int rand_puzzle = (rand() % 5 + 1);
switch (rand_puzzle) {
case 1:
int answer1;
cout << "\nPUZZLE: It’s a riddling imp. I hate riddling imps. But fine, he asks:\n"
"“A murderer has been condemned to death. He has to choose between three rooms.\n"
"The first is full of raging fires, the second with assassins who have loaded guns,\n"
"and the third room is full of lions who haven't eaten in three years.\n"
"Which room is safest for him?“\n\n";
cout << "\t1) Room 1\n\t2) Room 2\n\t3) Room 3\n\t4) Uhh...None?\n\n";
cout << "Choose Wisely (You have 15 seconds to answer this question or lose): ";
cin >> answer1;
if (answer1 == 1) {
cout << "\n\nThe murderer suffers fatal burns. Your answer has cost you the game.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setTime(0);
}
else if (answer1 == 2) {
cout << "\n\nThe murderer is shot by assassins. Your answer has cost you the game.\n"
<< "You lose all remaining time.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setTime(0);
}
else if (answer1 == 3) {
p.setPosition(p.getPosition() + 1);
cout << "\n\nThe murderer is certainly safe in this room. You have been\n"
<< "rewarded with five units of time.\n";
p.setTime(p.getTime() + 5);
if (p.getPosition() >= 20) {
cout << "\nYOU SUCCESSFULLY MADE IT TO THE END OF THE HALL. CONGRATULATIONS!\n\n";
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
}
else if (answer1 == 4) {
cout << "\n\nThe murderer is shot from behind for non-compliance.\n"
<< "Your answer has cost you the game. You forfeit your remaining time.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setTime(0);
}
else {
p.setPosition(p.getPosition() + 1);
cout << "\n\nYou can't even choose from the available options.\n"
<< "You are hopeless, and therefore lose the game by way of forfeiting all remaining time.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setTime(0);
}
break;
case 2:
int answer2;
if ((p.getTime() - 1) <= 0) {
cout << "\nYou have run out of time with nowhere to go!\n\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setTime(p.getTime() - 1);
break;
}
p.setTime(p.getTime() - 1);
cout << "\nPUZZLE: It’s a riddling imp. I hate riddling imps. But fine, he asks:\n"
"“You walk into a room with a rabbit holding a carrot, a pig eating\n"
"slop, and a chimp holding a banana. Which animal in the room is the\n"
"smartest?“\n\n";
cout << "\t1) The Rabbit\n\t2) The Pig\n\t3) The Chimp\n\t4) Uhh...none of those?\n\n";
cout << "Choose Wisely (You have 3 seconds to answer this question or lose): ";
cin >> answer2;
if (answer2 == 1) {
cout << "\n\nThe rabbit is a cunning creature, but not as sharp as another--\n"
<< "or so I thought. Lose 15 units of intelligence.\n";
if ((p.getIntel() - 15) <= 0) {
cout << "\nYou have no more intelligence. Seems accurate.\n\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setIntel(p.getIntel() - 15);
}
}
else if (answer2 == 2) {
p.setPosition(p.getPosition() + 1);
cout << "\n\nThe pig? ... Really? Where did you get that idea. Lose 15\n"
<< "units of intelligence\n";
if ((p.getIntel() - 15) <= 0) {
cout << "\nYou have no more intelligence. Seems accurate.\n\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setIntel(p.getIntel() - 15);
}
}
else if (answer2 == 3) {
p.setPosition(p.getPosition() + 1);
cout << "\n\nWell, the chimp IS a close relative to animal that SHOULD be the smartest.\n"
<< "In any case, you are wrong. You lose 15 units of intelligence\n";
if ((p.getIntel() - 15) <= 0) {
cout << "\nYou have no more intelligence. Seems accurate.\n\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setIntel(p.getIntel() - 15);
}
}
else if (answer2 == 4) {
p.setPosition(p.getPosition() + 1);
cout << "\n\nYou must indeed be the smartest animal in the room. You gain\n"
<< "8 units of intelligence.\n";
p.setIntel(p.getIntel() + 8);
if (p.getPosition() >= 20) {
cout << "\nYOU SUCCESSFULLY MADE IT TO THE END OF THE HALL. CONGRATULATIONS!\n\n";
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
}
else {
cout << "\n\nYou can't even choose from the available options.\n"
<< "You are hopeless, and therefore forfeit all intelligence.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setIntel(0);
}
break;
case 3:
int answer3;
if ((p.getTime() - 1) <= 0) {
cout << "\nYou have run out of time with nowhere to go!\n\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setTime(p.getTime() - 1);
break;
}
p.setTime(p.getTime() - 1);
cout << "\nPUZZLE: Mr. Smith has 4 daughters. Each of his daughters has a brother.\n"
"How many children does Mr. Smith have?\n\n";
cout << "Choose Wisely (Don't think too hard): ";
cin >> answer3;
if (answer3 == 5) {
p.setPosition(p.getPosition() + 1);
cout << "\n\nI guess you can count after all. I will reward you with $5.00...\n"
"for each of Mr. Smith's children.\n";
p.setMoney(p.getMoney() + 25);
if (p.getPosition() >= 20) {
cout << "\nYOU SUCCESSFULLY MADE IT TO THE END OF THE HALL. CONGRATULATIONS!\n\n";
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
}
else if (answer3 != 5) {
cout << "\n\nThis was a rather trivial riddle. You could have even googled the answer.\n"
<< "Your mistake will cost you $5.00 for each of Mr. Smith's daughters...\n";
if ((p.getMoney() - 20) <= 0) {
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
}
break;
case 4:
int answer4;
if ((p.getTime() - 1) <= 0) {
cout << "\nYou have run out of time with nowhere to go!\n\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setTime(p.getTime() - 1);
break;
}
p.setTime(p.getTime() - 1);
cout << "\nGUESS THE NUMBER: I am thinking of a number between 1 - 10.\n"
"Guess the number correctly to advance your position in the hall by two steps.\n"
"Guess incorrectly and go back one step! MUAHAHAHahahahahahaaa...\n\n";
cout << "Test your psychic abilities and enter a number (1 - 10): ";
cin >> answer4;
if (answer4 == rand_answer4) {
cout << "\n\nIMPOSSIBLE! You must be able to speak to the beyond. Watch your back.\n"
<< "You are free to move two steps forward.\n";
p.setPosition(p.getPosition() + 2);
if (p.getPosition() >= 20) {
cout << "\nYOU SUCCESSFULLY MADE IT TO THE END OF THE HALL. CONGRATULATIONS!\n\n";
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
}
if (answer4 != rand_answer4) {
cout << "\n\nYou have affirmed you are nothing more than a mere mortal.\n"
<< "In the words of Gandalf, that means \"YOU SHALL NOT PASS!\"!\n"
<< "Move one step back, loser.\n";
p.setPosition(p.getPosition() - 1);
if (p.getPosition() < 0) {
p.setPosition(0);
}
}
break;
default:
int answer5;
cout << "\nTEST YOUR LOGIC: A man is standing in front of a painting of a man, "
"and he tells us the following: \n"
"\"Brothers and sisters have I none, but this man's father is my father's son\". \n"
"Who is on the painting?\n\n";
cout << "\t1) His father\n\t2) He himself\n\t3) His son\n\t4) His great uncle (once removed)\n\n";
cout << "Choose Wisely (TIME is of the essence): ";
cin >> answer5;
if (answer5 == 1) {
p.setPosition(p.getPosition() + 1);
cout << "\n\nIncorrect. You lose 7 units of time!\n";
if ((p.getTime() - 7) <= 0) {
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
}
p.setTime(p.getTime() - 7);
}
else if (answer5 == 2) {
p.setPosition(p.getPosition() + 1);
cout << "\n\nIncorrect. You lose 7 units of time!\n";
if ((p.getTime() - 7) <= 0) {
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
}
p.setTime(p.getTime() - 7);
}
else if (answer5 == 3) {
p.setPosition(p.getPosition() + 1);
cout << "\n\nWow. I'm impressed. Fine! I'll be nice and give you six units of time...\n";
p.setTime(p.getTime() + 6);
if (p.getPosition() >= 20) {
cout << "\nYOU SUCCESSFULLY MADE IT TO THE END OF THE HALL. CONGRATULATIONS!\n\n";
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
}
else if (answer5 == 4) {
p.setPosition(p.getPosition() + 1);
cout << "\n\nIncorrect. You lose 7 units of time! I thought this would be CLEARLY wrong.\n";
if ((p.getTime() - 7) <= 0) {
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
}
p.setTime(p.getTime() - 7);
}
else {
p.setPosition(p.getPosition() + 1);
cout << "\n\nSuperb fail... You can't even follow directions. \n"
"For that, you lose all remaining time.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
p.setTime(0);
}
break;
}
if (p.getTime() <= 0) {
cout << "\n\nTIME HAS FALLEN TO ZERO. YOU DIE.\n";
// Print new scores
hs.printScores();
exit(1);
}
else if (p.getIntel() <= 0) {
cout << "\n\nINTELLIGENCE HAS FALLEN TO ZERO. YOU DIE.\n";
// Print new scores
hs.printScores();
exit(1);
}
else if (p.getMoney() <= 0) {
cout << "\n\nMONEY HAS FALLEN TO ZERO. YOU DIE.\n";
// Print new scores
hs.printScores();
exit(1);
}
}
void encounter::faceGradStud() {
int rand_time = (rand() % 4 + 1);
cout << "\nYOU RUN INTO A FELLOW GRAD STUDENT!\n\n";
cout << "You lose " << rand_time << " unit(s) of time.\n\n";
if ((p.getTime() - rand_time) <= 0) {
cout << "\nYou have run out of time with nowhere to go!\n\n";
cout << "TIME HAS FALLEN TO ZERO. YOU DIE.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
p.setTime(p.getTime() - rand_time);
p.setPosition(p.getPosition() + 1);
if (p.getPosition() >= 20) {
cout << "\nYOU SUCCESSFULLY MADE IT TO THE END OF THE HALL. CONGRATULATIONS!\n\n";
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
cout << "You are " << (20 - p.getPosition()) << " steps from the goal. ";
cout << "Time left: " << p.getTime() << endl;
}
void encounter::faceProfessor() {
int rand_time = (rand() % 4 + 1);
cout << "\nYOU RUN INTO ONE OF YOUR PROFESSORS!\n\n";
cout << "You spent " << rand_time << " unit(s) of time, but gained 4 units of intelligence.\n\n";
if ((p.getTime() - rand_time) <= 0) {
cout << "\nYou have run out of time with nowhere to go!\n\n";
cout << "TIME HAS FALLEN TO ZERO. YOU DIE.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
p.setTime(p.getTime() - rand_time);
p.setPosition(p.getPosition() + 1);
if (p.getPosition() >= 20) {
cout << "\nYOU SUCCESSFULLY MADE IT TO THE END OF THE HALL. CONGRATULATIONS!\n\n";
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
p.setIntel(p.getIntel() + 4);
cout << "You are " << (20 - p.getPosition()) << " steps from the goal. ";
cout << "Time left: " << p.getTime() << endl;
}
void encounter::faceGrading() {
cout << "\nYOU FIND SOME PAPERS TO GRADE.\n\n";
cout << "You spent two units of time, but gained $5.00!\n\n";
if ((p.getTime() - 2) <= 0) {
cout << "\nYou have run out of time with nowhere to go!\n\n";
cout << "TIME HAS FALLEN TO ZERO. YOU DIE.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
p.setTime(p.getTime() - 2);
p.setPosition(p.getPosition() + 1);
if (p.getPosition() >= 20) {
cout << "\nYOU SUCCESSFULLY MADE IT TO THE END OF THE HALL. CONGRATULATIONS!\n\n";
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
p.setMoney(p.getMoney() + 5);
cout << "You are " << (20 - p.getPosition()) << " steps from the goal. ";
cout << "Time left: " << p.getTime() << endl;
}
void encounter::faceGruntWork() {
cout << "\nAttacked by grunt work!\n\n";
cout << "You lose two units of time AND 3 units of intelligence.\n\n";
if ((p.getTime() - 2) <= 0) {
cout << "\nYou have run out of time with nowhere to go!\n\n";
cout << "TIME HAS FALLEN TO ZERO. YOU DIE.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
if ((p.getIntel() - 3) <= 0) {
cout << "\nYou have run out of intelligence points!\n\n";
cout << "INTELLIGENCE HAS FALLEN TO ZERO. YOU DIE.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
p.setTime(p.getTime() - 2);
p.setPosition(p.getPosition() + 1);
if (p.getPosition() >= 20) {
cout << "\nYOU SUCCESSFULLY MADE IT TO THE END OF THE HALL. CONGRATULATIONS!\n\n";
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
p.setIntel(p.getIntel() - 3);
cout << "You are " << (20 - p.getPosition()) << " steps from the goal. ";
cout << "Time left: " << p.getTime() << endl;
}
void encounter::faceNothing() {
cout << "\nNOTHING HAPPENS!\n\n";
cout << "You spent one unit of time.\n\n";
if ((p.getTime() - 1) <= 0) {
cout << "\nYou have run out of time with nowhere to go!\n\n";
cout << "TIME HAS FALLEN TO ZERO. YOU DIE.\n";
// Updating scoreboard. Player lost.
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
p.setTime(p.getTime() - 1);
p.setPosition(p.getPosition() + 1);
if (p.getPosition() >= 20) {
cout << "\nYOU SUCCESSFULLY MADE IT TO THE END OF THE HALL. CONGRATULATIONS!\n\n";
hs.readCurrentScores();
hs.addScore();
hs.sortScores();
hs.writeScores();
hs.printScores();
exit(1);
}
cout << "You are " << (20 - p.getPosition()) << " steps from the goal. ";
cout << "Time left: " << p.getTime() << endl;
}
// Global class instance
encounter e;
/** Game menu handles all possible actions player could take
* from after starting the game */
class game_menu{
public:
int option;
void viewPlayer();
void boostIntel();
void boostMoney();
void endGame();
void selectGameOption();
void dealEncounters();
};
void game_menu::dealEncounters() {
int rand_case1 = (rand() % 100);
cout << "\nYou move forward one step, and..." << endl;
if (rand_case1 < 10) {
e.faceGrading();
}
else if (rand_case1 >= 10 && rand_case1 < 25) {
e.faceGruntWork();
}
else if (rand_case1 >= 25 && rand_case1 < 35) {
e.faceProfessor();
}
else if (rand_case1 >= 35 && rand_case1 < 50) {
e.faceGradStud();
}
else if (rand_case1 >= 50 && rand_case1 < 75) {
e.faceRandPuzzles();
}
else {
e.faceNothing();
}