-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBrawl.cpp
220 lines (198 loc) · 5.74 KB
/
Brawl.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
#include <string>
#include <vector>
#include <cassert>
#include "Brawl.h"
#include "Team.h"
#include "Weapon.h"
Brawl::Brawl(
Team givenTeamZero,
Team givenTeamOne)
: teamZero(givenTeamZero),
teamOne(givenTeamOne),
totalDroppedWeapons(0),
teamZeroTurn(0),
teamOneTurn(0),
isTeamZeroTurn(true)
{
}
// EFFECTS start the entire brawl until one team loses all members.
void Brawl::startBrawl()
{
teamZero.enterBrawl();
teamOne.enterBrawl();
std::cout << "Team 0 has entered the brawl! Here are its members:" << std::endl;
teamZero.printAllFighters(std::cout);
std::cout << std::endl;
std::cout << "Team 1 has entered the brawl! Here are its members:" << std::endl;
teamOne.printAllFighters(std::cout);
std::cout << std::endl;
int teamZeroSize = teamZero.getInCombatSize();
int teamOneSize = teamOne.getInCombatSize();
assert(teamZeroSize != 0 && teamOneSize != 0);
while (teamZeroSize != 0 && teamOneSize != 0)
{
std::string dummy;
std::cout << std::endl
<< "Type anything to see the next move. ";
std::cin >> dummy;
std::cout << std::endl
<< "\033[2J"
<< "\033[1;1H";
Fighter *currentFighter;
if (isTeamZeroTurn)
{
std::cout << "Team 0 will send out its fighter! " << std::endl
<< std::endl;
currentFighter = teamZero.getFighterK(teamZeroTurn);
requestAndEnactAction(currentFighter, teamZero, teamOne);
++teamZeroTurn;
isTeamZeroTurn = false;
}
else
{
std::cout << "Team 1 will send out its fighter! " << std::endl
<< std::endl;
currentFighter = teamOne.getFighterK(teamOneTurn);
requestAndEnactAction(currentFighter, teamOne, teamZero);
++teamOneTurn;
isTeamZeroTurn = true;
}
teamZeroSize = teamZero.getInCombatSize();
teamOneSize = teamOne.getInCombatSize();
if (teamZeroTurn >= teamZeroSize)
{
teamZeroTurn = 0;
}
if (teamOneTurn >= teamOneSize)
{
teamOneTurn = 0;
}
std::cout << "This is Team 0 and its members now:" << std::endl;
teamZero.printAllFighters(std::cout);
std::cout << std::endl;
std::cout << "This is Team 1 and its members now:" << std::endl;
teamOne.printAllFighters(std::cout);
}
teamZeroSize = teamZero.getInCombatSize();
teamOneSize = teamOne.getInCombatSize();
if (teamZeroSize == 0)
{
std::cout << "Team 0 wins!" << std::endl
<< std::endl;
}
else
{
std::cout << "Team 1 wins!" << std::endl
<< std::endl;
}
teamZero.exitBrawl();
teamOne.exitBrawl();
}
// EFFECTS return team one
const Team Brawl::getTeamZero() const
{
return teamZero;
}
// EFFECTS return team two
const Team Brawl::getTeamOne() const
{
return teamOne;
}
// EFFECTS return team two
std::vector<Weapon *> Brawl::getAllDroppedWeapons()
{
return droppedWeapons;
}
// REQUIRES 0 <= k < totalDroppedWeapons
// EFFECTS return kth dropped weapon
Weapon *Brawl::getDroppedOfWeaponK(const int &k)
{
assert(0 <= k && k < totalDroppedWeapons);
return droppedWeapons[k];
}
// REQUIRES 0 <= k < totalDroppedWeapons
// EFFECTS remove kth dropped weapon
void Brawl::removeDroppedOfWeaponK(const int &k)
{
assert(0 <= k && k < totalDroppedWeapons);
--totalDroppedWeapons;
delete droppedWeapons[k];
droppedWeapons.erase(droppedWeapons.begin() + k);
}
// EFFECTS add dropped weapon
void Brawl::addDroppedWeapon(const Weapon *weapon)
{
++totalDroppedWeapons;
Weapon *addedWeapon = WeaponFactory(weapon);
droppedWeapons.push_back(addedWeapon);
}
// REQUIRES fighter has an element and fighter must be in combat
// If player wants to pick up weapon, make sure that there are droppedWeapons.
// MODIFIES action and target
// EFFECTS Ask if fighter wants to attack or heal or grab weapon or skip turn. Do action.
void Brawl::requestAndEnactAction(Fighter *f, Team &allies, Team &opponents)
{
std::cout << "It is " << *f << "'s turn." << std::endl
<< std::endl;
std::vector<Fighter *> allOpponents = opponents.getAllFighters();
std::vector<Fighter *> allAllies = allies.getAllFighters();
std::string response = f->requestAction(allAllies, allOpponents, droppedWeapons);
if (response == "Attack")
{
int target = f->goAttack(allOpponents);
Fighter *defender = opponents.getFighterK(target);
if (defender->getCurrentHealth() == 0)
{
fighterExitsCombat(defender);
opponents.memberExitsCombat(target);
}
}
else if (response == "Heal")
{
f->goHeal(allAllies);
}
else if (response == "Grab")
{
int target = f->goGrabWeapon(droppedWeapons);
removeDroppedOfWeaponK(target);
}
else if (response == "Skip")
{
std::cout << *f << " skips their turn." << std::endl
<< std::endl;
}
}
// REQUIRES fighter health drops to zero
// EFFECTS if fighter exits combat, change dropped weapons
void Brawl::fighterExitsCombat(Fighter *fighter)
{
assert(fighter->getCurrentHealth() == 0);
std::cout << *fighter << " has exited the combat due to lack of health." << std::endl
<< std::endl;
fighter->exitCombat();
if (fighter->getNumberOfWeapons() == 0) {
return;
}
Weapon *copiedWeapon = fighter->getHighestDamageWeapon();
std::cout << *fighter << " has dropped the " << *copiedWeapon << "." << std::endl
<< std::endl;
addDroppedWeapon(copiedWeapon);
}
// EFFECTS Prints droppedWeapons to stream as "Dropped Weapon 1: Excalibur"
// followed by newline and then "Dropped Weapon 2: Gate of Babylon"
std::ostream &Brawl::printDroppedWeapons(std::ostream &os) const
{
for (int i = 0; i < totalDroppedWeapons; ++i)
{
os << "Dropped Weapon " << i << ": " << *droppedWeapons[i] << std::endl;
}
return os;
}
// EFFECTS delete all dropped weapons
Brawl::~Brawl()
{
while (totalDroppedWeapons != 0)
{
removeDroppedOfWeaponK(0);
}
}