Tuesday, 3 February 2009

The Game of Life, the implementation

My own implementation of John Conways Game Of Life can be downloaded here.
Note that this implementation is far from ready, it shows the base concepts used but still need some polish to be really userfriendly and graphical attractive.

Screenshot :

The main concepts I used here are the following :

First of all I need a Model, in my case I created a class LifeBoard (a subclass of Board) . The abstract class Board just implements a basic gameboard with a variable number of cells. 
The LifeBoard class is the implementation of a gameboard for the Game Of Life, it implements the Conway algorithm. 
Note that we could use a strategy (design pattern) to delegate the algorithm to it. For the sake of simplicity this is not done.

The underlying datastructure used is a Matrix. (see previous blog messages for an explanation).

The next thing we need is a View,  in this case I created a custom view : LifeView (subclass of NSView).
This view just implements a simple visual representation of the LifeBoard. (a rectangle with a grid and the 'active' cells are yellow rectangles).

And the last concept we need is a Controller, here implemented as GameOfLifeController.
This class just glues together the model and the view.

So far my Game Of Life. The extra classes I've developed are used now in other board games that I'm developing.

Rules of the game :

Click on a gray rectangle to activate a cell, click on a yellow rectangle to deactivate it.

Press start to start the game

1 comment: