2017-07-26 84 views
-1

我一直在嘗試編碼我自己的生活遊戲,因爲我想學習Python,我認爲這可能是一個很好的做法,我剛剛完成,除了我需要一個變量傳遞給另一種方法,但我真的不知道我是怎麼知道它可能看起來愚蠢 但稍等一下,看看代碼是不那麼容易,因爲它的聲音,除非我卡在生活編碼遊戲的一個愚蠢的階段

import random,time 
f=3 
c=3 
contador = 0 
tablero = [] 
tablero_dibujado = [] 

class juego(object): 

    def tablero(self): #Create the Board 
     for i in range(f): 
      tablero.append([0]*c) 
      tablero_dibujado.append([0]*c) 

    def rellenar_tablero_random(self): #Fill the board with random 0 and 1 
     for i in range(f): 
      for j in range(c): 
       tablero[i][j] = random.randint(0,1) 
     print(tablero) 

    def rellenar_tablero_manual(self): #Just to fill the board manually if I want for some reason 
     tablero[0][1] = 1 
     for i in range(2): 
      tablero[1][i] = 1 
     print(tablero) 

    def distancia_vecino(self,cell_f,cell_c): #Measure Distance for possible neighbours 
     distancia_vecino = [(-1,-1),(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1)] 
     for i in range(8): 
      string = distancia_vecino[i] 
      comparar_string = str(string) 
      x,y = comparar_string.split(",") 
      x = str(x).replace(",","") 
      y = str(y).replace(",","") 
      x = x.replace("(","") 
      y = y.replace(")","") 
      y = y.replace(" ","") 
      x = int(x) + cell_f 
      y = int(y) + cell_c 

      if x>=f or x<0: 
       continue 
      else: 
       if y>=c or y<0: 
        continue 
       else: 
        game.detectar_vecino(cell_f,cell_c,x,y) 
     game.vida(cell_f,cell_c) 

    def detectar_vecino(self, cell_f, cell_c, x, y): #Check how many neighboards do I have 
     vecinos = 0 

     if tablero[cell_f][cell_c] == 1: 
      if tablero[cell_f][cell_c] == tablero[x][y]: 
       vecinos+=1 
     else: 
      if tablero[cell_f][cell_c] != tablero[x][y]: 
       vecinos+=1 
     contador = contador + vecinos 

    def iterar_tablero(self): #Just to iterate all the board to check the values 
     for i in range(f): 
      for j in range(c): 
       game.distancia_vecino(i,j) 
     a = input() #In order to the screen dont close after executing it 

    def vida(self, cell_f, cell_c): #Check if they are alive and if they should be alive or dead 

     print(contador) 
     if tablero[cell_f][cell_c] == 1: 
      if contador > 3 or contador < 2: 
       tablero[cell_f][cell_c] = 0 
     else: 
      if contador == 3: 
       tablero[cell_f][cell_c] = 1 

     game.dibujar_tablero() 

    def dibujar_tablero(self): #Draw the board in a more clearly way 
     contador1 = 0 
     for i in range(f): 
      for j in range(c): 
       if tablero[i][j] == 1: 
        tablero_dibujado[i][j] = "§" 
       else: 
        tablero_dibujado[i][j] = "■" 

     for i in tablero_dibujado: 
      print(" ") 
      for j in i: 
       print(j, end=" ") 
     print("") 
     time.sleep(2) 


game = juego() 
game.tablero() 
game.rellenar_tablero_manual() 
game.iterar_tablero() 

我需要的是,在detectar_vecino必須獲取板上所有單元格的鄰居,問題是這樣做我得到:在賦值之前引用了局部變量'contador'。我知道爲什麼會發生這種情況。但是我找不到任何替代方法,所以如果有人知道我該如何解決這個問題。

我只是想澄清,這心不是從任何地方的任何工作而言,我做這件事只是我自己的愛好,有了這個,我只是想感謝你的時間來完成我很感激

+1

如果您正在使用然後python3使用該變量在'detectar_vecino'函數之前寫這行'全球contador'。 –

+3

如果您使用英文名稱作爲變量和函數,它會使您的代碼更易於閱讀,適合大多數人使用。儘管多年來我已經寫了不少人生程序,但我不確定自己的邏輯,但我想你可以設置'contador = 0',或者通過參數將初始值傳遞給'detectar_vecino'。或者你可以遵循Shreyash S Sarnayak的建議並使其成爲全局的,儘管如果你可以避免使用可變全局變量更好,因爲它們破壞了程序的模塊化,並使程序更難調試。 –

+0

我不明白你爲什麼要用'comparar_string'做所有瘋狂的字符串。這是緩慢和低效的。如果你的程序只做了一次,那將會很糟糕,但是你可以在一個函數內部進行循環,這個函數會在板上的每個單元中被調用!你應該使用索引來訪問元組列表中的值。順便說一句,有一個像'distancia_vecino'這樣的變量名稱與其定義的函數名稱不是一個好主意。 –

回答

0

添加此線global contador像下面

def detectar_vecino(self, cell_f, cell_c, x, y): 
    global contador # Add this line. 
    vecinos = 0 

    if tablero[cell_f][cell_c] == 1: 
     if tablero[cell_f][cell_c] == tablero[x][y]: 
      vecinos+=1 
    else: 
     if tablero[cell_f][cell_c] != tablero[x][y]: 
      vecinos+=1 
    contador = contador + vecinos