-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
71 lines (64 loc) · 1.42 KB
/
main.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
function make2DArray(cols, rows) {
let arr = new Array(cols)
for (let i = 0; i < arr.length; i++) {
arr[i] = new Array(rows)
for (let j = 0; j < arr[i].length; j++) {
arr[i][j] = 0;
}
}
return arr
}
let grid;
let w = 10;
let cols, rows;
function setup() {
createCanvas(400, 400);
cols = width/w;
rows = height/w;
grid = make2DArray(cols, rows);
for (let i = 0; i < cols; i++) {
for(let j = 0; j < rows; j++) {
grid[i][j] = 0;
}
}
}
function mouseDragged() {
let col = floor(mouseX / w);
let row = floor(mouseY / w);
grid[col][row] = 1;
}
function draw() {
background(0);
for(let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
stroke(255)
fill(grid[i][j]*255);
let x = i * w;
let y = j * w;
square(x, y, w);
}
}
let nextGrid = make2DArray(cols, rows);
for(let i = 0; i < cols; i++) {
for (let j = 0; j < rows; j++) {
let state = grid[i][j];
if (state === 1) {
let below = grid[i][j + 1];
let belowLeft = grid[i - 1][j + 1];
let belowRight = grid[i + 1][j + 1]
if (below === 0) {
nextGrid[i][j + 1] = 1;
} else if (belowLeft === 0) {
nextGrid[i - 1][j + 1] = 1;
}
else if (belowRight === 0) {
nextGrid[i + 1][j + 1] = 1;
}
else {
nextGrid[i][j] = 1;
}
}
}
}
grid = nextGrid;
}