Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading