patternMinor
Playing the game of life on an infinite grid
Viewed 0 times
playinginfinitethegridgamelife
Problem
I would like to implement Conway's game of life on a grid that is infinite in all directions. It should be initialized with an initial pattern and then run, e.g, like this:
What data structure should I use?
Matrices usually start with (0,0). One solution is to keep track of the smallest and largest index of a living cell, and move the matrix accordingly (so in the above example, we will need a 3-by-3 matrix). However, this might require to copy the entire matrix whenever a new cell is born.
Another solution is to use a hashtable indexed by pairs of indices. However, in this case the calculation of the next step will be more expensive since cells will not be next to their neighbors.
Is there a better data structure for such infinite grid?
set(-500,200)
set(-501,201)
set(-502,202)
start()What data structure should I use?
Matrices usually start with (0,0). One solution is to keep track of the smallest and largest index of a living cell, and move the matrix accordingly (so in the above example, we will need a 3-by-3 matrix). However, this might require to copy the entire matrix whenever a new cell is born.
Another solution is to use a hashtable indexed by pairs of indices. However, in this case the calculation of the next step will be more expensive since cells will not be next to their neighbors.
Is there a better data structure for such infinite grid?
Solution
If you want to do better than iterating over each cell, or at least the range of live cells, use Hashlife.
http://www.drdobbs.com/jvm/an-algorithm-for-compressing-space-and-t/184406478?pgno=1
https://www.youtube.com/watch?v=l228ARRYkNE
http://www.drdobbs.com/jvm/an-algorithm-for-compressing-space-and-t/184406478?pgno=1
https://www.youtube.com/watch?v=l228ARRYkNE
Context
StackExchange Computer Science Q#89519, answer score: 2
Revisions (0)
No revisions yet.