Skip to content

Commit

Permalink
Merge pull request larymak#384 from Dishant-Tyagi/main
Browse files Browse the repository at this point in the history
new sudoku solver
  • Loading branch information
larymak authored May 8, 2024
2 parents 5b664d1 + b08c376 commit a946734
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
28 changes: 28 additions & 0 deletions GAMES/sudoku_solver/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
## Sudoku Solver GUI

# Overview
This is a simple Sudoku Solver implemented in Python with a graphical user interface (GUI) using the Tkinter library. The Sudoku Solver allows you to input a Sudoku puzzle and then solves it using a backtracking algorithm. You can visualize the solution step by step on the GUI.

# Features
Interactive GUI: The Sudoku Solver features a user-friendly interface built with Tkinter, allowing you to input Sudoku puzzles and visualize the solution.
Backtracking Algorithm: The Sudoku Solver uses a backtracking algorithm to find the solution to the input puzzle.
Step-by-Step Solution: You can click the "Solve" button to start solving the Sudoku puzzle step by step, and observe how the solver fills in the cells.

# How to Run
Make sure you have Python installed on your system.
Clone this repository or download the sudoku_solver.py file.
Open a terminal or command prompt and navigate to the directory containing sudoku_solver.py.
Run the command python sudoku_solver.py.
The Sudoku Solver GUI window will open, allowing you to input Sudoku puzzles and visualize the solution.

# How to Use
When the Sudoku Solver GUI window opens, you'll see a 9x9 grid representing the Sudoku puzzle.
enter value row by row in terminal ,empty space is denote by '0'.
After entering the puzzle, click the "Solve" button to start solving the Sudoku puzzle.
You can observe how the solver fills in the cells step by step. Once the puzzle is solved, you'll see the complete solution on the GUI.
Additional Notes
The Sudoku Solver uses a backtracking algorithm to find the solution to the puzzle. It tries different numbers in each cell and backtracks if it reaches a dead-end.
You can input any valid Sudoku puzzle into the solver, and it will find the solution if one exists.
If there are multiple solutions to the puzzle, the solver will find one of them.
The GUI provides visual feedback on the solution process, making it easy to understand how the solver works.
Enjoy using the Sudoku Solver GUI!
88 changes: 88 additions & 0 deletions GAMES/sudoku_solver/sudoku_solver.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import tkinter as tk

class SudokuSolverGUI:
def __init__(self, master):
self.master = master
self.master.title("Sudoku Solver")
self.board = [[0 for _ in range(9)] for _ in range(9)]
self.input_sudoku()
self.create_widgets()

def input_sudoku(self):
print("Enter the Sudoku puzzle values row by row:")
for i in range(9):
row_input = input().split()
for j in range(9):
self.board[i][j] = int(row_input[j])

def create_widgets(self):
self.canvas = tk.Canvas(self.master, width=450, height=450, bg="white")
self.canvas.pack()

for i in range(10):
line_width = 2 if i % 3 == 0 else 1
self.canvas.create_line(50 * i, 0, 50 * i, 450, width=line_width)
self.canvas.create_line(0, 50 * i, 450, 50 * i, width=line_width)

for i in range(9):
for j in range(9):
value = self.board[i][j]
if value != 0:
x, y = j * 50 + 25, i * 50 + 25
self.canvas.create_text(x, y, text=str(value), font=("Arial", 14, "bold"))

self.solve_button = tk.Button(self.master, text="Solve", command=self.solve_sudoku)
self.solve_button.pack()

def solve_sudoku(self):
self.solve_button.config(state="disabled")
self.solve()

def solve(self):
empty_cell = self.find_empty_cell()
if not empty_cell:
self.solve_button.config(state="normal")
return True

row, col = empty_cell
for num in range(1, 10):
if self.is_valid_move(num, row, col):
self.board[row][col] = num
self.update_cell(row, col, num)
if self.solve():
return True
self.board[row][col] = 0
self.update_cell(row, col, 0)
return False

def find_empty_cell(self):
for i in range(9):
for j in range(9):
if self.board[i][j] == 0:
return i, j
return None

def is_valid_move(self, num, row, col):
for i in range(9):
if self.board[row][i] == num or self.board[i][col] == num:
return False
start_row, start_col = 3 * (row // 3), 3 * (col // 3)
for i in range(start_row, start_row + 3):
for j in range(start_col, start_col + 3):
if self.board[i][j] == num:
return False
return True

def update_cell(self, row, col, num):
x, y = col * 50 + 25, row * 50 + 25
self.canvas.delete(f"number_{row}_{col}")
if num != 0:
self.canvas.create_text(x, y, text=str(num), font=("Arial", 14, "bold"), tags=f"number_{row}_{col}")

def main():
root = tk.Tk()
app = SudokuSolverGUI(root)
root.mainloop()

if __name__ == "__main__":
main()

0 comments on commit a946734

Please sign in to comment.