Inspired by Conway’s game of life, I decided to program a 2 dimensional Cellular Automaton in Actionscript 3.
I had been reading up on complexity theory for a recent project and the Cellular Automata just kept on rearing its head. Of course it displays the fundamental qualities of complexity; starting with an object following a set of simple rules and then creating multiple instances to form complex and unpredictable patterns.
In terms of visualisation, the Cellular Automata is interesting because it has potential to be used for many applications. Complexity Theory and Systems Theory are used to simulate various growth patterns such as that of a city or population. My personal ideas are of course much simpler, one being a 3 dimensional version which uses Papervision 3D; and, rather than switching cells on or off, would incrementally grow or shrink shapes in 3D space to create landscapes or cityscapes whose growth is determined by the development of the Automata.
This particular Actionscript 3 version is a fairly direct port of the 2 dimensional ‘Game of Life’ permutation, but is fun to play with none-the-less.
The rules are as follows:
Each cell can be either empty, new, alive or dead. As the grid updates, each cell observes its 8 neighbouring cells (or less if it is at a corner or edge) and changes it’s state depending on the state of said neighbouring cells.
- If less than 2 neighbours are alive, the cell dies (loneliness)
- If more than 3 neighbours are alive, the cell dies (overcrowding)
- If a living cell has 2 or 3 living neighbours it continues to live
- If a dead cell has 3 living neighbours it comes to life
There are many initial shapes which either form patterns of perpetual growth or at least interesting shapes as the system updates; some simply die out. Have a play and see if you can find shapes, or combinations of shapes which do interesting things. It’s quite addictive!
I have added a few presets to illustrate how certain groupings of cells that are alive when the system starts can behave in the grid as the automaton runs, and also a randomise button which will create a random grid of empty and alive cells and can form some interesting patterns as the system iterates and emergence begins to kick in.
The source code is of course available, so have a play with that and see what happens when you change the rules or create bigger grids.
A quick note about the Actionscript.
I have created a CAGrid class for building Cellular Automata, which can be instantiated at any size and with any amount of rows and colums.
/** * A grid of cells used to produce a 2 dimensional cellular automata * * @param gridSize The dimensions in pixels of the grid * @param segments The number of cells in each row or column * @param cellPad The padding (or spacing) inside each cell */ public function CAGrid( gridSize:int, segments:int, cellPad:int )
The CAGrid class contains methods for drawing the grid, resetting the grid, updating the system, randomising cells, saving configurations and loading presets, and is automatically redrawn when the size or rows / columns are changed during runtime using the size and segments setters respectively.
The Cell class contains a simple setter for status, and some constants for easy reference to the cell’s state. In this example the cell is drawn by choosing a colour which corresponds to the state of the cell, but a movieclip with labelled frames (for example) could just as easily be used to create more visually interesting results.
Oh, and included are some custom skinned CS3 components (button and comboBox) which look a bit like the Vista skin, so maybe those are goodies in themselves!Download: 2D Cellular Automata