Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
142 lines (116 loc) · 2.9 KB
/
Copy pathmain.cpp
File metadata and controls
142 lines (116 loc) · 2.9 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright 2019 Istituto Nazionale di Fisica Nucleare
// Licensed under the EUPL
#include <cassert>
#include <iostream>
#include <utility>
#include <vector>
enum class State : char { Dead, Alive };
class Board
{
using Data = std::vector<State>;
int N;
Data m_board;
public:
Board(int n) : N(n), m_board(N * N)
{
}
int size() const
{
return N;
}
State operator()(int i, int j) const noexcept
{
return (i >= 0 && i < N && j >= 0 && j < N) ? m_board[i * N + j]
: State::Dead;
}
State& operator()(int i, int j) noexcept
{
assert(i >= 0 && i < N && j >= 0 && j < N);
return m_board[i * N + j];
}
};
int neigh_count(Board const& board, int const r, int const c)
{
int result = -static_cast<int>(board(r, c));
for (int i = r - 1; i != r + 2; ++i) {
for (int j = c - 1; j != c + 2; ++j) {
result += static_cast<int>(board(i, j));
}
}
return result;
}
Board evolve(Board const& current)
{
int const N = current.size();
Board next(N);
for (int i = 0; i != N; ++i) {
for (int j = 0; j != N; ++j) {
int const c = neigh_count(current, i, j);
if (c == 3) {
next(i, j) = State::Alive;
} else if (c == 2) {
next(i, j) = current(i, j);
}
}
}
return next;
}
#include <SFML/Graphics.hpp>
constexpr int board_size = 64;
constexpr int cell_size = 10;
// don't show the borders of the board
constexpr int wsize_x = (board_size - 6) * cell_size;
constexpr int wsize_y = (board_size - 6) * cell_size;
class Display
{
sf::RenderWindow window{
sf::VideoMode(wsize_x, wsize_y), "Life", sf::Style::Close};
public:
void draw(Board const& board)
{
window.clear(sf::Color::White);
sf::RectangleShape rect(sf::Vector2f(cell_size, cell_size));
rect.setFillColor(sf::Color::Red);
rect.setOutlineThickness(1.f);
rect.setOutlineColor(sf::Color::White);
int const N = board.size();
for (int i = 3; i != N - 3; ++i) {
for (int j = 3; j != N - 3; ++j) {
if (board(i, j) == State::Alive) {
// note inversion of i and j
rect.setPosition((j - 3) * cell_size, (i - 3) * cell_size);
window.draw(rect);
}
}
}
window.display();
}
void wait_key_pressed()
{
sf::Event event;
window.waitEvent(event);
while (event.type != sf::Event::KeyPressed) {
window.waitEvent(event);
}
}
};
#include <chrono>
#include <thread>
Display display;
int main()
{
Board board(board_size);
board(28, 4) = State::Alive;
board(29, 5) = State::Alive;
board(30, 3) = State::Alive;
board(30, 4) = State::Alive;
board(30, 5) = State::Alive;
display.draw(board);
display.wait_key_pressed();
for (int i = 0; i != 150; ++i) {
board = evolve(board);
display.draw(board);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
// display.wait_key_pressed();
}
}