gpt4 book ai didi

python - (已关闭)Conway 的生命游戏无法正确更新(Python)

转载 作者:太空宇宙 更新时间:2023-11-04 00:35:16 26 4
gpt4 key购买 nike

这是康威用 Python 制作的生命游戏模拟的代码片段。出于检查它是否有效的目的,它非常简单 - 但它没有。无法弄清楚为什么,但据我所知,它与更新有关。

感谢任何关于它为什么更新它的方式的输入:

pygame 中的 Gif(相同代码): http://imgur.com/6US3Nje未正确更新: http://imgur.com/9gubzAF

import pprint,random
#here we make the initial board, 6x6
board = []
for y in range (6):
row = []
for x in range (6):
row.append(random.randint(0,1))
board.append(row)
#and display it
pprint.pprint(board)

#this function counts the neighbours for each cell
def neighbours(x,y):
counter = 0
neighbours = \
[(x-1,y+1),(x,y+1),(x+1,y+1),\
(x-1,y), (x+1,y),\
(x-1,y-1),(x,y-1),(x+1,y-1)]
for n in neighbours:
a, b = n
try:
counter += board[a][b]
except:
pass
#printed out the counter to check if it counted correctly (it does, as far as I could tell)
if x == 4 and y == 4:
print(counter)
return counter

#function to make a new board based off the old one - basically, the updater.
#here's where the problem might lie - but for the life of me I cannot tell where and why.
def new(board):
new_board = []
for y in range(6):
new_row = []
for x in range(6):
n = neighbours(x,y)
oldcell = board[x][y]
#everything is set up to be according to the rules
#(if dead(0) can only come alive with 3 alive cells
#(if alive(1) can continue to live with exactly 2 or 3 live neighbours
if oldcell == 0:
newcell = 0
if n == 3:
newcell = 1
elif oldcell == 1:
newcell = 1
if n > 3 or n < 2:
newcell = 0
new_row.append(newcell)
new_board.append(new_row)
return new_board

#displaying the board for 6 instances
for i in range (6):
nboard = new(board)
board = nboard
pprint.pprint(board)

提前致谢!

最佳答案

您在循环中混合了行和列,有效地用每个新板转换了整个矩阵。循环应如下所示:

for x in range(6):
new_row = []
for y in range(6):
...

此外,循环中的大部分 if/else 逻辑都可以简化为一行。这是否更易于阅读和理解由您决定。

def new(board):
new_board = []
for x in range(6):
new_row = []
for y in range(6):
n = neighbours(x,y)
oldcell = board[x][y]
newcell = int(n in (2, 3)) if oldcell else int(n == 3)
new_row.append(newcell)
new_board.append(new_row)
return new_board

如前所述in comments ,还有第二个(虽然不太明显)问题:虽然您的 try/except 跳过所有场外索引是一个好主意,但它没有捕捉到例如x-1 for x = 0,因为[-1]是Python中合法的索引(索引序列中的最后一个元素),使您的板“环绕”四个边缘中的两个。相反,您应该明确检查棋盘的边界。如果你愿意,你可以把它变成另一个“有趣”的单行:

def neighbours(x,y):
return sum(board[a][b] for a in (x-1,x,x+1) for b in (y-1,y,y+1)
if (a,b) != (x,y) and 0 <= a < len(board) and 0 <= b < len(board[a]))

当然,您也可以保留循环,只是将 try/except 替换为 if 检查。

关于python - (已关闭)Conway 的生命游戏无法正确更新(Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44332094/

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