Search code examples
pythonconways-game-of-life

Conway's game of life will not run Python


This is my code, it seems as though the temp matrix is not updating correctly, but I cannot figure out why. I am very new to python and really am having trouble solve this issue.

cell = [[0 for i in range(10)] for j in range(10)]
temp=[[0 for i in range(10)] for j in range(10)]
def load(): 
    cell[1][3]=1 
    cell[2][3]=1 
    cell[3][3]=1 



def cycle():
    for x in range(10):
        for y in range(10):
            checkSurroundingAndChange(x,y)


def checkSurroundingAndChange(i,j):
    surrounding=0
    if i-1>=0 and j+1<10 and cell[i-1][j+1] == 1: surrounding= surrounding+1
    if j+1<10 and cell[i][j+1] == 1: surrounding= surrounding+1
    if j+1<10 and i+1<10 and cell[i+1][j+1] == 1: surrounding= surrounding+1
    if i-1>=0 and cell[i-1][j] == 1: surrounding= surrounding+1
    if i+1<10 and cell[i+1][j] == 1: surrounding= surrounding+1
    if j-1>=0 and i-1>=0 and cell[i-1][j-1] == 1: surrounding= surrounding+1
    if j-1>=0 and cell[i][j-1] == 1: surrounding= surrounding+1
    if j-1>=0 and i+1<10 and cell[i+1][j-1] == 1: surrounding= surrounding+1
    if   cell[i][j]==0 and surrounding==3:temp[i][j]==1
    elif cell[i][j]==1 and surrounding<2:temp[i][j]==0 
    elif cell[i][j]==1 and (surrounding==2 or surrounding==3):temp[i][j]==1
    elif cell[i][j]==1 and surrounding>3:temp[i][j]==0



for i in range(10):
    print (cell[i])

print ("////////////////////////////////////////////////////////////////")    
load()
cycle()
for i in range(10):
    print (temp[i])

Solution

  • You have too many = here

    temp[i][j]==1
    

    Meaning that Python is just doing a comparison and throwing away the result.

    It should be an assignment. ie.

    temp[i][j] = 1
    

    same problem for the cases setting to 0

    Cramming up the code makes it much harder to spot bugs like this. You should try to find a more elegant way to implement checkSurroundingAndChange