-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame_Of_Life.pde
94 lines (77 loc) · 1.72 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
int virt_w, virt_h;
float scale;
int cells[][];
int cells_buf[][];
int COLS, ROWS;
int RESOLUTION;
long last_frame_time;
long tickout_time;
void settings()
{
scale = 1.5;
virt_w = floor(640 * scale);
virt_h = floor(360 * scale);
size(virt_w, virt_h);
last_frame_time = 0;
tickout_time = 200;
RESOLUTION = 8;
COLS = width / RESOLUTION;
ROWS = height / RESOLUTION;
cells = new int[COLS][ROWS];
cells_buf = new int[COLS][ROWS];
for (int i = 0; i < COLS; ++i) {
for (int j = 0; j < ROWS; ++j) {
if (random(1) > 0.65) cells[i][j] = 1;
else cells[i][j] = 0;
}
}
}
void draw()
{
Update();
Render();
}
void Update()
{
long interval = millis() - last_frame_time;
if (interval < tickout_time) return;
for (int i = 0; i < (COLS * ROWS); ++i)
{
int count = CountNeighbours(i % COLS, i / COLS);
println(count);
if (cells[i % COLS][i / COLS] == 1 && (count < 2 || count > 3))
{cells_buf[i % COLS][i / COLS] = 0;}
if (cells[i % COLS][i / COLS] == 0 && count == 3)
{cells_buf[i % COLS][i / COLS] = 1;}
}
arrayCopy(cells, cells_buf);
last_frame_time = millis();
}
void Render()
{
background(45);
fill(244);
for (int i = 0; i < (COLS * ROWS); ++i)
{
int x = i % COLS;
int y = i / COLS;
if (cells[x][y] == 0) continue;
square(x * RESOLUTION, y * RESOLUTION, RESOLUTION);
}
}
// TODO(rawxtl):
// - Implement rules
// - Implement counting
int CountNeighbours(int x, int y)
{
int count = 0;
for (int i = 0; i < 3; ++i) {
for (int j = 0; j < 3; ++j) {
int nx = x - i;
int ny = y - j;
if (nx < 0 || nx > COLS || ny < 0 || ny > ROWS) continue;
if (cells[nx][ny] == 1) count += 1;
}
}
return count > 0 ? count - 1: 0;
}