A few days ago I was getting bored and decided to to something fun. Yes, you can get bored at TUDelft... Well, actually you don't get bored, you get sick of all the work you have to do (apologies to anyone offended by this). So in a quest of something fun but still productive I found Conway's Game of Life.

Game Of Life

The Game of Life is not really a game because no one plays it. It is a cellular automaton, a zero player game in which the only human interaction is the initial setup. Then the game evolves by itself under the constraint of some rules, until it gets to a stable state.

The game's world is a 2D grid in which each node is a place that can hold one individual. Each individual can be dead or alive. Basically you can see the world as a black, white grid in which black represents living individuals and white dead ones (the figure on right).

Each grid represents a single generation. In order to get a new generation the current one must evolve. This process implies that every individual (dead or alive) node will be evaluated and, based on the result, will survive, die or resurrect. The evaluation function takes in consideration the number of alive neighbors a node has (a node can have up to 8 neighbors):

  1. If one individual has less than two neighbors alive, it will be dead in the next generation because of underpopulation.
  2. If one individual has more than three neighbors alive, it will be dead in the next generation because of overcrowding.
  3. If one individual is alive and has two or three neighbors alive, it will survive in the next generation.
  4. If one individual is dead and has three neighbors alive, it will resurrect (be alive) in the next generation.

That's all. Based on this transition, each individual gets from one state to another, from generation to generation. There are two situations in which no more changes will be made between generations:

  1. All individuals are dead.
  2. All individuals are in a stable state from which they cannot die. The square on right is a stable population that cannot change because all alive individuals have two or three neighbors and all dead individuals have at most two neighbors alive so they cannot resurrect.

The fun part was to implement this myself. I created a small MFC application that is able to load files with predefined configuration or to generate random populations up to 10.000 individuals. If you download the sample be aware it will crash if you load invalid files and do strange things - it is just a prototype.

Demo

Code and download

If you want just to 'play' then you can download the binary file (the archive also includes some great samples).

For those who love programming, the source code is also available. Technical details:

  • In order to prevent flickering the drawing is double buffered.
  • There is a small memory leak (4K every few seconds) but I am too tired to look now for it - will check it sometime later or if you find it, let me know.
  • The 'gol' files, that describe a predefined world, contain on the first line the width of the world, on the second the height and on the next lines the world where each node can be 0 or 1.
  • The solution is for Visual Studio 2010.

The archive containing source code

Have fun and please provide feedback!

More info and references