game of life...

Started by
12 comments, last by Devilogic 18 years ago
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
"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."
Advertisement
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)?
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
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?
"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."
Did anybody ever come up with a 3D version of Life? Always thought that would be cool.
Quote:Original post by EasilyConfused
Did anybody ever come up with a 3D version of Life? Always thought that would be cool.


yeah there are some: http://www.google.com/search?hl=en&q=3d+game+of+life&btnG=Google+Search

"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."
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).
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.
Homework! Seriously, how hard could something like this be? Read your assignment specification more closely.
Quote:Original post by FreJa
Quote:Original post by EasilyConfused
Did anybody ever come up with a 3D version of Life? Always thought that would be cool.


yeah there are some: http://www.google.com/search?hl=en&q=3d+game+of+life&btnG=Google+Search


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.

This topic is closed to new replies.

Advertisement