-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.cc
More file actions
40 lines (30 loc) · 701 Bytes
/
Copy pathagent.cc
File metadata and controls
40 lines (30 loc) · 701 Bytes
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
//
// Created by santor on 20/12/17.
//
#include <iostream>
#include "agent.h"
#include "environment.h"
Agent::Agent(long index)
: m_state(LIVE),
m_position(index) {}
void Agent::update()
{
int contAgentsLives = 0;
auto cells = Env()->neighborsFrom(m_position);
for (int i = 0; i < 8; ++i) {
PAgent agent = cells[i]->agent;
if (agent && agent->state() == LIVE)
++contAgentsLives;
}
if (m_state == DEAD) {
if (contAgentsLives == 3)
m_state = LIVE;
} else {
if (contAgentsLives < 2 || contAgentsLives > 3)
m_state = DEAD;
}
}
Agent::status Agent::state()
{
return m_state;
}