陷入人生编码游戏的愚蠢阶段
Stuck in a stupid phase of coding game of life
我一直在尝试自己编写生活游戏代码,因为我想学习 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' 在赋值之前被引用。我知道为什么会这样 happen.However 我找不到任何替代方法所以如果有人知道我该如何解决它请拜托。
我只是想澄清一下,这不是来自任何地方的任何工作,我只是出于业余爱好而自己做这件事,最后我只想感谢你抽出时间,我很感激
像下面这样添加这一行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
我一直在尝试自己编写生活游戏代码,因为我想学习 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' 在赋值之前被引用。我知道为什么会这样 happen.However 我找不到任何替代方法所以如果有人知道我该如何解决它请拜托。
我只是想澄清一下,这不是来自任何地方的任何工作,我只是出于业余爱好而自己做这件事,最后我只想感谢你抽出时间,我很感激
像下面这样添加这一行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