Home

Awesome

cellular-automata

Build Status NPM version

Installing and testing

With npm do:

npm install cellular-automata

To run the test suite, run the following command from the cellular-automata directory:

npm test

Features

Usage

Code

var CellularAutomata = require('cellular-automata');

// create a cellular automata with width of 200 cells and a height of 80 cells
var cellularAutomata = new CellularAutomata([200, 80]);

// fill the array with 95% of 0 values and 5% of 1 values
cellularAutomata.fillWithDistribution([[0, 95], [1, 5]]);

// define that the value out of the array should be interpreted as 0 values
cellularAutomata.setOutOfBoundValue(0);

cellularAutomata.setRule('23/3').iterate(5); // apply 5 times the S23/B3 rule (conway's life)
cellularAutomata.setRule('135/17').iterate(3); // apply 3 times the S135/B17 rule
cellularAutomata.setRule('234/12345678').iterate(5); // apply 5 times the S234/B12345768 rule

console.log(cellularAutomata.array); // ndarray containing the result

Result as an image

<img src="https://github.com/kchapelier/cellular-automata/raw/master/readme1.png" style="image-rendering:pixelated; width:400px;"></img>

Code

// create a cellular automata with width of 75 cells and a height of 75 cells
var cellularAutomata = new CellularAutomata([75, 75]);

// use the fluent interface and the shortcut method "apply"
cellularAutomata
    .setOutOfBoundValue(1)
    .apply('23/3', 16)
    .apply('23456/45678', 16)
    .apply('23456/478', 16);

console.log(cellularAutomata.array); // ndarray containing the result

Result as an image

<img src="https://github.com/kchapelier/cellular-automata/raw/master/readme2.png" style="image-rendering:pixelated; width:150px;"></img>

Public API

Constructor

new CellularAutomata(shape[, defaultValue = 0])

Methods

All methods are chainable

setOutOfBoundValue([outOfBoundValue = 0])

Define the value used for the neighbours out of the array's bounds.

setRng([rng = null])

Set the random number generation function used internally.

fillWithDistribution(distribution[, rng = null])

Fill the grid with a given distribution.

setRule(rule[, neighbourhoodType[, neighbourhoodRange = 1]])

Define the rule of the cellular automata and the neighbourhood to be used.

iterate([iterations = 1])

Apply the previously defined CA rule multiple times.

apply(rule[, iterations = 1[, neighbourhoodType[, neighbourhoodRange = 1]]])

Apply a given rule for a given number of iterations, shortcut method for setRule and iterate.

Properties

shape

The shape of the grid.

dimension

The dimension of the grid.

array

The ndarray containing all the current data in the grid.

Changelog

2.0.1 (2019-09-28) :

2.0.0 (2019-04-19) :

1.2.0 (2016-03-22) :

1.1.0 (2016-03-09) :

1.0.1 (2016-01-24) :

1.0.0 (2015-11-17) :

0.1.0 (2015-11-02) :

0.0.3 (2015-10-17) :

0.0.2 (2015-10-13) :

0.0.1 (2015-10-04) :

Roadmap

License

MIT

Learn more about cellular automata