-
Notifications
You must be signed in to change notification settings - Fork 5
/
solver.py
314 lines (258 loc) · 10.6 KB
/
solver.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import pygame
import sys
from copy import deepcopy
class Board:
def __init__(self):
self.cells = [[None for _ in range(9)] for _ in range(9)]
self.boxes = [Box() for _ in range(9)]
self.lines = [Line() for _ in range(18)]
for heightIdx in range(9):
for widthIdx in range(9):
boxIndex = int(widthIdx / 3) + 3 * int(heightIdx / 3)
verticalLineIndex = widthIdx
horizontalLineIndex = heightIdx + 9
newCell = Cell(self.boxes[boxIndex], self.lines[verticalLineIndex],
self.lines[horizontalLineIndex], (heightIdx, widthIdx))
self.cells[heightIdx][widthIdx] = newCell
self.boxes[boxIndex].cells.append(newCell)
self.lines[verticalLineIndex].cells.append(newCell)
self.lines[horizontalLineIndex].cells.append(newCell)
def isValid(self, what, where, values):
cellInQuestion = self.cells[where[0]][where[1]]
return cellInQuestion.isValid(what, values)
def solve(self, values):
global stop
global boardToDraw
if stop:
return None
boardToDraw = values
checkInput()
pygame.time.wait(waitTimeInMs)
drawBoard()
firstUnsolvedAddress = (-1, -1)
for i in range(9):
for j in range(9):
if values[i][j] == 0:
firstUnsolvedAddress = (i, j)
break
if firstUnsolvedAddress[0] != -1:
break
if firstUnsolvedAddress[0] == -1:
return values
for valueToTry in range(1, 10):
if self.isValid(valueToTry, firstUnsolvedAddress, values):
newValues = deepcopy(values)
newValues[firstUnsolvedAddress[0]][firstUnsolvedAddress[1]] = valueToTry
colourCell((125,125,125),firstUnsolvedAddress)
result = self.solve(newValues)
if result:
return result
return None
class Box:
def __init__(self):
self.cells = []
class Cell:
def __init__(self, box, lineV, lineH, pos):
self.box = box
self.lineV = lineV
self.lineH = lineH
self.pos = pos
def isValid(self, what, values):
for neighCell in self.box.cells:
if what == values[neighCell.pos[0]][neighCell.pos[1]]:
return False
for neighCell in self.lineV.cells:
if what == values[neighCell.pos[0]][neighCell.pos[1]]:
return False
for neighCell in self.lineH.cells:
if what == values[neighCell.pos[0]][neighCell.pos[1]]:
return False
return True
class Line:
def __init__(self):
self.cells = []
def checkInput():
global white
global black
global boardToDraw
global selectedSpace
global highlight
global valuesOnTheBoard
global gameBoard
global stop
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYUP:
if event.key == pygame.K_d:
white, black = black, white
if event.key == pygame.K_r:
valuesOnTheBoard = [[0 for _ in range(9)] for _ in range(9)]
boardToDraw = valuesOnTheBoard
stop = True
if event.key == pygame.K_SPACE:
stop = False
gameBoard.solve(valuesOnTheBoard)
highlight = None
if event.key == pygame.K_1 and selectedSpace != (-1, -1):
if gameBoard.isValid(1, selectedSpace, valuesOnTheBoard):
valuesOnTheBoard[selectedSpace[0]][selectedSpace[1]] = 1
boardToDraw = valuesOnTheBoard
selectedSpace = (-1, -1)
highlight = None
else:
flashCell(selectedSpace)
if event.key == pygame.K_2 and selectedSpace != (-1, -1):
if gameBoard.isValid(2, selectedSpace, valuesOnTheBoard):
valuesOnTheBoard[selectedSpace[0]][selectedSpace[1]] = 2
boardToDraw = valuesOnTheBoard
selectedSpace = (-1, -1)
highlight = None
else:
flashCell(selectedSpace)
if event.key == pygame.K_3 and selectedSpace != (-1, -1):
if gameBoard.isValid(3, selectedSpace, valuesOnTheBoard):
valuesOnTheBoard[selectedSpace[0]][selectedSpace[1]] = 3
boardToDraw = valuesOnTheBoard
selectedSpace = (-1, -1)
highlight = None
else:
flashCell(selectedSpace)
if event.key == pygame.K_4 and selectedSpace != (-1, -1):
if gameBoard.isValid(4, selectedSpace, valuesOnTheBoard):
valuesOnTheBoard[selectedSpace[0]][selectedSpace[1]] = 4
boardToDraw = valuesOnTheBoard
selectedSpace = (-1, -1)
highlight = None
else:
flashCell(selectedSpace)
if event.key == pygame.K_5 and selectedSpace != (-1, -1):
if gameBoard.isValid(5, selectedSpace, valuesOnTheBoard):
valuesOnTheBoard[selectedSpace[0]][selectedSpace[1]] = 5
boardToDraw = valuesOnTheBoard
selectedSpace = (-1, -1)
highlight = None
else:
flashCell(selectedSpace)
if event.key == pygame.K_6 and selectedSpace != (-1, -1):
if gameBoard.isValid(6, selectedSpace, valuesOnTheBoard):
valuesOnTheBoard[selectedSpace[0]][selectedSpace[1]] = 6
boardToDraw = valuesOnTheBoard
selectedSpace = (-1, -1)
highlight = None
else:
flashCell(selectedSpace)
if event.key == pygame.K_7 and selectedSpace != (-1, -1):
if gameBoard.isValid(7, selectedSpace, valuesOnTheBoard):
valuesOnTheBoard[selectedSpace[0]][selectedSpace[1]] = 7
boardToDraw = valuesOnTheBoard
selectedSpace = (-1, -1)
highlight = None
else:
flashCell(selectedSpace)
if event.key == pygame.K_8 and selectedSpace != (-1, -1):
if gameBoard.isValid(8, selectedSpace, valuesOnTheBoard):
valuesOnTheBoard[selectedSpace[0]][selectedSpace[1]] = 8
boardToDraw = valuesOnTheBoard
selectedSpace = (-1, -1)
highlight = None
else:
flashCell(selectedSpace)
if event.key == pygame.K_9 and selectedSpace != (-1, -1):
if gameBoard.isValid(9, selectedSpace, valuesOnTheBoard):
valuesOnTheBoard[selectedSpace[0]][selectedSpace[1]] = 9
boardToDraw = valuesOnTheBoard
selectedSpace = (-1, -1)
highlight = None
else:
flashCell(selectedSpace)
if event.type == pygame.MOUSEBUTTONUP:
stop = True
boardToDraw = valuesOnTheBoard
selectedSpace = getCellCoordsForScreenCoords(event.pos)
colourCell(green, selectedSpace)
def colourCell(withColour, cell):
global offH
global offW
global highlight
global highlightColour
cellWidth = int(width / 9)
highlight = pygame.Rect(offW + cell[1] * cellWidth, offH + cell[0] * cellWidth, cellWidth, cellWidth)
highlightColour = withColour
def flashCell(cellNumber):
for i in range(5):
colourCell(red, cellNumber)
drawBoard()
pygame.time.wait(10)
colourCell(white, cellNumber)
drawBoard()
pygame.time.wait(10)
colourCell(green, cellNumber)
drawBoard()
def getCellCoordsForScreenCoords(pos):
global offH
global offW
cellWidth = int(width / 9)
x = int((pos[1] - offW) / cellWidth)
y = int((pos[0] - offH) / cellWidth)
return x, y
def getScreenCoordsForCell(cellPos):
global offH
global offW
cellWidth = int(width / 9)
x = offH + cellWidth * cellPos[1] + int(cellWidth / 4)
y = offW + cellWidth * cellPos[0] + int(cellWidth / 4)
return x, y
def drawBoard():
global offW
global offH
screen.fill(white)
maxW = int(width / 9) * 9
maxH = int(height / 9) * 9
offW = int((width - maxW) / 2)
offH = int((height - maxH) / 2)
if highlight:
pygame.draw.rect(screen, highlightColour, highlight)
for i in range(0, 10):
if i % 3 == 0:
intensity = 3
else:
intensity = 1
pygame.draw.line(screen, black, (offW + int(width / 9) * i, offH), (offW + int(width / 9) * i, offH + maxH),
intensity)
pygame.draw.line(screen, black, (offW, offH + int(height / 9) * i), (offW + maxW, offH + int(height / 9) * i),
intensity)
myfont = pygame.font.Font("Quino.otf", int(0.1 * height))
for i in range(9):
for j in range(9):
if str(boardToDraw[i][j]) == "0":
charToDraw = ""
else:
charToDraw = str(boardToDraw[i][j])
label = myfont.render(charToDraw, 1, black)
screen.blit(label, getScreenCoordsForCell((i, j)))
pygame.display.flip()
pygame.init()
programIcon = pygame.image.load('icon.png')
pygame.display.set_icon(programIcon)
with open("settings.txt") as settingsFile:
for line in settingsFile:
lineSplitUp = line.split(" ")
if lineSplitUp[0] == "sleepTimeBetweenMoves":
waitTimeInMs = int(lineSplitUp[2])
if lineSplitUp[0] == "resolution":
size = width, height = int(lineSplitUp[2]), int(lineSplitUp[2])
white = (255, 255, 255)
black = (0, 0, 0)
red = (255, 0, 0)
green = (0, 255, 0)
highlight = None
stop = False
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Sudoku Solver by Tymscar")
gameBoard = Board()
valuesOnTheBoard = [[0 for _ in range(9)] for _ in range(9)]
boardToDraw = valuesOnTheBoard
while True:
checkInput()
drawBoard()