我试图用python创建我的生命游戏代码,但是当它检查活动的邻居时,它似乎遇到了问题。我试着解决这个问题已经有两天了,但我还是不知道我错过了什么。下面是分解成碎片的代码。
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
我在tkinter中设置了画布,并创建了一个决定单元格状态的函数,因此每个单元格都有1/x的机会首先存活。
def pprint(lst):
for row in lst:
print(row)
我在尝试调试时创建了一个漂亮的打印函数。
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
这个函数应该返回一个细胞所拥有的活邻居的数量。(在整个代码中调试时仍有注释)
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
这是主要的更新功能。它应该为生命2d数组中的每个单元运行nb函数,并在life2中确定它的值。"box“是一个2d数组,它包含可视网格上每个单元格的矩形tkinter id。(是的,打印函数也用于调试。)
#######
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
我创建box变量(w =单元格的宽度)并生成随机活单元。然后,我为“生命”创建了一个"0“框架(这样程序在拐角处时就不会中断nb检查。我知道我可以尝试/除了)。然后,我用参数(大小=一行的单元格数;这两个生命矩阵参数都是life2,因为第二个参数将在理论上完全重写)启动ud函数。
那么这个程序有什么问题呢?
发布于 2018-02-24 16:07:35
def ud(life,life2,size,box):
中的这一行def ud(life,life2,size,box):
使得life
和live2
都名称/指向相同的数据。您需要深入复制life2
并让life
指向绝对分离的数据。
深度复制,因为您的life2
包含其他列表,而且如果您只通过执行live = life2[:]
进行浅拷贝,那么live仍然会遇到相同的问题--live将有唯一的参考信息在其内部列出,但是内部列表的引用所指向的数据仍将被共享。您可以使用copy.deepcopy(...)
进行深度复制。
重命名
import copy
print()
a = [[1,3,5],[2,4,6]]
b = a # second name, same data
a[0] = [0,8,15] # replaces the ref on a[0] with a new one, b == a
a[1][2] = 99 # modifies a value inside the 2nd list ref, b == a
print (a,"=>",b) # b is just another name for a
输出
# nothing got preserved, both identical as b ist just another name for a
([[0, 8, 15], [2, 4, 99]], '=>', [[0, 8, 15], [2, 4, 99]])
浅拷贝
a = [[1,3,5],[2,4,6]]
b = a[:] # shallow copy
a[0] = [0,8,15] # replaces one ref with a new one (only in a)
a[1][2] = 99 # modifies the value inside 2nd list, same ref in a&b
print (a,"=>",b)
输出
# b got "unique" refs to the inner lists, if we replace one in a
# this does not reflect in b as its ref is unique. chaning values
# inside a inner list, will be reflected, as they are not uniqe
([[0, 8, 15], [2, 4, 99]], '=>', [[1, 3, 5], [2, 4, 99]])
深拷贝
a = [[1,3,5],[2,4,6]]
b = copy.deepcopy(a) # deep copy
a[0] = [0,8,15] # only a modified
a[1][2] = 99 # only in a modified
print (a,"=>",b)
输出
# completely independent - b shares no refs with former a, all got new'ed up
([[0, 8, 15], [2, 4, 99]], '=>', [[1, 3, 5], [2, 4, 6]])
如果您将上面的print
替换为
print(id(a), id(a[0]), id(a[1]), id(b), id(b[0]), id(b[1]))
您可以获得a
的唯一ID的输出以及它的争用以及b
及其内容的输出:
改名:
`a`: 139873593089632, 139873593146976, 139873593072384
`b`: 139873593089632, 139873593146976, 139873593072384
浅:
`a`: 139873593229040, 139873593089632, 139873593146040
`b`: 139873593227168, 139873593088552, 139873593146040
深:
`a`: 139873593253968, 139873593227168, 139873593072384
`b`: 139873593229040, 139873593089632, 139873593254112
https://stackoverflow.com/questions/48968886
复制相似问题