-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathz_solve_board_quickest_visual.py
255 lines (220 loc) · 7.58 KB
/
z_solve_board_quickest_visual.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
import os, time
if os.path.exists("board-solved.txt"):
os.remove('board-solved.txt')
os.system("python z_solve_board_quickest.py")
try:
f = open('board-solved.txt', 'r')
lines = f.readlines()
solvedboard = [[ int(n) for n in line.split() ] for line in lines ]
f.close()
except:
print("BOARD IS UNSOLVABLE, \nPLEASE CHECK THE NUMBERS AND TRY AGAIN")
os.remove('board-solved.txt')
time.sleep(3)
exit()
f = open('board.txt', 'r')
lines = f.readlines()
board = [[ int(n) for n in line.split() ] for line in lines ]
f.close()
maxNUMBERSgridNox = 0
maxNUMBERSgridNoy = 0
max = 0
for i in range(3):
for j in range(3):
tempmax = 0
for r in range(i * 3, i * 3 + 3):
for c in range( j * 3, j * 3 + 3 ):
if board[r][c] != 0 :
tempmax += 1
if tempmax > max:
max = tempmax
maxNUMBERSgridNox = i
maxNUMBERSgridNoy = j
maxNUMBERSgridNox *= 3
maxNUMBERSgridNoy *= 3
# to shift the columns
for i in range(3):
for j in range(9):
board[j][i], board[j][maxNUMBERSgridNoy+i] = board[j][maxNUMBERSgridNoy+i], board[j][i]
# to shift the rows
for i in range(3):
board[i], board[maxNUMBERSgridNox+i] = board[maxNUMBERSgridNox+i], board[i]
boardForVisual = [[ n for n in line ] for line in board ]
fixed = [[0 for _ in range(9)] for _ in range(9)]
for i in range(9):
for j in range(9):
if board[i][j] != 0:
fixed[i][j] = 1
# for start of board for visualization
startx = 0
starty = 0
found = False
for i in range(9):
if not found:
for j in range(9):
if fixed[i][j] == 0:
startx = i
starty = j
found = True
break
def drawboard():
for i in range(9):
if i%3 == 2:
pygame.draw.line(surface, (0, 0, 0), ((i+1)*(600/9), 0), ((i+1)*(600/9), 600), 3)
else:
pygame.draw.line(surface, (0, 0, 0), ((i+1)*(600/9), 0), ((i+1)*(600/9), 600), 1)
for i in range(9):
if i%3 == 2:
pygame.draw.line(surface, (0, 0, 0), (0, (i+1)*(600/9)), (600, (i+1)*(600/9)), 3)
else:
pygame.draw.line(surface, (0, 0, 0), (0, (i+1)*(600/9)), (600, (i+1)*(600/9)), 1)
# VISUALIZE BACKTRACKING ####################################################################
def forVeryHardBoardTLE():
surface.fill((255, 255, 255))
drawboard()
for j in range(9):
for i in range(9):
if solvedboard[i][j] != 0:
text = font.render(str(solvedboard[i][j]), 1, (0,0,0))
surface.blit(text, ((j)*(600/9)+22, (i)*(600/9)+15) )
win.blit(surface, (50,50))
pygame.display.update()
while True:
for event in pygame.event.get():
if event.type == QUIT:
os.remove('board-solved.txt')
pygame.quit()
exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
os.remove('board-solved.txt')
pygame.quit()
exit()
def redrawVisualize():
surface.fill((255, 255, 255))
drawboard()
for j in range(9):
for i in range(9):
if boardForVisual[i][j] != 0:
text = font.render(str(boardForVisual[i][j]), 1, (0,0,0))
surface.blit(text, ((j)*(600/9)+22, (i)*(600/9)+15) )
win.blit(surface, (50,50))
pygame.display.update()
def highlightVisualize(y,x):
# to exit while recurrsion
for event in pygame.event.get():
if event.type == QUIT:
os.remove('board-solved.txt')
pygame.quit()
exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
os.remove('board-solved.txt')
pygame.quit()
exit()
redrawVisualize()
for i in range(9):
for j in range(9):
if boardForVisual[i][j] != 0 and fixed[i][j] != 1 :
pygame.draw.rect(surface, (0,255,0), (j*(600/9) + 4, i*(600/9) + 4, (600/9)-8, (600/9)-8 ), 3)
pygame.draw.rect(surface, (255,0,0), (x*(600/9), y*(600/9), (600/9), (600/9) ), 3)
win.blit(surface, (50,50))
pygame.display.update()
def checkIfPlacingIsPossible(currentRow,currentCol,number):
gridRowNum = int( currentRow / 3)
gridColNum = int( currentCol / 3)
for r in range(9):
if boardForVisual[r][currentCol] == number + 1 :
return 0
for c in range(9):
if boardForVisual[currentRow][c] == number + 1 :
return 0
for r in range(gridRowNum * 3, gridRowNum * 3 + 3):
for c in range( gridColNum * 3, gridColNum * 3 + 3 ):
if r == currentRow and c == currentCol:
pass
elif boardForVisual[r][c] == number + 1:
return 0
return 1
def nextPos(currentRow,currentCol):
if currentCol == 8:
currentCol = 0
currentRow += 1
else:
currentCol += 1
while True:
if currentRow > 8:
redrawVisualize()
while True:
for event in pygame.event.get():
if event.type == QUIT:
os.remove('board-solved.txt')
pygame.quit()
exit()
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
os.remove('board-solved.txt')
pygame.quit()
exit()
if fixed[currentRow][currentCol] == 0:
return currentRow, currentCol
else:
currentCol += 1
if currentCol > 8:
currentCol = 0
currentRow += 1
def seekNextPosition(currentRow,currentCol) :
tr = currentRow * 1
tc = currentCol * 1
if currentCol == 8:
currentCol = 0
currentRow += 1
else:
currentCol += 1
while True:
if currentRow > 8:
return tr, tc
if fixed[currentRow][currentCol] == 0:
return currentRow, currentCol
else:
currentCol += 1
if currentCol > 8:
currentCol = 0
currentRow += 1
def visualizeBacktracking(currentRow,currentCol):
global iterations, divideFaster
for number in range(9):
nextx, nexty = seekNextPosition(currentRow,currentCol)
boardForVisual[nextx][nexty] = 0
if checkIfPlacingIsPossible(currentRow,currentCol,number) == 1 :
boardForVisual[currentRow][currentCol] = number + 1
if divideFaster <= 20:
highlightVisualize(currentRow,currentCol)
else:
forVeryHardBoardTLE()
time.sleep(0.1/divideFaster)
iterations += 1
if iterations > 200:
divideFaster += 2
iterations = 0
nextx, nexty = nextPos(currentRow,currentCol)
visualizeBacktracking(nextx,nexty)
else:
boardForVisual[currentRow][currentCol] = 0
# ##################################### END OF VISUALIZING BACKTRACKING
# pygame
import pygame
from pygame.locals import *
pygame.init()
pygame.display.set_caption("SUDOKU")
clock = pygame.time.Clock()
logoIMG = pygame.image.load("logo.png")
pygame.display.set_icon(logoIMG)
win = pygame.display.set_mode((700,700))
win.fill((255,255,255))
pygame.draw.rect(win, (0,0,0), (47,47,604,604), 3)
surface = pygame.Surface((600,600))
font = pygame.font.SysFont('Calibri', 45)
iterations = 0
divideFaster = 1
visualizeBacktracking(startx,starty)