这是Conway使用Python制作的Game of Life模拟代码片段。它是非常简单的,目的是检查它是否有效 - 而事实并非如此。无法弄清楚为什么,但据我所知,这是更新的内容。
感谢任何有关它为何更新其原因的输入:
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)
提前致谢!
答案 0 :(得分:3)
您正在循环中混合行和列,有效地将每个新板的整个矩阵转置。循环应该如下所示:
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
= x
的{{1}},因为0
是Python中的合法索引(索引序列中的最后一个元素),使您的主板“环绕”两个四个边缘。相反,您应该明确检查电路板的边界。如果你愿意,你可以把它变成另一个“有趣”的单行:
[-1]
当然,您也可以保留自己的循环,只需替换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
的{{1}}。