-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArmy.cpp
More file actions
140 lines (112 loc) · 3.25 KB
/
Army.cpp
File metadata and controls
140 lines (112 loc) · 3.25 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
#include "Army.h"
#include <numeric>
// Constructor
Army::Army(const std::string& name, int pikemen, int archery, int cavalry){ // Initialize name using member initializer list
this->armyID = name;
for (int i = 0; i < pikemen; ++i) {
addUnit(Pikemen());
}
for (int i = 0; i < archery; ++i) {
addUnit(Archer());
}
for (int i = 0; i < cavalry; ++i) {
addUnit(Cavalry());
}
}
// Helper methods
bool Army::canAffordPayment(int cost) const {
return coins >= cost;
}
void Army::addUnit(const Unit& unit) {
units.push_back(unit);
}
int Army::getTotalArmyStrength() const {
int res = 0;
for (auto unit: units) {
res += unit.getStrength();
}
return res;
}
void Army::handleBattleWinner(Army& winner, Army& loser) {
winner.addCoins(100);
removeStrongestUnits(loser, 2);
}
void Army::removeStrongestUnits(Army& army, int count) {
for(int i = 0; i < count && !army.units.empty(); ++i) {
auto strongestIt = std::max_element(army.units.begin(), army.units.end(),
[](const Unit& a, const Unit& b) {
return a.getStrength() < b.getStrength();
});
if (strongestIt != army.units.end()) {
army.units.erase(strongestIt);
}
}
}
void Army::recordBattleResult(Army& enemy, result_t result) {
this->history.emplace_back(enemy.getName(), result);
enemy.history.emplace_back(this->getName(),
result == Winner ? Loser : result == Loser ? Winner : Draw);
}
// Methods
void Army::attack(Army &t) {
int battleResult = this->getTotalArmyStrength() - t.getTotalArmyStrength();
if(battleResult > 0) {
handleBattleWinner(*this, t);
recordBattleResult(t, Winner);
}
else if(battleResult < 0) {
handleBattleWinner(t, *this);
recordBattleResult(t, Loser);
}
else {
removeStrongestUnits(*this, 1);
removeStrongestUnits(t, 1);
recordBattleResult(t, Draw);
}
}
void Army::addCoins(int n) {
coins += n;
}
void Army::removeCoins(int n) {
coins -= n;
}
bool Army::trainUnit(Unit &u) {
int trainingCost = u.getTrainingCost();
if(!canAffordPayment(trainingCost))
return false;
removeCoins(trainingCost);
u.trainUnit();
return true;
}
bool Army::transformUnit(Unit &u) {
if(!canAffordPayment(u.getTransformCost()))
return false;
removeCoins(u.getTransformCost());
u.transformUnit();
return true;
}
// Getters
int Army::getArmyStrength() const {
return getTotalArmyStrength();
}
Unit Army::getStrongestUnit() const {
if (units.empty()) {
return {0, 0, 0, 0};
}
return *std::max_element(units.begin(), units.end(),
[](const Unit& a, const Unit& b) {
return a.getStrength() < b.getStrength();
});
}
int Army::getCoins() const {
return coins;
}
const std::vector<Unit> &Army::getUnits() const {
return units;
}
std::string Army::getName() const {
return armyID;
}
const std::vector<std::pair<std::string, result_t>> &Army::getHistory() const {
return history;
}