我最近遇到了John Conway的Game of Life,并决定用Python编写代码。所以我做了,但它不能正常工作。
例如,我水平连续有3个活细胞。如果它正常工作,它将从一行中的3个活细胞振荡到一列中的3个活细胞,但它不会这样做。
我目前的代码是这样的:
# ("O" == dead cell, "X" == live cell)
# generation 1:
OOO
XXX
OOO
# generation 2:
OOO
OOO
OOO
它应该做的是:
# (O == dead cell, X == live cell)
# generation 1:
OOO
XXX
OOO
# generation 2:
OXO
OXO
OXO
我不知道代码gremlin在哪个区域生活,所以我只是在下面发布了整个代码。
import os
import time
# RULES OF LIFE
# 1. Any live cell with less than two live neighbours dies, as if caused by under-population.
# 2. Any live cell with two or three live neighbours lives on to the next generation.
# 3. Any live cell with more than three live neighbours dies, as if by overcrowding.
# 4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
def seed():
board[12][11] = "X"
board[12][12] = "X"
board[12][13] = "X"
def display():
for row in board:
print("".join(row))
def update():
for row in range(0, len(board)):
for column in range(0, len(board[row])):
neighbors = 0
for r in range(-1, 2):
for c in range(-1, 2):
if r != 0 and c != 0:
try:
if board[r][c] == "X":
neighbors += 1
except IndexError:
pass
if board[row][column] == " ": # dead cell
if neighbors == 3: # RULE 4.
temp_board[row][column] = "X"
elif board[row][column] == "X": # live cell
if neighbors < 2: # RULE 1.
temp_board[row][column] = " "
elif neighbors == 2 or neighbors == 3: # RULE 2.
pass
elif neighbors > 3: # RULE 3.
temp_board[row][column] = " "
HEIGHT = 25
WIDTH = 25
board = [[" " for column in range(0, WIDTH)] for row in range(0, HEIGHT)]
temp_board = list(board)
seed()
while True:
display()
update()
board = list(temp_board)
time.sleep(1)
os.system("cls")
我错过了什么?
答案 0 :(得分:0)
我发现了三个错误,可能会有更多错误。以下是我发现的摘要。我将在下面更详细地解释这些:
您开始设置代码来处理偏移:
for r in range(-1, 2):
for c in range(-1, 2):
但是,您永远不会将偏移添加到原始值。基本上,您只是检查网格的前几个空格。因此,您需要将if board[r][c] == "X"
更改为if board[row+ r][column + c] == "X"
。
你创建了代码来检查周围的单元格(上面显示的for循环),你知道你需要免除单元本身的测试。您使用此代码排除了单元格本身:
if r != 0 and c != 0
但是,只要偏移变量r
和c
中的任何一个为零,此条件就会失败。这发生在单元格的上方,下方,右侧和左侧。您需要将条件更改为:
if (r != 0) or (c != 0) # Parenthesis added for readability, not functionality
这样,如果任何一个偏移非零,你将检查相应的单元格。
temp_board = list(board)
之类的作业会对相同的数据进行新的引用。本质上,它与为同一个变量分配额外名称相同。因此,当您在update
函数中更新电路板时,您不是通过备用名更新电路板的临时副本,而是更新电路板本身。然后,当进行后续测试时,电路板的状态已经改变。
要在python中进行深度复制,您需要将import copy
添加到文件中,然后使用temp_board = copy.deepcopy(board)
等代码替换当前的复制尝试。