-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_of_life.pde
422 lines (359 loc) · 10.5 KB
/
game_of_life.pde
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
import java.util.Calendar;
/* ===================================================================================
--------------------------------------------------------------------------------------
BRUSH OF LIFE - Prototype for Generative Design class at FI MU
by David Kouril (www.dvdkouril.com)
--------------------------------------------------------------------------------------
Controls:
Left mouse button: draw!
Right mouse button: erase
Down key: single touch of brush
1 - 5: change brushes
0: show/hide brush
p: play/pause Conway's Game of Life simulation
c: clear screen
s: save screenshot
q: enable/disable fadeout effect
====================================================================================== */
// GOD CONSTANTS
int CELL_SIZE = 10;
/*int WINDOW_SIZE_X = 800;
int WINDOW_SIZE_Y = 600;*/
int WINDOW_SIZE_X = 1440;
int WINDOW_SIZE_Y = 900;
/*int WINDOW_SIZE_X = 1920;
int WINDOW_SIZE_Y = 1080;*/
int CELLS_NUM_X = WINDOW_SIZE_X / CELL_SIZE;
int CELLS_NUM_Y = WINDOW_SIZE_Y / CELL_SIZE;
int currentPatternBrush = 0;
boolean isInGame = false;
boolean play = false;
boolean hideBrush = false;
boolean fadeOutEffect = false;
boolean cellColorMode = false;
//boolean onePointBrush = true;
long framecount = 0;
Cell[][] cellsGrid;
Cell[][] nextCellsGrid;
void setup() {
fullScreen();
//size(800, 600);
background(0);
// current iteration of grid
cellsGrid = new Cell[CELLS_NUM_X][CELLS_NUM_Y];
createCells();
// next iteration of grid
nextCellsGrid = new Cell[CELLS_NUM_X][CELLS_NUM_Y];
// TODO: make it better
for (int j = 0; j < CELLS_NUM_Y; j++) {
for (int i = 0; i < CELLS_NUM_X; i++) {
nextCellsGrid[i][j] = new Cell(i, j, false);
}
}
}
void drawGameOfLife() {
drawCells();
if (!hideBrush) {
drawBrushPattern(patterns[currentPatternBrush]);
}
//_drawNeighbourCounts();
// CONWAY'S GAME OF LIFE
if (play /*&& (framecount % 5 == 0)*/) {
solveGrid();
//cellsGrid = nextCellsGrid; // switch the grid to the new iteration - NOT ACTUALLY HAHA
//copyArray(nextCellsGrid, cellsGrid, CELLS_NUM_X, CELLS_NUM_Y);
for (int j = 0; j < CELLS_NUM_Y; j++) {
for (int i = 0; i < CELLS_NUM_X; i++) {
cellsGrid[i][j].status = nextCellsGrid[i][j].status;
cellsGrid[i][j].hue = nextCellsGrid[i][j].hue;
}
}
}
}
void drawWelcomeScreen() {
background(0);
fill(255);
textAlign(CENTER, TOP);
textSize(50);
text("\"Brush of Life\"", width/2, height/2 - 80);
textSize(18);
text("David Kouřil (dvdkouril.com)", width/2, height/2);
textSize(20);
text("press [space] to start", width/2, height - 100);
}
void draw() {
//frameRate(15);
framecount++;
if (isInGame) { // play the game
drawGameOfLife();
} else { // show welcome screen
drawWelcomeScreen();
}
}
void _drawNeighbourCounts() { // debug method
for (int j = 0; j < CELLS_NUM_Y; j++) {
for (int i = 0; i < CELLS_NUM_X; i++) {
int liveNeighs = calculateLiveNeighbours(i, j);
fill(255);
textAlign(LEFT, TOP);
textSize(8);
text(liveNeighs, i * CELL_SIZE, j * CELL_SIZE);
}
}
}
/* I think that the cool effect with dragging point brush is because this function is evaluated independently
from draw() function */
void mouseDragged() {
if (mouseButton == LEFT) {
paintArray(patterns[currentPatternBrush]);
} else if (mouseButton == RIGHT) {
eraseWithBrush();
}
}
void mousePressed() {
if (mouseButton == LEFT) {
paintArray(patterns[currentPatternBrush]);
} else if (mouseButton == RIGHT) {
eraseWithBrush();
}
println("sup.");
}
void keyReleased() {
if (key == 's' || key == 'S') saveFrame("dvdisawesome" + timestamp() + "_##.png");
}
void keyPressed() {
if (key == 'p' || key == 'P') { // Play/pause
play = !play;
} else if (key == 'c' || key == 'C') { // Clear
//play = false; // maybe not needed
clearGrid();
} else if (keyCode == DOWN) { // put down the pattern (for putting just one pattern on exact position)
paintArray(patterns[currentPatternBrush]);
} else if (key == ' ') {
isInGame = true;
} else if (key == ESC) {
if (isInGame) { // if we are not on welcome screen, we do not want to quit
key = 0;
}
isInGame = false;
clearGrid();
} else if (key == '1') {
currentPatternBrush = 0;
} else if (key == '2') {
//paintArray(pulsar);
currentPatternBrush = 1;
} else if (key == '3') {
currentPatternBrush = 2;
} else if (key == '4') {
currentPatternBrush = 3;
} else if (key == '5') {
currentPatternBrush = 4;
} else if (key == '0') {
hideBrush = !hideBrush;
} else if (key == 'q') {
fadeOutEffect = !fadeOutEffect;
} else if (key == 'w') {
cellColorMode = !cellColorMode;
}
}
void paintArray(int [][] pattern) {
int patSizeX = pattern[0].length;
int patSizeY = pattern.length;
float randomHue = random(0, 255);
for (int j = 0; j < patSizeY; j++) {
for (int i = 0; i < patSizeX; i++) {
boolean patPosStatus = pattern[j][i] == 1 ? true : false;
int xOffset = (int)mouseX / CELL_SIZE;
int yOffset = (int)mouseY / CELL_SIZE;
if ( (i + xOffset >= CELLS_NUM_X) || (j + yOffset >= CELLS_NUM_Y) ) continue;
cellsGrid[i + xOffset][j + yOffset].status = patPosStatus;
if (patPosStatus) cellsGrid[i + xOffset][j + yOffset].hue = randomHue;
}
}
}
void drawBrush() {
// old code - just a circle
stroke(255);
fill(255, 0);
ellipse(mouseX, mouseY, 100, 100);
}
void drawOnePointBrush() {
fill(125, 125);
rect(mouseX, mouseY, CELL_SIZE, CELL_SIZE);
}
void drawBrushPattern(int[][] pattern) {
int patSizeX = pattern[0].length;
int patSizeY = pattern.length;
for (int j = 0; j < patSizeY; j++) {
for (int i = 0; i < patSizeX; i++) {
//boolean patPosStatus = pattern[i][j] == 1 ? true : false;
boolean patPosStatus = pattern[j][i] == 1 ? true : false;
if (!patPosStatus) continue;
int x = mouseX + i * CELL_SIZE;
int y = mouseY + j * CELL_SIZE;
int w = CELL_SIZE;
int h = CELL_SIZE;
fill(125, 125);
rect(x, y, w, h);
}
}
}
void paintWithBrush() {
int i = (int)mouseX / CELL_SIZE;
int j = (int)mouseY / CELL_SIZE;
if (i < 0 || j < 0) return;
if (i >= CELLS_NUM_X || j >= CELLS_NUM_Y) return;
//println("mouseX / CELL_SIZE = " + mouseX + " / " + CELL_SIZE + " = " + i);
cellsGrid[i][j].status = true;
}
void eraseWithBrush() {
int i = (int)mouseX / CELL_SIZE;
int j = (int)mouseY / CELL_SIZE;
println("mouseX / CELL_SIZE = " + mouseX + " / " + CELL_SIZE + " = " + i);
cellsGrid[i][j].status = false;
}
void solveGrid() {
for (int j = 0; j < CELLS_NUM_Y; j++) {
for (int i = 0; i < CELLS_NUM_X; i++) {
int liveNeighs = calculateLiveNeighbours(i, j);
boolean currentStatus = cellsGrid[i][j].status;
boolean newStatus = solveCell(currentStatus, liveNeighs);
nextCellsGrid[i][j].status = newStatus;
if (!currentStatus && newStatus) { // cell went from dead to alive
nextCellsGrid[i][j].hue = calculateNeighboursColor(i, j);
}
}
}
}
/*
Function calculates average hue of its live neighbours (only called for dead cell)
*/
float calculateNeighboursColor(int i, int j) {
int num = 0;
float sumhue = 0;
for (int l = j - 1; l <= j + 1; l++) {
for (int k = i - 1; k <= i + 1; k++) {
if (k == i && l == j) continue; // skip the current cell
if (k < 0 || l < 0) continue; // boundary condition
if (k >= CELLS_NUM_X || l >= CELLS_NUM_Y) continue; // boundary condition
if (cellsGrid[k][l].status) {
num += 1;
sumhue += cellsGrid[k][l].hue;
}
}
}
println(sumhue / num);
return sumhue / num;
}
int calculateLiveNeighbours(int i, int j) {
int num = 0;
for (int l = j - 1; l <= j + 1; l++) {
for (int k = i - 1; k <= i + 1; k++) {
if (k == i && l == j) continue; // skip the current cell
if (k < 0 || l < 0) continue; // boundary condition
if (k >= CELLS_NUM_X || l >= CELLS_NUM_Y) continue; // boundary condition
if (cellsGrid[k][l].status) {
num += 1;
}
}
}
return num;
}
boolean solveCell(boolean currentStatus, int liveNeighboursNum) {
if (currentStatus) { // cell is alive
if (liveNeighboursNum < 2) return false; // under-population... dies
if (liveNeighboursNum > 3) return false; // over-population... dies
return true; // 2 or 3 live neighbours...lives
} else { // cell is dead
if (liveNeighboursNum == 3) return true; // reproduction...alive
return false; // ...stays dead
}
}
void createCells() {
for (int j = 0; j < CELLS_NUM_Y; j++) {
for (int i = 0; i < CELLS_NUM_X; i++) {
//cellsGrid[i][j] = new Cell(i, j, false);
cellsGrid[i][j] = new Cell(i, j, false );
}
}
}
void clearGrid() {
for (int j = 0; j < CELLS_NUM_Y; j++) {
for (int i = 0; i < CELLS_NUM_X; i++) {
cellsGrid[i][j].status = false;
}
}
}
void createCellsRandom() {
for (int j = 0; j < CELLS_NUM_Y; j++) {
for (int i = 0; i < CELLS_NUM_X; i++) {
//cellsGrid[i][j] = new Cell(i, j, false);
cellsGrid[i][j] = new Cell(i, j, ((int)random(2)) == 1 );
}
}
}
void drawCells() {
for (int j = 0; j < CELLS_NUM_Y; j++) {
for (int i = 0; i < CELLS_NUM_X; i++) {
cellsGrid[i][j].drawCell();
}
}
}
void drawGrid() { // not used anymore
stroke(200);
for (int i = 0; i <= width; i += CELL_SIZE) {
line(i, 0, i, height);
}
for (int j = 0; j < height; j += CELL_SIZE) {
line(0, j, width, j);
}
stroke(0);
}
int[][][] patterns = {
// point ... yes, lol
{
{1}
},
// pulsar
{
{0,0,1,1,1,0,0,0,1,1,1,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0},
{1,0,0,0,0,1,0,1,0,0,0,0,1},
{1,0,0,0,0,1,0,1,0,0,0,0,1},
{1,0,0,0,0,1,0,1,0,0,0,0,1},
{0,0,1,1,1,0,0,0,1,1,1,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,0,0,0,1,1,1,0,0},
{1,0,0,0,0,1,0,1,0,0,0,0,1},
{1,0,0,0,0,1,0,1,0,0,0,0,1},
{1,0,0,0,0,1,0,1,0,0,0,0,1},
{0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,1,1,1,0,0,0,1,1,1,0,0}
},
// lightweight spaceship
{
{0,1,0,0,1},
{1,0,0,0,0},
{1,0,0,0,1},
{1,1,1,1,0}
},
// r-pentomino
{
{0,1,1},
{1,1,0},
{0,1,0}
},
// infinite growth
{
{1,1,1,0,1},
{1,0,0,0,0},
{0,0,0,1,1},
{0,1,1,0,1},
{1,0,1,0,1}
}
};
// timestamp
String timestamp() {
Calendar now = Calendar.getInstance();
return String.format("%1$ty%1$tm%1$td_%1$tH%1$tM%1$tS", now);
}