-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPredator.cpp
117 lines (101 loc) · 3.27 KB
/
Predator.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
#include "Predator.h"
#include <cmath>
Predator::Predator()
{
// Initialize Predator-specific attributes here, if any
}
Predator::Predator(World *world, int x, int y) : Organism(world, x, y)
{
breedUpperBound = 8;
world->setAt(x, y, this);
world->pushPredator(this);
}
Predator::~Predator()
{
world->setAt(this->x, this->y, nullptr);
world->removePredator(this);
}
// Breed. If a Predator survives for eight time steps,
// then at the end of the time step it will spawn off a new Predator in the same manner as the Prey.
void Predator::breed()
{
std::vector<std::pair<int, int>> adjacentCells = {
std::make_pair(this->x, this->y + 1),
std::make_pair(this->x, this->y - 1),
std::make_pair(this->x + 1, this->y),
std::make_pair(this->x - 1, this->y)
};
while (breedTicks >= 8) {
int random = rand() % adjacentCells.size();
int newX = adjacentCells[random].first;
int newY = adjacentCells[random].second;
if (newX >= 0
&& newX < world->WORLDSIZE
&& newY >= 0
&& newY < world->WORLDSIZE
&& world->getAt(newX, newY) == nullptr) {
world->setAt(newX, newY, new Predator(world, newX, newY));
breedTicks = 0;
return;
} else {
adjacentCells[random] = adjacentCells[adjacentCells.size() - 1];
adjacentCells.pop_back();
if (adjacentCells.size() == 0) {
breedTicks = 0;
return;
}
}
}
breedTicks++;
}
// Move. Every time step, if there is an adjacent Prey (up, down, left, or right),
// then the Predator will move to that cell and eat the Prey.
// Otherwise, the Predator moves according to the same rules as the Zoomies.
// Note that a Predator cannot eat other Swoopies.
void Predator::move()
{
// Check for adjacent cells
std::vector<std::pair<int, int>> adjacentCells = {
std::make_pair(this->x, this->y + 1),
std::make_pair(this->x, this->y - 1),
std::make_pair(this->x + 1, this->y),
std::make_pair(this->x - 1, this->y)
};
// Check if there is a Prey in an adjacent cell
for (auto &cell : adjacentCells)
{
if (cell.first >= 0
&& cell.first < world->WORLDSIZE
&& cell.second >= 0
&& cell.second < world->WORLDSIZE
&& world->getAt(cell.first, cell.second) != nullptr
&& world->getAt(cell.first, cell.second)->getType() == 1)
{
delete world->getAt(cell.first, cell.second);
world->setAt(cell.first, cell.second, this);
world->setAt(this->x, this->y, nullptr);
this->x = cell.first;
this->y = cell.second;
starveTicks = 0;
return;
}
}
Organism::move();
}
int Predator::getType()
{
// Return Predator-specific type identifier here
return 2;
}
// Starve. If a Predator has not eaten a Prey within the last three time steps,
// then at the end of the third time step it will starve and die.
// The Predator should then be removed from the grid of cells.
bool Predator::starve()
{
if (starveTicks >= 3) {
delete this;
return true;
}
starveTicks++;
return false;
}