-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrey.cpp
84 lines (71 loc) · 2.18 KB
/
Prey.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
#include "Prey.h"
#include <cstdlib>
#include <cmath>
#include <utility>
#include <vector>
Prey::Prey()
{
// Initialize Prey-specific attributes here, if they exist
}
Prey::Prey(World *world, int x, int y) : Organism(world, x, y)
{
Prey::breedUpperBound = 3;
world->setAt(x, y, this);
world->pushPrey(this);
}
Prey::~Prey()
{
world->setAt(this->x, this->y, nullptr);
world->removePrey(this);
}
// Breed. If a Prey survives for three time steps,
// then at the end of the time step (that is, after moving) the Prey will breed.
// This is simulated by creating a new Prey in an adjacent (up, down, left, or right) cell that is empty.
// If there is no empty cell available, then no breeding occurs.
// Once an offspring is produced, a Prey cannot produce an offspring until three more time steps have elapsed.
void Prey::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 >= 3) {
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 Prey(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. At every time step, randomly try to move up, down, left, or right.
// If the neighboring cell in the selected direction is occupied or would
// move the Prey off the grid, then the Prey stays in the current cell.
void Prey::move()
{
Organism::move();
}
int Prey::getType()
{
return 1;
}
bool Prey::starve()
{
return false;
}