Guest User

Untitled

a guest
Jan 18th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1. import random
  2.  
  3. from tkinter import *
  4. from tkinter import ttk
  5.  
  6.  
  7. def main_window():
  8. root.title('Tic Tac Toe')
  9. remove_widgets()
  10. global frame
  11.  
  12. frame = ttk.Frame(root, width=250, height=150, relief='groove')
  13. frame.pack_propagate(False)
  14. frame.pack(padx=25, pady=75)
  15.  
  16. play = ttk.Button(frame, text='Play', command=lambda: play_menu(do=0))
  17. play.pack(side='top', pady=(50, 0))
  18.  
  19. qb = ttk.Button(frame, text="Quit", command=root.destroy)
  20. qb.pack(side='top', pady=(0, 50))
  21.  
  22.  
  23. def play_menu(do):
  24. root.title('Tic Tac Toe')
  25. remove_widgets()
  26. if do == 'redeclare':
  27. redeclare_vars()
  28.  
  29. global frame
  30.  
  31. label = ttk.Label(root, text='Choose your side', font=('French Script MT', 20))
  32. label.pack(side='top', pady=(25, 0))
  33.  
  34. frame = ttk.Frame(root, width=250, height=150, relief='groove')
  35. frame.pack_propagate(False)
  36. frame.pack(padx=25, pady=25)
  37.  
  38. player_x = ttk.Button(frame, text='X', command=lambda: game(pl='X'))
  39. player_x.grid(column=0, row=0, padx=(5, 0), pady=(5, 0))
  40.  
  41. player_o = ttk.Button(frame, text='O', command=lambda: game(pl='O'))
  42. player_o.grid(column=1, row=0, padx=(0, 5), pady=(5, 0))
  43.  
  44. back = ttk.Button(frame, text='Back', command=main_window)
  45. back.grid(column=0, row=1, sticky=(E, W), padx=(5, 5), pady=(0, 5), columnspan=2)
  46.  
  47.  
  48. def game(pl=None):
  49. root.title('Tic Tac Toe')
  50. remove_widgets()
  51.  
  52. global frame, canvas, player, computer, move, stop_game
  53.  
  54. frame = ttk.Frame(root, width=650, height=700)
  55. frame.pack_propagate(False)
  56. frame.pack()
  57.  
  58. canvas = Canvas(frame, width=600, height=600)
  59. canvas.pack(side='top', pady=(25, 0))
  60.  
  61. restart = ttk.Button(frame, text='Restart', command=lambda:
  62. play_menu(do='redeclare'))
  63. restart.pack(side='bottom', pady=20)
  64.  
  65. draw_board()
  66. canvas.bind('<Button-1>', square_selector)
  67.  
  68. if pl == 'X':
  69. player = 'X'
  70. computer = 'O'
  71. move = 'player'
  72. elif pl == 'O':
  73. player = 'O'
  74. computer = 'X'
  75. move = 'computer'
  76. computer_move()
  77.  
  78.  
  79. def remove_widgets():
  80. for widget in root.winfo_children():
  81. widget.destroy()
  82.  
  83.  
  84. def square_status_lib(square):
  85. global statusLib, player
  86. status = None
  87.  
  88. if player == 'X':
  89. status = 'X'
  90. elif player == 'O':
  91. status = 'O'
  92. statusLib[square-1] = status
  93.  
  94.  
  95. def square_selector(event):
  96. if 1 <= event.x <= 199:
  97. if 1 <= event.y <= 199:
  98. player_move(square=1)
  99. elif 1 <= event.x <= 199:
  100. if 201 <= event.y <= 399:
  101. player_move(square=2)
  102. elif 1 <= event.x <= 199:
  103. if 401 <= event.y <= 599:
  104. player_move(square=3)
  105. elif 201 <= event.x <= 399:
  106. if 1 <= event.y <= 199:
  107. player_move(square=4)
  108. elif 201 <= event.x <= 399:
  109. if 201 <= event.y <= 399:
  110. player_move(square=5)
  111. elif 201 <= event.x <= 399:
  112. if 401 <= event.y <= 599:
  113. player_move(square=6)
  114. elif 401 <= event.x <= 599:
  115. if 1 <= event.y <= 199:
  116. player_move(square=7)
  117. elif 401 <= event.x <= 599:
  118. if 201 <= event.y <= 399:
  119. player_move(square=8)
  120. elif 401 <= event.x <= 599:
  121. if 401 <= event.y <= 599:
  122. player_move(square=9)
  123.  
  124.  
  125. def computer_move():
  126. global move, x1, y1, x2, y2
  127. status, a = 0, 0
  128.  
  129. while status is not None:
  130. a = random.randint(1, 9)
  131. status = statusLib[a-1]
  132. x1, y1, x2, y2 = squareLib[a - 1][0], squareLib[a - 1][1], squareLib[a - 1][2], squareLib[a - 1][3]
  133. if computer == 'X':
  134. draw_move()
  135. statusLib[a-1] = 'X'
  136. elif computer == 'O':
  137. draw_move()
  138. statusLib[a-1] = 'O'
  139. end_game()
  140. if not stop_game:
  141. move = 'player'
  142.  
  143.  
  144. def player_move(square):
  145. global x1, y1, x2, y2, move, squareLib, stop_game
  146.  
  147. if statusLib[square-1] is None:
  148. x1, y1, x2, y2 = squareLib[square-1][0], squareLib[square-1][1], squareLib[square-1][2], squareLib[square-1][3]
  149. draw_move()
  150. square_status_lib(square=square)
  151. end_game()
  152. if not stop_game:
  153. move = 'computer'
  154. computer_move()
  155.  
  156.  
  157. def draw_move():
  158. global player, x1, y1, x2, y2, canvas, move
  159.  
  160. if move == 'player':
  161. if player == 'X':
  162. canvas.create_line(x1, y1, x2, y2)
  163. canvas.create_line(x1, y2, x2, y1)
  164. elif player == 'O':
  165. canvas.create_oval(x1, y1, x2, y2)
  166. elif move == 'computer':
  167. if computer == 'X':
  168. canvas.create_line(x1, y1, x2, y2)
  169. canvas.create_line(x1, y2, x2, y1)
  170. elif computer == 'O':
  171. canvas.create_oval(x1, y1, x2, y2)
  172.  
  173.  
  174. def draw_board():
  175. global canvas
  176. canvas.create_line(0, 200, 600, 200)
  177. canvas.create_line(0, 400, 600, 400)
  178. canvas.create_line(200, 0, 200, 600)
  179. canvas.create_line(400, 0, 400, 600)
  180.  
  181.  
  182. def check_end_game():
  183. global tie, stop_game, fin
  184.  
  185. if statusLib[0] == statusLib[1] == statusLib[2] == 'X' or statusLib[0] == statusLib[1] == statusLib[2] == 'O':
  186. stop_game, fin = True, 1
  187. elif statusLib[3] == statusLib[4] == statusLib[5] == 'X' or statusLib[3] == statusLib[4] == statusLib[5] == 'O':
  188. stop_game, fin = True, 2
  189. elif statusLib[6] == statusLib[7] == statusLib[8] == 'X' or statusLib[6] == statusLib[7] == statusLib[8] == 'O':
  190. stop_game, fin = True, 3
  191. elif statusLib[0] == statusLib[3] == statusLib[6] == 'X' or statusLib[0] == statusLib[3] == statusLib[6] == 'O':
  192. stop_game, fin = True, 4
  193. elif statusLib[1] == statusLib[4] == statusLib[7] == 'X' or statusLib[1] == statusLib[4] == statusLib[7] == 'O':
  194. stop_game, fin = True, 5
  195. elif statusLib[2] == statusLib[5] == statusLib[8] == 'X' or statusLib[2] == statusLib[5] == statusLib[8] == 'O':
  196. stop_game, fin = True, 6
  197. elif statusLib[2] == statusLib[4] == statusLib[6] == 'X' or statusLib[2] == statusLib[4] == statusLib[6] == 'O':
  198. stop_game, fin = True, 7
  199. elif statusLib[0] == statusLib[4] == statusLib[8] == 'X' or statusLib[0] == statusLib[4] == statusLib[8] == 'O':
  200. stop_game, fin = True, 8
  201. elif all(k is not None for k in statusLib):
  202. stop_game, tie, fin = True, True, 0
  203. else:
  204. stop_game, fin = False, 0
  205.  
  206.  
  207. def end_game():
  208. global stop_game, tie, canvas
  209. check_end_game()
  210. text = ''
  211.  
  212. if stop_game:
  213. canvas.unbind('<Button-1>')
  214. if move == 'player' and not tie:
  215. text = 'You win'
  216. elif move == 'computer' and not tie:
  217. text = 'You lose'
  218. elif tie:
  219. text = 'It's a tie'
  220.  
  221. finisher()
  222. canvas.create_text(300, 300, text=text, font=('French Script MT', 50), fill='#000000')
  223.  
  224. elif not stop_game:
  225. pass
  226.  
  227.  
  228. def finisher():
  229. global fin
  230. x3, y3, x4, y4 = 0, 0, 0, 0
  231.  
  232. if fin != 0:
  233. if fin == 1:
  234. x3, y3, x4, y4 = 100, 100, 100, 500
  235. elif fin == 2:
  236. x3, y3, x4, y4 = 300, 100, 300, 500
  237. elif fin == 3:
  238. x3, y3, x4, y4 = 500, 100, 500, 500
  239. elif fin == 4:
  240. x3, y3, x4, y4 = 100, 100, 500, 100
  241. elif fin == 5:
  242. x3, y3, x4, y4 = 100, 300, 500, 300
  243. elif fin == 6:
  244. x3, y3, x4, y4 = 100, 500, 500, 500
  245. elif fin == 7:
  246. x3, y3, x4, y4 = 100, 500, 500, 100
  247. elif fin == 8:
  248. x3, y3, x4, y4 = 100, 100, 500, 500
  249. canvas.create_line(x3, y3, x4, y4)
  250. elif fin == 0:
  251. pass
  252.  
  253.  
  254. def redeclare_vars():
  255. global statusLib, myVal, x1, x2, y1, y2, canvas, move, fin, stop_game, tie
  256. statusLib = []
  257. myVal = None
  258. for l in range(0, 9):
  259. statusLib.append(myVal)
  260. x1, y1, x2, y2, canvas, move, fin = 0, 0, 0, 0, None, '', 0
  261. stop_game, tie = False, False
  262.  
  263.  
  264. root = Tk()
  265. root.minsize(width=300, height=300)
  266.  
  267. statusLib = []
  268. myVal = None
  269. for i in range(0, 9):
  270. statusLib.append(myVal)
  271. x1, y1, x2, y2, canvas, move, fin = 0, 0, 0, 0, None, '', 0
  272. stop_game, tie = False, False
  273. game_mode = IntVar()
  274.  
  275. squareLib = [
  276. [20, 20, 180, 180],
  277. [20, 220, 180, 380],
  278. [20, 420, 180, 580],
  279. [220, 20, 380, 180],
  280. [220, 220, 380, 380],
  281. [220, 420, 380, 580],
  282. [420, 20, 580, 180],
  283. [420, 220, 580, 380],
  284. [420, 420, 580, 580]
  285. ]
  286.  
  287. main_window()
  288. root.mainloop()
Add Comment
Please, Sign In to add comment