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()
Приведенный выше код начинается нормально, но затем быстро выдает только мертвые клетки. Кто-нибудь знает, в чем проблема? Буду признателен за помощь. Я пытался отладить это целую вечность. Я все еще новичок, поэтому на самом деле не знаю, как отлаживать код.
new_state = dead_state(cols, rows)
- Выглядит неправильно.new_state
фактически должен начинаться как копияgrid
. - person khelwood   schedule 03.06.2020