p5(sketch => {
let system;
let w;
let columns;
let rows;
let board;
let next;
sketch.setup = function() {
sketch.createCanvas(width, 400);
w = 20;
columns = sketch.floor(sketch.width / w);
rows = sketch.floor(sketch.height / w);
board = new Array(columns);
for (let i = 0; i < columns; i++) {
board[i] = new Array(rows);
}
next = new Array(columns);
for (let j = 0; j < columns; j++) {
next[j] = new Array(rows);
}
init();
};
sketch.draw = function() {
sketch.background(255);
generate();
for ( let i = 0; i < columns;i++) {
for ( let j = 0; j < rows;j++) {
if ((board[i][j] == 1)) sketch.fill(sketch.random(255), sketch.random(255), sketch.random(255));
else sketch.fill(255);
sketch.stroke(0);
sketch.rect(i * w, j * w, w-1, w-1);
}
}
}
sketch.mousePressed = function() {
init();
}
function init() {
for (let i = 0; i < columns; i++) {
for (let j = 0; j < rows; j++) {
if (i == 0 || j == 0 || i == columns-1 || j == rows-1) board[i][j] = 0;
else board[i][j] = sketch.floor(sketch.random(2));
next[i][j] = 0;
}
}
}
function generate() {
for (let x = 1; x < columns - 1; x++) {
for (let y = 1; y < rows - 1; y++) {
let neighbors = 0;
for (let i = -1; i <= 1; i++) {
for (let j = -1; j <= 1; j++) {
neighbors += board[x+i][y+j];
}
}
neighbors -= board[x][y];
if ((board[x][y] == 1) && (neighbors < 2)) next[x][y] = 0;
else if ((board[x][y] == 1) && (neighbors > 3)) next[x][y] = 0;
else if ((board[x][y] == 0) && (neighbors == 3)) next[x][y] = 1;
else next[x][y] = board[x][y];
}
}
let temp = board;
board = next;
next = temp;
}
});