I want to make a toroidal array for a Game of Life clone. How do I make it toroidal?
I’ve been thinking and the best solution I could think of, was just saying that the first row and column are equal to the last row and column.
Any other ideas?
EDIT: Maybe some people don’t know what the Game of Life is? It’s a small game with cells, in an array. These cells can be dead or alive and react to each other. Whenever a cell reaches the edge of my current solution, it bumps into a row/column of all dead cells. What I want to do now is, when a cell reaches the edge, it just pops up on the other side. (see the image below)

What my code looks like now:
#region check adjacent squares
if (grid[x - 1,y - 1] == true)
adjacentsquares++;
if (grid[x, y - 1] == true)
adjacentsquares++;
if (grid[x + 1, y -1] == true)
adjacentsquares++;
if (grid[x - 1, y] == true)
adjacentsquares++;
if (grid[x + 1, y] == true)
adjacentsquares++;
if (grid[x - 1, y + 1] == true)
adjacentsquares++;
if (grid[x, y + 1] == true)
adjacentSquares +=1;
if (grid[x + 1, y + 1] == true)
adjacentSquares +=1;
#endregion
What I now need is that, whenever I want to check an adjacent cell (that would be out of bounds in a regular array) that it checks the one on the other side.
If I check position [10,0]. I need to check adjacent cells [9, max], [10, max] and [11, max].
I’m not sure I get your idea right, but why don’t you create a method that encapsulates the access to your array and that accesses
, where x and y are both ints passed to this method.
Explaining it a little further:
Say you have an array like
array[sizeX, sizeY]. If you want to access the first position of your array, sayarray[0, 0], you could do it likearray[0, 0]orarray[sizeX, sizeY], since it wraps around both its X and Y dimensions (a toroidal array). Therefore, you could use the modulus operator, and always access your array like this:Or better yet, create a method that does this automatically for you.