-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathenvironment.cc
More file actions
119 lines (91 loc) · 2.35 KB
/
Copy pathenvironment.cc
File metadata and controls
119 lines (91 loc) · 2.35 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
107
108
109
110
111
112
113
114
115
//
// Created by santor on 19/09/17.
//
#include <iostream>
#include "environment.h"
#include "agent.h"
#include "defs.h"
Environment* Environment::s_instance = nullptr;
void Environment::initialize(long dimension)
{
if (!s_instance)
s_instance = new Environment(dimension);
}
Environment* Environment::get()
{
if (!s_instance)
throw std::invalid_argument("environment not initialized");
return s_instance;
}
void Environment::destroy()
{
delete s_instance;
}
// TODO: define if matriz is circular or not
Environment::Environment(long dimension)
{
m_dimension = dimension;
m_size = dimension * dimension;
m_elements = new PCell[m_size]; // TODO: check if can create array with size
}
Environment::~Environment()
{
delete m_elements;
}
long Environment::countElements() const
{
return m_size;
}
void Environment::addAgent(long index)
{
PAgent agent = std::make_shared<Agent>(index);
this->m_agents.push_back(agent);
PCell cell = std::make_shared<Cell>();
cell->agent = agent;
cell->index = index; // TODO: add agent in the first empty space, if index equals -1
m_elements[index] = cell;
}
const std::vector<PAgent> &Environment::agents() const
{
return m_agents;
}
void Environment::show()
{
for (int i = 0; i < m_dimension; ++i) {
for (int j = 0; j < m_dimension; ++j) {
PAgent agent = m_elements[(i * m_dimension) + j]->agent;
std::cout << ((agent->state() == Agent::LIVE) ? "·" : " ") << " ";
}
std::cout << std::endl;
}
}
long Environment::dimension() const
{
return m_dimension;
}
PCell* Environment::neighborsFrom(long index)
{
auto neighbors = new PCell[8];
long col = index % m_dimension;
long row = (index - col) / m_dimension;
int nindex = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
if (i != 1 || j != 1) {
long nrow = (correctPosition(row + i - 1));
long ncol = correctPosition(col + j - 1);
neighbors[nindex] = m_elements[(nrow * m_dimension) + ncol];
++nindex;
}
}
}
return neighbors;
}
long Environment::correctPosition(long position)
{
if (position >= m_dimension)
return 0;
else if (position < 0)
return (m_dimension - 1);
return position;
}