【问题标题】:Issues with resetting a tic tac toe board- Python tkinter重置井字游戏板的问题 - Python tkinter
【发布时间】:2021-07-21 13:39:18
【问题描述】:

我正在 Python tkinter 中制作井字游戏。每次点击 tic tac 板上的按钮时,我都会将 O 或 X 添加到列表中。我遇到的问题是当“X”或“O”获胜时,我调用重置函数来重置板。出于某种原因,在棋盘重置并且我再次玩游戏后,会出现一个消息框,上面写着“X”或“O”赢了,即使他们都没有赢。有人可以帮帮我吗?我的代码:

from tkinter import *
from tkinter import messagebox

screen = Tk()
screen.title("Tic Tac Toe")
screen.geometry("600x600+350+10")
turns = 0
pixel = PhotoImage(width=1, height=1)
buttons_list = []
clicked = [" ", " ", " ", " ", " ", " ", " ", " ", " "]



def click(c, d):
    global turns
    turns += 1

    has_been_clicked = (buttons_list[c][d].cget("text") != " ")

    if has_been_clicked is True:
        return


    if turns % 2 == 0:
        buttons_list[c][d].configure(text="X", fg="red", font=("Arial", 50))
        clicked[c * 3 + d] = "X"
    else:
        buttons_list[c][d].configure(text="O", fg="green", font=("Arial", 50))
        clicked[c * 3 + d] = "O"
    check()


def check():
    if (clicked[0] == "X" and clicked[1] == "X" and clicked[2] == "X") or \
            (clicked[3] == "X" and clicked[4] == "X" and clicked[5] == "X") or \
            (clicked[6] == "X" and clicked[7] == "X" and clicked[8] == "X") or \
            (clicked[0] == "X" and clicked[4] == "X" and clicked[8] == "X") or \
            (clicked[2] == "X" and clicked[4] == "X" and clicked[6] == "X") or \
            (clicked[0] == "X" and clicked[3] == "X" and clicked[6] == "X") or \
            (clicked[1] == "X" and clicked[4] == "X" and clicked[7] == "X") or \
            (clicked[2] == "X" and clicked[5] == "X" and clicked[8] == "X"):
            messagebox.showinfo("X is the winner!")
            reset()
    elif (clicked[0] == "O" and clicked[1] == "O" and clicked[2] == "O") or \
            (clicked[3] == "O" and clicked[4] == "O" and clicked[5] == "O") or \
            (clicked[6] == "O" and clicked[7] == "O" and clicked[8] == "O") or \
            (clicked[0] == "O" and clicked[4] == "O" and clicked[8] == "O") or \
            (clicked[2] == "O" and clicked[4] == "O" and clicked[6] == "O") or \
            (clicked[0] == "O" and clicked[3] == "O" and clicked[6] == "O") or \
            (clicked[1] == "O" and clicked[4] == "O" and clicked[7] == "O") or \
            (clicked[2] == "O" and clicked[5] == "O" and clicked[8] == "O"):
            messagebox.showinfo("O is the winner!")
            reset()


def reset():
    for b in buttons_list:
        for b2 in b:
            b2.configure(text=" ")

    for element in clicked:
        element = " "


for i in range(3):
    row = []
    for j in range(3):
        button = Button(screen, height=200, width=200, image=pixel, text=" ", compound="c", bg="papaya whip",
                        command=lambda i=i, j=j: click(i, j))
        button.grid(row=i, column=j)
        row.append(button)
    buttons_list.append(row)

screen.mainloop()

感谢 joostblack 帮我解决问题!更新代码:

from tkinter import *
from tkinter import messagebox

screen = Tk()
screen.title("Tic Tac Toe")
screen.geometry("600x600+350+10")
turns = 0
pixel = PhotoImage(width=1, height=1)
buttons_list = []
clicked = [" " for i in range(9)]



def click(c, d):
    global turns
    turns += 1

    has_been_clicked = (buttons_list[c][d].cget("text") != " ")

    if has_been_clicked is True:
        return


    if turns % 2 == 0:
        buttons_list[c][d].configure(text="X", fg="red", font=("Arial", 50))
        clicked[c * 3 + d] = "X"
    else:
        buttons_list[c][d].configure(text="O", fg="green", font=("Arial", 50))
        clicked[c * 3 + d] = "O"
    check()


def check():
    if (clicked[0] == "X" and clicked[1] == "X" and clicked[2] == "X") or \
            (clicked[3] == "X" and clicked[4] == "X" and clicked[5] == "X") or \
            (clicked[6] == "X" and clicked[7] == "X" and clicked[8] == "X") or \
            (clicked[0] == "X" and clicked[4] == "X" and clicked[8] == "X") or \
            (clicked[2] == "X" and clicked[4] == "X" and clicked[6] == "X") or \
            (clicked[0] == "X" and clicked[3] == "X" and clicked[6] == "X") or \
            (clicked[1] == "X" and clicked[4] == "X" and clicked[7] == "X") or \
            (clicked[2] == "X" and clicked[5] == "X" and clicked[8] == "X"):
            messagebox.showinfo("X is the winner!")
            reset()
    elif (clicked[0] == "O" and clicked[1] == "O" and clicked[2] == "O") or \
            (clicked[3] == "O" and clicked[4] == "O" and clicked[5] == "O") or \
            (clicked[6] == "O" and clicked[7] == "O" and clicked[8] == "O") or \
            (clicked[0] == "O" and clicked[4] == "O" and clicked[8] == "O") or \
            (clicked[2] == "O" and clicked[4] == "O" and clicked[6] == "O") or \
            (clicked[0] == "O" and clicked[3] == "O" and clicked[6] == "O") or \
            (clicked[1] == "O" and clicked[4] == "O" and clicked[7] == "O") or \
            (clicked[2] == "O" and clicked[5] == "O" and clicked[8] == "O"):
            messagebox.showinfo("Tic Tac Toe", "O is the winner!")
            reset()
    elif clicked.count("X") + clicked.count("O") == 9:
        messagebox.showinfo("Tic Tac Toe", "The game ended in a draw!")
        reset()


def reset():
    for b in buttons_list:
        for b2 in b:
            b2.configure(text=" ")

    for i in range(len(clicked)):
        clicked[i] = " "


for i in range(3):
    row = []
    for j in range(3):
        button = Button(screen, height=200, width=200, image=pixel, text=" ", compound="c", bg="papaya whip",
                        command=lambda i=i, j=j: click(i, j))
        button.grid(row=i, column=j)
        row.append(button)
    buttons_list.append(row)

screen.mainloop()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    你并没有真正重置clicked

    改变这个:

    for element in clicked:
        element = " "
    

    for i in range(len(clicked)):
        clicked[i] = " "
    

    【讨论】:

    • 我认为您不需要将列表声明为全局变量。没有它它对我有用。
    • @Roni 我的错。我在考虑我的方法。
    • 是的,有很多方法可以创建空字符串列表。你也可以[" " for i in range(9)]
    • @joostblack,应该是[" " for i in range(9+1)]
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 1970-01-01
    相关资源
    最近更新 更多