forked from deekshakulal/Sudoku_Puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sudoku.py
319 lines (242 loc) · 7.29 KB
/
Sudoku.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
import pygame
from random import shuffle
import time
import copy
class Sudoku:
def __init__(self, grid):
self.counter = 0
#path is for the matplotlib animation
self.path = []
self.grid = grid
self.generate_puzzle()
self.original = copy.deepcopy(self.grid)
def generate_puzzle(self):
"""generates a new puzzle and solves it"""
self.generate_solution(self.grid)
self.remove_numbers_from_grid()
self.print_grid(self.grid)
return self.grid
def solve_puzzle(self, grid):
"""solve the sudoku puzzle with backtracking"""
for i in range(0,81):
row = i//9
col = i%9
#find next empty cell
if grid[row][col] == 0:
for number in range(1,10):
#check that the number hasn't been used in the row/col/subgrid
if self.valid(grid,number,(row,col)):
grid[row][col] = number
if not self.find_empty_square(grid):
self.counter += 1
break
else:
if self.solve_puzzle(grid):
return True
break
grid[row][col] = 0
return False
def test_sudoku(self,grid):
"""tests each square to make sure it is a valid puzzle"""
for row in range(9):
for col in range(9):
num = grid[row][col]
#remove number from grid to test if it's valid
grid[row][col] = 0
if not self.valid(grid,num,(row,col)):
return False
else:
#put number back in grid
grid[row][col] = num
return True
def find_empty_square(self,grid):
"""return the next empty square coordinates in the grid"""
for i in range(9):
for j in range(9):
if grid[i][j] == 0:
return (i,j)
return
def generate_solution(self, grid):
"""generates a full solution with backtracking"""
number_list = [1,2,3,4,5,6,7,8,9]
for i in range(0,81):
row = i//9
col = i%9
#find next empty cell
if grid[row][col] == 0:
shuffle(number_list)
for number in number_list:
if self.valid(grid,number,(row,col)):
self.path.append((number,row,col))
grid[row][col] = number
if not self.find_empty_square(grid):
return True
else:
if self.generate_solution(grid):
#if the grid is full
return True
break
grid[row][col] = 0
return False
def get_non_empty_squares(self,grid):
"""returns a shuffled list of non-empty squares in the puzzle"""
non_empty_squares = []
for i in range(len(grid)):
for j in range(len(grid)):
if grid[i][j] != 0:
non_empty_squares.append((i,j))
shuffle(non_empty_squares)
return non_empty_squares
def remove_numbers_from_grid(self):
"""remove numbers from the grid to create the puzzle"""
#get all non-empty squares from the grid
non_empty_squares = self.get_non_empty_squares(self.grid)
non_empty_squares_count = len(non_empty_squares)
rounds = 3
while rounds > 0 and non_empty_squares_count >= 17:
#there should be at least 17 clues
row,col = non_empty_squares.pop()
non_empty_squares_count -= 1
#might need to put the square value back if there is more than one solution
removed_square = self.grid[row][col]
self.grid[row][col]=0
#make a copy of the grid to solve
grid_copy = copy.deepcopy(self.grid)
#initialize solutions counter to zero
self.counter=0
self.solve_puzzle(grid_copy)
#if there is more than one solution, put the last removed cell back into the grid
if self.counter!=1:
self.grid[row][col]=removed_square
non_empty_squares_count += 1
rounds -=1
return
#Returns a List of tuples while contain the position of emptyspaces in a (3 * 3) block
def findEmptyspaces(self, grid, block):
emptyspaces = list()
emptyspaces.clear()
for i in range(((block//3) * 3), (((block)//3) * 3) + 3):
for j in range((block % 3) * 3, ((((block) % 3) * 3) + 3)):
if(grid[i][j] == 0):
emptyspaces.append((i, j))
return emptyspaces
'''Inserts Values using Single possibility rule i.e Inserts a value only when the value cannot be assigned to another empty
space in the block '''
def InsertInemptySpace(self, grid, emptyspace):
self.i = 0
self.j = 0
self.m = 0
self.n = 0
canInsert = False
for empty in emptyspace:
self.i = empty[0]
self.j = empty[1]
#Allocates a value to an empty space
for value in range(1, 10):
canInsert = False
if(self.valid(grid, value, (self.i, self.j))):
grid[self.i][self.j] = value
#Checks if the same value can be inserted to any other empty space within the block
for emp in emptyspace:
if(len(emptyspace) == 1):
canInsert = False
break
self.m = emp[0]
self.n = emp[1]
if(emp != empty and grid[self.m][self.n] == 0):
grid[self.i][self.j] = 0
if(self.valid(grid, value, (self.m, self.n))):
grid[self.i][self.j] = 0
grid[self.m][self.n] = 0
canInsert = True
break
else:
grid[self.i][self.j] = value
grid[self.m][self.n] = 0
canInsert = False
if(not canInsert):
grid[self.i][self.j] = value
break
else:
grid[self.i][self.j] = 0
return grid
#returns a tuple i,j containing the position of empty space
def Findempty(self, grid):
self.grid = grid
for i in range(0, 9):
for j in range(0, 9):
if(grid[i][j] == 0):
return(i, j)
return False
#Checks if the value assigned at position pos is valid or not
def valid(self, grid, val, pos):
for i in range(0, 9):
if(grid[pos[0]][i] == val and pos[1] != i):
return False
for i in range(0, 9):
if(grid[i][pos[1]] == val and pos[0] != i):
return False
box_x = pos[1] // 3
box_y = pos[0] // 3
for i in range(box_y * 3, box_y * 3 + 3):
for j in range(box_x * 3, box_x * 3 + 3):
if(grid[i][j] == val and (i, j) != pos):
return False
return True
#The Backtracking algorithm fills out the remaining spaces in the grid.
def EnteratLast(self, grid):
row = 0
col = 0
self.grid = grid
empty = self.Findempty(self.grid)
if not empty:
return True
else:
row, col = empty
for i in range(1, 10):
if(self.valid(self.grid, i, (row, col))):
self.grid[row][col] = i
if(self.EnteratLast(self.grid)):
return True
self.grid[row][col] = 0
return False
#Prints the grid
def print_grid(self, grid):
for i in range(len(grid)):
if(i % 3 == 0 and i != 0):
print("- - - - - - - - - - - -")
for j in range(len(grid[0])):
if(j % 3 == 0 and j != 0):
print(" | ", end="")
if(j == 8):
print(grid[i][j])
else:
print(str(grid[i][j]) + " ", end="")
#The main solving function
def SudokuSolve(self, grid):
emptyspace = list()
n = 0
grid_prev = [[0 for x in range(9)] for y in range(9)]
#Iterates until the previous grid is same as the next one
#First solves the sudoku using Single Value only technique
while(grid_prev != grid):
grid_prev = grid
for block in range(0, 9):
emptyspace.clear()
emptyspace = self.findEmptyspaces(grid, block)
grid = self.InsertInemptySpace(grid, emptyspace)
n = n+1
#Calls Backtracking algorithm to fill out the remaining empty spaces
self.EnteratLast(grid)
if __name__ == "__main__":
grid = [[0 for x in range(9)] for y in range(9)]
print("Solve this")
start = time.time()
sudoku = Sudoku(grid)
sudoku.SudokuSolve(grid)
end = time.time()
print("Solution")
print(" ")
sudoku.print_grid(grid)
print("time taken")
print(end-start)