I'm trying to create my Game of life code in python, but when it checks for the living neighbours, it seems to have a problem. I've been tried to solve it for 2 days but I still don't know what I'm missing. Here is the code broken up into pieces.
from random import randrange
from tkinter import *
from time import sleep
root = Tk()
canv = Canvas(width = 800, height = 800)
canv.pack()
def value():
if randrange(16) == 0:
return 1
else:
return 0
I set up the canvas in tkinter and create a function which determines the state of a cell, so every cell has 1/x chance at being alive first.
def pprint(lst):
for row in lst:
print(row)
I created a pretty-print function when I tried to debug.
def nb(life,row,col):
count = 0
#print(x,y)
#print(life[x][y])
for r in [row-1, row, row+1]:
for c in [col-1,col,col+1]:
if life[r][c] == 1:
count += 1
if life[row][col] == 1:
count -= 1
#print(count,end=" ")
return count
This function it supposed to return the number of living neighbours a cell has. (notes remained from debugging all over the code)
def ud(life,life2,size,box):
#print(life)
while True:
try:
for row in range(1,size+1):
for col in range(1,size+1):
#print(row,col)
a = nb(life,row,col)
print("[{0},{1}]".format(row,col), end =" ")
print(a, end=" ")
if ((1<a) and (a<4)):
life2[row][col] = 1
canv.itemconfig(box[row-1][col-1], fill ="black")
else:
life2[row][col] = 0
canv.itemconfig(box[row-1][col-1], fill ="white")
print("")
print("ok")
#print(life2[19][19])
root.update()
pprint(life)
#sleep(5)
life = life2
sleep(800/size**2)
print("")
except:
break
This is the main updating function. It's supposed to run the nb function for every cell in the life 2d array and the determine it's value in life2. "box" is a 2d array, which contains the rectangle tkinter id for every cell on the visual grid. (Yes, print functions are also for debugging.)
#######
size = 10
w = 10
box = [[canv.create_rectangle(y*w,x*w,y*w+10,x*w+10) for y in range(size)] for x in range(size)]
life = [[value() for y in range(size)] for x in range(size)]
life2 = [[0 for y in range(size+2)] for x in range(size+2)]
for i in range(1,size+1):
life2[i] = [0] + life[i-1] + [0]
#life = life2
pprint(life)
root.update()
ud(life2,life2,size,box)
del box
del life
del life2
I create the box variable (w = width of a cell) and generate the random living cells. Then I create a "0" frame for "life" (so the program doesn't break on the nb checking when it's at the corners. And I know I could've use try/except). Then I launch the ud function with the parameters (size = number of cells in a row; both life-matrix arguments are life2, because the 2nd will be completely rewritten in theory)
So what is the problem with the program?