-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassignment7.cpp
More file actions
201 lines (174 loc) · 4.16 KB
/
assignment7.cpp
File metadata and controls
201 lines (174 loc) · 4.16 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
/*Date: 6/18/2019
The function is creating a game.
In this game we pick up two of four different types
of creatures to have a flight.*/
#include <iostream>
#include<string>
#include<ctime>
using namespace std;
class Creature {
private:
int strength;
int hitpoints;
public:
Creature();
Creature(int newStrength, int newHit);
void setStrength(int);
void setHit(int);
int getStrength();
int getHit();
virtual int getDamage();
virtual string getSpecies();
};
Creature::Creature() {
strength = 10;
hitpoints = 10;
}
Creature::Creature(int newStrength, int newHit) {
strength = newStrength;
hitpoints = newHit;
}
void Creature::setStrength(int newStrength) {
strength = newStrength;
}
void Creature::setHit(int newHit) {
hitpoints = newHit;
}
int Creature::getStrength() {
return strength;
}
int Creature::getHit() {
return hitpoints;
}
string Creature::getSpecies() {
return "Unknown";
}
int Creature::getDamage() {
int damage;
damage = (rand() % strength) + 1;
cout << getSpecies() << " attacks for " <<
damage << " points!" << endl;
return damage;
}
class Demon : public Creature {
public:
Demon() {}
Demon(int newStrength, int newHit) : Creature(newStrength, newHit) {}
int getDamage();
};
int Demon::getDamage() {
int damage = Creature::getDamage();
if ((rand() % 100) < 5) {
damage = damage + 50;
cout << "Demonic attack inflicts 50 "
<< " additional damage points!" << endl;
}
return damage;
}
class Elf : public Creature {
public:
Elf() {}
Elf(int newStrength, int newHit) : Creature(newStrength, newHit) {}
int getDamage();
string getSpecies();
};
string Elf::getSpecies() {
return "Elf";
}
int Elf::getDamage() {
int damage = Creature::getDamage();
if ((rand() % 10) == 0)
{
cout << "Magical attack inflicts " << damage <<
" additional damage points!" << endl;
damage = damage * 2;
}
return damage;
}
class Human : public Creature {
public:
Human() {}
Human(int newStrength, int newHit) : Creature(newStrength, newHit) {}
string getSpecies();
};
string Human::getSpecies() {
return "Human";
}
class Cyberdemon : public Demon {
public:
Cyberdemon() {}
Cyberdemon(int newStrength, int newHit) : Demon(newStrength, newHit) {}
string getSpecies();
};
string Cyberdemon::getSpecies() {
return "Cyberdemon";
}
class Balrog : public Demon {
public:
Balrog() {}
Balrog(int newStrength, int newHit) : Demon(newStrength, newHit) {}
int getDamage();
string getSpecies();
};
string Balrog::getSpecies() {
return "Balrog";
}
int Balrog::getDamage() {
int damage = Demon::getDamage();
int damage2 = (rand() % getStrength()) + 1;
cout << "Balrog speed attack inflicts " << damage2 <<
" additional damage points!" << endl;
damage = damage + damage2;
return damage;
}
void battleArena(Creature &creature1, Creature &creature2);
int main() {
srand(static_cast<int>(time(NULL)));
Human human1(30, 10);
human1.getDamage();
cout << endl;
Elf elf1;
elf1.getDamage();
cout << endl;
Balrog balrog1(50, 50);;
balrog1.getDamage();
cout << endl;
Cyberdemon cdemon(30, 40);
cdemon.getDamage();
cout << endl;
Elf elf2(50, 50);
Balrog balrog2(50, 50);
cout << endl;
battleArena(elf2, balrog2);
return 0;
}
void battleArena(Creature &creature1, Creature &creature2) {
do {
creature2.setHit(creature2.getHit() - creature1.getDamage());
cout << "Creature 2 has " << creature2.getHit() << " hit points." << endl;
creature1.setHit(creature1.getHit() - creature2.getDamage());
cout << "Creature 1 has " << creature1.getHit() << " hit points." << endl;
} while (creature1.getHit() > 0 && creature2.getHit());
cout << endl;
if (creature1.getHit() > creature2.getHit())
cout << "Creature 1 wins!" << endl;
else if (creature1.getHit() < creature2.getHit())
cout << "Creature 2 wins!" << endl;
else
cout << "It's tied." << endl;
}
/*
Sample run:
Human attacks for 26 points!
Elf attacks for 7 points!
Balrog attacks for 21 points!
Balrog speed attack inflicts 34 additional damage points!
Cyberdemon attacks for 24 points!
Elf attacks for 3 points!
Creature 2 has 47 hit points.
Balrog attacks for 49 points!
Balrog speed attack inflicts 15 additional damage points!
Creature 1 has -14 hit points.
Creature 2 wins!
Press any key to close this window . . .
*/