# Cellular automata in Processing.js

### How to use

- Press
**Start** to begin simulation. Press **Stop** to pause it.
**Clear World** sets all cells to zero state. **Randomize** randomly adds pixels to about 50% of the world.
- Drag your mouse over the canvas to add more pixels. Left click to add pixels, right click to erase.
- To change the simulation rules, enter rule in the input box and press
**enter**. Rule must in S/B/G notation.
- Requires HTML5 compatible browser.
- Source code is available for viewing: https://github.com/robinfhu/processingjs

### Overview

This program was inspired by

Mirek's Cellebration website, which talks in detail about

**cellular automata**.
These are mathematical simulations which involve iterating a collection of cells in a grid, based on a series of rules.

For instance, the well known automaton, **Conway's Game of Life**, has the following rules:

- Cells in the grid can either be ON or OFF.
- If a cell is OFF, and exactly 3 of its neighbors are ON, the cell turns ON in the next generation.
- If a cell is ON, and 2 or 3 of its neighbors are ON, it continues to survive. Otherwise, it dies in the next generation.

To see this in action, enter the rule

**23/3/2** in the rule box.

I created this program using Processing.js. This is a powerful JavaScript visualization engine that uses canvas in HTML 5. Warning though, this simulation can be very CPU intensive!

### Rules notation

Each rule has three parts: Survival rule, Birth rule and # of generations (S/B/G).

- The survival rule is a list of numbers 0-8 which specify how many neighboring cells must be alive in order for a given cell to survive.
Represented by "23" in The Game of Life.
- The birth rule is also a list of numbers 0-8, which specify how many neighboring cells must be alive in order for a dead cell to come alive.
Represented by "3" in The Game of Life.
- The # of generations is a special rule used to create complex automata. For The Game of Life, this number is "2", because a cell can only be alive or dead.

### 'Generations' Automata

Good examples include Brian's Brain ("/2/3") and Star Wars ("345/2/4"). More rules can be found here:

MCell Rules Lexicon
Lets examine how the Star Wars rule works: **345/2/4**.

- If a cell is OFF, and two neighbors are ON, the current cell is turned ON.
- If a cell is ON and 3,4 or 5 neighbors are ON, the current cell remains ON.
- If a cell is ON, but has 1,2,6,7 or 8 neighbors that are ON, the current cell becomes
**AGED**
- Aged cells are not dead, but they do not contribute to birthing new cells. They also do not contribute towards keeping cells in the ON state alive.
- The "4" in the notation represents the total number of states a cell can have: OFF, ON, Aged level 1 and Aged level 2.
- After a cell has reached Aged level 2, it dies and turns OFF.

### Technologies Learned from this Project

- Git source control. This is my first exposure to Git and GitHub.
- jQuery was used to make the application form respond to user events.
- Twitter Bootstrap was used for the UI.
- Processing.js was used for the visualization.
- Hours spent: 10.