1 #!/usr/bin/env pythyon3
3 from tkinter
import messagebox
6 # Button function: function to define a button
8 b
=Button(frame
,padx
=1,bg
="indianred4",activebackground
="tomato3",
9 width
=3,text
=" ",font
=('arial',60,'bold'), relief
="sunken",bd
=10)
12 # Change function: change operand for next player (X or O)
20 # Reset function: reset game (reset buttons to normal & clear text)
26 b
[i
][j
]["state"]=NORMAL
28 # Check function: checks for Victory or Draw
32 if(b
[i
][0]["text"]==b
[i
][1]["text"]==b
[i
][2]["text"]==a
or
33 b
[0][i
]["text"]==b
[1][i
]["text"]==b
[2][i
]["text"]==\
35 messagebox
.showinfo("Congrats!!","'"+a
+"' has won")
38 if(b
[0][0]["text"]==b
[1][1]["text"]==b
[2][2]["text"]==a
or
39 b
[0][2]["text"]==b
[1][1]["text"]==b
[2][0]["text"]==a
):
40 messagebox
.showinfo("Congrats!!","'"+a
+"' has won")
42 elif(b
[0][0]["state"]==b
[0][1]["state"]==b
[0][2]["state"]==\
43 b
[1][0]["state"]==b
[1][1]["state"]==b
[1][2]["state"]==\
44 b
[2][0]["state"]==b
[2][1]["state"]==b
[2][2]["state"]==DISABLED
):
45 messagebox
.showinfo("Tied!!","The match ended in a draw")
48 # Click function: handle clicks on board
50 b
[row
][col
].config(text
=a
,state
=DISABLED
,disabledforeground
=color
[a
])
53 label
.config(text
=a
+"'s Chance")
54 ############## Main Program ##########################################
55 root
=Tk() #Window defined
56 root
.title("Tic-Tac-Toe") # Title given
57 a
=r
.choice(['O','X']) # 2 operators (operands) defined
58 color
={'O':"deep sky blue",'X':"lawn green"}
62 b
[i
].append(button(root
))
63 b
[i
][j
].config(command
= lambda row
=i
,col
=j
:click(row
,col
))
64 b
[i
][j
].grid(row
=i
,column
=j
)
65 label
=Label(text
=a
+"'s Chance",font
=('arial',20,'bold'),bg
="rosy brown")
66 label
.grid(row
=3,column
=0,columnspan
=3,sticky
="nsew")