-1
我見過如何檢查井字遊戲的勝利,但我使用的圖像,我似乎無法弄清楚如何。我想過如果使用if語句並使3個按鈕與顯示的圖像相同,但這似乎不起作用。有任何想法嗎?如何檢查井字遊戲中的勝利?
from tkinter import*
#Window stuff
window = Tk()
window.title("Tic Tac Toe")
window.configure(background = "black")
window.geometry("400x400")
#Variables
global clickable
playerXturn = True
#Display X or O
def buttonClicked(c) :
global playerXturn
if playerXturn == True :
buttonList[c]["image"] = picX
playerXturn = False
labelTurn ["text"] = "O's turn"
elif clickable[c] == "" :
buttonList[c]["image"] = picO
playerXturn = True
labelTurn ["text"] = "X's turn"
#Check for a win
if button1 == picX and button2 == picX and button3 == picX:
print("r")
#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
#Labels
labelTurn = Label (window, text = "",)
labelTurn.grid (row = 4, column = 4)
#Lists
buttonList = [button1, button2, button3, button4, button5, button6, button7, button8, button9]
clickable = ["", "", "", "", "", "", "", "", ""]
window.mainloop()
的可能重複http://stackoverflow.com/questions/33178684/python-programming-3-4-2-naughts-and-crosses-best-way-對入住的how-to-看,如果-G/33179368#33179368 – BrianO