Skip to content

Create two-dimensional Cyclic Cellular Automaton. Export results as (animated) images. Includes presets of working rules.


Notifications You must be signed in to change notification settings


Repository files navigation

PHP Cyclic Cellular Automaton

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

This project can be used to run two-dimensional Cyclic Cellular Automaton (CCA). Results can be saved as static images of animated gifs. The configuration (of the CCA or images) can be set using various presets or customized to your own liking.


Via Composer

$ composer require barryvanveen/php-cca

Please note that you need PHP 7.0 or higher and the GD extension to install this package.


Creating a configuration

// using the ConfigBuilder
$builder = Builders\ConfigBuilder::createFromPreset(Config\Presets::PRESET_CCA);
$config = $builder->get();
// or build it from scratch
$config = new Config([
    Config\Options::NEIGHBORHOOD_SIZE => 1,
    Config\Options::NEIGHBORHOOD_TYPE => Config\NeighborhoodOptions::NEIGHBORHOOD_TYPE_NEUMANN,
    Config\Options::STATES => 14,
    Config\Options::THRESHOLD => 1,
    Config\Options::ROWS => 50,
    Config\Options::COLUMNS => 50,

In \Barryvanveen\CCA\Config\Presets.php you can find all available presets.

Running the CCA

// get a single state
$runner = new Runner($config, Factories\CCAFactory::create($config));
$state = $runner->getLastState(234);
// get a set of states
$runner = new Runner($config, Factories\CCAFactory::create($config));
$states = $runner->getFirstStates(123);
// get a set of states that loops (if possible)
$runner = new Runner($config, Factories\CCAFactory::create($config));
$states = $runner->getFirstLoop(500);  

The Runner is probably sufficient for most scenarios but if you want more control you can control the CCA yourself. Just look at the Runner implementation to get an idea of how this works.

Generating images

// create a static Gif from a single stae
$image = Generators\Gif::createFromState($config, $state);
// create a static Png from a single state
$image = Generators\Png::createFromState($config, $state);
// create an animated Gif
$image = Generators\AnimatedGif::createFromStates($config, $states);


The /examples folder contains some scripts to generate different kinds of images. Here are some example images:

static gif from amoeba preset static gif from cca preset static gif from lavalamp preset static gif from cyclic spirals preset

animated gif from squarish spirals preset animated looping gif from cyclic spirals preset animated looping gif from cca preset animated looping gif from 313 preset


Please see the releases for more information on what has changed recently.


$ composer test


Please see CONTRIBUTING and CODE_OF_CONDUCT for details.


If you discover any security related issues, please email instead of using the issue tracker.


All preset configurations are taken from which is the work of Mirek Wójtowicz.

The colors for the images are generated using talesoft/phim which is a project by Torben Köhn.

Animated gifs are created using lunakid/anim-gif.



The MIT License (MIT). Please see License File for more information.