2017-06-08 25 views
0

作爲對自己的挑戰,我試圖編寫康威的人生模擬器遊戲。儘管從輸入值到打印出第一代和第二代,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() 

注意事項:

  1. 我用一個文件作爲輸入值[網站下載都可以在這裏找到:www.csc.kth .se /〜lk/P/glidare.txt]
  2. 我剛挑選了一個任意數字作爲我的邊界輸入
  3. Everthing正常工作,除了更新第二代和第三代之間的新單元 - 因此我暫停CT一定有什麼毛病我怎麼寫我的方法set_board
  4. 初始位置就是著名的滑翔機

如果我跑這三代,這是結果:

-------------------- 
---X---------------- 
-X-X---------------- 
--XX---------------- 
-------------------- 
-------------------- 
-------------------- 
-------------------- 
-------------------- 
-------------------- 

-------------------- 
--X----------------- 
---XX--------------- 
--XX---------------- 
-------------------- 
-------------------- 
-------------------- 
-------------------- 
-------------------- 
-------------------- 

-------------------- 
--X----------------- 
---XX--------------- 
--XX---------------- 
-------------------- 
-------------------- 
-------------------- 
-------------------- 
-------------------- 
-------------------- 

作爲一個可以注意到第三個沒有正確更新。有沒有解決這個問題?我應該正確更新我的電路板嗎?

任何關於如何修復或使我的代碼更好的幫助非常感謝。我相當新手編碼器,所以請溫柔;我知道這不是最好的也不是最優的代碼。

回答

0

什麼看起來像正在發生的是,您的主板沒有得到更新的新狀態。

我不知道python,所以我不打算逐行調試這段代碼(或者自己編寫python代碼),但一般來說,一個生命遊戲模擬器應該看起來像這樣(我留下了一些東西抽象出來,像board的底層表示,因爲這些都是實現細節會改變,並且特定於您的代碼):

#definition of board.get_neighbor_count(cell) 
neighbors = 0 
for(row in [cell.row - 1, cell.row + 1]) 
    for(column in [cell.column - 1, cell.column + 1]) 
     #We skip the center cell. 
     if(row == 0 && column == 0) continue 
     #We don't want to scan cells like [-1,0] or [20,0], which would be out of 
     #bounds. I don't know how python handles OOB accesses, but I imagine the program 
     #would crash instead of behaving gracefully 
     if(!board.in_bounds(row, column)) continue 
     if(board.get_cell(row, column).alive) neighbors++ 
return neighbors 

#============================================================== 

#definition of board.get_next_generation() 
Game_Board new_board(rows = board.rows, columns = board.columns, ruleset = board.ruleset) 
for(row in [0, board.rows-1]) 
    for(column in [0, board.columns-1]) 
     cell = board.get_cell(row, column) 

     neighbors = board.get_neighbor_count(cell) 

     if(cell.alive && board.ruleset.survives(neighbors)) 
      new_board.put(cell) 
     else if(cell.dead && board.ruleset.births(neighbors)) 
      new_board.put(cell) 

return new_board 

#============================================================== 

#definition of main() 
#Should create an empty board size 20x20. I'm assuming they'll be addressed 
#[0...rows-1],[0...columns-1] 
Game_Board board{rows = 20, columns = 20, ruleset = 23S/3B} 
#I'm putting values here for a Gosper Glider, but if your implementation is correct 
#one could put literally anything here 
board.put_starting_values({10,10}, {10,11}, {10,12}, {11,12}, {12,11}) 
board.print_state() 
num_of_generations = 50 
while(num_of_generations > 0) 
    #Note that I'm assigning a new value to `board` here! This is deliberate and 
    #intended to convey explicit semantics, in that `board` should now refer to the 
    #newly generated state, not the preexisting state. If, for whatever reason, 
    #you need to maintain the original state, either store each successive generation 
    #in an array or just the original state in a separate variable. 
    board = board.get_next_generation() 
    board.print_state() 
    num_of_generations-- 

print("Done!") 

你需要做的是降低你的main()下降到只是這個邏輯流程在這裏,並找出錯誤的地方,因爲這是一個(非常簡單)GOL模擬器的完整邏輯。按照我寫的方式寫它會使錯誤非常明顯。就目前而言,您的代碼中包含很多的代碼,其中像from_dead_to_alive這樣的函數正試圖複製neighbors所做的工作(一個函數本身過於複雜)。與此同時,您有一個名爲status的完整獨立實體,負責跟蹤這些單元格的評估結果,您不應因此而依賴於並且因爲依賴它而混淆您的代碼。