# game of life...

This topic is 4428 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hey, Do you know Conway's Game Of Life ? Ok, how would you create the data structure to support this? The simplest and most straightforward way would be to just have a huge bidimensional array and pick random values for that array at first (for initialization)... but im sure its not the best way memory and performance wise... any ideas? Thanks

##### Share on other sites
I found that using a C styled approach, I got the best performance out of my Life Game I did in SDL. For that, I just used an int * for the board (accessed with the row * NumCols + col forumla) and then used memcpy's to apply the rules. Here's the snippet for that:

void ApplyRules(const int &delay){	// Copy the current board into the temporary board	memcpy(mTempBoard, mBoard, size);	for(int r = 0; r < mRows; r++ )	{		for(int c = 0; c < mCols; c++ )		{			int neig = CountNeighbors(r, c);			// Underpopulation			if(neig < 2 )				mTempBoard[r * mCols + c] = mDeadValue;			// Birth			else if(neig == 3)				mTempBoard[r * mCols + c] = mAliveValue;			// Overpopulation			else if(neig > 3 )				mTempBoard[r * mCols + c] = mDeadValue;		}	}	// Copy the updated temporary board into the real board	memcpy(mBoard, mTempBoard, size);	generation++;}

By using that, I moved the previous bottleneck when I was using a std::vector from the cosntant clears/copys to the graphics package I am using -- SDL, although it seems very fast when working with it. In terms of space, well my method is not that "effective" (Rows * Cols * sizeof(int)) but the advantage I end up getting is simple implementation and quite fast. I'd have to reconsider if I wanted to simulate a "huge" world though.

Have a look for yourself though! Life-Demo (just demo). My code has quite a few extras added in, such as file loading/saving and the like, but for this demo I just threw together (did this project for fun 10/16/05), I just took it out so it's just a core system. Hope this helps a bit, is this a for fun project or does it have a specific purpose (i.e. you are moving into AI programming)?

##### Share on other sites
You munged the link dude. Here is is: http://en.wikipedia.org/wiki/Conways_Game_of_Life

The only real way to store it is a multidimensional array, afaict.

##### Share on other sites
k, thanks for the replies...
Its mainly for fun.

I dont think multidimensional arrays are the only way to go however.
I was thinking of a tree where each node can have a max of 8 childs (one for each adjacent cell). This would remove all the for cycles and the checking of the rules would be pretty simple: if(cell.children.count<2) cell.kill() , etc.

what do u think?

##### Share on other sites
Did anybody ever come up with a 3D version of Life? Always thought that would be cool.

##### Share on other sites
Isn't it possible to keep two exactly the same sized bords in memory, then having two pointers called currentBoard and temporaryBoard respectively. Then you would on update switch them and step over each square in the currentBoard, quering for the corresponding number of neighbours in the temporaryBoard? For any other functions after update it's just to check the currentBoard pointer for the most recent board. This way you avoid two memory copies (if that's an issue, shouldn't be anyhow with small arrays).

##### Share on other sites
That would be the problem (I think) with using a tree structure. When I put a simple little 2D Life together using arrays I remember finding that you had to work from the current board onto a copy, so that a cell that had just "died" this turn would still persist for the rest of the turn to influence cells around it.

Slightly off topic, I started trying to turn it into a two player game where you ran around firing those

***
*
* (or whatever, you know what I mean)

shape things that move diagonally across the board at each other while avoiding all the live cells but gave up. Shame really. Might have been good.

##### Share on other sites
Homework! Seriously, how hard could something like this be? Read your assignment specification more closely.

##### Share on other sites
Quote:
Original post by FreJa
Quote:
 Original post by EasilyConfusedDid anybody ever come up with a 3D version of Life? Always thought that would be cool.

There is also a 1D Game of Life. I am only vaguely familiar with it, but I know that Stephen Wolfram (creator of Mathematica) has written some book(s) on the topic.

• 10
• 16
• 14
• 18
• 15