重置井字游戏板的问题 - Python tkinter

时间:2021-07-21 13:39:18

标签: python tkinter

我正在 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()

1 个答案:

答案 0 :(得分:2)

您并没有真正重置 clicked

改变这个:

for element in clicked:
    element = " "

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