-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdomain.cpp
More file actions
106 lines (99 loc) · 2.75 KB
/
domain.cpp
File metadata and controls
106 lines (99 loc) · 2.75 KB
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
#include "domain.h"
bool Position::operator==(const Position& other)
{
return (other.x == this->x && other.y == this->y);
}
PredatorPrey::PredatorPrey()
{
/* Default is 2 predators in a 100 by 100 world */
width = 100;
height = 100;
for (int i = 0; i < 2; ++i)
{
Predator pred;
pred.pos.x = 0;
pred.pos.y = 0;
pred.index = i;
Predators.push_back(pred);
}
PreyPos.x = 50;
PreyPos.y = 50;
}
PredatorPrey::PredatorPrey(int width, int height, int num_pred)
{
this->width = width;
this->height= height;
for (int i = 0; i < num_pred; ++i)
{
Predator pred;
pred.pos.x = 0;
pred.pos.y = 0;
pred.index = i;
Predators.push_back(pred);
}
/* Set prey in middle of world */
PreyPos.x = width/2;
PreyPos.y = height/2;
}
Position PredatorPrey::Distance(Position p1, Position p2)
{
/* Returns the delta between the observer (p1) and
* the observed (p2). The sign remains so the information
* about relative orientation is preserved. */
Position d;
d.x = p1.x - p2.x;
d.y = p1.y - p2.y;
return d;
}
/* If a single predator occupies the same cell as the prey,
* the prey is captured */
bool PredatorPrey::Captured()
{
for (auto itr=this->Predators.begin(); itr != this->Predators.end(); ++itr)
{
if (itr->pos == this->PreyPos)
{
return true;
}
}
return false;
}
vector<Position> PredatorPrey::PredatorState(int index)
{
/* The state is a vector of the position offsets
* from my current position to the prey, and to all other
* predators, as defined in Yong, Miikkulainen 2001 */
Predator* current = &(Predators[index]);
vector<Position> state;
state.push_back(this->Distance(current->pos, this->PreyPos));
for (auto itr = this->Predators.begin(); itr != this->Predators.end(); ++itr)
{
if (itr->index == current->index)
{
continue;
}
state.push_back(this->Distance( current->pos, itr->pos));
}
return state;
}
void PredatorPrey::TakeAction(int index, int action)
{
Predator* current = &(Predators[index]);
switch(action)
{
case 0: break; /* STAY */
case 1: current->pos.y -= 1; /* UP */
case 2: current->pos.y += 1; /* DOWN */
case 3: current->pos.x -= 1; /* LEFT */
case 4: current->pos.x += 1; /* RIGHT */
}
/* Check for invalid positions */
if (current->pos.x < 0)
current->pos.x += this->width;
if (current->pos.x >= width)
current->pos.x -= this->width;
if (current->pos.y < 0)
current->pos.y += this->height;
if (current->pos.y >= height)
current->pos.y -= this->height;
}