Skip to content

Commit 7aa4f47

Browse files
Merge pull request avinashkranjan#147 from YashIndane/master
This is sudoku solver that shows how it solves sudoku using back-tracking algorithm (fill.gif and grid.gif added)
2 parents e2b97b8 + 28139b6 commit 7aa4f47

File tree

4 files changed

+161
-0
lines changed

4 files changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
import numpy as np
2+
import pygame
3+
import sys
4+
import time
5+
6+
problem = [[0 , 2 , 4 , 0 , 0 , 6 , 0 , 0 , 8],
7+
[6 , 0 , 9 , 0 , 2 , 0 , 0 , 1 , 0],
8+
[5 , 7 , 0 , 0 , 0 , 0 , 0 , 0 , 0],
9+
[0 , 0 , 6 , 9 , 0 , 8 , 0 , 0 , 1],
10+
[0 , 8 , 1 , 0 , 0 , 0 , 6 , 5 , 0],
11+
[7 , 0 , 0 , 3 , 0 , 1 , 8 , 0 , 0],
12+
[0 , 0 , 0 , 0 , 0 , 0 , 0 , 8 , 2],
13+
[0 , 9 , 0 , 0 , 8 , 0 , 1 , 0 , 4],
14+
[8 , 0 , 0 , 2 , 0 , 0 , 5 , 9 , 0]]
15+
16+
17+
np_problem = np.array(problem)
18+
19+
pygame.init()
20+
screen = pygame.display.set_mode((440 , 440))
21+
pygame.display.set_caption('Sudoku Vizualizer')
22+
animation_time = 0.20
23+
24+
image = pygame.image.load('grid.gif')
25+
image_fill = pygame.image.load('fill.gif')
26+
font = pygame.font.SysFont("calibri",30)
27+
28+
coordinates = []
29+
sta = True
30+
31+
32+
while sta :
33+
34+
screen.fill((255,255,255))
35+
screen.blit(image, (0, 0))
36+
37+
x , y = 39 , 33
38+
39+
for row in problem :
40+
temp_co = []
41+
for col in row :
42+
43+
if col != 0 :
44+
label=font.render(str(col),2,(255,0,0))
45+
screen.blit(label , (x , y))
46+
47+
temp_co.append([x , y])
48+
x += 43
49+
50+
x = 39
51+
y += 43
52+
53+
coordinates.append(temp_co)
54+
55+
56+
for event in pygame.event.get() :
57+
58+
if event.type == pygame.QUIT : sys.exit()
59+
60+
61+
fixed_coordinates = [] # first getting the coordinates where fixed numbers are present
62+
empty_coordinates = []
63+
for i , sub_array in enumerate(problem) :
64+
temp = [[i , c] for c , sub_element in enumerate(sub_array) if sub_element > 0]
65+
temp2 = [[i , j] for j , sub_element2 in enumerate(sub_array) if sub_element2 == 0]
66+
for z in temp : fixed_coordinates.append(z)
67+
for w in temp2 : empty_coordinates.append(w)
68+
69+
l , m , r = [0 , 3 , 6] , [1 , 4 , 7] , [2 , 5 , 8]
70+
71+
avoid_dict = {idx : [] for idx in list(range(0 , len(empty_coordinates)))}
72+
73+
def generate_bounds(r , c) -> list:
74+
75+
lower_bound_c = c if c in l else c - 1 if c in m else c - 2
76+
upper_bound_c = c + 3 if c in l else c + 2 if c in m else c + 1
77+
78+
lower_bound_r = r if r in l else r - 1 if r in m else r - 2
79+
upper_bound_r = r + 3 if r in l else r + 2 if r in m else r + 1
80+
81+
return [lower_bound_c , upper_bound_c , lower_bound_r , upper_bound_r]
82+
83+
84+
def backtrack(return_coordinates) :
85+
86+
n_r , n_c = empty_coordinates[empty_coordinates.index(return_coordinates) - 1] # getting back element coordinates
87+
88+
while [n_r , n_c] != empty_coordinates[empty_coordinates.index(return_coordinates) + 1]:
89+
90+
if np_problem[n_r , n_c] != 0 :
91+
avoid_dict[empty_coordinates.index([n_r , n_c])].append(np_problem[n_r , n_c])
92+
93+
fix_flag = False
94+
r , c = n_r , n_c
95+
for num in range(1 , 10) :
96+
97+
l_b_c , u_b_c , l_b_r , u_b_r = generate_bounds(r , c)
98+
99+
if all([num not in np_problem[l_b_r : u_b_r , l_b_c : u_b_c] , num not in np_problem[r , :] , num not in np_problem[: , c]]) :
100+
if num not in avoid_dict.get(empty_coordinates.index([n_r , n_c])) :
101+
np_problem[n_r , n_c] , fix_flag = num , True
102+
103+
screen.blit(image_fill , coordinates[n_r][n_c])
104+
pygame.display.update()
105+
106+
label_n1=font.render(str(num),2,(0,0,200))
107+
screen.blit(label_n1 , coordinates[n_r][n_c])
108+
pygame.display.update()
109+
110+
time.sleep(animation_time)
111+
112+
break
113+
114+
if fix_flag : n_r , n_c = empty_coordinates[empty_coordinates.index([n_r , n_c]) + 1]
115+
116+
if not fix_flag :
117+
np_problem[n_r , n_c] = 0
118+
119+
screen.blit(image_fill , coordinates[n_r][n_c])
120+
pygame.display.update()
121+
time.sleep(animation_time)
122+
123+
avoid_dict[empty_coordinates.index([n_r , n_c])].clear()
124+
n_r , n_c = empty_coordinates[empty_coordinates.index([n_r , n_c]) - 1]
125+
126+
127+
for r in range(9) :
128+
for c in range(9) :
129+
130+
if [r , c] not in fixed_coordinates :
131+
132+
fix_flag = False
133+
134+
for num in range(1 , 10) :
135+
136+
l_b_c , u_b_c , l_b_r , u_b_r = generate_bounds(r , c)
137+
138+
if all([num not in np_problem[l_b_r : u_b_r , l_b_c : u_b_c] , num not in np_problem[r , :] , num not in np_problem[: , c]]) :
139+
140+
np_problem[r , c] , fix_flag = num , True
141+
142+
label_n=font.render(str(num),2,(0,0,200))
143+
screen.blit(label_n , coordinates[r][c])
144+
pygame.display.update()
145+
146+
time.sleep(animation_time)
147+
148+
break
149+
150+
if not fix_flag : backtrack([r , c])
151+
152+
x = input() #just to hold window from closing
153+
154+
155+
156+
157+
158+
159+
160+
161+
1.04 KB
Loading
18.4 KB
Loading
131 KB
Loading

0 commit comments

Comments
 (0)