game of life...

Started by
4 comments, last by igni ferroque 18 years ago
Hey, The most common approach to the game of life is to do make an array, where each cell is accessed the following way: array[row*numColumns+column] (check http://www.gamedev.net/community/forums/topic.asp?topic_id=385408. This immediately takes care of the stiching between the top and bottom of the board, so it doesn't seem that the board is limited. However, how should I stich the left and right? 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
What do you mean by stiching between top and bottom or left and right?
In Game of Life, neither the top and bottom or left and right should be connected to each other, like east and west are in our world. The game board is defined as 'endless', forever stretching out in each direction.
So do you want the top linked to the bottom and the left to the right (like a torus)?

If so, it seems like the easiest way to do it would be to use the modulus operator (% in C, C++, python, and probably some other languages).

If you have a 2d array like cells[row][column] (in C/C++), to find the cell to the lower right diagonally you could do cells[(row+1)%numRows][(column+1)%numColumns]. For the upper left, you could do cells[[(row+NumRows-1)%numRows][(column+numColumns-1)%numColumns].

This would provide the stitching; if you look at the cell at 0,0, this code will 'see' the cell to the upper-left as cell[numRows-1][numColumns-1], or the very lower right. As long as you do all of your accessing like that, you'd have the stitching done.
Quote:Original post by Sijmen
In Game of Life, neither the top and bottom or left and right should be connected to each other, like east and west are in our world. The game board is defined as 'endless', forever stretching out in each direction.


k, then what data structure should i use? Since the use of arrays implies setting its size, and you're saying it shouldnt be sized...

thanks again

"Through me the road to the city of desolation,Through me the road to sorrows diuturnal,Through me the road among the lost creation."
Optimized versions usually split the game into tiles (smaller grids). Using tiles has a lot of advantages; one being the ability to extend the grid with minimal cost. If you stitch the tiles together using a doubly linked list (C++'s list container would do nicely), you can quickly add a new tile or row of tiles as needed. You can also increase performance by keeping track of which tiles have live cells so you don't waste time computing empty regions.
Free Mac Mini (I know, I'm a tool)

This topic is closed to new replies.

Advertisement