(已关闭)Conway 的生命游戏未正确更新 (Python)

(Closed) Conway's Game of Life not updating correctly (Python)

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

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

Gif in pygame(相同代码): 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

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

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 检查。