Showing posts with label matrix. Show all posts
Showing posts with label matrix. Show all posts

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

Tuesday, 27 January 2009

The Matrix: revisited

One important class is still missing from the matrix implementation: the enumerator.
An enumerator is in my opinion , a workaround for the fact that the concept of 'BLOCKS' are missing in Objective-C.
If it was possible to use blocks like , for example, in Smalltalk then enumerators would be useless.

In this case I made a subclass of the abstract class NSEnumerator and called it MatrixEnumerator.
The protocol definiton of NSEnumerator obliges us to implement 2 methods ( nextObject and allObjects ).

I've added one more method and that is setTraversalOrder: ; with this method you can specify how the traversal will take place (by row first then by column or by column first then by row).

A new version of the Matrix project (including the enumerator) can be found here
Some extra functionality is also added to the Matrix class, it's now possible to get a row or a column from a matrix. Note that the rows or columns are copies from the original matrix.

Tuesday, 20 January 2009

The Matrix

I do not understand why Apple (or NeXt) didn't include a Matrix class in their foundation classes.
In gaming applications it would be very handy to have a 2-dimensional (or 3-dimensional) data structure.

Of course when I talk about a Matrix I don't mean the mathematical variant, therefore you can find much more performant implementations; but for the more common things like cell's on a board, I made my own Matrix class.

This first version is a very simple one, the underlying idea is to have an array of arrays. So a NxM matrix is implemented as a an array with N elements where each element is again an array of M elements.

Of course everything is kept simple now, in a latter version I'll more functions (like row access, column acces, transpose, copy, better initialization etc) and I'll add more complex things like SparseMatrix, Vectors, MatrixEnumerator etc.

I'll show now some code snippets , the full version can be downloaded here.

1. Matrix initialization


storage = [NSMutableArray arrayWithCapacity: j ] ;
for ( x = 0 ; x<=j ; x++ )
{

[storage insertObject: [NSMutableArray arrayWithCapacity: i ] atIndex: x] ;

for ( y = 0 ; y <= i ; y++)
{
// initialize each element with something
[[storage objectAtIndex: x]
insertObject: @"" atIndex: y ] ;

}
}


In the above snippet you see the initialization of a Matrix of size IxJ , as underlying datastructure I'll use the NSMutableArray.
Note that this initialization is oke for small matrices, and is usefull to be used in applications like chess or checkers.

2. Accessors for Matrix

Unfortunealy we don't have operator overloading like we have in C++ , but anyway. To access the elements of a matrix we have 2 accessors (a specialized get and set so to say).

- (void) atX: (int ) i atY: (int ) j put: (id) obj
{
//...
// store an element in the storage room
[[storage objectAtIndex: j]

replaceObjectAtIndex: i withObject: obj] ;
}


- (id) atX: (int ) i atY: (int ) j
{
//...
// get an element from the storage room
return [[storage objectAtIndex: j] objectAtIndex: i] ;

}


Now so far a basic matrix structure. Next time I'll go for some more advanced functions and structures.

Thursday, 15 January 2009

Game of life

My own Game of Life (based on John Conway's game) is almost finished .

It just need some polishing and then done.

In a next message I'll explain how I started to implement a Matrix class. I developed it to have a generic datastructure to hold my cells.