作爲對自己的挑戰,我試圖編寫康威的人生模擬器遊戲。儘管從輸入值到打印出第一代和第二代,everthing都能正常工作,但在處理下一代時遇到了麻煩。康威的生活遊戲:從第二代升級到第三代
這是到目前爲止我的代碼:
class Game_setup:
def __init__(self, cells):
self.boundry = 20
self.cells = cells
self.x_cord = []
self.y_cord = []
self.cord_pairs = []
self.coordinates = []
self.dead_pairs = []
def inital_values(self):
coordinates = []
for x in range(int(self.boundry)):
for y in range(int(self.boundry)):
coordinates.append([x, y])
self.coordinates = coordinates
return coordinates
def intial_cells(self):
cord_pairs = []
with open(self.cells, 'r', encoding="utf-8") as file:
for line in file:
row = line.split()
cord_pairs.append([int(row[0]),int(row[1])])
x = []
y = []
for number_of_coordinates in range(len(cord_pairs)):
x.append(cord_pairs[number_of_coordinates][0])
y.append(cord_pairs[number_of_coordinates][1])
self.x_cord = x
self.y_cord = y
self.cord_pairs = cord_pairs
return cord_pairs
def neighbours(self, n):
neighbours = 0
x_coordinate = self.cord_pairs[n][0]
y_coordinate = self.cord_pairs[n][1]
if [x_coordinate,y_coordinate+1] in self.cord_pairs:
neighbours += 1
if [x_coordinate,y_coordinate-1] in self.cord_pairs:
neighbours += 1
if [x_coordinate+1,y_coordinate] in self.cord_pairs:
neighbours += 1
if [x_coordinate+1,y_coordinate-1] in self.cord_pairs:
neighbours += 1
if [x_coordinate+1,y_coordinate+1] in self.cord_pairs:
neighbours += 1
if [x_coordinate-1,y_coordinate] in self.cord_pairs:
neighbours += 1
if [x_coordinate-1,y_coordinate-1] in self.cord_pairs:
neighbours += 1
if [x_coordinate-1,y_coordinate+1] in self.cord_pairs:
neighbours += 1
return neighbours
def from_dead_to_alive(self,pair):
x_coordinate = pair[0]
y_coordinate = pair[1]
neighbours = 0
if [x_coordinate,y_coordinate+1] in self.cord_pairs and [x_coordinate,y_coordinate] not in self.cord_pairs:
neighbours += 1
if [x_coordinate,y_coordinate-1] in self.cord_pairs and [x_coordinate,y_coordinate] not in self.cord_pairs:
neighbours += 1
if [x_coordinate+1,y_coordinate] in self.cord_pairs and [x_coordinate,y_coordinate] not in self.cord_pairs:
neighbours += 1
if [x_coordinate+1,y_coordinate-1] in self.cord_pairs and [x_coordinate,y_coordinate] not in self.cord_pairs:
neighbours += 1
if [x_coordinate+1,y_coordinate+1] in self.cord_pairs and [x_coordinate,y_coordinate] not in self.cord_pairs:
neighbours += 1
if [x_coordinate-1,y_coordinate] in self.cord_pairs and [x_coordinate,y_coordinate] not in self.cord_pairs:
neighbours += 1
if [x_coordinate-1,y_coordinate-1] in self.cord_pairs and [x_coordinate,y_coordinate] not in self.cord_pairs:
neighbours += 1
if [x_coordinate-1,y_coordinate+1] in self.cord_pairs and [x_coordinate,y_coordinate] not in self.cord_pairs:
neighbours += 1
if neighbours == 3:
self.dead_pairs.append([x_coordinate,y_coordinate])
return neighbours
def evaluate_initial_position(self,y_coordinate): # n är y koordinaterna som itereras över
coordinates_to_print = []
if y_coordinate in self.y_cord:
x_in_y = [x_coordinate for x_coordinate, y_values in enumerate(self.y_cord) if y_values == y_coordinate]
for items in range(len(x_in_y)):
coordinates_to_print.append(self.x_cord[x_in_y[items]])
for number_of_rows in range(self.boundry):
board_rows = ''.join('X' if item in coordinates_to_print else '-' for item in list(range(self.boundry)))
return print(board_rows)
def nxt_gen_cell_status(self):
status = {}
for lenght_initial_values in range(len(life.intial_cells())):
if life.neighbours(lenght_initial_values) == 3 or life.neighbours(lenght_initial_values) == 2:
status[tuple(self.cord_pairs[lenght_initial_values])] = "Alive"
elif life.neighbours(lenght_initial_values) < 2 or life.neighbours(lenght_initial_values) > 3:
status[tuple(self.cord_pairs[lenght_initial_values])] = "Dead"
for lenght_dead_cells in range(len(self.dead_pairs)):
status[tuple(self.dead_pairs[lenght_dead_cells])] = "Alive"
return status
def new_cells(self,status):
del self.cord_pairs[:]
for alive_cell in range(len(list(status.keys()))):
kord = list(status.keys())[alive_cell]
if status[kord] == "Alive":
self.cord_pairs.append(list(kord))
return self.cord_pairs
def set_board(self):
x = []
y = []
for new_coordinate in range(len(self.cord_pairs)):
x.append(self.cord_pairs[new_coordinate][0])
y.append(self.cord_pairs[new_coordinate][1])
self.x_cord = x
self.y_cord = y
return self.cord_pairs, self.y_cord
cells = 'www.csc.kth.se/~lk/P/glidare.txt'
life = Game_setup(cells)
def main():
cell_status_ditction = {}
life.intial_cells()
generation = input("How many generations would you like to see?" + "\n")
i = 0
while i < int(generation):
for boundry in range(10):
life.evaluate_initial_position(boundry)
for next_cells in range(len(life.inital_values())):
life.from_dead_to_alive(life.inital_values()[next_cells])
cell_status = life.nxt_gen_cell_status()
cell_status_ditction.update(cell_status)
life.new_cells(cell_status_ditction)
life.set_board()
cell_status_ditction.clear()
print("\n" + "\n")
i += 1
main()
注意事項:
- 我用一個文件作爲輸入值[網站下載都可以在這裏找到:www.csc.kth .se /〜lk/P/glidare.txt]
- 我剛挑選了一個任意數字作爲我的邊界輸入
- Everthing正常工作,除了更新第二代和第三代之間的新單元 - 因此我暫停CT一定有什麼毛病我怎麼寫我的方法
set_board
- 初始位置就是著名的滑翔機
如果我跑這三代,這是結果:
--------------------
---X----------------
-X-X----------------
--XX----------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--X-----------------
---XX---------------
--XX----------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
--X-----------------
---XX---------------
--XX----------------
--------------------
--------------------
--------------------
--------------------
--------------------
--------------------
作爲一個可以注意到第三個沒有正確更新。有沒有解決這個問題?我應該正確更新我的電路板嗎?
任何關於如何修復或使我的代碼更好的幫助非常感謝。我相當新手編碼器,所以請溫柔;我知道這不是最好的也不是最優的代碼。