首页 > 解决方案 > 生命游戏返回所有死网格。该怎么办?

问题描述

width = 600
height = 600
cell_size = 10
cols = int(width / cell_size)
rows = int(height / cell_size)

def dead_state(w, h):
    # returns a grid filled with 0's

def random_state(w, h):
    # returns grid randomly filled with 0's and 1's.

def draw_cell(x, y, state):
    # draws a cell with pos x and y.

def count_neighbours(state, x, y):
    # returns the sum of all neighbours of a cell.

def next_state(grid):
    new_state = dead_state(cols, rows)

    for x in range(cols):
        for y in range(rows):
            total = count_neighbours(grid, x, y)
            if total == 3 and grid[x][y] == 0:
                new_state[x][y] = 1
            elif (total < 2 or total > 3):
                new_state[x][y] = 0
    return new_state


def main():
    game_board = random_state(cols, rows)
    while True:
        for x in range(rows):
            for y in range(cols):
                draw_cell(x, y, game_board)
        print(game_board)
        game_board = next_state(game_board)

main()

上面的代码一开始很好,但很快就只给出了死细胞。有人知道这里的问题是什么吗?我将不胜感激。多年来,我一直在尝试调试它。我还是一个初学者,所以我真的不知道如何调试代码。

标签: pythonpygameconways-game-of-life

解决方案


推荐阅读