python tic tac toe游戏停止和值检查函数返回错误

2024-05-13 02:05:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用python运行一个简单的tic-tac-toe游戏脚本。我使用一个列表来跟踪表的哪个单元格中有“X”、“Y”或什么都没有。要设置值,您必须输入要玩的单元格的坐标,如果坐标不存在或已被占用,您会收到一条错误消息,游戏会要求您再次输入。问题是,每当我输入一个数字,我就会收到错误信息,程序就会停止。我看不出错误,有人能帮我吗? cose如下所示:

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()

Tags: thetoinselfforifischeck
1条回答
网友
1楼 · 发布于 2024-05-13 02:05:20

问题是print_tabella突变tabella。当您这样做时:

    tabella_convertita = self.tabella

。。。您不是在创建新列表,而是self.tabella的同义词。因此,无论您对tabella_convertita做什么,都会发生在self.tabella上:数字内容将被替换为字符

相反,你应该:

    tabella_convertita = self.tabella[:]

相关问题 更多 >