erynur

3 in a row(v2)

Apr 7th, 2019
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.99 KB | None | 0 0
  1. try:
  2.     from tkinter import Tk, Button, Label, Frame, Menu, StringVar, messagebox
  3.     from functools import partial
  4. except ImportError:
  5.     from Tkinter import Tk, Button, Label, Frame, Menu, StringVar, messagebox
  6.     from fuctools import partial
  7.  
  8.  
  9. # For menubar to quit
  10. def quit_application():
  11.     quit()
  12.  
  13.  
  14. # Check if three X or O in a row
  15. def is_won():
  16.     if (label_list[0].cget('text') == 'X') & (label_list[1].cget('text') == 'X') & (label_list[2].cget('text') == 'X'):
  17.         label_list[0].config(bg='green')
  18.         label_list[1].config(bg='green')
  19.         label_list[2].config(bg='green')
  20.         show_winner('X')
  21.     elif (label_list[3].cget('text') == 'X') & (label_list[4].cget('text') == 'X') & (label_list[5].cget('text') == 'X'):
  22.         label_list[3].config(bg='green')
  23.         label_list[4].config(bg='green')
  24.         label_list[5].config(bg='green')
  25.         show_winner('X')
  26.     elif (label_list[6].cget('text') == 'X') & (label_list[7].cget('text') == 'X') & (label_list[8].cget('text') == 'X'):
  27.         label_list[6].config(bg='green')
  28.         label_list[7].config(bg='green')
  29.         label_list[8].config(bg='green')
  30.         show_winner('X')
  31.     elif (label_list[0].cget('text') == 'X') & (label_list[3].cget('text') == 'X') & (label_list[6].cget('text') == 'X'):
  32.         label_list[0].config(bg='green')
  33.         label_list[3].config(bg='green')
  34.         label_list[6].config(bg='green')
  35.         show_winner('X')
  36.     elif (label_list[1].cget('text') == 'X') & (label_list[4].cget('text') == 'X') & (label_list[7].cget('text') == 'X'):
  37.         label_list[1].config(bg='green')
  38.         label_list[4].config(bg='green')
  39.         label_list[7].config(bg='green')
  40.         show_winner('X')
  41.     elif (label_list[2].cget('text') == 'X') & (label_list[5].cget('text') == 'X') & (label_list[8].cget('text') == 'X'):
  42.         label_list[2].config(bg='green')
  43.         label_list[5].config(bg='green')
  44.         label_list[8].config(bg='green')
  45.         show_winner('X')
  46.     elif (label_list[0].cget('text') == 'X') & (label_list[4].cget('text') == 'X') & (label_list[8].cget('text') == 'X'):
  47.         label_list[0].config(bg='green')
  48.         label_list[4].config(bg='green')
  49.         label_list[8].config(bg='green')
  50.         show_winner('X')
  51.     elif (label_list[2].cget('text') == 'X') & (label_list[4].cget('text') == 'X') & (label_list[6].cget('text') == 'X'):
  52.         label_list[2].config(bg='green')
  53.         label_list[4].config(bg='green')
  54.         label_list[6].config(bg='green')
  55.         show_winner('X')
  56.  
  57.     elif (label_list[0].cget('text') == 'O') & (label_list[1].cget('text') == 'O') & (label_list[2].cget('text') == 'O'):
  58.         label_list[0].config(bg='green')
  59.         label_list[1].config(bg='green')
  60.         label_list[2].config(bg='green')
  61.         show_winner('O')
  62.     elif (label_list[3].cget('text') == 'O') & (label_list[4].cget('text') == 'O') & (label_list[5].cget('text') == 'O'):
  63.         label_list[3].config(bg='green')
  64.         label_list[4].config(bg='green')
  65.         label_list[5].config(bg='green')
  66.         show_winner('O')
  67.     elif (label_list[6].cget('text') == 'O') & (label_list[7].cget('text') == 'O') & (label_list[8].cget('text') == 'O'):
  68.         label_list[6].config(bg='green')
  69.         label_list[7].config(bg='green')
  70.         label_list[8].config(bg='green')
  71.         show_winner('O')
  72.     elif (label_list[0].cget('text') == 'O') & (label_list[3].cget('text') == 'O') & (label_list[6].cget('text') == 'O'):
  73.         label_list[0].config(bg='green')
  74.         label_list[3].config(bg='green')
  75.         label_list[6].config(bg='green')
  76.         show_winner('O')
  77.     elif (label_list[1].cget('text') == 'O') & (label_list[4].cget('text') == 'O') & (label_list[7].cget('text') == 'O'):
  78.         label_list[1].config(bg='green')
  79.         label_list[4].config(bg='green')
  80.         label_list[7].config(bg='green')
  81.         show_winner('O')
  82.     elif (label_list[2].cget('text') == 'O') & (label_list[5].cget('text') == 'O') & (label_list[8].cget('text') == 'O'):
  83.         label_list[2].config(bg='green')
  84.         label_list[5].config(bg='green')
  85.         label_list[8].config(bg='green')
  86.         show_winner('O')
  87.     elif (label_list[0].cget('text') == 'O') & (label_list[4].cget('text') == 'O') & (label_list[8].cget('text') == 'O'):
  88.         label_list[0].config(bg='green')
  89.         label_list[4].config(bg='green')
  90.         label_list[8].config(bg='green')
  91.         show_winner('O')
  92.     elif (label_list[2].cget('text') == 'O') & (label_list[4].cget('text') == 'O') & (label_list[6].cget('text') == 'O'):
  93.         label_list[2].config(bg='green')
  94.         label_list[4].config(bg='green')
  95.         label_list[6].config(bg='green')
  96.         show_winner('O')
  97.  
  98.     elif (
  99.         ((label_list[0].cget('text') == 'O') or (label_list[0].cget('text') == 'X')) and
  100.         ((label_list[1].cget('text') == 'O') or (label_list[1].cget('text') == 'X')) and
  101.         ((label_list[2].cget('text') == 'O') or (label_list[2].cget('text') == 'X')) and
  102.         ((label_list[3].cget('text') == 'O') or (label_list[3].cget('text') == 'X')) and
  103.         ((label_list[4].cget('text') == 'O') or (label_list[4].cget('text') == 'X')) and
  104.         ((label_list[5].cget('text') == 'O') or (label_list[5].cget('text') == 'X')) and
  105.         ((label_list[6].cget('text') == 'O') or (label_list[6].cget('text') == 'X')) and
  106.         ((label_list[7].cget('text') == 'O') or (label_list[7].cget('text') == 'X')) and
  107.         ((label_list[8].cget('text') == 'O') or (label_list[8].cget('text') == 'X'))
  108.     ):
  109.         show_winner('No one')
  110.  
  111.  
  112. # Show winner window
  113. def show_winner(winner):
  114.     messagebox.showinfo(title='Game is finished', message=f'{winner} has won the game.')
  115.     root.destroy()
  116.  
  117.  
  118. # If pressed, change label to X and make buttons disappear
  119. def hide_button_x(n, label,):
  120.     button_list_x[n].pack_forget()
  121.     button_list_o[n].pack_forget()
  122.     for m in range(len(label_list)):
  123.         if label == label_list[m]:
  124.             label_text_list[m].set('X')
  125.         button_list_x[m]['state'] = 'disabled'
  126.         button_list_o[m]['state'] = 'normal'
  127.     is_won()
  128.  
  129.  
  130. # If pressed, change label to O and make buttons disappear
  131. def hide_button_o(n, label):
  132.     button_list_x[n].pack_forget()
  133.     button_list_o[n].pack_forget()
  134.     for m in range(len(label_list)):
  135.         if label == label_list[m]:
  136.             label_text_list[m].set('O')
  137.         button_list_x[m]['state'] = 'normal'
  138.         button_list_o[m]['state'] = 'disabled'
  139.     is_won()
  140.  
  141.  
  142. root = Tk()
  143.  
  144.  
  145. # Menubar to Quit the game
  146. menuBar = Menu(root)
  147. root.config(menu=menuBar)
  148.  
  149. subMenuFile = Menu(menuBar, tearoff=0)
  150. menuBar.add_cascade(label='File', menu=subMenuFile)
  151. subMenuFile.add_command(label='Quit', command=quit_application)
  152.  
  153.  
  154. # Creating a 3x3 grid
  155. frame_list = []
  156. for i in range(9):
  157.     frame_list.append(Frame(root))
  158. i = 0
  159. for j in range(3):
  160.     for k in range(3):
  161.         frame_list[i].grid(row=j, column=k)
  162.         i += 1
  163.         pass
  164.     pass
  165.  
  166.  
  167. # Label text to display 'X' or 'O'
  168. label_text_list = []
  169. for i in range(9):
  170.     label_text_list.append(StringVar())
  171.     label_text_list[i].set('?')
  172.  
  173.  
  174. # Pack label to grid, above buttons
  175. label_list = []
  176. for i in range(9):
  177.     label_list.append(Label(frame_list[i], textvariable=label_text_list[i]))
  178.     for j in range(0, 2):
  179.         for k in range(0, 2):
  180.             label_list[i].pack(side='top')
  181.  
  182.  
  183. button_list_x = []
  184. button_list_o = []
  185.  
  186. # Buttons to change label to X in grid
  187. for i in range(9):
  188.     button_list_x.append(
  189.         Button(
  190.             frame_list[i],
  191.             text='X',
  192.             command=partial(hide_button_x, i, label_list[i]),
  193.             state='normal'
  194.         )
  195.     )
  196.     button_list_x[i].pack(side='left')
  197.  
  198.  
  199. # Buttons to chance label to O in grid
  200. for i in range(9):
  201.     button_list_o.append(
  202.         Button(
  203.             frame_list[i],
  204.             text='O',
  205.             command=partial(hide_button_o, i, label_list[i]),
  206.             state='disabled'
  207.             )
  208.     )
  209.     button_list_o[i].pack(side='right')
  210.  
  211. # Create the window
  212. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment