python tic tac toe game stop and value check function return 错误
python tic tac toe game stop and value check function return error
我正在尝试 运行 python 中的简单井字游戏脚本。我使用一个列表来跟踪 table 的哪个单元格中有“X”、“Y”或什么都没有。要设置您必须输入要玩的单元格坐标的值,如果坐标不存在或已被占用,您会收到一条错误消息,游戏会要求您再次输入。问题是每当我输入一个数字时,我都会收到错误消息并且程序停止。我无法发现错误,任何人都可以帮助我吗?
成本如下:
class Tris:
def __init__(self, xo = True):
self.tabella = [0, 0, 0,
0, 0, 0,
0, 0, 0] # -this is the starting table
self.xo = xo # -to check if it is "x" turn or "o" turn
self.finito = False # -to check if the game is unfinished
# -just to print grafic style of the table: ignore that
def print_tabella(self):
giocatore = "X" if self.xo else "Y"
tabella_convertita = self.tabella
for n, i in enumerate(tabella_convertita):
if i == 0:
tabella_convertita[n] = " "
elif i == 1:
tabella_convertita[n] = "X"
else:
tabella_convertita[n] = "O"
t1 = "|".join(str(i) for i in tabella_convertita[0:3])
t2 = "|".join(str(i) for i in tabella_convertita[3:6])
t3 = "|".join(str(i) for i in tabella_convertita[6:9])
spazio_casella = "-+-+-"
testo_segnaposto = """use following numbers to set X/O:
0|1|2
-+-+-
3|4|5
-+-+-
6|7|8"""
turno_di = f"turn : {giocatore}"
tab_finale = t1 + "\n" + spazio_casella + "\n" + t2 + "\n" + spazio_casella + "\n" + t3 + "\n"+ testo_segnaposto +"\n" + turno_di
return tab_finale
def controlla(self, casella):
if casella.isalpha(): # check if the input is not numerical
return False
else:
if not 0 <= int(casella) <= 8: # the value must be between 0 and 8
return False
else:
return self.tabella[int(casella)] == 0 # the cell must not be containing another symbol
def is_winner(self): # check if there is a row, a column or a diagonal with the same symbol
lista_righe = [[self.tabella[0], self.tabella[1], self.tabella[2]], [self.tabella[3], self.tabella[4], self.tabella[5]],
[self.tabella[6], self.tabella[7], self.tabella[8]]]
lista_colonne = [[self.tabella[0], self.tabella[3], self.tabella[6]], [self.tabella[1], self.tabella[4], self.tabella[7]],
[self.tabella[2], self.tabella[5], self.tabella[8]]]
lista_diagonali = [[self.tabella[0], self.tabella[4], self.tabella[8]], [self.tabella[2], self.tabella[4], self.tabella[6]], ]
lista_vincite = [lista_colonne, lista_diagonali, lista_righe]
winner = False
for i in lista_vincite:
for liste in i:
if liste[0] == liste[1] and liste[1] == liste[2] and liste[0] != 0:
winner = True
break
return winner
def gioca(self):
while self.finito == False: # check if the game is finished
if self.is_winner(): # check if there is a winner
self.finito = True
break
print(self.print_tabella()) # print the table in grafic form
inp = input("choose a number to set X/O: ")
if not self.controlla(inp): # check if the input is in valid form
print("invalid number or cell already used") # or if the chosen cell is available
else:
self.xo = not self.xo # to track if it is X turn or O turn
if self.xo:
self.tabella[int(inp)] = 1
else:
self.tabella[int(inp)] = 2
gioco_tris = Tris()
gioco_tris.gioca()
问题是 print_tabella
变异了 tabella
。当你这样做时:
tabella_convertita = self.tabella
...您不是在创建新列表,而只是 self.tabella
的同义词。所以无论你对 tabella_convertita
做什么,都会发生在 self.tabella
上:数字内容被替换为字符。
改为:
tabella_convertita = self.tabella[:]
我正在尝试 运行 python 中的简单井字游戏脚本。我使用一个列表来跟踪 table 的哪个单元格中有“X”、“Y”或什么都没有。要设置您必须输入要玩的单元格坐标的值,如果坐标不存在或已被占用,您会收到一条错误消息,游戏会要求您再次输入。问题是每当我输入一个数字时,我都会收到错误消息并且程序停止。我无法发现错误,任何人都可以帮助我吗? 成本如下:
class Tris:
def __init__(self, xo = True):
self.tabella = [0, 0, 0,
0, 0, 0,
0, 0, 0] # -this is the starting table
self.xo = xo # -to check if it is "x" turn or "o" turn
self.finito = False # -to check if the game is unfinished
# -just to print grafic style of the table: ignore that
def print_tabella(self):
giocatore = "X" if self.xo else "Y"
tabella_convertita = self.tabella
for n, i in enumerate(tabella_convertita):
if i == 0:
tabella_convertita[n] = " "
elif i == 1:
tabella_convertita[n] = "X"
else:
tabella_convertita[n] = "O"
t1 = "|".join(str(i) for i in tabella_convertita[0:3])
t2 = "|".join(str(i) for i in tabella_convertita[3:6])
t3 = "|".join(str(i) for i in tabella_convertita[6:9])
spazio_casella = "-+-+-"
testo_segnaposto = """use following numbers to set X/O:
0|1|2
-+-+-
3|4|5
-+-+-
6|7|8"""
turno_di = f"turn : {giocatore}"
tab_finale = t1 + "\n" + spazio_casella + "\n" + t2 + "\n" + spazio_casella + "\n" + t3 + "\n"+ testo_segnaposto +"\n" + turno_di
return tab_finale
def controlla(self, casella):
if casella.isalpha(): # check if the input is not numerical
return False
else:
if not 0 <= int(casella) <= 8: # the value must be between 0 and 8
return False
else:
return self.tabella[int(casella)] == 0 # the cell must not be containing another symbol
def is_winner(self): # check if there is a row, a column or a diagonal with the same symbol
lista_righe = [[self.tabella[0], self.tabella[1], self.tabella[2]], [self.tabella[3], self.tabella[4], self.tabella[5]],
[self.tabella[6], self.tabella[7], self.tabella[8]]]
lista_colonne = [[self.tabella[0], self.tabella[3], self.tabella[6]], [self.tabella[1], self.tabella[4], self.tabella[7]],
[self.tabella[2], self.tabella[5], self.tabella[8]]]
lista_diagonali = [[self.tabella[0], self.tabella[4], self.tabella[8]], [self.tabella[2], self.tabella[4], self.tabella[6]], ]
lista_vincite = [lista_colonne, lista_diagonali, lista_righe]
winner = False
for i in lista_vincite:
for liste in i:
if liste[0] == liste[1] and liste[1] == liste[2] and liste[0] != 0:
winner = True
break
return winner
def gioca(self):
while self.finito == False: # check if the game is finished
if self.is_winner(): # check if there is a winner
self.finito = True
break
print(self.print_tabella()) # print the table in grafic form
inp = input("choose a number to set X/O: ")
if not self.controlla(inp): # check if the input is in valid form
print("invalid number or cell already used") # or if the chosen cell is available
else:
self.xo = not self.xo # to track if it is X turn or O turn
if self.xo:
self.tabella[int(inp)] = 1
else:
self.tabella[int(inp)] = 2
gioco_tris = Tris()
gioco_tris.gioca()
问题是 print_tabella
变异了 tabella
。当你这样做时:
tabella_convertita = self.tabella
...您不是在创建新列表,而只是 self.tabella
的同义词。所以无论你对 tabella_convertita
做什么,都会发生在 self.tabella
上:数字内容被替换为字符。
改为:
tabella_convertita = self.tabella[:]