Repository navigation
Expand file tree
/
Copy pathgameoflife.cpp
More file actions
103 lines (86 loc) · 2.27 KB
/
Copy pathgameoflife.cpp
File metadata and controls
103 lines (86 loc) · 2.27 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
#include "gameoflife.h"
#include <random>
GameOfLife::GameOfLife(int rows, int cols)
: m_cols(cols)
, m_rows(rows)
, m_board(rows * cols, false)
{
}
GameOfLife::GameOfLife(const GameOfLife& other)
: m_cols(other.m_cols)
, m_rows(other.m_rows)
, m_board(other.m_board)
{
}
GameOfLife* GameOfLife::clone()
{
return new GameOfLife(*this);
}
void GameOfLife::generate_board()
{
std::mt19937 gen(123456);
std::uniform_int_distribution<> distrib(1, 3);
for (int row = 0; row < m_rows; row++)
for (int col = 0; col < m_cols; col++)
if (distrib(gen) == 1) // 33% chance
set_cell(row, col, true);
}
uint8_t GameOfLife::count_alive_neighbors(int cell_row, int cell_col)
{
uint8_t alive = 0;
for (int delta_row = -1; delta_row <= 1; delta_row++)
{
for (int delta_col = -1; delta_col <= 1; delta_col++)
{
const int row = cell_row + delta_row;
const int col = cell_col + delta_col;
if (delta_row == 0 || delta_col == 0)
continue;
if (row < 0 || row >= m_rows)
continue;
if (col < 0 || col >= m_cols)
continue;
if (get_cell(row, col))
alive++;
}
}
return alive;
}
size_t GameOfLife::count_alive_cells()
{
size_t alive = 0;
for (int row = 0; row < m_rows; row++)
for (int col = 0; col < m_cols; col++)
if (get_cell(row, col))
alive++;
return alive;
}
void GameOfLife::update_board()
{
auto copy = *this;
for (int row = 0; row < m_rows; row++)
{
for (int col = 0; col < m_cols; col++)
{
bool alive = get_cell(row, col);
uint8_t neighbors = count_alive_neighbors(row, col);
if (alive && (neighbors < 2 || neighbors > 3))
{
copy.set_cell(row, col, false);
}
else if (!alive && (neighbors == 3))
{
copy.set_cell(row, col, true);
}
}
}
m_board = copy.m_board;
}
bool GameOfLife::get_cell(int row, int col)
{
return m_board[row * m_rows + col];
}
void GameOfLife::set_cell(int row, int col, bool value)
{
m_board[row * m_rows + col] = value;
}