xxxxxxxxxx
56
let cells = [];
const cellSize = 3;
let currentRow =0;
function setup() {
createCanvas(400, 400);
background("black");
// stroke('white');
const numCells = width / cellSize;
for (let i = 0; i < numCells; i++) {
cells[i] = round(random(0,1));
}
//cells[round(numCells / 2)] = 1;
frameRate(10);
}
function draw() {
fill("white");
// stroke("black");
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);
}
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;
// print(sum);
if (sum == 1) {
newCells[i] =1;
} else {
newCells[i] =0
}
// newCells[i] =;
}
cells = newCells;
// rotate(2);
}