caLIb - JavaScript CA simulation library

For scripts to aid with computation or simulation in cellular automata.
Post Reply
User avatar
b-engine
Posts: 3762
Joined: October 26th, 2023, 4:11 am
Location: Somewhere on where Earth At
Contact:

caLIb - JavaScript CA simulation library

Post by b-engine »

caLib is a JavaScript library for cellular automata (and its superset) simulation.
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);
  }
}
Bugs
Kindly report any bugs or issues here.
For other questions, ask in this thread instead.
User avatar
b-engine
Posts: 3762
Joined: October 26th, 2023, 4:11 am
Location: Somewhere on where Earth At
Contact:

Re: caLIb - JavaScript CA simulation library

Post by b-engine »

Update 0.2.0

Added toRule(), which converts rulestrings to functions usable by caLib. Currently only support B/S notation.

Example before:

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);
  }
}
Example after:

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 = toRule("B3/S23");

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);
  }
}
User avatar
hth3
Posts: 362
Joined: February 15th, 2025, 10:04 am
Location: The Sun

Re: caLib - JavaScript CA simulation library

Post by hth3 »

Can you use it in Turbowarp and other Scratch mods as an extension?
Also, I want to contribute to it, but I use Codeberg only, not GitHub, how can I contribute?
Can't trust someone who misspells typset as typeset.
Contribute to CheckerLife!
Oppose KOSA now, save the internet!
The Sandboxer Sandbox (Discord server)
RIP Unname New Web
User avatar
b-engine
Posts: 3762
Joined: October 26th, 2023, 4:11 am
Location: Somewhere on where Earth At
Contact:

Re: caLib - JavaScript CA simulation library

Post by b-engine »

hth3 wrote: November 10th, 2025, 7:57 am Can you use it in Turbowarp and other Scratch mods as an extension?
I never tried, and guess no. In fact I never tested if it works.
hth3 wrote: November 10th, 2025, 7:57 am Also, I want to contribute to it, but I use Codeberg only, not GitHub, how can I contribute?
This repository is only available on GitHub, sorry.
Post Reply