我正在用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()
你不是真的在重置
clicked
更改此项:
到
相关问题 更多 >
编程相关推荐