def main(grid):
for i in range(1): # number of generations
grid = modifier(grid)
afficher(grid)
def modifier(grid):
Ltemp = [] # temporary matrix where modified grid is stored
for i in range(len(grid)): # process by indexes, line
Ltemp.append([]) # new line in Ltemp
for j in range(len(grid[i])): # column
temp = 0 # counter of the near alive cells
if i-1 >= 0: # to prevent IndexError, slower than a "try-except" form but easier to correct, left cell
temp += grid[i-1][j] # ad 1 if alive, 0 if dead
print(grid[i-1][j],end='') # test
if i+1 < len(grid): # right cell
temp += grid[i+1][j]
print(grid[i+1][j],end='')
if j-1 >= 0: # up cell
temp += grid[i][j-1]
print(grid[i][j-1],end='')
if j+1 < len(grid[i]): # down cell
temp += grid[i][j+1]
print(grid[i][j+1],end='')
if i-1 >= 0 and j-1 >= 0: # up left cell
temp += grid[i-1][j-1]
print(grid[i-1][j-1],end='')
if i-1 >= 0 and j+1 < len(grid[i]): # down left cell
temp += grid[i-1][j+1]
print(grid[i-1][j+1],end='')
if i+1 < len(grid) and j-1 >= 0: # up right cell
temp += grid[i+1][j-1]
print(grid[i+1][j-1],end='')
if i+1 < len(grid) and j+1 < len(grid[i]): # down right cell
temp += grid[i+1][j+1]
print(grid[i+1][j+1],end='')
if grid[i][j] == 0 and temp == 3: # the 1rst rule, if dead and surrounded by 3 alive cells, become alive
Ltemp[i].append(1)
print(Ltemp[i][j],end='')
elif grid[i][j] == 1 and (temp != 2 or temp != 3): # the 2nd rule, if alive and surrounded by less than 2 or more than 3 alive cells, die
Ltemp[i].append(0)
elif grid[i][j] == 1: # still the 2nd, if 2 or 3 cells around, stay alive
Ltemp[i].append(1)
else: # the first, if not 3 cells, stay dead
Ltemp[i].append(0)
print(temp)
return Ltemp
def afficher(grid):
for elm in grid: # line
for car in elm: # column
if car == 0: # dead cell case
print('\u25FB',end='') # empty box
else: # alive cell case
print('\u25FC',end='') # black box
print()
它没有按应有的方式工作。在出口中,最后一个数字是“temp”(存活细胞的数量)。前面的是细胞的状态。每一行对应一个单元格。每当您看到更少的数字时,这条线就会发生变化。该图案应该是 Kok 星系的变体。
我认为问题出在第 52 至 60 行,但我找不到哪里。
对问题出在哪里是对的!逻辑在规则的应用上出现了一些错误。以下是对的代码的更正,并附有解释:
def main(grid):
for i in range(1): # number of generations
grid = modifier(grid)
afficher(grid)
def modifier(grid):
Ltemp = [] # temporary matrix where modified grid is stored
for i in range(len(grid)): # process by indexes, line
Ltemp.append([]) # new line in Ltemp
for j in range(len(grid[i])): # column
temp = 0 # counter of the near alive cells
if i-1 >= 0: # to prevent IndexError, slower than a "try-except" form but easier to correct, left cell
temp += grid[i-1][j] # ad 1 if alive, 0 if dead
if i+1 < len(grid): # right cell
temp += grid[i+1][j]
if j-1 >= 0: # up cell
temp += grid[i][j-1]
if j+1 < len(grid[i]): # down cell
temp += grid[i][j+1]
if i-1 >= 0 and j-1 >= 0: # up left cell
temp += grid[i-1][j-1]
if i-1 >= 0 and j+1 < len(grid[i]): # down left cell
temp += grid[i-1][j+1]
if i+1 < len(grid) and j-1 >= 0: # up right cell
temp += grid[i+1][j-1]
if i+1 < len(grid) and j+1 < len(grid[i]): # down right cell
temp += grid[i+1][j+1]
# 应用生命游戏规则
if grid[i][j] == 0 and temp == 3: # 如果细胞死亡并且周围有 3 个活细胞,则它将变为活细胞
Ltemp[i].append(1)
elif grid[i][j] == 1 and (temp == 2 or temp == 3): # 如果细胞存活并且周围有 2 个或 3 个活细胞,则它将保持存活状态
Ltemp[i].append(1)
else: # 在所有其他情况下,细胞死亡或保持死亡状态
Ltemp[i].append(0)
return Ltemp
def afficher(grid):
for elm in grid: # line
for car in elm: # column
if car == 0: # dead cell case
print('\u25FB',end='') # empty box
else: # alive cell case
print('\u25FC',end='') # black box
print()
解释更正后的代码:
- 合并规则: 生命游戏的规则可以简化为两个主要条件:
- 出生: 如果一个死亡的细胞正好被三个活细胞包围,它就会变成一个活细胞。
-
存活: 如果一个活细胞被两个或三个活细胞包围,它就会存活到下一代。
-
简化条件: 的原始代码在第 52 到 60 行有几个条件,这有点多余且容易出错。更正后的代码通过结合逻辑条件并直接应用生命游戏规则来简化此过程。
通过这些更改,的生命游戏模拟应该可以正确地实现规则并演化 Kok 星系模式或任何其他提供的初始模式。
标签:python,conways-game-of-life From: 78794677