canvas = {
const size = Math.round(930/cellSize);
const grid = Array.from({ length: size })
.fill(0)
.map(() => Array.from({ length: size }).fill(0));
const center = Math.round(size/2);
grid[center][center] = 1;
const border = new Set([[center, center]])
const context = DOM.context2d(size * cellSize, size * cellSize);
context.lineWidth = 0.5;
context.strokeStyle = '#333';
context.fillStyle = 'white';
context.fillRect(0, 0, size * cellSize, size * cellSize);
context.fillStyle = 'green';
context.fillRect(center * cellSize, center * cellSize, cellSize, cellSize);
let frame = 0;
while (true) {
try {
const chosenBorderCell = Array.from(border)[randInt(border.size - 1)];
const [x, y] = chosenBorderCell;
let foundSpace = false;
let xn = x;
let yn = y;
for (let i = 0; i < tries; ++i) {
xn = x;
yn = y;
const direction = randInt(4);
if (direction === 0) {
yn--;
} else if (direction === 1) {
xn++;
} else if (direction === 2) {
yn++;
} else if (direction === 3) {
xn--;
}
if (grid[xn][yn] === 0 && nFreeNeighbors(xn, yn, grid) > 5) {
foundSpace = true;
break;
}
}
if (!foundSpace) {
border.delete(chosenBorderCell);
context.fillStyle = 'green';
context.fillRect(x * cellSize, y * cellSize, cellSize, cellSize);
}
if (foundSpace) {
grid[xn][yn] = 1;
context.fillStyle = '#27C939';
context.fillRect(xn * cellSize, yn * cellSize, cellSize, cellSize);
border.add([xn, yn]);
}
} catch (e) {
}
if (++frame > 50) {
yield (frame=0, context.canvas);
}
}
}