-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolver.py
205 lines (174 loc) · 6.63 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
import pygame # used for the display
from time import sleep
pygame.init()
screen = pygame.display.set_mode((1000, 1000)) # create 1000px x 1000px window
pygame.display.set_caption("Sudoku solver")
grid = [[0 for _ in range(9)] for x in range(9)]
# sudoku grid is 9x9 0 can be used as placeholder since it isn't used in game
solvedCells = set()
gDelay = [0.2]
def resetgrid(grid):
grid[0] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
grid[1] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
grid[2] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
grid[3] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
grid[4] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
grid[5] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
grid[6] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
grid[7] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
grid[8] = [0, 0, 0, 0, 0, 0, 0, 0, 0]
step = 1000/9
font1 = pygame.font.SysFont("Ariel", 60)
def createChallengeGrid(grid): # create a predifined sudoku table
grid[0] = [9, 8, 5, 4, 0, 1, 0, 0, 0]
grid[1] = [0, 0, 0, 0, 3, 0, 0, 0, 0]
grid[2] = [1, 0, 6, 0, 0, 0, 0, 0, 0]
grid[3] = [0, 0, 0, 5, 0, 0, 0, 0, 0]
grid[4] = [4, 0, 2, 0, 0, 9, 0, 0, 3]
grid[5] = [0, 9, 0, 0, 6, 3, 4, 0, 0]
grid[6] = [0, 6, 0, 0, 1, 0, 0, 0, 0]
grid[7] = [0, 0, 0, 3, 0, 6, 0, 0, 5]
grid[8] = [2, 0, 0, 0, 8, 0, 0, 0, 1]
def drawGrid(): # this isn't a pure function but oh well
for i in range(9)[1:]:
if i % 3 == 0:
pygame.draw.line(screen, (0, 0, 0), (i*step, 0), (i*step, 1000), 3)
else:
pygame.draw.line(screen, (0, 0, 0), (i*step, 0), (i*step, 1000), 1)
for i in range(9)[1:]:
if i % 3 == 0:
pygame.draw.line(screen, (0, 0, 0), (0, i*step), (1000, i*step), 3)
else:
pygame.draw.line(screen, (0, 0, 0), (0, i*step), (1000, i*step), 1)
def populateGrid(): # this isn't a pure function too
for i, row in enumerate(grid):
for j, cell in enumerate(row):
if cell != 0:
cellVal = font1.render(str(cell), 1, (0, 0, 0))
screen.blit(cellVal, ((i+0.5)*step, (j+0.5)*step))
#print(cell, end=' ')
#print('')
def solve():
delay = gDelay[0]
# prevX and prevY are just to make visualization nicer
#
# rules of sudoku:
# 1. There can't be two same numbers in each 3x3 square
# 2. There can't be two same numbers in a row
# 3. There can't be two same numbers in a collumn
empty = findEmpty()
if not empty:
return True
x, y = empty
for guess in range(1, 10):
colorSolvedCells(solvedCells)
drawGrid()
pygame.draw.rect(screen, (0, 0, 255), (y*step, x *
step, step, step))
populateGrid()
cellVal = font1.render(str(guess), 1, (0, 0, 0))
screen.blit(cellVal, ((y+0.5)*step, (x+0.5)*step))
pygame.display.update()
sleep(delay)
for event in pygame.event.get():
# Quit the game window
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
gDelay[0] -= 0.1
if gDelay[0] < 0:
gDelay[0] = 0
if event.key == pygame.K_RIGHT:
gDelay[0] += 0.1
if validate(x, y, guess):
solvedCells.add((x, y))
grid[y][x] = guess
if solve():
return True
grid[y][x] = 0
solvedCells.discard((x, y))
return False
def findEmpty(): # needed for the backtracking algorithm
for x in range(9):
for y in range(9):
if grid[y][x] == 0:
return (x, y)
return False
def validate(x, y, val):
# check if the given inputs are suitable
# returns true if inputs are suitable
for i in range(9):
if grid[y][i] == val:
return False
for i in range(9):
if grid[i][x] == val:
return False
for i in range(9):
pass
qx = x//3*3 # qx and qy are the coords of the top left corner of the small square containing x and y
qy = y//3*3
for i in range(3):
for j in range(3):
if val == grid[qy + j][qx + i]:
return False
return True
def colorSolvedCells(solvedCells):
screen.fill((255, 255, 255))
for x, y in solvedCells:
pygame.draw.rect(screen, (0, 128, 0), (y*step, x *
step, step, step))
def colorSelected(x, y):
screen.fill((255, 255, 255))
pygame.draw.rect(screen, (128, 128, 128), (y*step, x *
step, step, step))
resetgrid(grid)
screen.fill((255, 255, 255))
run = True
normalRun = True # False means its in autosolve mode
selectedCords = (-1, -1)
while run:
for event in pygame.event.get():
# Quit the game window
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONDOWN:
pos = pygame.mouse.get_pos()
print(pos)
selectedCords = (int(pos[0]//step), int(pos[1]//step))
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_f:
createChallengeGrid(grid)
if event.key == pygame.K_r:
resetgrid(grid)
if event.key == pygame.K_s:
gDelay[0] = 0.2
solvedCells = set()
solve()
if selectedCords != (-1, -1):
if event.key == pygame.K_0:
grid[selectedCords[0]][selectedCords[1]] = 0
if event.key == pygame.K_1:
grid[selectedCords[0]][selectedCords[1]] = 1
if event.key == pygame.K_2:
grid[selectedCords[0]][selectedCords[1]] = 2
if event.key == pygame.K_3:
grid[selectedCords[0]][selectedCords[1]] = 3
if event.key == pygame.K_4:
grid[selectedCords[0]][selectedCords[1]] = 4
if event.key == pygame.K_5:
grid[selectedCords[0]][selectedCords[1]] = 5
if event.key == pygame.K_6:
grid[selectedCords[0]][selectedCords[1]] = 6
if event.key == pygame.K_7:
grid[selectedCords[0]][selectedCords[1]] = 7
if event.key == pygame.K_8:
grid[selectedCords[0]][selectedCords[1]] = 8
if event.key == pygame.K_9:
grid[selectedCords[0]][selectedCords[1]] = 9
colorSelected(selectedCords[1], selectedCords[0])
drawGrid()
populateGrid()
pygame.display.update()
pygame.quit()