SUDOKU SOLVER
Name: Tanmay Bijoor and Yashmith KY
Class:11 B
School Name: Madhava Kripa English Nursery and Higher Primary
School
School link: [Link]
Subject: Computer Science
Reg No:
Submitted To: Tr. Shilpa A.G
INDEX
[Link] Topic Pg no
1 Certificate 1-2
2 Acknowledgement 3
3 References 4
4 Introduction 5-6
5 Algorithm 7
6 Source Code 8-16
7 Output Screen 17-18
8 Future 19
Enhancement
9 Hardware and 20
software
10 Thank you 21
CERTIFICATE
This is to certify that _____Tanmay Bijoor_____, has
satisfactorily completed their minor project in the subject
Computer Science on the topic ____Sudoku Solver___ during the
Academic Year 2024-25.
Date:
Signature of Examiner Signature of Principal
CERTIFICATE
This is to certify that ___Yashmith KY___, has
satisfactorily completed their minor project in the subject
Computer Science. on the topic ____Sudoku Solver___ during the
Academic Year 2024-25.
Date:
Signature of Examiner Signature of Principal
ACKNOWLEDGEMENT
The success and outcome of this project required a lot of guidance
and assistance from many people, and I am extremely privileged to
have gotten this all through the completion of my project.
This project has helped me not only get marks as well as increase my
knowledge.
I would like to extend my gratitude to principal “Mrs. Swati
Sudhakar Kulkarni”
For giving me this wonderful opportunity to do this project.
I would also like to express my special thanks to my Computer
Science Teacher “[Link] A.G” for her guidance and support in
completing my project
I would also like to thank my parents and friends who helped me
complete this project within a limited time.
References
• Sumit Arora class 11 Computer Science Textbook
• [Link]
• Recursion 'Super Power' (in Python) - Computerphile
• Python Object Oriented Programming (OOP) - For Beginners
Introduction
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
1.
Source Code
import tkinter as tk
import sudoku
class SudokuCell:
def __init__(self, master, row, col):
[Link] = [Link](master, width=2) # Use default font
family
[Link](row=row, column=col, padx=1, pady=1)
[Link] = [Link]([Link])
[Link](textvariable=[Link])
[Link].trace_add("write", self.validate_input)
def get_value(self):
return int([Link]()) if [Link]().isdigit()
else 0
def set_value(self, val):
[Link](str(val))
def validate_input(self, *args):
if [Link]().isdigit() and 0 < int([Link]()) <
10:
[Link](fg='black') # Valid input
else:
[Link](0, 'end') # Clear invalid input
[Link](fg='red') # Indicate invalid input
def create_sudoku_grid(master):
frame = [Link](master)
[Link](row=0, column=0)
cells = []
for i in range(3):
row = []
for j in range(3):
subgrid = [Link](frame, borderwidth=1,
relief='solid')
[Link](row=i, column=j)
subgrid_cells = []
for k in range(3):
for l in range(3):
cell = SudokuCell(subgrid, k, l)
subgrid_cells.append(cell)
[Link](subgrid_cells)
[Link](row)
return cells
class SudokuSolver:
def __init__(self):
self.welcome_window = [Link]()
self.welcome_window.title("Welcome to Sudoku
Solver")
self.welcome_label = [Link](self.welcome_window,
text="Welcome to the Sudoku Solver!", font=("Arial", 16))
self.instructions_label =
[Link](self.welcome_window, text="Enter the Sudoku
puzzle in the grid below. (Numbers only)", font=("Arial",
12))
self.start_button = [Link](self.welcome_window,
text="Start Solving", command=self.show_solver)
self.welcome_label.pack(pady=20)
self.instructions_label.pack(pady=10)
self.start_button.pack(pady=20)
self.welcome_window.mainloop()
def show_solver(self):
self.welcome_window.destroy()
self.solver_window = [Link]()
self.solver_window.title("Sudoku Solver")
[Link] = create_sudoku_grid(self.solver_window) #
Call function to create grid
solve_button = [Link](self.solver_window,
text="Solve", command=self.solve_button_click)
solve_button.grid(row=9, columnspan=9)
self.solver_window.focus_force()
self.solver_window.mainloop()
def solve_button_click(self):
self.user_input = []
for row_block in range(3):
for cell_row in range(3):
row_values = []
for col_block in range(3):
for cell_col in range(3):
cell_value =
[Link][row_block][col_block][cell_row * 3 +
cell_col].get_value()
row_values.append(cell_value)
self.user_input.append(row_values)
sud = [Link](self.user_input,3)
if not [Link](sud):
print("Puzzle is not solvable!!!")
else:
print('Before Solving:')
[Link]()
print('After Solving:')
[Link](sud).show()
self.solver_window.destroy()
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
#Checking the Box
box_no = (3*(row_number//self.b),
3*(column_number//self.b))
for i in range(self.b):
slice_row = self.l[box_no[0]+i]
slice_col = slice_row[box_no[1]:box_no[1]+self.b]
if n in slice_col:
return False
return True
def show(self):
for i in range(len(self.l)):
if (i) % 3 == 0:
for j in range(len(self.l[i])+1):
print('━━━', end='')
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:
puzzle.l[i][j] = temp
continue
return True
def solve(puzzle):
if not [Link]():
return puzzle
else:
row,col = [Link]()
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
i) Able to generate the puzzle on its own
ii)Allow the user to solve it within the app
iii) Enhance the GUI, making it more interactive
Hardware and software requirement
The hardware and software used in this program are:
[Link]:[Link]