Advertisement
GalinaKG

Untitled

May 10th, 2022
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.71 KB | None | 0 0
  1. from tkinter import *
  2. import tkinter.messagebox as msg
  3.  
  4. root = Tk()
  5. root.title('TIC-TAC-TOE---Project Gurukul')
  6. # labels
  7. Label(root, text="player1 : X", font="times 15").grid(row=0, column=1)
  8. Label(root, text="player2 : O", font="times 15").grid(row=0, column=2)
  9.  
  10. digits = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  11.  
  12. # for player1 sign = X and for player2 sign= Y
  13. mark = ''
  14.  
  15. # counting the no. of click
  16. count = 0
  17.  
  18. panels = ["panel"] * 10
  19.  
  20.  
  21. def win(panels, sign):
  22. return ((panels[1] == panels[2] == panels[3] == sign)
  23. or (panels[1] == panels[4] == panels[7] == sign)
  24. or (panels[1] == panels[5] == panels[9] == sign)
  25. or (panels[2] == panels[5] == panels[8] == sign)
  26. or (panels[3] == panels[6] == panels[9] == sign)
  27. or (panels[3] == panels[5] == panels[7] == sign)
  28. or (panels[4] == panels[5] == panels[6] == sign)
  29. or (panels[7] == panels[8] == panels[9] == sign))
  30.  
  31.  
  32. def checker(digit):
  33. global count, mark, digits
  34.  
  35. # Check which button clicked
  36.  
  37. if digit == 1 and digit in digits:
  38. digits.remove(digit)
  39. ##player1 will play if the value of count is even and for odd player2 will play
  40. if count % 2 == 0:
  41. mark = 'X'
  42. panels[digit] = mark
  43. elif count % 2 != 0:
  44. mark = 'O'
  45. panels[digit] = mark
  46.  
  47. button1.config(text=mark)
  48. count = count + 1
  49. sign = mark
  50.  
  51. if (win(panels, sign) and sign == 'X'):
  52. msg.showinfo("Result", "Player1 wins")
  53. root.destroy()
  54. elif (win(panels, sign) and sign == 'O'):
  55. msg.showinfo("Result", "Player2 wins")
  56. root.destroy()
  57.  
  58. if digit == 2 and digit in digits:
  59. digits.remove(digit)
  60.  
  61. if count % 2 == 0:
  62. mark = 'X'
  63. panels[digit] = mark
  64. elif count % 2 != 0:
  65. mark = 'O'
  66. panels[digit] = mark
  67.  
  68. button2.config(text=mark)
  69. count = count + 1
  70. sign = mark
  71.  
  72. if (win(panels, sign) and sign == 'X'):
  73. msg.showinfo("Result", "Player1 wins")
  74. root.destroy()
  75. elif (win(panels, sign) and sign == 'O'):
  76. msg.showinfo("Result", "Player2 wins")
  77. root.destroy()
  78.  
  79. if digit == 3 and digit in digits:
  80. digits.remove(digit)
  81.  
  82. if count % 2 == 0:
  83. mark = 'X'
  84. panels[digit] = mark
  85. elif count % 2 != 0:
  86. mark = 'O'
  87. panels[digit] = mark
  88.  
  89. button3.config(text=mark)
  90. count = count + 1
  91. sign = mark
  92.  
  93. if (win(panels, sign) and sign == 'X'):
  94. msg.showinfo("Result", "Player1 wins")
  95. root.destroy()
  96. elif (win(panels, sign) and sign == 'O'):
  97. msg.showinfo("Result", "Player2 wins")
  98. root.destroy()
  99.  
  100. if digit == 4 and digit in digits:
  101. digits.remove(digit)
  102.  
  103. if count % 2 == 0:
  104. mark = 'X'
  105. panels[digit] = mark
  106. elif count % 2 != 0:
  107. mark = 'O'
  108. panels[digit] = mark
  109.  
  110. button4.config(text=mark)
  111. count = count + 1
  112. sign = mark
  113.  
  114. if (win(panels, sign) and sign == 'X'):
  115. msg.showinfo("Result", "Player1 wins")
  116. root.destroy()
  117. elif (win(panels, sign) and sign == 'O'):
  118. msg.showinfo("Result", "Player2 wins")
  119. root.destroy()
  120.  
  121. if digit == 5 and digit in digits:
  122. digits.remove(digit)
  123.  
  124. if count % 2 == 0:
  125. mark = 'X'
  126. panels[digit] = mark
  127. elif count % 2 != 0:
  128. mark = 'O'
  129. panels[digit] = mark
  130.  
  131. button5.config(text=mark)
  132. count = count + 1
  133. sign = mark
  134.  
  135. if (win(panels, sign) and sign == 'X'):
  136. msg.showinfo("Result", "Player1 wins")
  137. root.destroy()
  138. elif (win(panels, sign) and sign == 'O'):
  139. msg.showinfo("Result", "Player2 wins")
  140. root.destroy()
  141.  
  142. if digit == 6 and digit in digits:
  143. digits.remove(digit)
  144.  
  145. if count % 2 == 0:
  146. mark = 'X'
  147. panels[digit] = mark
  148. elif count % 2 != 0:
  149. mark = 'O'
  150. panels[digit] = mark
  151.  
  152. button6.config(text=mark)
  153. count = count + 1
  154. sign = mark
  155.  
  156. if (win(panels, sign) and sign == 'X'):
  157. msg.showinfo("Result", "Player1 wins")
  158. root.destroy()
  159. elif (win(panels, sign) and sign == 'O'):
  160. msg.showinfo("Result", "Player2 wins")
  161. root.destroy()
  162.  
  163. if digit == 7 and digit in digits:
  164. digits.remove(digit)
  165.  
  166. if count % 2 == 0:
  167. mark = 'X'
  168. panels[digit] = mark
  169. elif count % 2 != 0:
  170. mark = 'O'
  171. panels[digit] = mark
  172.  
  173. button7.config(text=mark)
  174. count = count + 1
  175. sign = mark
  176.  
  177. if (win(panels, sign) and sign == 'X'):
  178. msg.showinfo("Result", "Player1 wins")
  179. root.destroy()
  180. elif (win(panels, sign) and sign == 'O'):
  181. msg.showinfo("Result", "Player2 wins")
  182. root.destroy()
  183.  
  184. if digit == 8 and digit in digits:
  185. digits.remove(digit)
  186.  
  187. if count % 2 == 0:
  188. mark = 'X'
  189. panels[digit] = mark
  190. elif count % 2 != 0:
  191. mark = 'O'
  192. panels[digit] = mark
  193.  
  194. button8.config(text=mark)
  195. count = count + 1
  196. sign = mark
  197.  
  198. if (win(panels, sign) and sign == 'X'):
  199. msg.showinfo("Result", "Player1 wins")
  200. root.destroy()
  201. elif (win(panels, sign) and sign == 'O'):
  202. msg.showinfo("Result", "Player2 wins")
  203. root.destroy()
  204.  
  205. if digit == 9 and digit in digits:
  206. digits.remove(digit)
  207.  
  208. if count % 2 == 0:
  209. mark = 'X'
  210. panels[digit] = mark
  211. elif count % 2 != 0:
  212. mark = 'O'
  213. panels[digit] = mark
  214.  
  215. button9.config(text=mark)
  216. count = count + 1
  217. sign = mark
  218.  
  219. if (win(panels, sign) and sign == 'X'):
  220. msg.showinfo("Result", "Player1 wins")
  221. root.destroy()
  222. elif (win(panels, sign) and sign == 'O'):
  223. msg.showinfo("Result", "Player2 wins")
  224. root.destroy()
  225.  
  226. ###if count is greater then 8 then the match has been tied
  227. if (count > 8 and win(panels, 'X') == False and win(panels, 'O') == False):
  228. msg.showinfo("Result", "Match Tied")
  229. root.destroy()
  230.  
  231.  
  232. ####define buttons
  233. button1 = Button(root, width=15, font=('Times 16 bold'), height=7, command=lambda: checker(1))
  234. button1.grid(row=1, column=1)
  235. button2 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(2))
  236. button2.grid(row=1, column=2)
  237. button3 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(3))
  238. button3.grid(row=1, column=3)
  239. button4 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(4))
  240. button4.grid(row=2, column=1)
  241. button5 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(5))
  242. button5.grid(row=2, column=2)
  243. button6 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(6))
  244. button6.grid(row=2, column=3)
  245. button7 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(7))
  246. button7.grid(row=3, column=1)
  247. button8 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(8))
  248. button8.grid(row=3, column=2)
  249. button9 = Button(root, width=15, height=7, font=('Times 16 bold'), command=lambda: checker(9))
  250. button9.grid(row=3, column=3)
  251.  
  252. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement