-5

I'm trying to do a Game of Life simulator in python.
There is a 2d array, and needs to make conditional statements based upon the values of the neighbours to each element.

Ex: grid[y][x + 1], grid[y - 1][x], etc.

How exactly to refer to the other end of each grid axis in the case that +1 or -1 takes it out of range?
Is there any way of wrapping around to refer to the opposite end of the x and y axes of the array?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Is there some way of wrapping around to refer to the opposite end of the x and y axes of the array?

Modulus will cause a wrap around

(x + 1) % len(grid[row])

I don't think the logic of Game of life allows for wrap up around, though.

One strategy is to have a "buffer" of permanently dead cells around the perimeter of the grid

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Could you explain? – AgustusCaesar Apr 27 '17 at 02:02
  • Game of Life is (theoretically) on an infinite grid. It is not uncommon however to identify the left to right and top to bottom edges so it is topologically taking place on a torus. Alternatively you can think of that what you're simulating is an infinite rectangular grid with the same pattern in each cell... – Persixty Apr 28 '17 at 13:52