I have a problem with my "Game of life" on python

Question:

I don’t know why but my "def" that checks 3 rules of "Game of live" doesn’t work correctly. I have 2 lists that contains 0 and some 1 to check the program. 3 points that should give this image but instead it gives this

def upd(mass,screen,WHITE,mass1):
    BLACK = (0,0,0)
    for i in range(len(mass)-1):
        for j in range(len(mass[i])-1):
            if mass[i][j] == 0:
                if near(mass,i,j) == True:
                    mass1[i][j]=1
                    print("case1")
            if mass[i][j] == 1:
                if (near(mass,i,j)==False):
                    mass1[i][j]=0
                    print("case 2")
                if (near(mass,i,j)==False):
                    mass1[i][j]=0
                    print("case 3")
    for i in range(len(mass1)-1):
        for j in range(len(mass1[i])-1):
            if mass1[i][j] == 1:
                p.draw.rect(screen, (WHITE), Rect((j*10,i*10), (10,10)))
            else:
                p.draw.rect(screen, (BLACK), Rect((j*10,i*10), (10,10)))
    mass=mass1
def near(mass,i,j):
    counter = 0
    if mass[i][j+1]==1:
        counter+=1
    if mass[i][j-1]==1:
        counter+=1
    if mass[i+1][j]==1:
        counter+=1
    if mass[i-1][j]==1:
        counter+=1
    if mass[i+1][j+1]==1:
        counter+=1
    if mass[i-1][j+1]==1:
        counter+=1
    if mass[i+1][j-1]==1:
        counter+=1
    if mass[i-1][j-1] == 1:
        counter+=1
    if counter<2 or counter == 0:
        return False
    if counter > 3:
        return False
    if counter == 3:
        return True

log that repeats every circle

I am not good in python so I think this code is quite scarry:)
I’ll be very grateful for any advice

Asked By: sndmndss

||

Answers:

mass = mass1 does not copy the contents of the grid, it just puts a reference to mass1 in mass (actually only in the local variable mass in scope of upd). You must deep copy the grid:

for i in range(len(mass1)):
    for j in range(len(mass1[i])):
        mass[i][j] == mass1[i][j]
Answered By: Rabbid76
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.