-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.cpp
More file actions
55 lines (44 loc) · 1.42 KB
/
Cell.cpp
File metadata and controls
55 lines (44 loc) · 1.42 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
#include "stdafx.h"
#include "CppUnitTest.h"
#include "Cell.h"
#include "RulesChecker.h"
class CellImplementation
{
private:
std::list<State> getNeighborsState() const
{
std::list<State> neighborsStateList;
for (std::list<Cell>::const_iterator neighborsIterator = neighbors.begin(); neighborsIterator != neighbors.end(); ++neighborsIterator)
neighborsStateList.push_back(neighborsIterator->getState());
return neighborsStateList;
}
public:
State state;
std::list<Cell> neighbors;
CellImplementation() : state(State::DEAD), neighbors(std::list<Cell>()) {}
CellImplementation(State state, std::list<Cell> neighbors) : state(state), neighbors(neighbors) {}
virtual ~CellImplementation() {}
int getAliveNeighbors() const
{
std::list<State> neighborsStateList = getNeighborsState();
int aliveNeighbors = 0;
for (std::list<State>::const_iterator iterator = neighborsStateList.begin(); iterator != neighborsStateList.end(); ++iterator)
if (State::ALIVE == (*iterator)) ++aliveNeighbors;
return aliveNeighbors;
}
};
Cell::Cell() : cellImpl(new CellImplementation) {}
Cell::Cell(State state, std::list<Cell> neighbors) : cellImpl(new CellImplementation(state, neighbors)) {}
Cell::~Cell()
{
cellImpl = nullptr;
delete cellImpl;
}
State Cell::getState() const
{
return cellImpl->state;
}
void Cell::nextGeneration()
{
cellImpl->state = RulesChecker::getNextState(cellImpl->state, cellImpl->getAliveNeighbors());
}