Advertisement
Guest User

Tic Tac Toe

a guest
Jan 23rd, 2020
1,634
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.12 KB | None | 0 0
  1. from kivy.app import App
  2.  
  3. from kivy.uix.boxlayout import BoxLayout
  4. from kivy.uix.gridlayout import GridLayout
  5.  
  6. from kivy.uix.button import Button
  7. from kivy.config import Config
  8.  
  9. Config.set("graphics","resizable","0")
  10. Config.set("graphics","width","300")
  11. Config.set("graphics","height","300")
  12.  
  13.  
  14. class MainApp(App):
  15.     def __init__(self):
  16.         super().__init__()
  17.         self.switch = True
  18.  
  19.     def tic_tac_toe(self, arg):
  20.         arg.disabled = True
  21.         arg.text = 'X' if self.switch else 'O'
  22.         self.switch = not self.switch
  23.  
  24.         coordinate = (
  25.             (0,1,2), (3,4,5), (6,7,8), # X
  26.             (0,3,6), (1,4,7), (2,5,8), # Y
  27.             (0,4,8), (2,4,6),          # D
  28.         )
  29.  
  30.         vector = lambda item: [self.buttons[x].text for x in item]
  31.  
  32.         color = [0,1,0,1]
  33.  
  34.         for item in coordinate:
  35.             if vector(item).count('X') == 3 or vector(item).count('O') == 3:
  36.                 win = True
  37.                 for i in item:
  38.                     self.buttons[i].color = color
  39.                 for button in self.buttons:
  40.                     button.disabled = True
  41.                 break
  42.  
  43.     def restart(self, arg):
  44.         self.switch = True
  45.  
  46.         for button in self.buttons:
  47.             button.color = [0,0,0,1]
  48.             button.text = ""
  49.             button.disabled = False
  50.  
  51.     def build(self):
  52.         self.title = "Крестики-нолики"
  53.  
  54.         root = BoxLayout(orientation="vertical", padding=5)
  55.  
  56.         grid = GridLayout(cols=3)
  57.         self.buttons = []
  58.         for _ in range(9):
  59.             button = Button(
  60.                 color = [0,0,0,1],
  61.                 font_size = 24,
  62.                 disabled = False,
  63.                 on_press = self.tic_tac_toe
  64.             )
  65.             self.buttons.append(button)
  66.             grid.add_widget(button)
  67.  
  68.         root.add_widget(grid)
  69.  
  70.         root.add_widget(
  71.             Button(
  72.                text = "Restart",
  73.                size_hint = [1,.1],
  74.                on_press = self.restart
  75.             )
  76.         )
  77.  
  78.         return root
  79.  
  80.  
  81. if __name__ == "__main__":
  82.     MainApp().run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement