-
Notifications
You must be signed in to change notification settings - Fork 0
/
animations.py
354 lines (260 loc) · 9.82 KB
/
animations.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
"""LEGACY GUI application. This was used to make illustrations for the Matura Project.
Basic and advanced Backtracking is Implemented."""
import pygame
import sys
from multiprocessing.pool import ThreadPool
from newBA import backtrackNew as backNew
from newDancesolver import dancingLinks
from config import activeConfig as c, Temp as t, Solvertype
# from OPBASolver import solveadv
from newOPBA import bfs
import XSolver
import makesud
# ***************Testing Config****************
# from testGrids import Grids
# t.currGrid = copy.deepcopy(Grids.grid9x9_1)
# t.currGrid = XSolver.exact_to_matrix(9, makesud.make_sudoku())
t.currGrid = [[3, 4, 0, 0, 1, 0, 9, 0, 0], [0, 0, 1, 0, 0, 4, 0, 8, 3], [5, 0, 0, 0, 0, 0, 0, 1, 0], [9, 1, 0, 0, 5, 0, 0, 0, 0], [0, 6, 4, 0, 0, 0, 1, 3, 0], [0, 0, 0, 0, 8, 0, 0, 4, 9], [0, 8, 0, 0, 0, 0, 0, 0, 2], [2, 3, 0, 9, 0, 0, 4, 0, 0], [0, 0, 9, 0, 4, 0, 0, 5, 8]]
# print(t.currGrid)
drawingselector = True
# ****************End of Config****************
def run():
setup()
mainloop()
def mainloop():
while t.pygameActive:
for event in pygame.event.get():
eventHandler(event)
draw(t.currGrid)
pygame.quit()
def resetGrid():
# t.currGrid = XSolver.exact_to_matrix(9, makesud.make_sudoku())
t.currGrid = [[3, 4, 0, 0, 1, 0, 9, 0, 0], [0, 0, 1, 0, 0, 4, 0, 8, 3], [5, 0, 0, 0, 0, 0, 0, 1, 0], [9, 1, 0, 0, 5, 0, 0, 0, 0], [0, 6, 4, 0, 0, 0, 1, 3, 0], [0, 0, 0, 0, 8, 0, 0, 4, 9], [0, 8, 0, 0, 0, 0, 0, 0, 2], [2, 3, 0, 9, 0, 0, 4, 0, 0], [0, 0, 9, 0, 4, 0, 0, 5, 8]]
# print(findPossi(t.currGrid))
def setup():
global root, font, running
pygame.init()
t.pygameActive=True
pygame.display.set_caption("Sudoku")
root = pygame.display.set_mode(
(c.resolutionField, c.resolutionField+c.spacebelowinPX))
root.fill((250, 250, 250))
font = pygame.font.SysFont(None, c.resolutionField//c.basesize**2)
t.selector_pos = [0, 0] # (Row,Col)
t.given = []
running = True
t.pygameActive=True
# setupconfig()
def setConfig(basesize=3, resolutionField=900, spacebelowinPX=100, displayinHexa=False, sleeptime=0):
c.basesize=basesize
c.resolutionField=resolutionField
c.spacebelowinPX=spacebelowinPX
c.displayinHexa=displayinHexa
c.sleeptime=sleeptime
return c
def draw_field():
for i in range(c.basesize**2+1):
pygame.draw.line(root, (0, 0, 0), (i*c.resolutionField//(c.basesize**2), 0),
(i*c.resolutionField//(c.basesize**2), c.resolutionField), 2)
pygame.draw.line(root, (0, 0, 0), (0, i*c.resolutionField//(c.basesize**2)),
(c.resolutionField, i*c.resolutionField//(c.basesize**2)), 2)
for i in range(c.basesize+1):
pygame.draw.line(root, (0, 0, 0), (0, i*c.resolutionField//(c.basesize)),
(c.resolutionField, i*c.resolutionField//(c.basesize)), 5)
pygame.draw.line(root, (0, 0, 0), (i*c.resolutionField//(c.basesize), 0),
(i*c.resolutionField//(c.basesize), c.resolutionField), 5)
def draw_num(grid):
if len(grid) != c.basesize**2:
print(
f"Expected Sudoku with side length {c.basesize**2}. Instead got a Sudoku with a side length of {len(grid)}")
print("Please check your config!")
print("Exiting now!")
global running
running = False
pygame.quit()
sys.exit()
return False
for row in range(0, c.basesize**2):
for col in range(0, c.basesize**2):
num = grid[row][col]
if num != 0:
if c.displayinHexa:
num = hex(num).split('x')[-1]
text = font.render(str(num), True, (0, 0, 0), (250, 250, 250))
textRe = text.get_rect()
textRect = textRe.move((row*c.resolutionField//(c.basesize**2)+(int(c.resolutionField/(
c.basesize**3*1.1))), (col*c.resolutionField//(c.basesize**2)+(c.resolutionField//(c.basesize**3*2)))))
root.blit(text, textRect)
def draw_footer():
pygame.draw.line(root, (0, 0, 0), (0, c.resolutionField),
(c.resolutionField, c.resolutionField), 10)
text = font.render(Solvertype.solvers[c.currsolverindex], True, (0, 0, 0), (250, 250, 250))
textRe = text.get_rect()
textRect = textRe.move((0,c.resolutionField+round(c.spacebelowinPX/3)))
root.blit(text, textRect)
def draw_selector():
row = t.selector_pos[0]
col = t.selector_pos[1]
selRect = pygame.Rect(col*c.resolutionField//c.basesize**2, row*c.resolutionField//c.basesize**2,
c.resolutionField//c.basesize**2, c.resolutionField//c.basesize**2)
pygame.draw.rect(root, (200, 0, 0), selRect, 5)
def draw(grid):
root.fill((250, 250, 250))
draw_field()
draw_num(grid)
draw_footer()
if drawingselector:
draw_selector()
pygame.display.update()
def move_selector(row_delta, col_delta):
row_old = t.selector_pos[0]
col_old = t.selector_pos[1]
if(0 <= row_old-row_delta < c.basesize**2):
t.selector_pos[0] = row_old-row_delta
if(0 <= col_old+col_delta < c.basesize**2):
t.selector_pos[1] = col_old+col_delta
def setSelector(row, col):
t.selector_pos[0] = int(row)
t.selector_pos[1] = int(col)
def changeselected(tonum):
t.currGrid[t.selector_pos[1]][t.selector_pos[0]] = tonum
def mouseSelect():
pos = pygame.mouse.get_pos()
x_mouse = pos[0]
y_mouse = pos[1]
if y_mouse > c.resolutionField-5 or x_mouse > c.resolutionField-5:
return
col = x_mouse//(c.resolutionField//c.basesize**2)
row = y_mouse//(c.resolutionField//c.basesize**2)
setSelector(row, col)
def givenChecker(grid):
global given
given = []
for ro, row in enumerate(grid):
for co, col in enumerate(row):
if col != 0:
given.append((ro, co))
# def guiSolve(bo):
# solutions = []
# # solve(t.currGrid, sols=solutions)
# solveadv(t.currGrid, sols=solutions)
# bo = solutions[0]
# print("Solved")
# print(bo)
# if len(solutions) == 0:
# print("There was no solution!")
# return bo
# return solutions[0]
def setCurrGrid(ret):
# global currGrid
# t.currGrid = ret
pass
def solveselect(curr,abc=None):
c.solver=Solvertype.solvers[c.currsolverindex]
if c.solver==Solvertype.backnorm:
# return solve(curr,abc)
return backNew(curr)
elif c.solver==Solvertype.backadv:
# return solveadv(curr,abc)
return bfs(curr)
elif c.solver==Solvertype.dancinglinks:
# return solveadv(curr,abc)
return dancingLinks(curr)
else:
print("Not implemented at the Moment. \n Coming soon!")
return t.currGrid
def solveThread(curr):
pool = ThreadPool(processes=1)
_ = pool.apply_async(
solveselect, (curr,c), callback=setCurrGrid)
def changeSolver(dir):
newindex= c.currsolverindex+dir
if newindex>len(Solvertype.solvers)-1:
newindex+=-len(Solvertype.solvers)
if newindex<0:
newindex+=len(Solvertype.solvers)
c.currsolverindex=newindex
# Eventhandling
def modifysleeptime(dirr):
newtime = c.sleeptime + dirr
if newtime>=0:
c.sleeptime = newtime
else:
c.sleeptime = 0
pygame.display.set_caption(f"Sudoku Sleeptime: {c.sleeptime:.2f}s")
def toggledrawingselector():
global drawingselector
drawingselector = not drawingselector
def eventHandler(event):
if event.type == pygame.QUIT:
global running
running = False
t.pygameActive=False
pygame.quit()
sys.exit()
controlls(event)
def controlls(event):
if event.type == pygame.KEYDOWN:
# Move Selector
if event.key == pygame.K_UP:
move_selector(1, 0)
if event.key == pygame.K_DOWN:
move_selector(-1, 0)
if event.key == pygame.K_RIGHT:
move_selector(0, 1)
if event.key == pygame.K_LEFT:
move_selector(0, -1)
if event.key == pygame.K_m:
# start_new_thread(confguirun,())
# t1 = threading.Thread(target=confguirun)
# t1.start()
pass
if event.key == pygame.K_r:
resetGrid()
if event.key == pygame.K_LCTRL:
changeSolver(-1)
if event.key == pygame.K_LSHIFT:
changeSolver(1)
if event.key == pygame.K_COMMA:
modifysleeptime(-0.02)
if event.key == pygame.K_PERIOD:
modifysleeptime(0.02)
if event.key == pygame.K_u:
toggledrawingselector()
# Output current grid in console
if event.key == pygame.K_p:
print(t.currGrid)
if event.key == pygame.K_i:
print("Saving to disk")
pygame.image.save(root, f"Image 111.png")
# pooll = ThreadPool(processes=1)
# _ = pooll.apply_async(confguirun, ())
# Num input
# TODO Input for numbers
if event.key == pygame.K_1:
changeselected(1)
if event.key == pygame.K_2:
changeselected(2)
if event.key == pygame.K_3:
changeselected(3)
if event.key == pygame.K_4:
changeselected(4)
if event.key == pygame.K_5:
changeselected(5)
if event.key == pygame.K_6:
changeselected(6)
if event.key == pygame.K_7:
changeselected(7)
if event.key == pygame.K_8:
changeselected(8)
if event.key == pygame.K_9:
changeselected(9)
if event.key == pygame.K_DELETE or event.key == pygame.K_0:
changeselected(0)
#
if event.key == pygame.K_s:
solveThread(t.currGrid)
if event.type == pygame.MOUSEBUTTONDOWN:
mouseSelect()
if __name__ == "__main__":
run()