-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
571 lines (546 loc) · 16.9 KB
/
main.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
#include "monster.h"
#include "PlayerCharacter.h"
#include "ability.h"
#include "StatBlock.h"
#include "Warrior.h"
#include "Rogue.h"
#include "Mage.h"
#include <iostream>
#include <stdlib.h>
#include <string>
#include <conio.h>
#include <fstream>
using namespace std;
// Decided to use these as globals, so I can use these variables in different functions.
string player_name;
string character_name;
int check_health_S = 0;
int check_health_K = 0;
int check_health_B = 0;
char c;
// This is map layout for the player to move in
char the_map1[13][26] = {
"xxxxxxxxxxxxxxxxxxxxxxxxx",
"x xxxx x",
"x xxxx x",
"x xxxx x",
"x xxxx x",
"x xxxx x",
"x xxxx x",
"xx xxxxxxxxxxxxxxxx xxxxx",
"xx xxxxxxxxxxxxxxxx xxxx",
"xx xxxxxxxxxxxxxxxx xxxx",
"xx xxxx",
"xxxxxxxxxxxxxxxxxxxxxxxxx",
"xxxxxxxxxxxxxxxxxxxxxxxxx",
};
// Decided to keep this public so I created a struct. This struct creates a monster for the game.
struct MinionMonster {
MinionMonster(string _name, int hp, int min, int max, int rpos, int cpos, int _expWorth) : monster(hp, min, max) {
xpos = rpos;
ypos = cpos;
expWorth = _expWorth;
name = _name;
}
Monster monster;
bool isLiving() {return (monster.HP.getCurrent() > 0);} //bool to determine when the fight is over.
int xpos;
int ypos;
int expWorth;
string name;
MinionMonster() = delete;
};
// its a way to "clear" the terminal when an action is taken
void clearScreen() {
cout << string(50, '\n');
}
// its the same function as the top one, but this allows you to see the stats from the previous fight turn.
void battleScreen() {
cout << string(2, '\n');
}
// this struct allows the user to create their character
struct Player{
Player(CharacterInfo* char_class) : player(char_class) {}
Player() = delete;
bool isLivin() {return (player.getCurrentHP() > 0);}
PlayerCharacter player;
int prev_xPos = 3;
int prev_yPos = 3;
int xPos = 3;
int yPos = 3;
};
MinionMonster* skeleton = nullptr;
MinionMonster* kobold = nullptr;
Player* mainCharacter = nullptr;
MinionMonster* bowser = nullptr;
void playerMovementOnMap(Player& player1) {
// check that the player didn't move into a wall
if (player1.xPos == player1.prev_xPos && player1.yPos == player1.prev_yPos) {
return;
}
if (the_map1[player1.xPos][player1.yPos] != 'x') {
// draw the character at new location
the_map1[player1.xPos][player1.yPos] = 'P';
// make old location a blank area
the_map1[player1.prev_xPos][player1.prev_yPos] = ' ';
// update current location to be previous before next update
player1.prev_xPos = player1.xPos;
player1.prev_yPos = player1.yPos;
}
else {
player1.xPos = player1.prev_xPos;
player1.yPos = player1.prev_yPos;
}
}
void checkCharacterStats(Player& player1) {
clearScreen();
int checking_stats;
int option_choice;
while(checking_stats == 0) {
cout << endl << "Player Name: " << player_name << endl
<< "Class Name: " << character_name << endl
<< "Level: " << mainCharacter->player.getLevel() << endl
<< "Stats: " << endl
<< "- HP: " << mainCharacter->player.getMaxHP() << endl
<< "- MP: " << mainCharacter->player.getMaxMP() << endl
<< "- STA: " << mainCharacter->player.getMaxSTA() << endl
<< "- STR: " << mainCharacter->player.getBaseStrength() << endl
<< "- INT: " << mainCharacter->player.getBaseIntellect() << endl
<< "- DEX: " << mainCharacter->player.getBaseDexterity() << endl
<< "Press 1 - to return to main menu." << endl;
cin >> option_choice;
switch(option_choice){
case(1):
checking_stats = 1;
break;
default:
break;
}
}
}
void savingGame(Player& player1) {
ofstream saving_file;
int skeleton_alive = 0;
int kobold_alive = 0;
int boss_alive = 0;
if (check_health_S == 0) {
skeleton_alive++;
}
if (check_health_K == 0) {
kobold_alive++;
}
if (check_health_B == 0) {
boss_alive++;
}
saving_file.open("Character_File.txt");
saving_file << player_name << endl
<< player1.player.getClassName() << endl
<< player1.player.getLevel() << endl
<< to_string(player1.xPos) << endl
<< to_string(player1.yPos) << endl
<< to_string(skeleton_alive) << endl
<< to_string(kobold_alive) << endl
<< to_string(boss_alive) << endl;
cout << endl << "Saving File..." << endl;
saving_file.close();
}
void createBattleSequence(Player& player1, MinionMonster& monsters) {
clearScreen();
// This is while loop will check if the player and monster is alive.
while (player1.isLivin() && monsters.isLiving()) {
int fight_choice = 0;
// This while loop will allow the user to select their battle choice. At level 1, you dont have abilities.
// So basic attacks is only thing you can use. But at level 2 you unlock one ability.
while (fight_choice == 0) {
int battle_choice = 0;
battleScreen();
cout << " Player vs " << monsters.name << endl;
cout << "HP: " << player1.player.getCurrentHP() << " / " << player1.player.getMaxHP() << " HP: " << monsters.monster.HP.getCurrent() << " / " << monsters.monster.HP.getMax() << endl;
cout << "MP: " << player1.player.getCurrentMP() << " / " << player1.player.getMaxMP() << " Min Damage: " << monsters.monster.getMinDamage() << " - Max Damage: " << monsters.monster.getMaxDamage() << endl;
cout << "STA: " << player1.player.getCurrentSTA() << " / " << player1.player.getMaxSTA() << endl;
cout << "Battle Options: 1 - attack, 2 - abilities" << endl;
cin >> battle_choice;
switch(battle_choice) {
case(1): // Case 1 provides the basic attack scenario
cout << endl << endl << "Using Basic Attack!" << endl;
monsters.monster.HP.reduceCurrent(player1.player.characterAttack());
fight_choice = 1;
break;
case(2):
vector <Ability> allAbilities = player1.player.getAbilityList();
if (player1.player.getLevel() == 1) {
battleScreen();
cout << endl << endl << " No abilities!" << endl;
cout << "Using Basic Attack!" << endl;
monsters.monster.HP.reduceCurrent(player1.player.characterAttack());
fight_choice = 2;
break;
}
else if (player1.player.getLevel() == 2) {
// This checks if the characters have enough MP or STA to use their ability
if (player1.player.checkForAttribute()) {
player1.player.printNoAttribute();
monsters.monster.HP.reduceCurrent(player1.player.characterAttack());
break;
}
else {
int fighting_choices = 0;
while (fighting_choices == 0) {
cout << endl << "Press 1 to use basic attack!" << endl;
cout << "Press 2 to use abilities!" << endl;
cout << endl << "Abilities:" << endl;
int ability_choice = 0;
int total_damage = 0;
// vector for abilities - provides all the stats of the abilities.
for (auto& elem : allAbilities) {
cout << elem.getName() << "-> "
<< " Health Effect: " << elem.getHealthEffect() << " Cost (MP): " << elem.getMPCost()
<< " Cost (STA): " << elem.getSTACost() << endl;
}
cin >> ability_choice;
switch(ability_choice) {
case(1):
cout << "Using basic Attack!!" << endl;
monsters.monster.HP.reduceCurrent(player1.player.characterAttack());
fight_choice = 2;
fighting_choices = 1;
break;
case(2):
monsters.monster.HP.reduceCurrent(player1.player.calculateAbiliyDamage());
player1.player.reduceAttribute();
fight_choice = 2;
fighting_choices = 2;
break;
default:
break;
}
}
}
}
}
}
if (monsters.monster.HP.getCurrent() > 0) {
if (skeleton->isLiving()) {
int damage_taking = skeleton->monster.Attack();
cout << endl << "Monster Attacks You!" << endl;
player1.player.takeDamage(damage_taking);
}
else if ((kobold->isLiving()) ) {
int damage_taking = kobold->monster.Attack();
cout << endl << "Monster Attacks You!" << endl;
player1.player.takeDamage(damage_taking);
}
else if ((bowser->isLiving()) ) {
int damage_taking = bowser->monster.Attack();
cout << endl << "Monster Attacks You!" << endl;
player1.player.takeDamage(damage_taking);
}
}
}
int dead_choice = 0;
int press_choice;
while (dead_choice == 0) {
if (player1.isLivin() && !skeleton->isLiving()) {
clearScreen();
cout << "You won vs the Monster!\n";
player1.player.gainEXP(skeleton->expWorth);
cout << "exp gained: " << skeleton->expWorth << endl;
player1.player.heal(player1.player.getMaxHP());
cout << "Press 1 - To Continue..." << endl;
check_health_S++;
skeleton->xpos = 0;
skeleton->ypos = 1;
}
if (player1.isLivin() && !kobold->isLiving()) {
clearScreen();
cout << "You won vs the Monster!\n";
player1.player.gainEXP(kobold->expWorth);
cout << "exp gained: " << kobold->expWorth << endl;
player1.player.heal(player1.player.getMaxHP());
if (player1.player.getLevel() == 2) {
cout << "LEVEL UP!!!" << endl;
cout << "Check your status window to see your stats!" << endl;
}
cout << "Press 1 - To Continue..." << endl;
check_health_K++;
kobold->xpos = 0;
kobold->ypos = 2;
}
if (player1.isLivin() && !bowser->isLiving()) {
clearScreen();
cout << "You won vs the Monster!\n";
player1.player.gainEXP(bowser->expWorth);
cout << "exp gained: " << bowser->expWorth << endl;
player1.player.heal(player1.player.getMaxHP());
cout << "Press 1 - To Continue..." << endl;
check_health_B++;
bowser->xpos = 0;
bowser->ypos = 3;
}
if (player1.player.getCurrentHP() == 0) {
cout << "YOU DIED!!!" << endl;
cout << "GAME OVER!!!!" << endl;
cout << "EXITING PROGRAM..." << endl;
exit(0);
}
cin >> press_choice;
switch(press_choice) {
case(1):
dead_choice = 1;
break;
default:
break;
}
}
}
void showmap() {
clearScreen();
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 26; j++) {
cout << the_map1[i][j];
}
cout << endl;
}
cout << endl << "Save Game: v End Game: m Check Character Stats: u" << endl;
}
int main(int argc, char** argv) {
int menu = 0;
int counter = 0;
int input_choice = 0;
int choice = 0;
int character_class = 0;
int character_menu = 0;
cout << " WELCOME TO A BASIC RPG-GAME! " << endl;
while(menu == 0) {
cout << "Press 1 - Load save file Press 2 - Create New Character" << endl;
cin >> input_choice;
switch(input_choice) {
case 1:
cout << "Loading file..." << endl;
menu = 1;
break;
case 2:
// provides an overview the class stats
cout << "Character Name: ";
cin >> player_name;
cout << endl;
cout << "Select Character Class:" << endl;
cout << " 1 - Warrior:" << endl
<< "Uses a sword to attack monsters." << endl
<< " Basic Stats:" << endl
<< "-Health Points (HP): 18" << endl
<< "-Mana Points (MP): 0" << endl
<< "-Stamina Points (STA): 10" << endl
<< "-Strength (STR): 6" << endl
<< "-Intelligence (INT): 2" << endl
<< "-Agility (AGI): 2" << endl;
cout << "---------------------------" << endl;
cout << " 2 - Mage:" << endl
<< "Uses magic to attack monsters." << endl
<< "Basic Stats:" << endl
<< "-Health Points (HP): 10" << endl
<< "-Mana Points (MP): 14" << endl
<< "-Stamina Points (STA): 0" << endl
<< "-Strength (STR): 1" << endl
<< "-Intelligence (INT): 9" << endl
<< "-Agility (AGI): 2" << endl;
cout << "---------------------------" << endl;
cout << " 3 - Rogue:" << endl
<< "Uses a danger to attack monsters." << endl
<< "Basic Stats:" << endl
<< "-Health Points (HP): 14" << endl
<< "-Mana Points (MP): 0" << endl
<< "-Stamina Points (STA): 10" << endl
<< "-Strength (STR): 6" << endl
<< "-Intelligence (INT): 3" << endl
<< "-Agility (AGI): 4" << endl;
// Gives the user the option to select their character.
while(character_menu == 0) {
if (counter > 0) {
cout << "Please select 1 - Warrior, 2 - Mage, 3 - Rogue!"<< endl;
}
cin >> character_class;
switch(character_class) {
case(1):
mainCharacter = new Player(new Warrior());
character_menu = 1;
character_name = "Warrior";
menu = 1;
break;
case(2):
mainCharacter = new Player(new Mage());
character_menu = 1;
character_name = "Mage";
menu = 1;
break;
case(3):
mainCharacter = new Player(new Rogue());
character_menu = 1;
character_name = "Rogue";
menu = 1;
break;
default:
counter++;
break;
}
}
break;
case(0):
menu == 0;
break;
default:
break;
}
}
if (input_choice == 1) {
string line;
ifstream myfile;
vector<string> chrInfo;
myfile.open("Character_File.txt");
if (myfile.is_open()) {
while (getline(myfile, line)) {
chrInfo.push_back(line);
}
myfile.close();
}
else {
cout << "Cannot open the save file!" << endl;
cout << "Exiting game..." << endl;
exit(0);
}
player_name = chrInfo[0];
character_name = chrInfo[1];
int character_level = stoi(chrInfo[2]);
int character_xposition = stoi(chrInfo[3]);
int character_yposition = stoi(chrInfo[4]);
int isSkeleton_alive = stoi(chrInfo[5]);
int isKobold_alive = stoi(chrInfo[6]);
int isBowser_alive = stoi(chrInfo[7]);
if (character_name == "Warrior") {
if (character_level == 1) {
mainCharacter = new Player(new Warrior());
}
else if (character_level == 2) {
mainCharacter = new Player(new Warrior());
mainCharacter->player.gainEXP(100);
}
}
else if (character_name == "Mage") {
if (character_level == 1) {
mainCharacter = new Player(new Mage());
}
else if (character_level == 2) {
mainCharacter = new Player(new Mage());
mainCharacter->player.gainEXP(100);
}
}
else if (character_name == "Rogue") {
if (character_level == 1) {
mainCharacter = new Player(new Rogue());
}
else if (character_level == 2) {
mainCharacter = new Player(new Rogue());
mainCharacter->player.gainEXP(100);
}
}
// if the monsters are alive, they will be place back in same location as last time
// else they are going to placed outside the map.
if (isSkeleton_alive == 1) {
skeleton = new MinionMonster("skeleton", 8, 1, 3, 7, 2, 50);
the_map1[skeleton->xpos][skeleton->ypos] = 'M';
}
else {
skeleton = new MinionMonster("skeleton", 8, 1, 3, 13, 2, 50);
the_map1[skeleton->xpos][skeleton->ypos] = 'M';
}
if (isKobold_alive == 1) {
kobold = new MinionMonster("kobold", 11, 1, 4, 7, 19, 50);
the_map1[kobold->xpos][kobold->ypos] = 'M';
}
else {
kobold = new MinionMonster("kobold", 11, 1, 4, 13, 3, 50);
the_map1[kobold->xpos][kobold->ypos] = 'M';
}
if (isBowser_alive == 1) {
bowser = new MinionMonster("(BOSS) BOWSER", 18, 3, 8, 2, 20, 100);
the_map1[bowser->xpos][bowser->ypos] = 'B';
}
else {
bowser = new MinionMonster("(BOSS) BOWSER", 18, 3, 9, 13, 4, 100);
the_map1[bowser->xpos][bowser->ypos] = 'B';
}
mainCharacter->xPos = character_xposition;
cout << "Break";
mainCharacter->yPos = character_yposition;
the_map1[mainCharacter->xPos][mainCharacter->yPos] = 'P';
}
else if (input_choice == 2) {
skeleton = new MinionMonster("skeleton", 8, 1, 3, 7, 2, 50);
kobold = new MinionMonster("kobold", 11, 1, 4, 7, 19, 50);
bowser = new MinionMonster("(BOSS) BOWSER", 18, 3, 9, 2, 20, 100);
the_map1[bowser->xpos][bowser->ypos] = 'B';
the_map1[skeleton->xpos][skeleton->ypos] = 'M';
the_map1[kobold->xpos][kobold->ypos] = 'M';
the_map1[mainCharacter->xPos][mainCharacter->yPos] = 'P';
}
bool running = true;
showmap();
while(running) {
cout << endl << "move (W/A/S/D): ";
char c = getch();
// player movement
switch (c) {
case 'w':
mainCharacter->xPos--;
break;
case 'a':
mainCharacter->yPos--;
break;
case 's':
mainCharacter->xPos++;
break;
case 'd':
mainCharacter->yPos++;
break;
case 'v':
break;
case 'u':
break;
case 'm':
break;
default:
break;
}
if (c == 'u') {
checkCharacterStats(*mainCharacter);
}
if (c == 'v') {
savingGame(*mainCharacter);
}
if (c == 'm') {
cout << endl << "Exiting Game..." << endl;
delete mainCharacter;
mainCharacter = nullptr;
delete skeleton;
skeleton = nullptr;
delete kobold;
kobold = nullptr;
delete bowser;
bowser = nullptr;
exit(0);
}
// cin.clear();
playerMovementOnMap(*mainCharacter);
if (mainCharacter->xPos == skeleton->xpos && mainCharacter->yPos == skeleton->ypos) {
createBattleSequence(*mainCharacter, *skeleton);
}
else if (mainCharacter->xPos == kobold->xpos && mainCharacter->yPos == kobold->ypos) {
createBattleSequence(*mainCharacter, *kobold);
}
else if (mainCharacter->xPos == bowser->xpos && mainCharacter->yPos == bowser->ypos) {
createBattleSequence(*mainCharacter, *bowser);
}
showmap();
}
return EXIT_SUCCESS;
}