-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.cpp
157 lines (151 loc) · 3.77 KB
/
driver.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
#include <string>
#include <iostream>
#include <fstream>
#include "StrLib.h"
#include "hero.h"
#include "powers.h"
string filename;
vector<Hero*> heroes;
//Read in info from text file, add them to vector
void loadHeroes(string filename, vector<Hero*> &heroes)
{
heroes.clear();
string line = "";
std::ifstream fileInput(filename);
while (getline(fileInput, line))
{
heroes.push_back(new Hero(line));
}
}
//prints out hero names, powers, and health
void printRoster(vector<Hero*> heroes)
{
for (int i = 0; i < heroes.size(); i++)
{
cout << heroes[i]->getname() << endl;
heroes[i]->printHealth();
heroes[i]->printPowers();
cout << "________________________________________________________" << endl;
}
}
//selects hero based on user input
Hero* selectHero(string userInput)
{
if (stoi(userInput))
{
int choice = stoi(userInput);
if (choice >= 0 && choice < heroes.size())
{
return heroes[choice];
}
else
{
cout << "Please enter a number between 1 and " << heroes.size() + 1 << " to indicate your chosen hero" << endl;
}
}
else
{
cout << "Please enter a valid integer between 1 and " << heroes.size() - 1 << endl;
}
return 0;
}
//Uses randomization to have heroes fight each other based on predetermined hierarchy of powers and health
void heroCombat()
{
cout << "The following heroes are available to battle: " << endl;
for (int i = 1; i < heroes.size() + 1; i++)
{
cout << "\t" << i << ". " << heroes[i - 1]->getname() << endl;
}
cout << "Enter a number to choose your first hero!" << endl;
string userInput;
cin >> userInput;
Hero* hero1 = selectHero(userInput);
cout << "You chose " << hero1->getname() << endl;
cout << "Enter a number to choose your second hero!" << endl;
string in2;
cin >> in2;
Hero* hero2 = selectHero(in2);
cout << "You chose " << hero2->getname() << endl;
cout << hero1->getname() << " is preparing to fight " << hero2->getname() << "\n\n" << endl;
while (hero1->getHealth() > 0 && hero2->getHealth() > 0)
{
Powers* power1 = heroes[stoi(userInput) - 1]->useRandomPower();
Powers* power2 = heroes[stoi(in2) - 1]->useRandomPower();
cout << hero1->getname() << " uses their ";
power1->use();
cout << " against " << hero2->getname() << "'s ";
power2->use();
cout << "\n" << endl;
int result = power1->fight(power2);
if (result == 1)
{
hero2->takeDamage();
}
else if (result == -1)
{
hero1->takeDamage();
}
else if (result == 0)
{
hero1->takeDamage();
hero2->takeDamage();
}
cout << "\n" << endl;
hero1->printHealth();
hero2->printHealth();
cout << "__________________________________" << endl;
}
if (hero1->getHealth() == 0)
{
cout << hero2->getname() << " has defeated " << hero1->getname() << " in battle!\n\n" << endl;
}
else if (hero2->getHealth() == 0)
{
cout << hero1->getname() << " has defeated " << hero2->getname() << " in battle!\n\n" << endl;
}
hero1->resetHealth();
hero2->resetHealth();
}
int main()
{
cout << "Welcome to... THE THUNDERDOME!\n" << endl;
bool cont = true;
do
{
//create menu
cout << "Choose one of the following: " << endl;
cout << "\t1. Load heroes\n\t2. Print roster\n\t3. Fight!\n\t4. Quit" << endl;
int choice;
cin >> choice;
if (choice == 1)
{
cout << "\n\nEnter the name of the file containing your heroes' information: ";
cin >> filename;
loadHeroes(filename, heroes);
for (int i = 0; i < heroes.size(); i++)
{
heroes[i]->setPowers();
}
cout << "\n\n" << heroes.size() << " heroes loaded!\n\n" << endl;
}
else if (choice == 2)
{
printRoster(heroes);
}
else if (choice == 3)
{
cout << "Let's get ready to rumble!!!" << endl;
heroCombat();
}
else if (choice == 4)
{
for (int i = 0; i < heroes.size(); i++)
{
delete heroes[i];
}
break;
}
} while (cont = true);
return 0;
}