With npm do:
npm install cellular-automata
To run the test suite, run the following command from the cellular-automata
directory:
npm test
- Doesn't have any dependency to the DOM.
- Can easily apply different successive rules.
- Can be used in any dimension (1D, 2D, 3D and more).
- Allow the cellular automata rules to be passed as a string in one of several common CA rule format, see cellular-automata-rule-parser.
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
// 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
new CellularAutomata(shape[, defaultValue = 0])
- shape : Shape of the grid (ie: [800,600] for a 2d grid of 800 cells of width and 600 cells of height).
- defaultValue : Default value of the cells.
All methods are chainable
setOutOfBoundValue([outOfBoundValue = 0])
Define the value used for the neighbours out of the array's bounds.
- outOfBoundValue : The value to use, either an integer, the string "wrap" to enable grid wrapping or the string "clamp" to use the nearest in-bound cell.
setRng([rng = null])
Set the random number generation function used internally.
- rng : A function to use as random number generator, defaults to Math.random.
fillWithDistribution(distribution[, rng = null])
Fill the grid with a given distribution.
- distribution : An array of two dimensions representing the distribution to fill the grid with. (ie: [[0,90], [1,10]] for 90% of 0 and 10% of 1). Null values are ignored.
- rng : A function used as random number generator, defaults to the internal RNG function.
setRule(rule[, neighbourhoodType[, neighbourhoodRange = 1]])
Define the rule of the cellular automata and the neighbourhood to be used.
- rule : Either a valid rule string (see cellular-automata-rule-parser) or a function taking as arguments the value of the current cell and an array containing the values of all its neighbours.
- neighbourhoodType : Neighbourhood type (moore, von-neumann, axis, corner, edge or face), only used when the rule is a function.
- neighbourhoodRange : Neighbourhood range, only used when the rule is a function.
iterate([iterations = 1])
Apply the previously defined CA rule multiple times.
- iteration : Number of iterations.
apply(rule[, iterations = 1[, neighbourhoodType[, neighbourhoodRange = 1]]])
Apply a given rule for a given number of iterations, shortcut method for setRule and iterate.
- rule : Either a valid rule string (see cellular-automata-rule-parser) or a function taking as arguments the value of the current cell and an array containing the values of all its neighbours.
- iteration : Number of iterations.
- neighbourhoodType : Neighbourhood type (moore, von-neumann, axis, corner, edge or face), only used when the rule is a function.
- neighbourhoodRange : Neighbourhood range, only used when the rule is a function.
shape
The shape of the grid.
dimension
The dimension of the grid.
array
The ndarray containing all the current data in the grid.
- Update dependencies.
- Minor refactoring.
- Reduce npm package size.
- Update dependencies.
- Add travis support.
- Less direct support for older browser (now use
const
andlet
variable declarations).
- Update the rule parser to support the extended stochastic rule format.
- Add the method setRng() to set the internal RNG function.
- Support for 'clamp' out-of-bound value.
- Update the rule parser.
- Better documentation.
- The method fillWithDistribution now ignores null values.
- Rename the properties currentArray to array and dimensions to dimension.
- Remove the property defaultValue, the method switchArray and the method replace.
- Declare stable.
- Update the rule parser.
- Supports unconventional neighbourhood types (axis, corner, edge and face).
- Sort neighbourhood to allow position dependent rules.
- Update the rule parser.
- Update the rule parser.
- First implementation.
- More tests.
MIT