Cellular Automata

A **cellular automaton** (automata is plural) is a grid of cells that "live" and "die" based on certain rules about their neighbor cells. Popularized by John Horton Conway's *Game of Life*, they can be used in procedural content generation to create dungeons and caves.

## Cellular Automata Algorithm

Here an algorithm for creating two-dimensional caves is presented. These could also be treated as dungeons, with enemies and treasures placed throughout.

### Algorithm

```
let chanceToLive = a number between 0 and 1 representing how likely it is for a cell to being alive
let grid = an array of x by y cells
let tempGrid = an array the same size as grid
let iterations = the number of times to refine the cellular automata
        let value = random value from 0 to 1
        if value is less than chanceToLive, start this cell as being dead
        else start this cell as being alive
            count the number of live and dead neighbors the cell has
            if the cell is alive and has less than two live neighbors, mark it as dead on tempGrid
            if the cell is alive and has two or three living neighbors, mark it as alive on tempGrid
            if the cell is alive and has more than three living neighbors, mark it as dead on tempGrid
            if the cell is dead and has exactly three live neighbors, mark it as alive on tempGrid
    set grid = tempGrid

```

### Algorithm Explained

This relatively simple algorithm is actually the basis for Conway's *Game of Life*. First it decides the probability for a cell on the grid to be alive at the start. Then it creates both the actual grid, and a temporary grid used later in the process. It also decides how many times to go through the main for loop. More iterations usually results in smoother walls, although there's a point after which the effects are minimal.

In the main for loop, the algorithm goes through each cell and looks at its neighbors. Cells in the corners have three, cells along the sides have five, and cells inside the grid have eight. Four rules are applied to check if the cell should live or die:

* Less than two neighbors, and it dies of loneliness.
* Two or three neighbors, it is healthy enough to survive the step.
* More than three neighbors, it dies of overcrowding.
* Exactly three neighbors, a dead cell comes to life.

## Case Studies

The classical case study for cellular automata is the *Game of Life*. Another example presented is *Brogue*, a Roguelike game that uses cellular automata to generate levels.

### Game of Life

The mechanics of this has already been discussed. Despite the relative simplicity of the rules, the *Game of Life* has many interesting characteristics. For instance, there are different types of distinguishable cell patterns that emerge. Some patterns, with names like "square," "boat," and "ship" appear and then remain until touched by some other pattern. Other patterns, with names like "glider" and "starship," move across the grid at a steady pace, cycling through a specific set of configurations along the way.

### Brogue

Brogue is a free-to-play game that represents its terrain, entities, and items as ASCII characters. The player traverses through caves in search of the Amulet of Yendor, which is the only way out of the cave system.

## Code Examples

Code examples may be found in the cellular folder of the code repository.

### Basic Game of Life Implementation

This is a simple implementation of *Game of Life*. It uses a random number generator to decide which cells start as alive or dead.

## Citations

* Cook, Michael. "Generate Random Cave Levels Using Cellular Automata." *Envato Tuts+*. 23 July, 2016. Web. 22 Nov., 2016. https://gamedevelopment.tutsplus.com/tutorials/generate-random-cave-levels-using-cellular-automata--gamedev-9664
* R. van der Linden, R. Lopes and R. Bidarra, "Procedural Generation of Dungeons," in *IEEE Transactions on Computational Intelligence and AI in Games*, vol. 6, no. 1, pp. 78-89, March 2014.
* Lipa, Chris. "Conway's Game of Life." *Cornell Math Explorers' Club*. Cornell University. n.d. Web. 22 Nov., 2016. http://www.math.cornell.edu/~lipa/mec/lesson6.html
* Walker, Brian. "Brogue." *Brogue*. n.d. Web. 22 Nov., 2016. https://sites.google.com/site/broguegame/