python - 生命游戏返回所有死网格。该怎么办?
问题描述
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()
上面的代码一开始很好,但很快就只给出了死细胞。有人知道这里的问题是什么吗?我将不胜感激。多年来,我一直在尝试调试它。我还是一个初学者,所以我真的不知道如何调试代码。
解决方案
推荐阅读
- node.js - 未注册的 Sinon Spy 已被调用(Async/Await)
- xslt - 递归调用函数在 XSLT 中给出错误
- docker - 如何在没有端口问题的情况下多次运行 docker-compose?
- reactjs - 哪些 React Hooks 可以匿名定义?
- python - 如何在 django 中编写子查询
- javascript - 为什么 react 不触发组件更新?
- android - 我的 react-native 应用程序未启动(启动 2 秒并关闭)
- c# - 我应该如何在可以处理表单提交的 .net Core Razor 页面应用程序上实现可重用组件?
- python - Python - 如何修复 CWE-117:日志的不正确输出中和
- c# - 请给出派生自 ScrollableControl 并实现 IContainerControl 的类的基本示例