-
Notifications
You must be signed in to change notification settings - Fork 0
/
OOP - hw07 - noble.cpp
118 lines (110 loc) · 3.95 KB
/
OOP - hw07 - noble.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
#include "Noble.h"
#include <iostream>
using namespace std;
namespace WarriorCraft
{
Noble::Noble(const std::string &nm) : name(nm) {}
std::string Noble::getName() const { return name; }
void Noble::battle(Noble& otherNoble) {
double totalStrength1;
double totalStrength2;
double strengthFactor;
double individualStrength;
totalStrength1 = (getArmyStr());
totalStrength2 = (otherNoble.getArmyStr());
if (totalStrength1 == 0 && totalStrength2 == 0) {//if both armies have no strength
cout << name << " battles " << otherNoble.getName() << endl;
cout << "Oh, NO! They're both dead! Yuck!" << endl;
}
else if (totalStrength1 == 0) {
cout << name << " battles " << otherNoble.getName() << endl;
cout << " He's dead " << otherNoble.getName() << endl;
}
else if (totalStrength2 == 0) {
cout << name << " battles " << otherNoble.getName() << endl;
cout << "he's dead " << name << endl;
}
else if (totalStrength1 == totalStrength2) {//if both armies have the same total strength they annihilate
cout << name << " battles" << otherNoble.getName() << endl;
cout << "mutual annihilation " << name << " and " << otherNoble.getName() << " die at each other's hands" << endl;
setArmyStr(0);
otherNoble.setArmyStr(0);
}
else if (totalStrength1 > totalStrength2) {//if the first army is stronger
cout << name << " battles " << otherNoble.getName() << endl;
cout << name << " defeats " << otherNoble.getName() << endl;
strengthFactor = (totalStrength2 / totalStrength1);
for (size_t i = 0; i < army.size(); i++) {
individualStrength = army[i]->getStrength();
army[i]->setStrength(individualStrength - (individualStrength*strengthFactor));
}
for (size_t i = 0; i < otherNoble.army.size(); i++)
otherNoble.army[i]->setStrength(0);
}
else if (totalStrength2 > totalStrength1) {//if the second army is stronger
cout << otherNoble.getName() << " defeats " << name << endl;
strengthFactor = (totalStrength1 / totalStrength2);
for (size_t i = 0; i < army.size(); i++) {
individualStrength = otherNoble.army[i]->getStrength();
otherNoble.army[i]->setStrength(individualStrength - (individualStrength*strengthFactor));
}
for (size_t i = 0; i < otherNoble.army.size(); i++)
otherNoble.army[i]->setStrength(0);
}
}
void Noble::display() const
{
if (army.size() == 0)
cout << name << "'s army is empty :-(\n";
else
{
cout << name << " has an army of " << army.size() << endl;
for (size_t i = 0; i < army.size(); i++)
{
cout << "\t";
army[i]->display();
}
}
}
void Noble::hire(Warrior &warriorName)
{
army.push_back(&warriorName);
warriorName.setPointer(*this);
}
void Noble::fire(Warrior& badWarrior) {
cout << badWarrior.getName() << ", you are fired!" << name << endl;
for (size_t i = 0; i < army.size(); i++) {
if (badWarrior.getName() == army[i]->getName()) {
while (i + 1 <army.size()) { //while the warrior who has to be fired is not the last one in the vector, switch it with the next one
army[i] = army[i + 1];
}
army.pop_back();//when the right warrior is last, pop him out
}
}
badWarrior.getFired();
}
double Noble::getArmyStr()const {
double armyStr = 0;
for (size_t i = 0; i < army.size(); i++) {
armyStr += (army[i]->getStrength());
}
return armyStr;
}
void Noble::setArmyStr(double strengthFactor) {
if (strengthFactor == 0) {
for (size_t i = 0; i < army.size(); i++) {
army[i]->setStrength(strengthFactor);
}
}
else {
for (size_t i = 0; i < army.size(); i++) {
army[i]->setStrength(army[i]->getStrength() - army[i]->getStrength()*strengthFactor);
}
}
}
ostream& operator<<(ostream &os, const Noble &noble)
{
noble.display();
return os;
}
}