-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcellgrid.cpp
More file actions
123 lines (99 loc) · 3.73 KB
/
cellgrid.cpp
File metadata and controls
123 lines (99 loc) · 3.73 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
116
117
118
119
120
121
122
123
#include "cellgrid.h"
/*
* Conoway's Game of Life Simulation
* Created by Liam Morrison 11/13/2021
* Uses SFML Graphics API
*/
CellGrid::CellGrid(int width, int height, const vector<vector<Cell*>*> &startGrid) : width{width}, height{height} {
// Shallow copy
for (int i = 0; i < width; i++) {
cellGrid.push_back(*startGrid[i]);
nextGeneration.push_back(new vector<CELL_STATE*>);
for (int j = 0; j < height; j++) {
// Load the cell grid
cellGrid[i].push_back(startGrid[i]->at(j));
// Load first generation
nextGeneration[i]->push_back(new CELL_STATE(*cellGrid[i][j]->getState()));
}
}
}
CellGrid::~CellGrid() {
for (int x = 0; x < nextGeneration.size(); x++) {
for (int y = 0; y < nextGeneration.size(); y++) {
delete nextGeneration.at(x)->at(y);
}
delete [] nextGeneration.at(x);
}
}
/*
1. Any live cell with fewer than two live neighbours dies, as if by underpopulation.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overpopulation.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
*/
bool CellGrid::determineNextGeneration() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int numberOfLiveCells = 0;
if ((i+1 < width && j+1 < height) && *cellGrid[i+1][j+1]->getState() == ALIVE) {
numberOfLiveCells++;
}
// i+1 && j
if ((i+1 < width) && *cellGrid[i+1][j]->getState() == ALIVE) {
numberOfLiveCells++;
}
// i && j+1
if ((j+1 < height) && *cellGrid[i][j+1]->getState() == ALIVE) {
numberOfLiveCells++;
}
// i+1 && j-1
if ((i+1 < width && j-1 >= 0) && *cellGrid[i+1][j-1]->getState() == ALIVE) {
numberOfLiveCells++;
}
// i-1 && j+1
if ((i-1 >= 0 && j+1 < height) && *cellGrid[i-1][j+1]->getState() == ALIVE) {
numberOfLiveCells++;
}
// i-1 && j-1
if ((i-1 >= 0 && j-1 >= 0) && *cellGrid[i-1][j-1]->getState() == ALIVE) {
numberOfLiveCells++;
}
// i && j-1
if ((j-1 >= 0) && *cellGrid[i][j-1]->getState() == ALIVE) {
numberOfLiveCells++;
}
// i-1 && j
if ((i-1 >= 0) && *cellGrid[i-1][j]->getState() == ALIVE) {
numberOfLiveCells++;
}
if (*cellGrid[i][j]->getState() == ALIVE) {
if (numberOfLiveCells < 2 || numberOfLiveCells > 3) {
*nextGeneration[i]->at(j) = DEAD;
}
} else if (*cellGrid[i][j]->getState() == DEAD) {
if (numberOfLiveCells == 3) {
*nextGeneration[i]->at(j) = ALIVE;
}
}
}
}
return true;
}
bool CellGrid::birthNextGeneration() {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
if (*nextGeneration[i]->at(j) == ALIVE) {
cellGrid[i][j]->setState(ALIVE);
} else {
cellGrid[i][j]->setState(DEAD);
}
}
}
return true;
}
vector<vector<Cell*>> &CellGrid::getCellGrid() {
return cellGrid;
}
vector<vector<CELL_STATE*>*> &CellGrid::getNextGeneration() {
return nextGeneration;
}