xxxxxxxxxx
59
let cells = [];
const cellSize = 2;
let currentRow = 0;
function setup() {
createCanvas(400, 400);
background('black');
// const numCells = width / cellSize;
// for (let i = 0; i < numCells; i++) {
// cells[i] = 0;
// }
// cells[round(numCells / 2)] = 1;
// lets make the starting on cells random
const numCells = width / cellSize;
for (let i = 0; i < numCells; i++) {
cells[i] = random([0, 1]);
}
}
function draw() {
fill('white');
noStroke();
// translate(0, currentRow * cellSize);
currentRow++;
for (let i = 0; i < cells.length; i++) {
if (cells[i] == 0) {
fill('black');
} else {
fill('white');
}
// rect(i * cellSize, 0, cellSize, cellSize);
if (i * cellSize > currentRow * cellSize) {
rect(i * cellSize, currentRow * cellSize, cellSize, cellSize);
}
// rect(i * cellSize, height - currentRow * cellSize, cellSize, cellSize);
}
let newCells = [];
for (let i = 0; i < cells.length; i++) {
const left = cells[i - 1] ?? 0;
const middle = cells[i];
const right = cells[i + 1] ?? 0;
const sum = left + middle + right;
if (sum == 1) {
newCells[i] = 1;
} else {
newCells[i] = 0;
}
}
cells = newCells;
if (currentRow * cellSize >= 0.5 * height) {
noLoop();
}
}