-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCell.cpp
More file actions
52 lines (42 loc) · 1.05 KB
/
Cell.cpp
File metadata and controls
52 lines (42 loc) · 1.05 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
#include "Cell.hpp"
#include "numeric"
#include <iostream>
Cell::Cell(int _id) {
id = _id;
isAlive = (((rand() & 100)) > 50); // 50% chance to start alive
nextState = false;
};
bool Cell::getState() const {
return isAlive;
}
int Cell::getId() const {
return id;
}
// calculate the next state of the cell and set the nextState field
void Cell::calcNextState() {
// count all neighbors that are alive
int aliveAmount = 0;
for (auto n: neighbors) {
if (n->getState()) {
aliveAmount++;
}
}
// set the next iteration state according to Conway's rules
if (isAlive && (aliveAmount == 2 || aliveAmount == 3)) {
nextState = true;
return;
}
if (!isAlive && (aliveAmount == 3)) {
nextState = true;
return;
}
nextState = false;
}
// set the cell state to the next iteration
void Cell::updateState() {
isAlive = nextState;
}
// add a neighbor to the cell's neighbor vector
void Cell::addNeighbor(Cell* cell) {
neighbors.emplace_back(cell);
}