-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
315 lines (261 loc) · 9.1 KB
/
Copy pathmain.cpp
File metadata and controls
315 lines (261 loc) · 9.1 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
author: @fikret0
date: November 13, 2021
GitHub: @fikret0
*/
#include <bits/stdc++.h>
#include <SFML/Graphics.hpp>
#define WIDTH 700
#define HEIGHT 700
#define XCELLS 35
#define YCELLS 35
int cellw = WIDTH / XCELLS, cellh = HEIGHT / YCELLS;
std::map<std::pair<int, int>, int> grid;
int camX = 0, camY = 0;
sf::RenderWindow mwindow(sf::VideoMode({WIDTH, HEIGHT}), "Conway's Game of Life");
bool darkmode = false;
int getneighbours(int ln, int col){
int c = 0;
for(int y = ln - 1; y <= ln + 1; y++){
for(int x = col - 1; x <= col + 1; x++){
if(x == col && y == ln) continue;
c += grid[{x, y}];
}
}
return c;
}
float defaultdelay = 0.5f;
float fastdelay = 0.05f;
float delay = defaultdelay;
sf::Clock gameclock;
enum Direction { STOPPED, FORWARD, BACKWARD };
Direction direction = STOPPED;
std::vector<std::map<std::pair<int, int>, int>> history;
int histPos = 0;
int histBase = 0;
void pushHistory(const std::map<std::pair<int, int>, int>& state){
history.resize(histPos + 1);
history.push_back(state);
histPos++;
}
std::map<std::pair<int, int>, int> computeNext(){
std::set<std::pair<int, int>> active;
for(const auto& kv : grid){
if(kv.second){
for(int y = kv.first.second - 1; y <= kv.first.second + 1; y++){
for(int x = kv.first.first - 1; x <= kv.first.first + 1; x++){
active.insert({x, y});
}
}
}
}
std::map<std::pair<int, int>, int> tmp = grid;
for(const auto& p : active){
int col = grid[p];
int nb = getneighbours(p.second, p.first);
if(col == 1){
if(nb < 2 || nb > 3){
tmp[p] = 0;
}
}
else{
if(nb == 3){
tmp[p] = 1;
}
}
}
return tmp;
}
void timemanager(){
float elapsed = gameclock.getElapsedTime().asSeconds();
if (elapsed >= delay){
if(direction == FORWARD){
grid = computeNext();
pushHistory(grid);
}
else if(direction == BACKWARD){
if(histPos > histBase){
histPos--;
grid = history[histPos];
}
else{
mwindow.setTitle("Conway's Game of Life - Start of History");
}
}
gameclock.restart();
}
}
void cleargrid(){
grid.clear();
histBase = histPos;
history[histBase] = grid;
}
void togglecell(int ln, int col){
std::pair<int, int> p = {col, ln};
if(grid[p]){
grid.erase(p);
}
else{
grid[p] = 1;
}
}
void setcell(int ln, int col, int v){
std::pair<int, int> p = {col, ln};
if(v) grid[p] = 1;
else grid.erase(p);
history[histPos] = grid;
}
bool painting = false;
int paintTarget = 1;
std::pair<int, int> lastPaint = {0, 0};
int main(int argc, char **argv){
std::cout << "GitHub: @fikret0 (c) 2021" << std::endl << "Conway's Game of Life" << std::endl << "--dark for dark mode" << std::endl << std::endl
<< "Space: Play / pause (forward)" << std::endl
<< "Z: Play backwards / pause" << std::endl
<< "C: Clear the grid" << std::endl
<< "Tab: Fast forward" << std::endl
<< "Arrows: Navigate canvas" << std::endl;
for(int ai = 0; ai < argc; ai++){
std::string arg = argv[ai];
if(arg == "--dark"){
darkmode = true;
}
}
sf::Color gridcolor(200, 200, 200);
sf::Color cellcolor(0, 0, 0);
sf::Color bgcolor(255, 255, 255);
if(darkmode){
gridcolor = sf::Color(55, 55, 55);
cellcolor = sf::Color(255, 255, 255);
bgcolor = sf::Color(0, 0, 0);
}
history.push_back(grid);
histPos = 0;
histBase = 0;
while (mwindow.isOpen()){
while (const auto event = mwindow.pollEvent()){
if (event->is<sf::Event::Closed>()){
mwindow.close();
}
else if(const auto* key = event->getIf<sf::Event::KeyPressed>()){
if(key->code == sf::Keyboard::Key::Space){ // play / pause forward
if(direction == BACKWARD) continue; // must stop reverse with Z first
if(direction == FORWARD) direction = STOPPED;
else direction = FORWARD;
mwindow.setTitle(direction == FORWARD ? "Conway's Game of Life - Playing" : "Conway's Game of Life");
gameclock.restart();
}
else if(key->code == sf::Keyboard::Key::Z){ // play backwards
if(direction == FORWARD) continue; // can't reverse while going forwards
if(direction == BACKWARD) direction = STOPPED;
else direction = BACKWARD;
mwindow.setTitle(direction == BACKWARD ? "Conway's Game of Life - Reversing" : "Conway's Game of Life");
gameclock.restart();
}
else if(key->code == sf::Keyboard::Key::C){ // clear
cleargrid();
}
else if(key->code == sf::Keyboard::Key::Tab){ // fast forward
if(delay != fastdelay){
delay = fastdelay;
mwindow.setTitle("Conway's Game of Life - Fast Forward");
}
}
else if(key->code == sf::Keyboard::Key::Up){
camY -= 1;
}
else if(key->code == sf::Keyboard::Key::Down){
camY += 1;
}
else if(key->code == sf::Keyboard::Key::Left){
camX -= 1;
}
else if(key->code == sf::Keyboard::Key::Right){
camX += 1;
}
}
else if(const auto* key = event->getIf<sf::Event::KeyReleased>()){
if(key->code == sf::Keyboard::Key::Tab){ // restore default speed
delay = defaultdelay;
mwindow.setTitle("Conway's Game of Life");
}
}
else if (const auto* mouse = event->getIf<sf::Event::MouseButtonPressed>()){
if (mouse->button == sf::Mouse::Button::Left){
int col = floor(mouse->position.x / cellw) + camX;
int ln = floor(mouse->position.y / cellh) + camY;
painting = true;
paintTarget = grid[{col, ln}] ? 0 : 1;
lastPaint = {col, ln};
histBase = histPos;
history[histBase] = grid;
gameclock.restart();
setcell(ln, col, paintTarget);
}
}
else if (const auto* mouse = event->getIf<sf::Event::MouseButtonReleased>()){
if (mouse->button == sf::Mouse::Button::Left){
painting = false;
}
}
else if (const auto* mouse = event->getIf<sf::Event::MouseMoved>()){
if (painting){
int col = floor(mouse->position.x / cellw) + camX;
int ln = floor(mouse->position.y / cellh) + camY;
if (lastPaint != std::make_pair(col, ln)){
lastPaint = {col, ln};
gameclock.restart();
setcell(ln, col, paintTarget);
}
}
}
}
mwindow.clear(bgcolor);
if(direction != STOPPED){
timemanager();
}
else{
gameclock.restart();
}
float th = 1.f;
int startX = camX - 1;
int startY = camY - 1;
int endX = camX + XCELLS + 1;
int endY = camY + YCELLS + 1;
/* Draw the grid */
for(int xc = startX; xc <= endX; xc++){
int c = (xc - camX) * cellw;
sf::RectangleShape line;
line.setPosition(sf::Vector2f(c, 0));
line.setSize(sf::Vector2f(th, HEIGHT));
line.setFillColor(gridcolor);
line.setOutlineThickness(0);
mwindow.draw(line);
}
for(int yc = startY; yc <= endY; yc++){
int c = (yc - camY) * cellh;
sf::RectangleShape line;
line.setPosition(sf::Vector2f(0, c));
line.setSize(sf::Vector2f(WIDTH, th));
line.setFillColor(gridcolor);
line.setOutlineThickness(0);
mwindow.draw(line);
}
/* Draw the cells */
for(const auto& kv : grid){
if(!kv.second) continue;
int cx = kv.first.first;
int cy = kv.first.second;
if(cx < camX || cx >= camX + XCELLS || cy < camY || cy >= camY + YCELLS){
continue;
}
sf::RectangleShape cell;
cell.setPosition(sf::Vector2f((cx - camX) * cellw + th, (cy - camY) * cellh + th));
cell.setSize(sf::Vector2f(cellw - th, cellh - th));
cell.setFillColor(cellcolor);
cell.setOutlineThickness(0);
mwindow.draw(cell);
}
mwindow.display();
}
}