-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtic_tac_toe.py
143 lines (119 loc) · 3.82 KB
/
tic_tac_toe.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from kivy.app import App
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.config import Config
Config.set("graphics", "resizable", "0")
Config.set("graphics", "width", "300")
Config.set("graphics", "height", "300")
choice = ['X', 'O']
switch = 0
class MainApp(App):
def tic_tac_toe(self, arg):
global switch
arg.disabled = True
arg.text = choice[switch]
if not switch:
switch = 1
else:
switch = 0
coordinate = (
(0, 1, 2), (3, 4, 5), (6, 7, 8), # X
(0, 3, 6), (1, 4, 7), (2, 5, 8), # Y
(0, 4, 8), (2, 4, 6), # D
)
vector = (
[self.button[x].text for x in (0, 1, 2)],
[self.button[x].text for x in (3, 4, 5)],
[self.button[x].text for x in (6, 7, 8)],
[self.button[y].text for y in (0, 3, 6)],
[self.button[y].text for y in (1, 4, 7)],
[self.button[y].text for y in (2, 5, 8)],
[self.button[d].text for d in (0, 4, 8)],
[self.button[d].text for d in (2, 4, 6)],
)
win = False
color = [0, 1, 0, 1] # Green
for index in range(8):
if vector[index].count('X') == 3 \
or vector[index].count('O') == 3:
win = True
for i in coordinate[index]:
self.button[i].color = color
break
if win:
for index in range(9):
self.button[index].disabled = True
def restart(self, arg):
global switch
switch = 0
for index in range(9):
self.button[index].color = [0, 0, 0, 1]
self.button[index].text = ""
self.button[index].disabled = False
def build(self):
self.title = "Крестики-нолики"
root = BoxLayout(orientation="vertical", padding=5)
grid = GridLayout(cols=3)
self.button = [0 for _ in range(9)]
for index in range(9):
self.button[index] = Button(
color=[0, 0, 0, 1],
font_size=24,
disabled=False,
on_press=self.tic_tac_toe
)
grid.add_widget(self.button[index])
root.add_widget(grid)
root.add_widget(
Button(
text="Restart",
size_hint=[1, .1],
on_press=self.restart
)
)
return root
if __name__ == "__main__":
MainApp().run()
# from kivy.app import App
# from kivy.uix.boxlayout import BoxLayout
#
# from kivy.uix.button import Button
#
#
# class LabeWidget(BoxLayout):
# def clicker_func(self, instance):
# print(instance.text + 'was clicked')
#
# def __init__(self, **kwargs):
# super().__init__(**kwargs)
# b1 = Button(text="Кнопка 1")
# b2 = Button(text="Кнопка 2")
# b3 = Button(text="Кнопка 3")
# b1.bind(on_press=self.clicker_func)
# b2.bind(on_press=self.clicker_func)
# b3.bind(on_press=self.clicker_func)
# self.add_widget(b1)
# self.add_widget(b2)
# self.add_widget(b3)
#
# box_layout = BoxLayout(orientation='vertical')
# a_b1 = Button(text="BOX 1")
# a_b2 = Button(text="BOXS 2")
# a_b3 = Button(text="BOXING 3")
# a_b1.bind(on_press=self.clicker_func)
# a_b2.bind(on_press=self.clicker_func)
# a_b3.bind(on_press=self.clicker_func)
# box_layout.add_widget(a_b1)
# box_layout.add_widget(a_b2)
# box_layout.add_widget(a_b3)
# self.add_widget(box_layout)
#
#
# class LabApp(App):
# def build(self):
# return LabeWidget()
#
#
# if __name__ == '__main__':
# LabApp().run()