gpt4 book ai didi

python - 康威的生命游戏不会运行 Python

转载 作者:太空宇宙 更新时间:2023-11-03 17:04:15 28 4
gpt4 key购买 nike

这是我的代码,似乎临时矩阵没有正确更新,但我不明白为什么。我对 python 很陌生,确实很难解决这个问题。

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])

最佳答案

这里有太多=

temp[i][j]==1

这意味着 Python 只是进行比较并丢弃结果。

这应该是一个任务。 IE。

temp[i][j] = 1

案例设置为0时也出现同样的问题

塞满代码会让发现这样的错误变得更加困难。您应该尝试找到一种更优雅的方式来实现 checkSurroundingAndChange

关于python - 康威的生命游戏不会运行 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34734596/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com