Advertisement
Guest User

tictactoe

a guest
Apr 8th, 2020
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.47 KB | None | 0 0
  1. from tkinter import*
  2. z=0
  3. br=0
  4. s=[]
  5. x=[]
  6. o=[]
  7. pobjeda=False
  8. def main():
  9.     global s
  10.     a=Tk()
  11.     z=0
  12.     for i in range(1, 10):
  13.         s.append(button(i, a))
  14.     a.mainloop()
  15.     return 0
  16.  
  17. def button(i,a):
  18.     t=Button(a, text=' ', height=5, width=10, command=lambda i=i: onClick(i,t,a), font=('Arial',30))
  19.     t.grid(row=(i-1) // 3, column=(i-1) % 3)
  20.     return 0
  21.  
  22. def onClick(i,t,a):
  23.     global z
  24.     global br
  25.     global pobjeda
  26.     if (z):
  27.         if int(i)not in x and int(i) not in o and not pobjeda:
  28.             br+=1
  29.             x.append(int(i))
  30.             t.configure(text='X', bg="#aa00ff", fg="#220033")
  31.             z=0
  32.             if (1 in x and 2 in x and 3 in x or 4 in x and 5 in x and 6 in x or 7 in x and 8 in x and 9 in x or 1 in x and 4 in x and 7 in x or 2 in x and 5 in x and 8 in x or 3 in x and 6 in x and 9 in x or 1 in x and 5 in x and 9 in x or 3 in x and 5 in x and 7 in x):
  33.                 p=Label(a,text='X pobjedio',font=('Arial',30))
  34.                 p.grid(row=3,column=0)
  35.                 ni=Button(a, text='NOVA IGRA', height=5, width=10, command=lambda a=a: novaIgra(a), font=('Arial',15))
  36.                 iz=Button(a, text='IZLAZ', height=5, width=10, command=lambda a=a: izlaz(a), font=('Arial',15))
  37.                 ni.grid(row=3,column=1)
  38.                 iz.grid(row=3,column=2)
  39.                 pobjeda=True
  40.  
  41.     else:
  42.         if int(i)not in x and int(i) not in o and not pobjeda:
  43.             br+=1
  44.             o.append(int(i))
  45.             t.configure(text='O', bg="#ff9999", fg="#4d0000")
  46.             z=1
  47.             if (1 in o and 2 in o and 3 in o or 4 in o and 5 in o and 6 in o or 7 in o and 8 in o and 9 in o or 1 in o and 4 in o and 7 in o or 2 in o and 5 in o and 8 in o or 3 in o and 6 in o and 9 in o or 1 in o and 5 in o and 9 in o or 3 in o and 5 in o and 7 in o):
  48.                 p=Label(a,text='O pobjedio',font=('Arial',30))
  49.                 p.grid(row=3,column=0)
  50.                
  51.                 ni=Button(a, text='NOVA IGRA', height=5, width=10, command=lambda a=a: novaIgra(a), font=('Arial',15))
  52.                 iz=Button(a, text='IZLAZ', height=5, width=10, command=lambda a=a: izlaz(a), font=('Arial',15))
  53.                 ni.grid(row=3,column=1)
  54.                 iz.grid(row=3,column=2)
  55.                 pobjeda=True
  56.     if (br%9==0 and not pobjeda):
  57.         p=Label(a,text='Nerješeno',font=('Arial',30))
  58.         p.grid(row=3,column=0)
  59.         ni=Button(a, text='NOVA IGRA', height=5, width=10, command=lambda a=a: novaIgra(a), font=('Arial',15))
  60.         iz=Button(a, text='IZLAZ', height=5, width=10, command=lambda a=a: izlaz(b,a), font=('Arial',15))
  61.         ni.grid(row=3,column=1)
  62.         iz.grid(row=3,column=2)
  63. def izlaz(a):
  64.     a.destroy()
  65.     return 0
  66. def novaIgra(a):
  67.     global x
  68.     global o
  69.     global pobjeda
  70.     pobjeda=False
  71.     x=[]
  72.     o=[]
  73.     a.destroy()
  74.     main()
  75.  
  76. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement