10 Mar 2015

A while ago, I came across Conway’s Game of Life and I was instantly fascinated for much the same reason I believe most people are: it’s so simple and elegant that it gives you a sense that anything can happen. I finally had the opportunity to implement it using JavaScript and an HTML canvas and it was loads of fun.

Conway’s Game of Life is cellular automaton, in which you can define some initial state of an *infinite* field of cells on a two-dimensional plane. The field is a grid such that each cell (square in the grid) has exactly 8 neighbors. The object is to determine the state of each cell (alive of dead) periodically based on the state of its neighbors. Each iteration of this is called a generation. There a few simple rules.

- A live cell that has less than 2 neighbors or more than 3 neighbors dies (as if of underpopulation or overpopulation, respectively).
- A dead cell that has exactly 3 neighbors becomes alive in the next generation.

The code was quite straightforward: simply a matter of representing the grid in a two-dimensional array with values `true`

or `false`

representing the state of the cell. However, I quickly realized that in order to properly implement the game of life I had to simulate an infinite field into which the cells could grow. After some quick Googling I found that a simple work-around was connecting the top of the field with the bottom and the left to the right. This creates a torus (the surface of a donut).

I did not anticipate encountering geometry but at that point, I was hooked! Of course, this solution does not actually simulate infinity; it still limits the number of cells to the dimensions of the canvas. However, for a weekend project, my *only* alternative was that every cell beyond the canvas was always dead, and I found the torus much more interesting for a number of reason that will make this post too long to remain interesting.

The JavaScript was quite simple: execute the `getNextGeneration()`

function in a `window.setInterval()`

with some alloted time between each iteration (I’ve been toying around with the idea of making the page more interactive by using React.js; currently there are several fake buttons I have been too lazy to write code for or remove).

Beyond the code, there is something inherently enchanting about cellular automata. There is obviously a deterministic outcome for any given seed and field but the permutations explode so quickly that it is overwhelming. It *feels* stochastic. In my opinion this is a lot like real life. I believe one can logically reason about *anything* given enough data and the ability to compute and analyze it. Yes, including human emtions and the so-called irrational decision making often found in nature. In theory, everything can be traced down to an atomic (and sub-atomic) level and be rebuilt step by step by adhering to the regular laws of physics (afterall “the universe itself is computable on a universal Turing machine”). The chaos theoretical implications are so stimulating.

Perhaps, when you’re bored enough, everything feels much more significant, but I really do believe Conway’s Game of Life allows us to represent at the most elementary state of being and gives us just enough to let our imaginations run wild!