Skip to content

Commit a946734

Browse files
authored
Merge pull request larymak#384 from Dishant-Tyagi/main
new sudoku solver
2 parents 5b664d1 + b08c376 commit a946734

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed

GAMES/sudoku_solver/readme.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
## Sudoku Solver GUI
2+
3+
# Overview
4+
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.
5+
6+
# Features
7+
Interactive GUI: The Sudoku Solver features a user-friendly interface built with Tkinter, allowing you to input Sudoku puzzles and visualize the solution.
8+
Backtracking Algorithm: The Sudoku Solver uses a backtracking algorithm to find the solution to the input puzzle.
9+
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.
10+
11+
# How to Run
12+
Make sure you have Python installed on your system.
13+
Clone this repository or download the sudoku_solver.py file.
14+
Open a terminal or command prompt and navigate to the directory containing sudoku_solver.py.
15+
Run the command python sudoku_solver.py.
16+
The Sudoku Solver GUI window will open, allowing you to input Sudoku puzzles and visualize the solution.
17+
18+
# How to Use
19+
When the Sudoku Solver GUI window opens, you'll see a 9x9 grid representing the Sudoku puzzle.
20+
enter value row by row in terminal ,empty space is denote by '0'.
21+
After entering the puzzle, click the "Solve" button to start solving the Sudoku puzzle.
22+
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.
23+
Additional Notes
24+
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.
25+
You can input any valid Sudoku puzzle into the solver, and it will find the solution if one exists.
26+
If there are multiple solutions to the puzzle, the solver will find one of them.
27+
The GUI provides visual feedback on the solution process, making it easy to understand how the solver works.
28+
Enjoy using the Sudoku Solver GUI!

GAMES/sudoku_solver/sudoku_solver.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import tkinter as tk
2+
3+
class SudokuSolverGUI:
4+
def __init__(self, master):
5+
self.master = master
6+
self.master.title("Sudoku Solver")
7+
self.board = [[0 for _ in range(9)] for _ in range(9)]
8+
self.input_sudoku()
9+
self.create_widgets()
10+
11+
def input_sudoku(self):
12+
print("Enter the Sudoku puzzle values row by row:")
13+
for i in range(9):
14+
row_input = input().split()
15+
for j in range(9):
16+
self.board[i][j] = int(row_input[j])
17+
18+
def create_widgets(self):
19+
self.canvas = tk.Canvas(self.master, width=450, height=450, bg="white")
20+
self.canvas.pack()
21+
22+
for i in range(10):
23+
line_width = 2 if i % 3 == 0 else 1
24+
self.canvas.create_line(50 * i, 0, 50 * i, 450, width=line_width)
25+
self.canvas.create_line(0, 50 * i, 450, 50 * i, width=line_width)
26+
27+
for i in range(9):
28+
for j in range(9):
29+
value = self.board[i][j]
30+
if value != 0:
31+
x, y = j * 50 + 25, i * 50 + 25
32+
self.canvas.create_text(x, y, text=str(value), font=("Arial", 14, "bold"))
33+
34+
self.solve_button = tk.Button(self.master, text="Solve", command=self.solve_sudoku)
35+
self.solve_button.pack()
36+
37+
def solve_sudoku(self):
38+
self.solve_button.config(state="disabled")
39+
self.solve()
40+
41+
def solve(self):
42+
empty_cell = self.find_empty_cell()
43+
if not empty_cell:
44+
self.solve_button.config(state="normal")
45+
return True
46+
47+
row, col = empty_cell
48+
for num in range(1, 10):
49+
if self.is_valid_move(num, row, col):
50+
self.board[row][col] = num
51+
self.update_cell(row, col, num)
52+
if self.solve():
53+
return True
54+
self.board[row][col] = 0
55+
self.update_cell(row, col, 0)
56+
return False
57+
58+
def find_empty_cell(self):
59+
for i in range(9):
60+
for j in range(9):
61+
if self.board[i][j] == 0:
62+
return i, j
63+
return None
64+
65+
def is_valid_move(self, num, row, col):
66+
for i in range(9):
67+
if self.board[row][i] == num or self.board[i][col] == num:
68+
return False
69+
start_row, start_col = 3 * (row // 3), 3 * (col // 3)
70+
for i in range(start_row, start_row + 3):
71+
for j in range(start_col, start_col + 3):
72+
if self.board[i][j] == num:
73+
return False
74+
return True
75+
76+
def update_cell(self, row, col, num):
77+
x, y = col * 50 + 25, row * 50 + 25
78+
self.canvas.delete(f"number_{row}_{col}")
79+
if num != 0:
80+
self.canvas.create_text(x, y, text=str(num), font=("Arial", 14, "bold"), tags=f"number_{row}_{col}")
81+
82+
def main():
83+
root = tk.Tk()
84+
app = SudokuSolverGUI(root)
85+
root.mainloop()
86+
87+
if __name__ == "__main__":
88+
main()

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy