-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic tac toe.py
63 lines (48 loc) · 1.8 KB
/
tic tac toe.py
1
from tkinter import *from tkinter import messageboximport random as rdef button(frame): b=Button(frame,padx=1,bg="papaya whip",width=3,text=" ",font=('arial',60,'bold'),relief="sunken",bd=10) return bdef change_a(): global a for i in ['0','X']: if not(i==a): a=1 breakdef reset(): global a for i in range (3): for j in range (3): b[i][j]["text"]=" " b[i][j]["state"]=NORMAL a=r.choice(['0','X'])def Check(): for i in range (3): if(b[i][0]["text"] == b[i][1]["text"]== b[i][2]["text"]==a or b[0][i]["text"]==b[1][i]["text"]==b[2][i]["text"]==a): messagebox.showinfo("Congrats!!"," ' "+a+ "has won") reset() if b[0][0]["text"]==b[1][1]["text"]==b[2][2]["text"]==a or b[0][2]["text"]==b[1][1]["text"]==b[2][0]["text"]==a: messagebox.showinfo("Congrats!!","'"+a+"has won") reset() elif(b[0][0]["state"]==b[0][1]["state"]==b[0][2]["state"]==b[1][0]["state"]==b[1][1]["state"]==b[1][2]["state"]==b[2][0]["state"]==[2][1]["state"]==b[2][2]["state"]==DISABLED): messagebox.showinfo("tied!!", "the match ended in draw") reset()def click(row,col): b[row][col].config(text=a,state=DISABLED,disabledforeground=Colour[a]) Check() change_a() Label.config(text=a+" 's chance")root=Tk()root.title("tic tak toe")a=r.choice(['0','X'])Colour={'0':"deep sky blue",'X':"lawn green"}b=[[],[],[]]for i in range(3): for j in range(3): b[i].append(button(root)) b[i][j].config(command=lambda row=i,col = j:click(row,col)) b[i][j].grid(row=i,column=j) label=Label(text=a+" 's chance",font=('arial',20,'bold')) label.grid(row=3,column=0,columnspan=3) root.mainloop()