-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
188 lines (166 loc) · 6.56 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""This is the main file of the program. It contains all of the GUI
implementation (aside from what is implemented in sudokusolver.kv.)
"""
# Set the default size of the application window
from kivy.config import Config
Config.set('graphics', 'width', '600')
Config.set('graphics', 'height', '660')
from kivy.app import App
from kivy.uix.gridlayout import GridLayout
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.relativelayout import RelativeLayout
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.popup import Popup
from kivy.uix.button import Button
from kivy.properties import StringProperty
from kivy.graphics import Line, Color
from sudoku import sudoku
class SudokuSolverApp(App):
"""Main application"""
def build(self):
return MainLayout()
class MainLayout(BoxLayout):
"""Parent layout of all the gui items"""
# This layout is defined in the .kv file.
pass
class CellGrid(GridLayout):
"""Sudoku board that is displayed to the user"""
def __init__(self, board=None ,**kwargs):
super().__init__(**kwargs)
# Create the board object
self.board = sudoku.Board(board)
# Create all of the CellBox objects, one for each cell in self.board
for cell in self.board.all_cells:
self.add_widget(CellBox(cell))
# If board was passed, we need to update the CellBoxes to show those
# values
self._update_grid(False)
# Create 4 divider lines. No points specified because they will be
# updated by self._update_dividers
with self.canvas.after:
self.dividers = []
Color(0,0,0,1)
for i in range(4):
self.dividers.append(Line(width = 4, cap = 'none'))
# Call self._update_dividers whenever CellGrid is resized or moved
self.bind(size = self._update_dividers)
self.bind(x = self._update_dividers)
self.bind(y = self._update_dividers)
def _update_dividers(self, inst, value):
"""Called whenever CellGrid is resized or moved, defines points of the
divider lines
"""
top = self.y + self.height
bottom = self.y
right = self.x + self.width
left = self.x
# Vertical lines
for i, div in enumerate(self.dividers[:2], 1):
x = left + ((i/3) * self.width)
div.points = [x, top, x, bottom]
# Horizontal lines
for i, div in enumerate(self.dividers[2:], 1):
y = bottom + ((i/3) * self.height)
div.points = [left, y, right, y]
def _update_grid(self, show_poss = True):
"""Checks if cell objects have a value, if so, update the text of the
CellBox
"""
for cell_box in self.children:
# If the cell has a value, display it
if cell_box.cell.value:
cell_box.text = str(cell_box.cell.value)
cell_box.poss_disp.text = ''
# If cell doesn't have a value, clear its display
else:
cell_box.text = ''
if show_poss:
cell_box.poss_disp.text = str(cell_box.cell.possibilities)
def on_solve_click(self):
"""Processes user inputs and calls the solve method on the puzzle"""
# Take the values entered by the user into the TextInputs and put them
# in the actual cell objects
for cell_box in self.children:
value = self._sanitize(cell_box.text)
cell_box.cell.set_value(value)
# This accounts for cells out of which the value was deleted by user
if not value:
cell_box.cell.reset()
# Solve the board and store there success state
solved = self.board.solve()
self._update_grid()
# If the board wasn't solved, show the error popup
if not solved:
popup = ErrorPopup()
popup.open()
def on_reset_click(self):
"""Clears all of the values from the display and resets the board
object
"""
for cell in self.children:
# Reset the actual cell object
cell.cell.reset()
self._update_grid(False)
def _sanitize(self, input):
"""Sanitizes user input from the sudoku board grid"""
# Cut off all but the first char of the input
if input:
input = input[0]
# If possible, convert to int
try:
input = int(input)
except:
return None
# Don't accept zeros
if 0 < input < 10:
return input
else:
return None
class CellBox(RelativeLayout):
"""This is a wrapper for the CellBoxText and the possibilities overlay
label
"""
possibilities_property = StringProperty('')
def __init__(self, cell, **kwargs):
super().__init__(**kwargs)
# TextInput widget that displays the value and takes input
self.text_input = CellBoxText(cell)
self.add_widget(self.text_input)
# Label widget to display the possibilities if the value is unknown.
# This object is not used in the current implementation.
self.possibilities = self.possibilities_property
self.poss_disp = (Label(text = self.possibilities,
color = [0,0,0,.8],
text_size = self.size,
halign = 'center',
valign = 'middle',
padding = [15, 15]
))
# This line is commented out to disable the poss_disp overlay.
# self.add_widget(self.poss_disp)
# Point references to cell and text into the TextInput child object
@property
def cell(self):
return self.text_input.cell
@cell.setter
def cell(self, value):
self.text_input.cell = value
@property
def text(self):
return self.text_input.text
@text.setter
def text(self, value):
self.text_input.text = value
class CellBoxText(TextInput):
"""Text Input and display for the cell's value"""
text_property = StringProperty('')
def __init__(self, cell, **kwargs):
super().__init__(**kwargs)
self.cell = cell
self.text = self.text_property
class ErrorPopup(Popup):
# This popup is defined in the .kv file.
pass
if __name__ == "__main__":
SudokuSolverApp().run()