-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAntArmy.cpp
86 lines (76 loc) · 1.96 KB
/
AntArmy.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
#include "AntArmy.h"
#include <typeinfo>
#include "State.h"
#include "GuardAnt.h"
AntArmy::AntArmy(void)
{
}
AntArmy::~AntArmy(void)
{
}
void AntArmy::updateAnts(State & state, std::vector<Location> newLocations)
{
std::vector<Ant *>::iterator mIter = myAntArmy.begin();
while (mIter != myAntArmy.end()) // Go through each ant in my army.
{
bool isStillAlive = false;
std::vector<Location>::iterator vIter = newLocations.begin();
while (vIter != newLocations.end()) // Check against the newly updated ants to see if still alive.
{
if ((*(*mIter)).location == (*vIter))
{
isStillAlive = true;
vIter = newLocations.erase(vIter);
mIter++;
break;
}
vIter++;
}
if (!isStillAlive)
{
// Get rid of the ant if it's not found, but advance the iterator to the next position.
delete *mIter;
mIter = myAntArmy.erase(mIter); // Post increment so that erase will happen to the original value before the increment happens.
}
}
std::vector<Location>::iterator vIter = newLocations.begin();
while (vIter != newLocations.end()) // Add new ants to my army.
{
//Ant newAnt((*vIter), state); // Gonna have to make recruitmentCenter function to create appropriate ant type.
//myAntArmy.push_back(newAnt);
myAntArmy.push_back(recruitNewAnt((*vIter), state));
vIter++;
}
}
void AntArmy::chooseMoves(State & state)
{
std::vector<Ant *>::iterator mIter = myAntArmy.begin();
while (mIter != myAntArmy.end())
{
(*(*mIter)).chooseMove(state);
mIter++;
}
}
Ant * AntArmy::recruitNewAnt(Location loc, State & state)
{
int numberOfGuards = 0;
std::vector<Ant *>::iterator mIter = myAntArmy.begin();
while (mIter != myAntArmy.end())
{
if (typeid(*(*mIter)) == typeid(GuardAnt))
{
numberOfGuards++;
}
mIter++;
}
if (numberOfGuards < (myAntArmy.size() / 10) && numberOfGuards < (state.myHills.size() * 6))
{
GuardAnt *newAnt = new GuardAnt(loc, state);
return newAnt;
}
else
{
Ant *newAnt = new Ant(loc, state);
return newAnt;
}
}