It supports arbitrary dimension and any CA rule, thanks to JavaScript.
How to use
This is supposed to upload to npm, but for some reason it failed. You could still get the library from /src/src.js .
Usage
Code: Select all
// Example: CGoL
import caLib from "caLib";
let grid = caLib.newGrid(10, 2, 0);
grid = caLib.updateCell(grid, [1, 0], 1);
grid = caLib.updateCell(grid, [1, 1], 1);
grid = caLib.updateCell(grid, [1, 2], 1);
const cgol = (cell, coords, getCell) => {
const [x, y] = coords;
let live = 0;
for (let dx = -1; dx <= 1; dx++) {
for (let dy = -1; dy <= 1; dy++) {
if (dx === 0 && dy === 0) continue; // skip self
if (getCell([x + dx, y + dy]) === 1) live++;
}
}
if (cell === 1) {
return live === 2 || live === 3 ? 1 : 0;
} else {
return live === 3 ? 1 : 0;
}
};
for (let step = 1; step <= 15; step++) {
grid = caLib.step(grid, cgol);
console.log(`Step ${step}:`);
for (let y = 0; y < 5; y++) {
let row = "";
for (let x = 0; x < 5; x++) {
row += caLib.updateCell(grid, [x, y], 0);
row += caLib.step(grid, cgol) ? "1 " : "0 ";
}
console.log(row);
}
}
Kindly report any bugs or issues here.
For other questions, ask in this thread instead.