-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
193 lines (175 loc) · 4.58 KB
/
main.cpp
File metadata and controls
193 lines (175 loc) · 4.58 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
#include <concepts>
#include <fstream>
#include <iostream>
#include <map>
#include <ostream>
#include <string>
#include <vector>
class Character;
class PhysicalItem;
class Weapon;
class Potion;
class Spell;
template <typename CurClass>
concept PhysicalDerived = std::is_base_of<CurClass, PhysicalItem>::value;
class Character {
private:
int healthPoints;
std::string name;
protected:
void obtainItemSideEffect(const PhysicalItem&);
void loseItemSideEffect(const PhysicalItem&);
friend std::ostream& operator<<(std::ostream& out, const Character&);
public:
int getHP() const;
std::string getName() const;
void takeDamage(int);
void heal(int);
};
int Character::getHP() const {
return healthPoints;
}
std::string Character::getName() const {
return name;
}
void Character::takeDamage(int damage) {
healthPoints -= damage;
}
void Character::heal(int healVolume) {
healthPoints += healVolume;
}
class PhysicalItem {
private:
bool isUsableOnce;
Character owner;
std::string name;
protected:
Character getOwner;
void useCondition(const Character&, const Character&);
void giveDamageTo(Character&, int);
void giveHealTo(Character&, int);
void afterUse();
virtual void useLogic(const Character&, const Character&) = 0;
friend std::ostream& operator<<(std::ostream&, const PhysicalItem&);
public:
PhysicalItem();
PhysicalItem(const Character&, const std::string&);
void use(const Character&, const Character&);
std::string getName() const;
virtual void setup() = 0;
};
PhysicalItem::PhysicalItem() : isUsableOnce(false), owner(), name(nullptr) {}
PhysicalItem::PhysicalItem(const Character& ch, const std::string& name) : isUsableOnce(false), owner(ch), name(name) {}
std::string PhysicalItem::getName() const {
return name;
}
void PhysicalItem::giveDamageTo(Character &target, int damage) {
target.takeDamage(damage);
}
void PhysicalItem::giveHealTo(Character &target, int healVolume) {
target.heal(healVolume);
}
class Weapon : public PhysicalItem {
private:
int damage;
virtual void useLogic(const Character&, const Character&) override;
public:
int getDamage();
void setup() override;
friend std::ostream& operator<<(std::ostream& out, const Weapon& weapon);
};
int Weapon::getDamage() {
return damage;
}
std::ostream& operator<<(std::ostream& out, const Weapon& weapon) {
out << weapon.getName() << ":" << weapon.damage;
};
class Potion : public PhysicalItem {
private:
int healValue;
virtual void useLogic(const Character&, const Character&) override;
public:
int getHealValue();
void setup() override;
};
int Potion::getHealValue() {
return healValue;
}
class Spell : public PhysicalItem {
private:
std::vector<Character> allowedTargets;
void useLogic(const Character&, const Character&) override;
public:
size_t getNumAllowedTargets();
void setup() override;
};
size_t Spell::getNumAllowedTargets() {
return allowedTargets.size();
}
template <PhysicalDerived T>
class Container {
protected:
std::map<std::string, T> elements;
public:
virtual void add(T);
void remove(T) = 0;
void remove(std::string) = 0;
bool find(T) = 0;
T find(std::string) = 0;
};
template <PhysicalDerived T>
void Container<T>::add(T item) {
std::string itemName = item.getName();
elements[itemName] = item;
}
template <PhysicalDerived T>
void Container<T>::remove(T item) {
if (elements.size() == 0 || !find(item))
throw std::runtime_error("Error caught");
elements.erase(item.getName());
}
template <PhysicalDerived T>
void Container<T>::remove(std::string name) {
if (elements.size() == 0 || !find(name))
throw std::runtime_error("Error caught");
elements.erase(name);
}
template <PhysicalDerived T>
bool Container<T>::find(T item) {
return elements.contains(item.getName());
}
template <PhysicalDerived T>
T Container<T>::find(std::string name) {
if (auto searched = elements.find(name); searched != elements.end())
return searched->second;
return NULL;
}
template <PhysicalDerived T>
class ContainerWithMaxCapacity : public Container<T> {
private:
int maxCapacity;
public:
void add(T) override;
void show(std::ofstream& out);
};
template <PhysicalDerived T>
void ContainerWithMaxCapacity<T>::show(std::ofstream& out) {
bool first = true;
for (T element : Container<T>::elements) {
if (first) {
out << element;
first = false;
} else {
out << ' ' << element;
}
}
out << '\n';
}
template <PhysicalDerived T>
void ContainerWithMaxCapacity<T>::add(T item) {
if (Container<T>::elements.size() == maxCapacity)
throw std::runtime_error("Error caught");
Container<T>::add(item);
}
int main() {
}