-
Notifications
You must be signed in to change notification settings - Fork 22
/
conway.js
116 lines (96 loc) · 2.78 KB
/
conway.js
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
// Conway's Game of Life
//
// https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
//
// Intentionally slowed down the framerate, otherwise certain elements
// can become a bit strobe-y...
//
// Author: John Vidler
return class MyEffect {
constructor(display) {
this.display = display;
this.reset();
}
#clear() {
for (let x = 0; x < this.display.width; x++) {
for (let y = 0; y < this.display.height; y++) {
this.display.setPixel(x, y, [0, 0, 0]);
}
}
this.display.flush();
}
reset() {
this.buffer = new Array();
this.frame = new Array();
for (let y = 0; y < this.display.height; y++) {
this.buffer[y] = new Array();
this.frame[y] = new Array();
for (let x = 0; x < this.display.width; x++) {
this.buffer[y][x] = false;
this.frame[y][x] = Math.random() > 0.93;
}
}
this.#clear();
}
getCell( x, y ) {
if( x < 0 )
x += this.display.width;
if( y < 0 )
y += this.display.height;
return this.buffer[y%this.display.height][x%this.display.width] || false;
}
setCell( x, y, state ) {
if( x < 0 )
x += this.display.width;
if( y < 0 )
y += this.display.height;
this.frame[y%this.display.height][x%this.display.width] = state;
}
process() {
let count = 0;
for (let x = 0; x < this.display.width; x++) {
for (let y = 0; y < this.display.height; y++) {
let near =
this.getCell( x+1, y-1 ) +
this.getCell( x+1, y ) +
this.getCell( x+1, y+1 ) +
this.getCell( x, y-1 ) +
this.getCell( x, y+1 ) +
this.getCell( x-1, y-1 ) +
this.getCell( x-1, y ) +
this.getCell( x-1, y+1 );
let state = this.getCell( x, y );
if( state ) {
if( near > 3 )
this.setCell( x, y, false ); // Overpopulation
if( near < 3 )
this.setCell( x, y, false ); // Underpopulation
}
if( near === 3 )
this.setCell( x, y, true ); // Reproduction
if( near === 2 )
this.setCell( x, y, state ); // Persist
count = (state?count+1:count)
}
}
return count;
}
update() {
this.frameDelay = (this.frameDelay || 0) % 2;
if( this.frameDelay == 0 ) {
const alive = this.process();
const color = [255, 255, 255];
for (let x = 0; x < this.display.width; x++) {
for (let y = 0; y < this.display.height; y++) {
if( this.frame[y][x] || false )
this.display.setPixel(x, y, color);
else
this.display.setPixel(x, y, [0,0,0]);
this.buffer[y][x] = this.frame[y][x] || false;
}
}
this.display.flush();
}
this.frameDelay++;
}
}