0% found this document useful (0 votes)
4 views17 pages

Sudoku Solver

The document outlines a minor project titled 'Sudoku Solver' completed by students Tanmay Bijoor and Yashmith KY from Madhava Kripa English Nursery and Higher Primary School. It details the project's components, including an introduction to the Tkinter-based application, the algorithm used for solving Sudoku puzzles, the source code, and future enhancements. The project aims to create an interactive user interface for Sudoku solving while providing educational value in GUI development and algorithm design.

Uploaded by

yashmithy12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views17 pages

Sudoku Solver

The document outlines a minor project titled 'Sudoku Solver' completed by students Tanmay Bijoor and Yashmith KY from Madhava Kripa English Nursery and Higher Primary School. It details the project's components, including an introduction to the Tkinter-based application, the algorithm used for solving Sudoku puzzles, the source code, and future enhancements. The project aims to create an interactive user interface for Sudoku solving while providing educational value in GUI development and algorithm design.

Uploaded by

yashmithy12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

SUDOKU SOLVER

Name: Tanmay Bijoor and Yashmith KY


Class:11 B
School Name: Madhava Kripa English Nursery and Higher Primary
School
School link: https://madhavakripa.edu.in/
Subject: Computer Science
Reg No:
Submitted To: Tr. Shilpa A.G
INDEX

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

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 “Mrs.Shilpa 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.
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
Source Code

import tkinter as tk import


sudoku

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

def set_value(self, val): self.value.set(str(val))

def validate_input(self, *args):

10:
if self.value.get().isdigit() and 0 < int(self.value.get()) <

self.cell.config(fg='black') # Valid input else:


self.cell.delete(0, 'end') # Clear invalid input self.cell.config(fg='red') # Indicate invalid
input

def create_sudoku_grid(master): frame =


tk.Frame(master) frame.grid(row=0, column=0)
cells = []
for i in range(3): row = []
for j in range(3):
subgrid = tk.Frame(frame, borderwidth=1, relief='solid')
subgrid.grid(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)
row.append(subgrid_cells) cells.append(row)

return cells

class SudokuSolver: def init


(self):
self.welcome_window = tk.Tk()
self.welcome_window.title("Welcome to Sudoku Solver")
self.welcome_label = tk.Label(self.welcome_window, text="Welcome to the Sudoku
Solver!", font=("Arial", 16))
self.instructions_label = tk.Label(self.welcome_window, text="Enter the Sudoku
puzzle in the grid below. (Numbers only)", font=("Arial", 12))
self.start_button = tk.Button(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 = tk.Tk() self.solver_window.title("Sudoku Solver")

self.cells = create_sudoku_grid(self.solver_window) #
Call function to create grid

solve_button = tk.Button(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 = self.cells[row_block][col_block][cell_row * 3 +
cell_col].get_value()
row_values.append(cell_value) self.user_input.append(row_values)
sud = sudoku.puzzle(self.user_input,3)

if not sudoku.check(sud): print("Puzzle is not solvable!!!")


else:
print('Before Solving:')
sud.show() print('After Solving:')
sudoku.solve(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 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

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: 1.Spyder:https://www.spyder-

ide.org/
References

• Sumit Arora class 11 Computer Science Textbook

• https://www.geeksforgeeks.org/

• Recursion 'Super Power' (in Python) - Computerphile

• Python Object Oriented Programming (OOP) - For Beginners

You might also like