-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlife.cpp
More file actions
83 lines (68 loc) · 1.49 KB
/
Copy pathlife.cpp
File metadata and controls
83 lines (68 loc) · 1.49 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
#include "main.h"
// Count the number of alive neighbors of a cell
int countcell(int x, int y) {
int cnt=0;
if(getpx(x-1,y-1)) cnt++;
if(getpx(x,y-1)) cnt++;
if(getpx(x+1,y-1)) cnt++;
if(getpx(x-1,y)) cnt++;
if(getpx(x+1,y)) cnt++;
if(getpx(x-1,y+1)) cnt++;
if(getpx(x,y+1)) cnt++;
if(getpx(x+1,y+1)) cnt++;
return cnt;
}
int cells_born=0;
// Run the life simulation for a single column
// After running the last column, it flips the display buffers
void dolife(int x) {
if(disp==disp1) {
disp0 = disp2;
} else {
disp0 = disp1;
}
//for(int x=0; x<112; x++) {
for(int y=0; y<16; y++) {
boolean alive = getpx(x,y);
int cnt = countcell(x,y);
if(alive) {
if(cnt < 2) {
setpx(x,y,0);
} else if(cnt > 3) {
setpx(x,y,0);
} else {
setpx(x,y,1);
}
} else {
if(cnt==3) {
setpx(x,y,1);
cells_born++;
}
}
}
//}
if(x==111) {
// If nothing much is happening, restart simulation
if(cells_born < 8)
randomize_display();
cells_born=0;
for(int i=0; i<224; i++)
disp[i] = 0;
disp = disp0;
}
}
int generations=0;
int life_x_ctr=0;
void loop_life() {
dolife(life_x_ctr);
life_x_ctr++;
if(life_x_ctr>111) {
generations++;
Serial.println(generations);
if(generations > 1000) {
randomize_display();
generations = 0;
}
life_x_ctr=0;
}
}