## Uncharted Waters

Jul 17 2017   9:00AM GMT

# Conway’s Game of Life As a Kata

Profile: Matt Heusser

Tags:
Programming
Test Driven Development

In a previous article I wrote about Katas and how understanding them can help make programmers stronger. Most Katas are relatively simple: they are usually used for small tasks, like to score a card for a bowling game or to convert roman numerals into numbers. Few of them have a real display, a real model, or significant complexity.

But have you heard of Conway’s game of life?

Conway’s game of life is a program that simulates a universe under specific rules. In the past I have not understood Conway’s Game;it is typically described as a”multidimensional cellular automata“, and I never quite understood the definition of those words. In this article I will use my experience to describe Conway’s game of life in more common words, and also explain how we can use it to become better programmers and technical staff.

## The Game Defined

To have this game make sense, imagine a world in two dimensions – up/down and left/right. This is summed up in Conway’s grid. Each point on the grid is an animal, and each point is either alive or dead.

Every new turn, or generation, represents a hard winter. If a point on a grid is alive, but it is surrounded by no other alive points,, or only  one other alive point, it dies of starvation. If there are two or three, it does fine, but if there are four or more points surrounding it, the animal dies of overpopulation. If a point was dead, but is later surrounded by three other points, it generates new life.

Watching the game run can be very fascinating. If there are not enough points, or animals, and all of the are too far apart, all life soon is gone from the grid. Too many points/animals living too close together can also be fatal. In the middle you can eventually have it transition to a stable system (four points in a square, surrounded by emptiness, are a stable system)or transition in an infinite loop, going back to start and then going through all the permutations again. The glider pattern, at left, rotates itself off-screen, and when it hits the edge it would ‘die’.

Another fascinating pattern is the “glider gun”, in which two points collide with each other. When they collide, they periodically “throw off” a glider, or make new life, which then moves down and to the right until it hits the edge. Here’s an example of the glider gun in action:

This is actually a very advanced programming problem. First, you have to represent the state of the universe – the model. You will then need some way to read that model from disk, write it to disk, and display it on the screen. Finally, you need some method to calculate the next-generation. That method needs to make a blank copy of the old generation, do a nested for-loop from the top-left to bottom-right, and calculate the number of live elements “around” the current one. Then it needs to loop through all the rules, check each rule, and enforce any that are valid. Finally, you’ll want to display the next generation.

Each of those pieces – the original model, the disk-retrieve, the logic to calculate the next generation, and the display, are roughly as complex as a typical kata. I didn’t have a timer running, but I expect that I typically spend an hour or two each on normal Katas, and probably spent ten on my Game of Life implementation in Ruby.

Youtube tells me that a quick game of life implementation can be done in an hour.

I noticed watching the video that the author is not writing any unit tests. Also notice the note at the end of the video – I can’t make this up “I do realize this is kind of buggy, but I did this in one hour.”

It’s an impressive feat. I suppose that if you want to work on speed coding a little throwaway program you write for fun is the place to do it.

## Using Conway’s Game Of Life As a Kata

Personally, I like to use katas for deliberate practice – a place where I can practice doing things “right” without compromise.

To run my version of Conway’s game of life you can download the directory and type this (or something similar)  in the root directory:

ruby ./game_of_life.rb 30 ./sample_starting_files/12×12.txt 0.25

That will run through 30 generations (if you want to change this, change the 30 in the command line) using a sample file I wrote (that is 12×12.txt) with each generation spaced 0.25 seconds from the next.

If you want to dig into the source code look for the few lines of code the unit tests don’t cover and a couple of compromises I made in the implementation.

My katas are a public demonstration of my coding abilities.

What are yours?