I am currently adapting electronut's "A simple Python matplotlib implementation of Conway's Game of Life" to run without the use of the numpy module (not for any reason but for my own entertainment and practice). I believe I'm running into this issue as described in other posts regarding problems with implementing Conway's Game of Life. This is the result of my code so far. I believe that my cells are updating before they are supposed to; however, I don't understand how that could be given the fact that I am updating cells on a copied grid and swapping the copied grid in an entirely different function. Any help would be appreciated, at this point I'm more curious as to what is causing this issue than I am frustrated with it. Here's my code:
# based off of electronut's "A simple Python matplotlib implementation of Conway's Game of Life"
# https://gist.github.com/electronut/5836145
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import random as rnd
#init biz
cellCount = 100
ALIVE = 255
DEAD = 0
# states in which a cell can exist
states = [ALIVE, DEAD]
#generate initial grid
grid = [[rnd.choice(states) for i in range(cellCount)] for j in range(cellCount)]
#return a new grid for next generation
def nextGen():
global grid
newGrid = grid.copy()
for i in range(cellCount):
for j in range(cellCount):
#calculate neighbor total
total = (grid[(i-1)%cellCount][j] + grid[(i+1)%cellCount][j] +
grid[i][(j-1)%cellCount] + grid[i][(j+1)%cellCount] +
grid[(i-1)%cellCount][(j-1)%cellCount] + grid[(i+1)%cellCount][(j+1)%cellCount] +
grid[(i+1)%cellCount][(j-1)%cellCount] + grid[(i-1)%cellCount][(j+1)%cellCount])/255
if grid[i][j] == ALIVE:
if (total < 2) or (total > 3):
newGrid[i][j] = DEAD
else:
if total == 3:
newGrid[i][j] = ALIVE
return newGrid
#update global grid using next gen grid
def update(data):
global grid
new = nextGen()
mat.set_data(new)
grid = new
return [new]
#matplotlib magic
fig, ax = plt.subplots()
mat = ax.matshow(grid)
ani = animation.FuncAnimation(fig, update, interval=100,
save_count=50)
plt.show()