xxxxxxxxxx
61
let cells = [];
let rows = [];
const cellSize = 4;
let currentRow = 0;
function setup() {
createCanvas(765, 400);
background('black');
const numCells = width / cellSize;
for (let i = 0; i < numCells; i++) {
cells[i] = round(random(0,1));
}
cells[round(numCells / 2)] = 1;
const numRows = height / cellSize;
for (let i = 0; i < numRows; i++) {
rows[i] = round(random(0,1));
}
rows[round(numRows / 2)] = 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);
}
let newCells = [];
let newRows = [];
for (let i = 0; i < cells.length; i++) {
for (let j = 0; j <rows.length; j++){
const rowTop = rows[i - 1] ??0;
const left = cells[i - 1] ?? 0;
const rowMiddle = rows[i];
const middle = cells[i];
const rowBottom = rows[i +1] ?? 0;
const right = cells[i + 1] ?? 0;
const sum = rowTop + right + rowBottom;
if (sum == 2 || sum % 2) {
newCells[i] = 1;
} else {
newCells[i] = 0;
}
}
}
cells = newCells;
rows = newRows;
}