Showing posts with label board games. Show all posts
Showing posts with label board games. Show all posts

Friday, 13 February 2009

Tetris : the implementation of a classical game in Objective-C/Cocoa

First of all the source code of the game can be found here. Background for the game can be found on wikipedia.

This implementation is a first attempt to implement it, in this version I was more intrested to get it to work than in the nice graphical aspects.
As you can see in the picture , it surely won't win a prize as the most sexy application :-).



This game again is an implementation of the classical MVC design pattern, with the difference that their are 2 views.

The model is a TetrisBoard (subclass of Board) on which you can move a piece (the so-called Tetrominoe (superclass of a set of pieces)).
The tetrominoe is the abstract superclass of all the tetrominoe's. It's very handy to model the pieces as a class, later on I can add things like a skin, or a another way of rotating. 

There are 2 views, the TetrisView and the TetrominoeView. The first one is the graphical representation of the  TetrisBoard; the second shows the next tetrominoe.

The model and the view(s) are connected via the TetrisController.

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