Sudoku Solver
Sudoku Solver
S.no Topic Pg no
1 Certificate
2 Acknowledgement
3 Introduction
4 Algorithm
5 Source Code
6 Output Screen
7 Future
Enhancement
8 Hardware and
software
9 References
CERTIFICATE
Date:
Date:
Welcome to our Tkinter-based Sudoku Solver project! This application is designed to make
solving Sudoku puzzles interactive and user-friendly. Using the Python Tkinter library, our
Sudoku Solver combines a graphical interface with an efficient solving algorithm to improve
your puzzle- solving experience.
Our project aims to develop an intuitive user interface for easy Sudoku puzzle input and
solving, implement input validation to ensure the integrity of the puzzle, design and integrate a
backtracking algorithm for efficient and accurate puzzle solving, and offer both practical utility
for solving puzzles and educational value for understanding GUI development and algorithm
design in Python.
The application features a clean and simple interface built with the Tkinter library. Users can
input numbers directly into the Sudoku grid, with each cell dynamically linked to ensure real-
time updates and validation. The application provides real-time input validation, displaying
valid inputs (numbers 1-9) in black and highlighting invalid inputs (non- digits or out-of-range
numbers) in red while clearing them from the cell. The Sudoku grid is dynamically created with
each cell represented by an instance of the Sudoku Cell
class. This ensures flexibility and scalability, with each cell linked to a StringVar for real-time
validation. The solver uses a recursive backtracking algorithm to fill in the missing numbers in
the Sudoku grid. By clicking the "Solve" button, users can see the complete solution to their
puzzle in seconds.
Grid setup involves creating a 9x9 matrix of entry cells using Tkinter. Each cell is an instance of
the SudokuCell class, which handles input validation and stores the user-entered values. The
backtracking algorithm solves Sudoku puzzles by attempting to fill the grid one cell at a time
and backtracking whenever an invalid entry is detected, ensuring that the final solution adheres
to Sudoku rules.
Algorithm
1. Start
2. Initialize Sudoku solver object
3. Show Welcome Window with a “Start solving” button.
4. Show Solver window with the Sudoku Grid and a “Solve” button.
5. Get User Input
6. Initialise puzzle object
7. Search for empty box. If found, continue. Else goto step 11
8. Get coordinates of empty box.
9. Insert a valid number. (1-10). If all numbers are invalid, continue. Else goto step 7.
10. Reset box. Goto step 7.
11. Display solved puzzle
12. Destroy the Solver Window
13. Stop
Source Code
class SudokuCell:
def init (self, master, row, col):
self.cell = tk.Entry(master, width=2) # Use default font family
self.cell.grid(row=row, column=col, padx=1, pady=1) self.value = tk.StringVar(self.cell)
self.cell.config(textvariable=self.value) self.value.trace_add("write", self.validate_input)
def get_value(self):
return int(self.value.get()) if self.value.get().isdigit() else 0
10:
if self.value.get().isdigit() and 0 < int(self.value.get()) <
for k in range(3):
for l in range(3):
cell = SudokuCell(subgrid, k, l)
subgrid_cells.append(cell)
row.append(subgrid_cells) cells.append(row)
return cells
self.welcome_label.pack(pady=20)
self.instructions_label.pack(pady=10)
self.start_button.pack(pady=20)
self.welcome_window.mainloop()
self.cells = create_sudoku_grid(self.solver_window) #
Call function to create grid
self.solver_window.focus_force()
self.solver_window.mainloop()
solver = SudokuSolver()
class puzzle:
def init (self, list, box_size): self.l = list
self.b = box_size
def is_valid(self, row_number, column_number, n): #Checking the row
if n in self.l[row_number]: return False
#Checking the column for i in self.l:
if n is i[column_number]: return False
print('━')
print('│', end='')
for j in range(len(self.l[i])): if self.l[i][j] !=
0:
print(f' {self.l[i][j]} ', end='') else:
print(' ', end='') if
(j+1) % 3 == 0:
print('│', end='') print()
for j in range(len(self.l[i])+1): print('━━━', end='')
print()
def find(self):
for i in range(len(self.l)): for j in
range(len(self.l)):
if self.l[i][j] == 0: return (i, j)
return False def
check(puzzle):
for j in range(9): for i in
range(9):
if puzzle.l[i][j] != 0: temp =
puzzle.l[i][j] puzzle.l[i][j] =
None
if not puzzle.is_valid(i,j,temp): puzzle.l[i][j] = temp
return False else:
def solve(puzzle):
if not puzzle.find(): return puzzle
else:
row,col = puzzle.find() for i in
range(1,10):
if puzzle.is_valid(row,col,i): puzzle.l[row][col] = i
if solve(puzzle): return puzzle
puzzle.l[row][col] = 0
Output Screen
Future Enhancement
ide.org/
References
• https://www.geeksforgeeks.org/