【问题标题】:Why isn't my method of checking for a winner in tic tac toe not working?为什么我在井字游戏中检查获胜者的方法不起作用?
【发布时间】:2016-04-15 00:33:04
【问题描述】:

我看到一个人正在玩他的游戏的视频,它很有效。我正在以完全相同的方式检查获胜者,但没有发生任何事情。我使用的是图像而不是字母,但这真的会有所作为。有什么建议?

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 == 9 :
        labelNewGame["text"] = "GAME OVER"
        labelNewGame["bg"] = "red" 
        labelTurn["text"] = ""
        labelTurn["bg"] = "black"

#Three in a row
    elif (button1["image"] == picX and button2["image"] == picX and button3["image"] == picX or
          button4["image"] == picX and button5["image"] == picX and button6["image"] == picX or
          button7["image"] == picX and button8["image"] == picX and button9["image"] == picX or
          button1["image"] == picX and button4["image"] == picX and button7["image"] == picX or
          button2["image"] == picX and button5["image"] == picX and button8["image"] == picX or
          button3["image"] == picX and button6["image"] == picX and button9["image"] == picX or
          button1["image"] == picX and button5["image"] == picX and button9["image"] == picX or
          button3["image"] == picX and button5["image"] == picX and button7["image"] == picX) :
        print ("X wins")

    elif (button1["image"] == picO and button2["image"] == picO and button3["image"] == picO or
          button4["image"] == picO and button5["image"] == picO and button6["image"] == picO or
          button7["image"] == picO and button8["image"] == picO and button9["image"] == picO or
          button1["image"] == picO and button4["image"] == picO and button7["image"] == picO or
          button2["image"] == picO and button5["image"] == picO and button8["image"] == picO or
          button3["image"] == picO and button6["image"] == picO and button9["image"] == picO or
          button1["image"] == picO and button5["image"] == picO and button9["image"] == picO or
          button3["image"] == picO and button5["image"] == picO and button7["image"] == picO) :
        print ("O wins") 


#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 = ["", "", "", "", "", "", "", "", ""]

#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)
labelNewGame = Label (window, text = "Continue Playing", height = 2, width = 14,)     
labelNewGame.grid (row = 5, column = 4)

window.mainloop() 

【问题讨论】:

  • 您可以扩展 Button 类以添加一个属性来指示它所处的状态(“X”、“O”或“空白”),这样可以更简单地检查。

标签: python image if-statement button


【解决方案1】:

看起来你的问题是当你设置时

buttonList[c]["image"] = picX

buttonList[c]["image"] 的真正值是字符串 "pyimage1",而 picX 是 tkinter.PhotoImage 类型。鉴于此,如果不是比较诸如

之类的东西,它应该可以解决您的问题
button1["image"] == picX

你应该做类似的事情

 buttonN["image"] == str(picX)

对 picO 做同样的事情。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多