-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
2842 lines (2383 loc) · 121 KB
/
Game.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
#include <iostream>
#include <fstream>
#include <time.h>
#include <cstdlib>
#include "Game.h"
#include "Hacker.h"
#include "Map.h"
#include "Player.h"
#include "Puzzle.h"
using namespace std;
// constructor
Game::Game()
{
carmen_progress = 0;
current_server_room = 0;
num_hackers_defeated = 0;
room_hackers_defeated = 1;
for(int i = 0; i < num_hackers; i++)
{
hackers[i] = Hacker();
}
Player player = Player("PLAYER");
}
// getters
int Game::getCarmenProgress()
{
return carmen_progress;
}
int Game::getServerRoom()
{
return current_server_room;
}
int Game::getNumHackersDefeated()
{
return num_hackers_defeated;
}
Hacker Game::getHacker(int i)
{
return hackers[i];
}
int Game::getRoomHackersDefeated()
{
return room_hackers_defeated;
}
// setters
void Game::setCarmenProgress(int cp)
{
carmen_progress = cp;
}
void Game::setServerRoom(int r)
{
current_server_room = r;
}
void Game::setNumHackersDefeated(int d)
{
num_hackers_defeated = d;
}
void Game::setRoomHackersDefeated(int rd)
{
room_hackers_defeated = rd;
}
// the methods that control the game
/*
* This function does the setting up of the game. It does this by calling the necessary function for
* start up such as reading the hackers into their array, getting the player's name and player object,
* allowing the player to buy things in the first room, creating the server room, and starting their first turn.
* Parameters: none
* Return: nothing
*/
void Game::start()
{
// initialize a variable to hold the return of the readHackers function
bool read = readHackers();
// check to make sure the hackers were properly read
if(!read)
{
// if not, state that it has failed and return
// (if this does ever happen, the game will need to be restarted)
cout << "Failed, Please Restart" << endl;
return;
}
// declare a variable to hold the player's name and get the players name through their input
string player_name;
cout << "Welcome! Please enter your name: ";
cin >> player_name;
// set their name to be what they inputted
player.setName(player_name);
// welcome the player personally
cout << "Welcome, " << player.getName() << "!" << endl;
// print out what the player needs to do to play the game
cout << "Carmen and her hackers are trying to steal all of the information from 5 server rooms." << endl;
cout << "Your job is to explore the server rooms and defeat the hackers!" << endl;
cout << "You need to do this before you get too frustrated, your computer breaks down, or Carmen reaches 100% progress." << endl;
cout << endl;
cout << "However, you will need to purchase some starting items before you go." << endl;
// print what they currently have inventory wise
cout << "You have 200 dogecoins, 1 computer and 1 VPN." << endl;
cout << "You will need to spend your money wisely, but be warned." << endl;
cout << "As you progress, the prices only increase." << endl;
cout << "(The description of the item is shown when you select it in the menu.)" << endl;
cout << endl;
// send them to best buy to start
bestbuy();
// call the move server room function, but this actually just generates the first map
moveServerRoom();
// start their first turn
turn();
}
/*
* This function displays the current stats of the player and game. It gives their computer's maintenance,
* number of viruses, number of each computer, number of antivirius, number of VPNs, their internet provider
* level, their current amount of dogecoin, their frustration level, carmen's progess, and the number of hackers
* they have defeated.
* Parameters: none
* Return: void, but prints all stats
*/
void Game::displayStats()
{
// print out each individual stat on a separate line with getter functions
cout << "Status Update:" << endl;
cout << "Computer Maintenance Level: " << player.getComputerMaintenance() << endl;
cout << "Number of Viruses in Computer: " << player.hasVirius() << endl;
cout << "Computer Parts Available:" << endl;
cout << "Number of CPUs: " << player.getCPU() << endl;
cout << "Number of GPUs: " << player.getGPU() << endl;
cout << "Number of Power Supply Units: " << player.getPowerSupplyUnits() << endl;
cout << "Number of Computer Cases: " << player.getComputerCases() << endl;
cout << "Number of Internet Cards: " << player.getInternetCards() << endl;
cout << "Number of Keyboards and Mice: " << player.getKeyboardMouse() << endl;
cout << "Number of Antivirus USB Sticks: " << player.getNumAntivirus() << endl;
cout << "VPNs Available: " << player.getVPN() << endl;
cout << "Internet Provider Level: " << player.getInternetLevel() << endl;
cout << "Dogecoin Available: " << player.getDogecoins() << endl;
cout << "Frustration Level: " << player.getFrustration() << endl;
cout << "Carmen’s Progress: " << getCarmenProgress() << endl;
cout << "Number of Hackers Defeated: " << getNumHackersDefeated() << endl;
cout << endl;
}
/*
* This function controls each turn through looping displaying a menu of options and then looping
* until the player quits or the player wins or loses.
* The first action is to fight a hacker, which is done if the player is on a hacker tile. The second
* action is to talk to an NPC, which is done if the player is on a NPC tile. The third action is to repair
* the player's computer. The fourth action is to use antivirus software on the computer to remove viruses.
* The fifth action is to explore the server room (or move server rooms if the player is able to). The sixth
* action is to browse stackoverflow to lower frustration. The seventh action is to quit the game.
* Parameters: none
* Return: void, but will always eventually call the gameEnd function and then break.
*/
void Game::turn()
{
// declare a variable to hold user input
int user_choice = 0;
// do-while loop is perfect because it prints the menu first before switch the user input
do
{
// print out the menu of choices
cout << "==========Actions==========" << endl;
cout << "1. Fight a Hacker" << endl;
cout << "2. Speak to NPC" << endl;
cout << "3. Repair your Computer" << endl;
cout << "4. Use Antivirus Software" << endl;
cout << "5. Explore Server Room (or move server room)" << endl;
cout << "6. Browse StackOverflow" << endl;
cout << "7. Quit" << endl;
// get user input and store in variable declared earlier
cout << "Please Enter an Action: ";
cin >> user_choice;
// switch the user's input
switch(user_choice)
{
// if case 1, then fight hacker
case 1: {
// first make sure the player is located on a hacker
if(!map.isHackerLocation())
{
// otherwise state they are not next to a hacker and break the case
cout << "You are not currently next to a hacker!" << endl;
break;
}
// start a random seed with time(NULL)
srand(time(NULL));
// declare a variable to hold a random number from 0-4
int rand_num = rand() % 4;
// declare a variable to hold the hacker's number in the hackers array
int hacker_number = 0;
// declare a blank hacker to hold the selected hacker
Hacker hack;
// loop until an appropriate hacker is found
do
{
// generate a random number from 0 to 4
rand_num = rand() % 4;
// switch the current server room
switch(current_server_room)
{
// each case correspons to a different server room 1-5
case 1: {
// set the hack to be a hacker with level 1
// (index in the hacker's array 0-3)
hack = hackers[rand_num];
// set the hacker number to be the random number
hacker_number = rand_num;
break;}
case 2: {
// set the hack to be a hacker with level 2
// (index in the hacker's array 4-7)
hack = hackers[rand_num + 4];
// set the hacker number to be the random number
hacker_number = rand_num + 4;
break;}
case 3: {
// set the hack to be a hacker with level 3
// (index in the hacker's array 8-11)
hack = hackers[rand_num + 8];
// set the hacker number to be the random number
hacker_number = rand_num + 8;
break;}
case 4: {
// set the hack to be a hacker with level 4
// (index in the hacker's array 12-15)
hack = hackers[rand_num + 12];
// set the hacker number to be the random number
hacker_number = rand_num + 12;
break;}
case 5: {
// set the hack to be a hacker with level 5
// (index in the hacker's array 16-19)
hack = hackers[rand_num + 16];
// set the hacker number to be the random number
hacker_number = rand_num + 16;
break;}
}
// loop until an alive hacker at the appropriate level is found
}while(!hack.getAlive());
// call the fightHacker with the hack selected earlier and store the result
int fight_result = fightHacker(hack);
// if the fight result is 0, the player won the battle
if(fight_result == 0)
{
// the hacker was defeated, so set them to be not alive, so they will not be encountered again
hackers[hacker_number].setAlive(false);
// print that the player won, get 50 dogecoins, but still lose a computer part
cout << "You WIN!" << endl;
cout << "You get 50 dogecoins, but still manage to drop a computer part." << endl;
// get their current amount of dogecoings and add 50
int current_doge = player.getDogecoins();
player.setDogecoins(current_doge + 50);
// 10% of getting virus
// generate a random number from 0-9
int rand_virius = rand() % 10;
// if the random number is 0, then infect the computer with a virus
if(rand_virius == 0)
{
// increase the current number of viruses by 1
int current_virius = player.hasVirius();
player.setVirius(current_virius + 1);
// print that the player was infected with a virus
cout << "Your computer has been infected with a virius!" << endl;
}
// 30% chance of taking 10 computer maintenance
// generate a random number from 0-9
int rand_main = rand() % 10;
// make sure the server room is not 5
if(current_server_room == 5)
{
// nothing happends if the player is in the last server room
}
// if the random number is 0, 1, or 2, then decrease computer maintenance by 10
// (because if it is either one of these then the player got the 30% chance of dropping 10 maintenance)
else if(rand_main == 0 || rand_main == 1 || rand_main == 2)
{
// decrease the current computer maintenance by 10
int current_maintenance = player.getComputerMaintenance();
player.setComputerMaintenance(current_maintenance - 10);
// print that the computer dropped by 10 maintenance
cout << "Hm, your 'e' key fell off. Your computer's maintenance dropped by 10." << endl;
}
// call the function removeRandomComputerPart to remove the computer part
removeRandomComputerPart();
// increase the number of room hackers defeated by 1
// (the total hackers defeated is )
int current_room_defeated = getRoomHackersDefeated();
setRoomHackersDefeated(current_room_defeated + 1);
}
// if the fight result is -1, the player lost and the hacker won
else if(fight_result == -1)
{
// increase carmen's current progress by 25
int current_carmen = getCarmenProgress();
setCarmenProgress(current_carmen + 25);
// print that the player lost the battle and carmen gained 25 progress and lost 20 maintenance
cout << "You lost the battle! Carmen's progress level has increased by 25 and your computer has lost 20 of its maintenance!" << endl;
// decrease the current computer maintenance by 20
int current_maintenance = player.getComputerMaintenance();
player.setComputerMaintenance(current_maintenance - 20);
// 10% to get a virus
// generate a random number between 0-9
int rand_virius = rand() % 10;
// if the random number is 0, then infect the computer with a virus
if(rand_virius == 0)
{
// increase the current number of viruses by 1
int current_virius = player.hasVirius();
player.setVirius(current_virius + 1);
// print that the player was infected with a virus
cout << "Your computer has been infected with a virius!" << endl;
}
// 30% chance of dropping 10 computer maintenance
// generate a random number from 0-9
int rand_main = rand() % 10;
// make sure the server room is not 5
if(current_server_room == 5)
{
// nothing happends if the player is in the last server room
}
// if the random number is 0, 1, or 2, then decrease computer maintenance by 10
// (because if it is either one of these then the player got the 30% chance of dropping 10 maintenance)
else if(rand_main == 0 || rand_main == 1 || rand_main == 2)
{
// decrease the current computer maintenance by 10
int current_maintenance = player.getComputerMaintenance();
player.setComputerMaintenance(current_maintenance - 10);
// print that the computer dropped by 10 maintenance
cout << "Hm, your 'h' key fell off. Your computer's maintenance dropped by 10." << endl;
}
// call the function removeRandomComputerPart to remove the computer part
removeRandomComputerPart();
}
// if the fight result is -2, the player decided to forfeit
else if(fight_result == -2)
{
// print that they lost all of their spare computer parts
cout << "You decided to forfeit and lost all of your spare computer parts!" << endl;
// set each computer part to 0
player.setCPU(0);
player.setGPU(0);
player.setPowerSupplyUnits(0);
player.setComputerCases(0);
player.setInternetCards(0);
player.setKeyboardMouse(0);
}
break;}
// if case 2, then the player wants to talk to an NPC
case 2: {
// make sure the player is next to an NPC on the map
if(!map.isNPCLocation())
{
// if not, then print that they are not and break the case
cout << "You are not next to a NPC." << endl;
break;
}
// declare a variable to hold whether or not the player wants to play a puzzle or take their chances
bool c = false;
// declare a character to hold the input
char user_input;
// propt the user and accept user input about whether they would like to do a puzzle or take their chances
cout << "Do you want to do the NPC's puzzle or take your chances? (y or n)" << endl;
cin >> user_input;
// check to see if the player inputted yes
if(user_input == 'y')
{
// set that they do want to complete a puzzle
c = true;
}
// otherwise they inputted no or an invalid input
else
{
// set that they want to take their chances with the NPC
c = false;
}
// call the function talkToNPC with the user's choice as a parameter
talkToNPC(c);
break;}
// if case 3, then the player wants to repair their computer
case 3: {
// call the repair computer function in the player class
player.repairComputer();
break;}
// if case 4, then use antivirus software
case 4: {
// check to make sure the player actually has a virus on their computer
if(player.hasVirius() <= 0)
{
// if they don't, then print that and break the case
cout << "You don't have any viriuses on your computer!" << endl;
break;
}
// get the player's current number of antivirus sticks
int num_anti = player.getNumAntivirus();
// make sure the player has an antivirus stick and a virus
if(num_anti > 1 && player.hasVirius() > 1)
{
// decrease the number of antivirus sticks by 1
player.setNumAntivirus(num_anti - 1);
// set the number of virus to 0
player.setVirius(0);
}
// otherwise, the player does not have a stick of antivirus
else
{
// print that the player does not have a stick
cout << "You don't have a stick of antivirius to remove the virus." << endl;
}
break;}
// if case 5, then move around the map for up to 10 turns
case 5: {
// print that they will move 10 times, but will stop at npcs, and bestbuys. A hack will instantly try to fight them
cout << "You get to move 10 times before your next turn and will stop if you encounter an npc, bestbuy, or hacker!" << endl;
// see if the player can move server rooms if the number of room hackers defeated is greater than 1
if(room_hackers_defeated > 0)
{
// declare a variable to hold the player's input
char input;
// propt the player to move server rooms and get user input
cout << "You have defeated at least one hacker in this room, would you like to move server rooms? (y or n)" << endl;
cin >> input;
// check to see if they said yes
if(input == 'y')
{
// if they did, then move rooms and allow them to move around the new room
cout << "You have chosen to move server rooms. Explore the new room!" << endl;
// call the moveServerRoom function
moveServerRoom();
}
}
// declare a variable to hold the player's move
char player_move;
// loop for 10 times
for(int i = 0; i < 10; i++) {
// display the map with the displayMap function
map.displayMap();
// display the valid moves with the displayMoves function
cout << "Valid moves are: " << endl;
map.displayMoves();
// prompt the user and get a valid move
cout << "Input a valid move: ";
cin >> player_move;
cout << endl;
// execute the move with the executeMove function
map.executeMove(player_move);
// check to see if the position the player is now on is a best buy
if (map.isBestBuyLocation()) {
// if it is, say that they have stopped at the best buy
cout << "You have stopped at the Best Buy!" << endl;
// call the bestbuy function
bestbuy();
// break from the case
break;
}
// check to see if the position the player is now next to a hacker
if (map.isHackerLocation()) {
// if they are, then print it
cout << "You ran into one of Carmen's Hackers!" << endl;
// start a random seed with time(NULL)
srand(time(NULL));
// declare a variable to hold a random number from 0-4
int rand_num = rand() % 4;
// declare a variable to hold the hacker's number in the hackers array
int hacker_number = 0;
// declare a blank hacker to hold the selected hacker
Hacker hack;
// loop until an appropriate hacker is found
do
{
// generate a random number from 0 to 4
rand_num = rand() % 4;
// switch the current server room
switch(current_server_room)
{
// each case correspons to a different server room 1-5
case 1: {
// set the hack to be a hacker with level 1
// (index in the hacker's array 0-3)
hack = hackers[rand_num];
// set the hacker number to be the random number
hacker_number = rand_num;
break;}
case 2: {
// set the hack to be a hacker with level 2
// (index in the hacker's array 4-7)
hack = hackers[rand_num + 4];
// set the hacker number to be the random number
hacker_number = rand_num + 4;
break;}
case 3: {
// set the hack to be a hacker with level 3
// (index in the hacker's array 8-11)
hack = hackers[rand_num + 8];
// set the hacker number to be the random number
hacker_number = rand_num + 8;
break;}
case 4: {
// set the hack to be a hacker with level 4
// (index in the hacker's array 12-15)
hack = hackers[rand_num + 12];
// set the hacker number to be the random number
hacker_number = rand_num + 12;
break;}
case 5: {
// set the hack to be a hacker with level 5
// (index in the hacker's array 16-19)
hack = hackers[rand_num + 16];
// set the hacker number to be the random number
hacker_number = rand_num + 16;
break;}
}
// loop until an alive hacker at the appropriate level is found
}while(!hack.getAlive());
// call the fightHacker with the hack selected earlier and store the result
int fight_result = fightHacker(hack);
// if the fight result is 0, the player won the battle
if(fight_result == 0)
{
// the hacker was defeated, so set them to be not alive, so they will not be encountered again
hackers[hacker_number].setAlive(false);
// print that the player won, get 50 dogecoins, but still lose a computer part
cout << "You WIN!" << endl;
cout << "You get 50 dogecoins, but still manage to drop a computer part." << endl;
// get their current amount of dogecoings and add 50
int current_doge = player.getDogecoins();
player.setDogecoins(current_doge + 50);
// 10% of getting virus
// generate a random number from 0-9
int rand_virius = rand() % 10;
// if the random number is 0, then infect the computer with a virus
if(rand_virius == 0)
{
// increase the current number of viruses by 1
int current_virius = player.hasVirius();
player.setVirius(current_virius + 1);
// print that the player was infected with a virus
cout << "Your computer has been infected with a virius!" << endl;
}
// 30% chance of taking 10 computer maintenance
// generate a random number from 0-9
int rand_main = rand() % 10;
// make sure the server room is not 5
if(current_server_room == 5)
{
// nothing happends if the player is in the last server room
}
// if the random number is 0, 1, or 2, then decrease computer maintenance by 10
// (because if it is either one of these then the player got the 30% chance of dropping 10 maintenance)
else if(rand_main == 0 || rand_main == 1 || rand_main == 2)
{
// decrease the current computer maintenance by 10
int current_maintenance = player.getComputerMaintenance();
player.setComputerMaintenance(current_maintenance - 10);
// print that the computer dropped by 10 maintenance
cout << "Hm, your 'e' key fell off. Your computer's maintenance dropped by 10." << endl;
}
// call the function removeRandomComputerPart to remove the computer part
removeRandomComputerPart();
// increase the number of room hackers defeated by 1
// (the total hackers defeated is )
int current_room_defeated = getRoomHackersDefeated();
setRoomHackersDefeated(current_room_defeated + 1);
}
// if the fight result is -1, the player lost and the hacker won
else if(fight_result == -1)
{
// increase carmen's current progress by 25
int current_carmen = getCarmenProgress();
setCarmenProgress(current_carmen + 25);
// print that the player lost the battle and carmen gained 25 progress and lost 20 maintenance
cout << "You lost the battle! Carmen's progress level has increased by 25 and your computer has lost 20 of its maintenance!" << endl;
// decrease the current computer maintenance by 20
int current_maintenance = player.getComputerMaintenance();
player.setComputerMaintenance(current_maintenance - 20);
// 10% to get a virus
// generate a random number between 0-9
int rand_virius = rand() % 10;
// if the random number is 0, then infect the computer with a virus
if(rand_virius == 0)
{
// increase the current number of viruses by 1
int current_virius = player.hasVirius();
player.setVirius(current_virius + 1);
// print that the player was infected with a virus
cout << "Your computer has been infected with a virius!" << endl;
}
// 30% chance of dropping 10 computer maintenance
// generate a random number from 0-9
int rand_main = rand() % 10;
// make sure the server room is not 5
if(current_server_room == 5)
{
// nothing happends if the player is in the last server room
}
// if the random number is 0, 1, or 2, then decrease computer maintenance by 10
// (because if it is either one of these then the player got the 30% chance of dropping 10 maintenance)
else if(rand_main == 0 || rand_main == 1 || rand_main == 2)
{
// decrease the current computer maintenance by 10
int current_maintenance = player.getComputerMaintenance();
player.setComputerMaintenance(current_maintenance - 10);
// print that the computer dropped by 10 maintenance
cout << "Hm, your 'h' key fell off. Your computer's maintenance dropped by 10." << endl;
}
// call the function removeRandomComputerPart to remove the computer part
removeRandomComputerPart();
}
// if the fight result is -2, the player decided to forfeit
else if(fight_result == -2)
{
// print that they lost all of their spare computer parts
cout << "You decided to forfeit and lost all of your spare computer parts!" << endl;
// set each computer part to 0
player.setCPU(0);
player.setGPU(0);
player.setPowerSupplyUnits(0);
player.setComputerCases(0);
player.setInternetCards(0);
player.setKeyboardMouse(0);
}
// break the case, so they stop moving
break;
}
// check to see if the player ran into an NPC
if (map.isNPCLocation()) {
// if they did, then print that they stopped next to them
cout << "You have stopped next to an NPC!" << endl;
// break from the case
break;
}
// there is a chance of misfortune after every step the player takes, so call the misfortune function
// after every step
misfortune();
}
break;}
// if case 6, then browse stack overflow
case 6: {
// call the browseStackOverflow function
browseStackOverflow();
break;}
// if case 7, then the player has decided to quit the game
case 7: {
// print thank you for playing
cout << "Thanks for playing!" << endl;
// call the gameEnd function with 4 as the parameter
gameEnd(4);
// return the void function, so get kicked out of the function
return;
break;}
// if any other input, its invalid, so print that
default: cout << "Please enter an integer between 1 and 7" << endl; break;
}
// after every turn, 30% chance of misfortune, so call misfortune function
misfortune();
// after every turn, frustration goes up by 2
int current_frustration = player.getFrustration();
player.setFrustration(current_frustration + 2);
// after every turn, increase money by 5 and 5 per GPU they have
int current_doge = player.getDogecoins();
player.setDogecoins(current_doge + 5 + (5 * player.getGPU()));
// after every turn, update computer maintenance
int current_maintenance = player.getComputerMaintenance();
player.setComputerMaintenance(current_maintenance + (10 * player.hasVirius()));
// after every turn, check to see if game lost or won
// check to make sure carmen's progress is not at 100
if(getCarmenProgress() >= 100)
{
// if it is, then print that the game has lost
cout << "OH NO! It looks like the hackers have succeed in stealing all of the information out of the servers." << endl;
cout << "Better luck next time!" << endl;
// run the game end function with 1 as the parameter
gameEnd(1);
// return the void function, so get kicked out of the function
return;
}
// check to see if the computer's maintenance is at or below 0
if(player.getComputerMaintenance() <= 0)
{
// if it is at or below 0, then check to see if they have a backup computer
if(player.getBackupComputer())
{
// if they do, then remove the backup computer and set the maintenance to 100
player.setBackupComputer(false);
player.setComputerMaintenance(100);
// print that the computer did run out of maintenance, but that they had a backup
cout << "Your computer just ran out of maintenance, but luckily you had a backup!" << endl;
}
// otherwise, they don't have a backup computer and the game is lost
else
{
// print out that the computer broke down and they have no backup
cout << "Your computer just ran out of maintenance and can no longer function." << endl;
cout << "You didn't have a backup, so you have lost!" << endl;
// call the gameEnd function with 2 as the parameter
gameEnd(2);
// return the void function, so get kicked out of the function
return;
}
}
// check to see if the player has 100 or more frustration
if(player.getFrustration() >= 100)
{
// if they have, then they rage quit the game
// print that they have rage quit
cout << "OH NO! You have rage quit!" << endl;
cout << "Looks like you couldn’t handle Carmen’s hackers." << endl;
// call the gameEnd function with 3 as the parameter
gameEnd(3);
// return the void function, so get kicked out of the function
return;
}
// check to see if the player has won the game by getting to the final server room and defeated a hacker
if(getRoomHackersDefeated() > 0 && current_server_room == 5)
{
// if they have, then print that they won
cout << "YOU WIN!" << endl;
cout << "You managed to defeat a hacker in every room and stop Caren from stealing all of the server information!" << endl;
// increase the hackers defeated by the number of hackers defeated in room 5
int current_hackers_defeated = getNumHackersDefeated();
setNumHackersDefeated(current_hackers_defeated + getRoomHackersDefeated());
// call the gameEnd function with 0 as the parameter
gameEnd(0);
// return the void function, so get kicked out of the function
return;
}
// after every turn, display stats
displayStats();
// loop until the player chooses to quit or the function is returned
}while(user_choice != 7);
}
// this method reads the lines from the hackers.txt file and puts each hacker into the hackers array
// Algorithm:
// 1: open the file
// 2: read each line and split it to put into each hacker
// 3: close the file
// 4: return true if the file could be opened and false otherwise
/*
* This method reads the lines from the hackers.txt file and separates each line into hacker name
* and room number. It then puts this into the hacker array for later use.
* Parameters: No parameters
* Return: true if the array was successfully updated and false otherwise
*/
bool Game::readHackers()
{
// create a variable to read the file
ifstream fin;
// try to open the file
fin.open("hackers.txt");
// check to make sure the file opened and return false if it did not
if(!fin.is_open())
{
return false;
}
// initialize a variable to hold the index in the hackers array
int index = 0;
// declare a string variable to hold each line of the file
string line;
// initialize a variable to hold the size of the split array
int split_size = 2;
// declare a string array to hold the split
string split_line[split_size];
// loop until the end of the file or the hackers array is full
while(!fin.eof() && index < 20)
{
// get each line from the file
getline(fin, line);
// check to make sure it is actually a line
if(!isspace(line[0]) && line.length() > 1)
{
// call the split function
split(line, ',', split_line, split_size);
// set the hacker at the current index to be the two parts of the split array
hackers[index] = Hacker(split_line[0], stoi(split_line[1]));
// increase the current index of the hackers
index++;
}
}
// close the file
fin.close();
// return true because the hackers were successfully uploaded
return true;
}
/*
* This was a testing function for the read hackers method. It simply prints all of the
* hackers names in the order of the array
* Parameters: None
* Return: void, but prints out all of the hackers names currently stored in the hackers array
*/
void Game::printHackers()
{
// loop through the hackers array and print each name
for(int i = 0; i < num_hackers; i++)
{
cout << hackers[i].getName() << endl;
}
}
/*
* This function takes a hacker and then prompts the player if they want to fight the hacker or
* forfeit. If they choose to fight, the hacker's stats are compared against the player and the
* calculation from the write up is done. If the end result is greater than 0, the player wins,
* but if the result is less than 0, the hacker wins. It then returns 0 if the player wins, -1 if
* the hacker wins, and -2 if the player decides to forfeit.
* Parameter: an object of type Hacker
* Return: 0 if the player wins, -1 if the hacker wins, and -2 if the player forfeits
*/
int Game::fightHacker(Hacker hack)
{
// declare a variable to hold user input
int user_input = 0;
// do-while for the menu
do
{
// print the menu and its options
cout << "You have encountered " << hack.getName() << "!" << endl;
cout << "Do you choose to " << endl;
cout << "1. FIGHT" << endl;
cout << "2. FORFEIT" << endl;
// prompt the user and get user input
cout << "Enter an integer:" << endl;
cin >> user_input;
// switch the user input for the 4 options it could be
switch(user_input)
{
// if case 1, the player has decided to fight the hacker
case 1: {
// generate a random seed with time(NULL)
srand(time(NULL));