-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game.cpp
301 lines (226 loc) · 5.62 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
#include "Game.h"
// -------------------- Object class ----------------------
Object::Object(){}
Object::Object(string name, int value)
: name_(name), value_(value) {
owner_ = nullptr;
}
Object::~Object() {
}
string Object::getName() const {
return name_;
}
int Object::getValue() const {
return value_;
}
string Object::print() const {
std::stringstream ss;
ss << getType() << ", name: " << name_ << ", value: " << value_;
return ss.str();
}
void Object::use() {
// IMPLEMENT ME
}
std::ostream& operator<<(std::ostream& os, const Object& obj) {
os << obj.print();
return os;
}
// ------------------- Food class ----------------------
Food::Food(string name, int value) {
name_ = name;
value_ = value;
if (value < 0) {
throw std::invalid_argument("Value must not be negative");
}
}
Food::~Food() {}
string Food::getType() const{
return "Food";
}
void Food::use(){
}
// ------------------- Weapon class ----------------------
Weapon::Weapon(string name, int value) {
name_ = name;
value_ = value;
if (value_ < 0) {
throw std::invalid_argument("Value must not be negative");
}
}
Weapon::~Weapon() {}
string Weapon::getType() const {
return "Weapon";
}
void Weapon::use(){
}
// ------------------- Armour class ----------------------
Armour::Armour(string name, int value) {
name_ = name;
value_ = value;
if (value_ < 0) {
throw std::invalid_argument("Value must not be negative");
}
}
Armour::~Armour() {}
string Armour::getType() const{
return "Armour";
}
void Armour::use(){
}
// ------------------- Player class ----------------------
Player::Player(string name) : name_(name), health_(100), stamina_(100) {
items_ = std::vector<std::unique_ptr<Object>>();
weapon_in_use_ =nullptr;
armours_in_use_ = std::vector<Armour*>();
dead_ = false;
}
Player::~Player() {
items_.clear();
}
string Player::getName() const {
return name_;
}
int Player::getHealth() const {
return health_;
}
int Player::getStamina() const {
return stamina_;
}
void Player::setHealth(int health){
health_ = health;
if (health_ <= 0) {
dead_ = true;
}
}
void Player::pickup(std::unique_ptr<Object> obj) {
if (isDead()) {
throw std::logic_error(name_ + " is already dead.");
}
items_.push_back(std::move(obj));
}
string Player::getInventory() const {
std::stringstream ss;
ss << "List of items:";
if (items_.empty()) {
ss << " none";
}
else {
for (const auto& item : items_) {
ss << "\n " << item->print();
}
}
return ss.str();
}
string Player::print() const {
std::stringstream ss;
ss << "Name: " << name_ << "\nType: " << getType() << "\nHealth: " << health_ << "\nStamina: " << stamina_ << "\nList of items:\n" << getInventory();
if (getType() == "Fighter") {
ss << "\nWeapon in use: ";
if (weapon_in_use_) {
ss << weapon_in_use_->getName();
} else {
ss << "none";
}
}
ss << "\nArmour in use: ";
if (armours_in_use_.size() > 0) {
for (const auto& a : armours_in_use_) {
ss << a->getName() << ", ";
}
} else {
ss << "none";
}
return ss.str();
}
bool Player::use(string name) {
for (auto &object : items_) {
if (object->getName() == name) {
object->use();
return true;
}
}
return false;
}
std::ostream& operator<<(std::ostream& os, const Player& p) {
os << p.print();
return os;
}
bool Player::isDead(){
return dead_;
}
// ------------------- Fighter class ----------------------
Fighter::Fighter(string name) : Player(name){
health_ = 100;
stamina_ = 100;
weapon_in_use_ = nullptr;
armours_in_use_ = std::vector<Armour*>();
dead_ = false;
}
Fighter::~Fighter() {}
bool Fighter::attack(Player& other) {
if (isDead() || other.isDead()) {
if(other.isDead())
throw std::logic_error("Stop " + other.getName() + " is already dead.");
else
throw std::logic_error("Stop " + name_ + " is already dead.");
}
if (stamina_ < 10) {
return false;
}
stamina_ -= 10;
int attack_strength = 10;
if (weapon_in_use_) {
attack_strength += weapon_in_use_->getValue();
}
int defend_strength = 0;
for (const auto& a : armours_in_use_) {
defend_strength += a->getValue();
}
if (attack_strength > defend_strength) {
int damage = attack_strength - defend_strength;
other.health_ -= damage;
if (other.health_ <= 0) {
other.dead_ = true;
other.health_ = 0;
}
return true;
}
return false;
}
std::string Fighter::getType() const{
return "Fighter";
}
// ------------------- Healer class ----------------------
Healer::Healer(string name) : Player(name) {
health_ = 100;
stamina_ = 100;
armours_in_use_ = std::vector<Armour*>();
dead_ = false;
}
Healer::~Healer() {}
bool Healer::heal(Player& other) {
if (isDead() || other.isDead()) {
if(other.isDead())
throw std::logic_error("Stop! " + other.name_ + " is already dead.");
else
throw std::logic_error("Stop! " + name_ + " is already dead.");
}
if (stamina_ < 10) {
return false;
}
stamina_ -= 10;
if (other.health_ <= 0) {
throw std::logic_error("Stop! " + other.name_ + " is already dead.");
}
if (other.health_ == 100) {
return false;
}
other.health_ += 20;
if (other.health_ > 100) {
other.health_ = 100;
}
return true;
}
string Healer::getType() const {
return "Healer";
}