康威的人生游戏不会运行Python
Conway's game of life will not run Python
这是我的代码,似乎临时矩阵没有正确更新,但我不知道为什么。我是 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 的新手,在解决这个问题时确实遇到了麻烦。
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