If statement 在没有赢家的情况下,如何为井字游戏制作游戏序列?
我想展示一个标签,上面写着当没有赢家时游戏结束。我只是想做一个else声明,但是一旦有一个点击游戏就会显示出来,因为elif声明是错误的。任何想法If statement 在没有赢家的情况下,如何为井字游戏制作游戏序列?,if-statement,button,label,If Statement,Button,Label,我想展示一个标签,上面写着当没有赢家时游戏结束。我只是想做一个else声明,但是一旦有一个点击游戏就会显示出来,因为elif声明是错误的。任何想法 from tkinter import* #Window stuff window = Tk() window.title("Tic Tac Toe") window.configure(background = "black") window.geometry("400x400") #Variables global clickable
from tkinter import*
#Window stuff
window = Tk()
window.title("Tic Tac Toe")
window.configure(background = "black")
window.geometry("400x400")
#Variables
global clickable
playerXturn = True
ticker = 0
#Display X or O
def buttonClicked(c) :
global playerXturn
if playerXturn == True :
buttonList[c]["image"] = picX
buttonList[c]["state"] = DISABLED
playerXturn = False
labelTurn ["text"] = "O's turn"
elif clickable[c] == "" :
buttonList[c]["image"] = picO
buttonList[c]["state"] = DISABLED
playerXturn = True
labelTurn ["text"] = "X's turn"
#Info for user
global ticker
ticker += 1
if ticker == 500 :
labelInfo["text"] = "Timed Out"
labelInfo["bg"] = "red"
#Three in a row
elif (button1["image"] == str(picX) and button2["image"] == str(picX) and button3["image"] == str(picX) or
button4["image"] == str(picX) and button5["image"] == str(picX) and button6["image"] == str(picX) or
button7["image"] == str(picX) and button8["image"] == str(picX) and button9["image"] == str(picX) or
button1["image"] == str(picX) and button4["image"] == str(picX) and button7["image"] == str(picX) or
button2["image"] == str(picX) and button5["image"] == str(picX) and button8["image"] == str(picX) or
button3["image"] == str(picX) and button6["image"] == str(picX) and button9["image"] == str(picX) or
button1["image"] == str(picX) and button5["image"] == str(picX) and button9["image"] == str(picX) or
button3["image"] == str(picX) and button5["image"] == str(picX) and button7["image"] == str(picX)) :
#print ("X")
labelInfo["text"] = "X Wins"
labelInfo["bg"] = "red"
button1["state"] = DISABLED
button2["state"] = DISABLED
button3["state"] = DISABLED
button4["state"] = DISABLED
button5["state"] = DISABLED
button6["state"] = DISABLED
button7["state"] = DISABLED
button8["state"] = DISABLED
button9["state"] = DISABLED
labelTurn ["bg"] = "black"
elif (button1["image"] == str(picO) and button2["image"] == str(picO) and button3["image"] == str(picO) or
button4["image"] == str(picO) and button5["image"] == str(picO) and button6["image"] == str(picO) or
button7["image"] == str(picO) and button8["image"] == str(picO) and button9["image"] == str(picO) or
button1["image"] == str(picO) and button4["image"] == str(picO) and button7["image"] == str(picO) or
button2["image"] == str(picO) and button5["image"] == str(picO) and button8["image"] == str(picO) or
button3["image"] == str(picO) and button6["image"] == str(picO) and button9["image"] == str(picO) or
button1["image"] == str(picO) and button5["image"] == str(picO) and button9["image"] == str(picO) or
button3["image"] == str(picO) and button5["image"] == str(picO) and button7["image"] == str(picO)) :
#print("O")
labelInfo["text"] = "O Wins"
labelInfo["bg"] = "red"
button1["state"] = DISABLED
button2["state"] = DISABLED
button3["state"] = DISABLED
button4["state"] = DISABLED
button5["state"] = DISABLED
button6["state"] = DISABLED
button7["state"] = DISABLED
button8["state"] = DISABLED
button9["state"] = DISABLED
labelTurn ["bg"] = "black"
#Images
picX = PhotoImage (file = "x.gif")
picO = PhotoImage (file = "o.gif")
picBlank = PhotoImage (file = "sw.gif")
#Buttons
button1 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(0))
button1.grid (row = 0, column = 0)
#button1["state"] = DISABLED
button2 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(1))
button2.grid (row = 0, column = 1)
#button2["state"] = DISABLED
button3 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(2))
button3.grid (row = 0, column = 2)
#button3["state"] = DISABLED
button4 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(3))
button4.grid (row = 1, column = 0)
#button4["state"] = DISABLED
button5 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(4))
button5.grid (row = 1, column = 1)
#button5["state"] = DISABLED
button6 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(5))
button6.grid (row= 1, column = 2)
#button6["state"] = DISABLED
button7 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(6))
button7.grid (row = 2, column = 0)
#button7["state"] = DISABLED
button8 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(7))
button8.grid (row = 2, column = 1)
#button8["state"] = DISABLED
button9 = Button (window, text = "", image = picBlank, command = lambda: buttonClicked(8))
button9.grid (row = 2, column = 2)
#button9["state"] = DISABLED
#Lists
buttonList = [button1, button2, button3, button4, button5, button6, button7, button8, button9]
clickable = ["", "", "", "", "", "", "", "", ""]
#Reset
def processReset():
button1["state"] = NORMAL
button2["state"] = NORMAL
button3["state"] = NORMAL
button4["state"] = NORMAL
button5["state"] = NORMAL
button6["state"] = NORMAL
button7["state"] = NORMAL
button8["state"] = NORMAL
button9["state"] = NORMAL
button1["image"] = picBlank
button2["image"] = picBlank
button3["image"] = picBlank
button4["image"] = picBlank
button5["image"] = picBlank
button6["image"] = picBlank
button7["image"] = picBlank
button8["image"] = picBlank
button9["image"] = picBlank
labelTurn["bg"] = "white"
labelInfo["text"] = "Continue Playing"
labelInfo["bg"] = "white"
#Extra labels and buttons
labelMenu = Label (window, text = "Menu and Info", height = 2, width = 24, bg = "yellow")
labelMenu.grid (row = 4, column = 4)
labelTurn = Label (window, text = "X goes first", height = 2, width = 10)
labelTurn.grid (row = 6, column = 4)
labelInfo = Label (window, text = "Continue Playing", height = 2, width = 14)
labelInfo.grid (row = 5, column = 4)
buttonReset = Button(window, text = "Reset/New Game", bg = "Orange", command = processReset)
buttonReset.grid (row = 7, column = 4)
labelNote = Label (window, text = "Note: Losing player goes first next game", bg = "Pink")
labelNote.grid (row = 8, column = 4)
window.mainloop()
您希望在else-if链的末尾有一个
elif
语句,用于检查是否还有剩余的空格。如果没有剩余的空格,并且在此之前的声明中没有宣布胜利者,那么一定是平局
比如:
elif (check that no empty board spaces remain) :
#print("-")
labelInfo["text"] = "It's a tie!"
设置一个标志来表示游戏赢了,然后设置一个单独的
if
afterwrardsif(所有动作都完成了,但没有赢){每个人都输/平局}
你给了我一个主意,我所做的是,当一个游戏是一个时,将一个变量设置为true,然后我有一个嵌套的if语句,在9次点击后,如果变量为false,它会将标签更改为“Its a tie”。