Skip to content

erfanshafieeee/Solving-Sudoku-Table-with-Backtracking-Algorithm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

🎯 Sudoku Solver with Backtracking Algorithm 🎯

🧩 Overview

Welcome to the Sudoku Solver, a sleek and interactive tool designed to solve any Sudoku puzzle you throw at it! Whether you're stuck on a challenging puzzle or want to witness the magic of algorithms in action, this Python-based Sudoku solver is here to help.

Built with Tkinter, this GUI-based program utilizes the Backtracking Algorithm to solve puzzles step-by-step, right before your eyes! Simply load a puzzle or enter your own, and watch as the program effortlessly solves it.


✨ Features

  • Preloaded Puzzle: Jump straight in with a pre-filled puzzle ready to solve!
  • Custom Input: Clear the grid and input your own Sudoku puzzle.
  • Dynamic Input Feedback:
    • Green cells for valid numbers (1-9).
    • Red cells for invalid entries (anything non-numeric or out-of-range).
    • White cells for empty values.
  • Visual Solving Process: The solver runs step-by-step with a slight delay, so you can follow the algorithm's thought process.
  • Sudoku Validation: Before solving, the input is checked to ensure it's a valid Sudoku configuration.
  • Clear Board: Wipe the board and start fresh with a single click.
  • User-Friendly Interface: Clean and intuitive, designed for ease of use and interaction.

🛠️ How It Works

At the heart of this Sudoku Solver is the Backtracking Algorithm, a classic recursive approach used to solve constraint satisfaction problems.

📖 Backtracking Algorithm in a Nutshell

  1. Start by selecting the first empty cell.
  2. Try placing a number (1-9) in the empty cell:
    • Check if the number is allowed:
      • It doesn't repeat in the same row.
      • It doesn't repeat in the same column.
      • It doesn't repeat in the 3x3 grid.
  3. If the number is valid, place it and move to the next cell.
  4. If the number is invalid, backtrack by resetting the cell and trying the next number.
  5. Repeat this process until the entire puzzle is solved or all possibilities are exhausted.

This method ensures that every possibility is explored while respecting the Sudoku rules, making it both powerful and efficient.


🎮 How to Use

  1. Run the Program:

    • Launch the Python script to open the Sudoku solver.
  2. Use the Preloaded Puzzle:

    • The program starts with a pre-filled Sudoku board. Hit Solve to see the magic happen!
  3. Clear the Board:

    • Want to try your own puzzle? Click the Clear Board button to reset the grid.
  4. Input Your Puzzle:

    • Simply click on any cell and enter numbers (1-9). The cell will turn green if valid or red if invalid.
  5. Solve Your Puzzle:

    • Once you're done entering your numbers, hit the Solve button to watch the solver in action.
  6. Invalid Puzzle Warning:

    • If the inputted puzzle is invalid, you'll get a friendly error message letting you know.

🚀 Technology & Libraries

  • Python 3.x: The backbone of this program.
  • Tkinter: Python’s de facto GUI library, providing the interactive interface.

🧑‍💻 Code Breakdown

The Backtracking Algorithm Explained

This is a depth-first search algorithm used for solving constraint-based problems like Sudoku. It explores all possibilities for each cell before moving on to the next.

  1. Try placing numbers in empty cells.
  2. Validate the placement according to Sudoku's rules.
  3. Backtrack if an invalid configuration arises.
  4. Continue until the board is either solved or unsolvable.

🏗️ Installation & Setup

To get started with the Sudoku Solver, follow these steps:

  1. Clone the repository:

    git clone https://github.com/yourusername/sudoku-solver.git
  2. Navigate to the project directory:

    cd sudoku-solver
  3. Run the program:

    python sudoku_solver.py

🎨 How the Colors Work

  • Green: Valid entries (numbers between 1-9).
  • Red: Invalid entries (out-of-range or non-numeric values).
  • White: Blank or unfilled cells, ready for input.

🧠 Why Backtracking?

Backtracking is perfect for Sudoku because it efficiently checks all potential number placements, ensuring the puzzle remains solvable under all constraints. It systematically tries different values for each empty cell, backtracking if conflicts occur, and moves forward if the value is valid.

Benefits of Backtracking:

  • Constraint Satisfaction: Adheres strictly to Sudoku rules (row, column, sub-grid).
  • Efficiency: Avoids unnecessary work by backtracking as soon as a conflict is found.
  • Complete Solution: Guarantees a solution if one exists, or confirms the puzzle is unsolvable.

🎉 Enjoy solving Sudoku puzzles with the power of algorithms!

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages