-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSudoku-Solver-with-GUI.py
125 lines (103 loc) · 4.02 KB
/
Sudoku-Solver-with-GUI.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
from tkinter import *
root = Tk()
root.geometry('275x283')
class SolveSudoku():
def __init__(self):
self.allZero()
self.startSolution()
# Set the empty cells to 0
def allZero(self):
for i in range(9):
for j in range(9):
if savedNumbers[i][j].get() not in ['1','2','3','4','5','6','7','8','9']:
savedNumbers[i][j].set(0)
# Start the Algorithm
def startSolution(self, i=0, j=0):
i,j = self.findNextCellToFill(i, j)
# If i == -1 the position is Ok or the Sudoku is Solved
if i == -1:
return True
for e in range(1,10):
if self.isValid(i,j,e):
savedNumbers[i][j].set(e)
if self.startSolution(i, j):
return True
# Undo the current cell for backtracking
savedNumbers[i][j].set(0)
return False
# Search the Nearest Cell to fill
def findNextCellToFill(self, i, j):
for x in range(i,9):
for y in range(j,9):
if savedNumbers[x][y].get() == '0':
return x,y
for x in range(0,9):
for y in range(0,9):
if savedNumbers[x][y].get() == '0':
return x,y
return -1,-1
# Check the Validity of savedNumbers[i][j]
def isValid(self, i, j, e):
for x in range(9):
if savedNumbers[i][x].get() == str(e):
return False
for x in range(9):
if savedNumbers[x][j].get() == str(e):
return False
# Finding the Top x,y Co-ordinates of the section containing the i,j cell
secTopX, secTopY = 3 *int((i/3)), 3 *int((j/3))
for x in range(secTopX, secTopX+3):
for y in range(secTopY, secTopY+3):
if savedNumbers[x][y].get() == str(e):
return False
return True
class Launch():
# Set Title, Grid and Menu
def __init__(self, master):
# Title and settings
self.master = master
master.title("Sudoku Solver")
font = ('Arial', 18)
color = 'white'
# Front-end Grid
self.__table = []
for i in range(1,10):
self.__table += [[0,0,0,0,0,0,0,0,0]]
for i in range(0,9):
for j in range(0,9):
self.__table[i][j]=Entry(master,width=2,font=font,bg=color,cursor='arrow',borderwidth=0,highlightcolor='yellow',highlightthickness=1,highlightbackground='black',textvar=savedNumbers[i][j])
self.__table[i][j].bind('<Motion>', self.correctGrid)
self.__table[i][j].bind('<FocusIn>', self.correctGrid)
self.__table[i][j].bind('<Button-1>', self.correctGrid)
self.__table[i][j].grid(row=i, column=j)
# Front-End Menu
menu = Menu(master)
master.config(menu = menu)
menu.add_command(label = 'Exit', command = master.quit)
menu.add_command(label = 'Solve', command = self.solveInput)
menu.add_command(label = 'Clear', command = self.clearAll)
# Correct the Grid if inputs are uncorrect
def correctGrid(self, event):
for i in range(9):
for j in range(9):
if savedNumbers[i][j].get() == '':
continue
if len(savedNumbers[i][j].get()) > 1 or savedNumbers[i][j].get() not in ['1','2','3','4','5','6','7','8','9']:
savedNumbers[i][j].set('')
# Clear the Grid
def clearAll(self):
for i in range(9):
for j in range(9):
savedNumbers[i][j].set('')
# Calls the class SolveSudoku
def solveInput(self):
solution = SolveSudoku()
# Global Matrix where are stored the numbers
savedNumbers = []
for i in range(1,10):
savedNumbers += [[0,0,0,0,0,0,0,0,0]]
for i in range(0,9):
for j in range(0,9):
savedNumbers[i][j] = StringVar(root)
app = Launch(root)
root.mainloop()