[英]Conway's Game of Life Creating Board and Turning Cells On
我只有一個(最有可能的)最后一個問題。 從我發送上一個問題開始,我已經取得了一些進展,到目前為止我已經編寫了 5/6 個函數(1、2、3、5 和 6),而函數 4 實際上只是將函數 5 和 6 放在一起,即是我需要幫助的。 到目前為止,這是我的代碼:
"""
Function 1: create a blank grid
input: nothing
return: a blank grid
Function 2: print a given grid
input: a grid
return: nothing
Function 3: load a pattern
input: a file name, a grid
return: nothing
Function 4: advance a grid one generation
input: a grid
return: a new grid advanced by one generation
Function 5: advance a cell one generation
input: a row, a column, a grid
return: whether the cell is alive or not (True or False)
Function 6: determine the number of living neighbors of a cell
input: a row, a column, a grid
return: the number of living neighbors of the cell
"""
living_cell = "O"
dead_cell = "-"
def create_blank_grid():
# # line = [dead_cell for i in range(59)]
# line = []
# for i in range(59):
# line.append(dead_cell)
#
# # grid = [["-"] * 30 for i in range(60)]
# line.append("\n")
# # grid = [line for i in range(30)]
line = []
for i in range(59):
line.append(dead_cell)
line.append("\n")
grid = []
for j in range(30):
grid.append(line[:])
return grid
# print(create_blank_grid())
def print_grid(grid):
for i in grid:
# print(len(i))
for j in i:
if j == "\n":
print(" " * (69 - 59))
else:
print(j, end="")
# print_grid(create_blank_grid())
def load_design(file_name, grid):
myFile = open(file_name, "r")
coordinates = []
for line in myFile:
real_line = line.split()
row, col = real_line[0], real_line[1]
coordinates.append((row, col))
myFile.close()
# turn on certain cells here
for row, col in coordinates:
row, col = int(row), int(col)
# print(row, col)
grid[row][col] = living_cell
# print_grid(grid)
for i in grid:
# print(len(i))
for j in i:
print(j, end="")
# load_design("hertz-oscillator.in", create_blank_grid())
def num_living_neighbors(row, col, grid):
living_neighbors_count = 0
if grid[row][col + 1] == living_cell:
living_neighbors_count += 1
if grid[row][col - 1] == living_cell:
living_neighbors_count += 1
if grid[row + 1][col] == living_cell:
living_neighbors_count += 1
if grid[row - 1][col] == living_cell:
living_neighbors_count += 1
if grid[row + 1][col + 1] == living_cell:
living_neighbors_count += 1
if grid[row + 1][col - 1] == living_cell:
living_neighbors_count += 1
if grid[row - 1][col + 1] == living_cell:
living_neighbors_count += 1
if grid[row - 1][col - 1] == living_cell:
living_neighbors_count += 1
return living_neighbors_count
def adv_cell_one_gen(row, col, grid):
row = int(row)
col = int(col)
# is alive, less than 2 alive neighbors
if grid[row][col] == living_cell and num_living_neighbors(row, col, grid) < 2:
return False
# is alive, 2 or 3 alive neighbors
if grid[row][col] == living_cell and (
num_living_neighbors(row, col, grid) == 2 or num_living_neighbors(row, col, grid) == 3):
return True
# is alive, more than 4 alive neighbors
if grid[row][col] == living_cell and num_living_neighbors(row, col, grid) > 4:
return False
# is dead, has 3 alive neighbors
if grid[row][col] == dead_cell and num_living_neighbors(row, col, grid) == 3:
return True
def adv_grid_one_gen(grid):
# for i in range(len(grid)):
# for j in range(i):
# adv_cell_one_gen(j, i, grid)
for i in grid:
for j in i:
if adv_cell_one_gen(j, i, grid) == True:
grid[i][j] = living_cell
else:
grid[i][j] = dead_cell
return grid
# print(adv_grid_one_gen(create_blank_grid()))
adv_grid_one_gen(create_blank_grid)
函數一:create_blank_grid
功能二:print_grid
函數 3:load_design
函數 4:adv_grid_one_gen
函數 5:adv_cell_one_gen
函數 6:num_living_neighbors
我知道在 adv_cell_one_gen 函數中,我將 row 和 col 轉換為整數(row = int(row) 和 col = int(col)),這會導致錯誤,但如果我不這樣做,那么我由於索引沒有整數,會得到索引錯誤。 我認為我的代碼在其他地方存在根本性缺陷,我不知道在哪里,但我認為我處於死胡同。 這是我的問題,我希望你能幫忙。
在"".join(line)
, line 應該是一個字符串列表。
但是,如果我們查看您的代碼,您會附加living_cell
或dead_cell
,它們是您在前兩行中聲明為布爾值的。 因此, line 現在是一個布爾值列表,這是錯誤的輸入類型。
我不確定你想在這里實現什么,但如果你只想打印輸出,你可以這樣做
line.append(str(living_cell))
line.append(str(dead_cell))
您收到該錯誤是因為您已將單元格定義為布爾值:
living_cell = True
dead_cell = False
''.join()
函數采用列表中的元素,並將它們作為單個字符串與您定義的任何分隔符連接在一起。 分隔符是您在調用join()
之前立即放入字符串的任何內容。 在這種情況下,它的''
。
join()
僅適用於充滿字符串的可迭代對象。 您提供了一個可迭代的(您的line
列表),但它充滿了布爾值而不是字符串。
要克服此錯誤,您至少有三個選擇:
這可能就像這樣簡單:
living_cell = "True"
dead_cell = "False"
但是,我懷疑如果您的其他函數依賴於這些布爾值變量才能正常運行,這會破壞您的其余代碼。
join
期間更改數據類型當您調用join()
函數時,您可以使用列表理解將所有內容更改為字符串:
print("".join([str(cell) for cell in line])
如果所有這些功能都試圖做的是打印出電路板(似乎是這種情況),這應該可以完成工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.