Ray
Ray

Reputation: 33

Why isn't my method of checking for a winner in tic tac toe not working?

I saw a video of a guy going through his game and it works. I am checking for a winner the same exact way but nothing is happening. I am using images instead of letters but does that really make a difference. Any suggestions?

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() 

Upvotes: 1

Views: 63

Answers (1)

sgfw
sgfw

Reputation: 312

It looks like your problem is that when you set

buttonList[c]["image"] = picX

the true value that buttonList[c]["image"] takes on is the string "pyimage1", while picX is of the type tkinter.PhotoImage. In light of this, it should fix your problem if instead of comparing things like

button1["image"] == picX

you should do something like

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

and do the same with picO.

Upvotes: 1

Related Questions