From 136f4bbfbafd6178e13d8bb264a762b85d454b25 Mon Sep 17 00:00:00 2001 From: deeshant Date: Mon, 29 Apr 2024 21:05:31 +0530 Subject: [PATCH 1/3] new sudoku solver in Game section --- GAMES/sudoku_solver/sudoku_solver.py | 88 ++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 GAMES/sudoku_solver/sudoku_solver.py diff --git a/GAMES/sudoku_solver/sudoku_solver.py b/GAMES/sudoku_solver/sudoku_solver.py new file mode 100644 index 00000000..1047dc5c --- /dev/null +++ b/GAMES/sudoku_solver/sudoku_solver.py @@ -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() From c6b38573fdb54e126acb38115d58b8c652369a6f Mon Sep 17 00:00:00 2001 From: deeshant Date: Mon, 29 Apr 2024 21:22:02 +0530 Subject: [PATCH 2/3] added readme --- GAMES/sudoku_solver/readme.md | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 GAMES/sudoku_solver/readme.md diff --git a/GAMES/sudoku_solver/readme.md b/GAMES/sudoku_solver/readme.md new file mode 100644 index 00000000..e72028e1 --- /dev/null +++ b/GAMES/sudoku_solver/readme.md @@ -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! \ No newline at end of file From b08c376cd252c93a734a50b620894f41acfdd3f6 Mon Sep 17 00:00:00 2001 From: deeshant Date: Mon, 29 Apr 2024 21:26:53 +0530 Subject: [PATCH 3/3] mend --- GAMES/sudoku_solver/readme.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/GAMES/sudoku_solver/readme.md b/GAMES/sudoku_solver/readme.md index e72028e1..c4730c4f 100644 --- a/GAMES/sudoku_solver/readme.md +++ b/GAMES/sudoku_solver/readme.md @@ -1,21 +1,21 @@ -##Sudoku Solver GUI +## Sudoku Solver GUI -##Overview +# 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 +# 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 +# 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 +# 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.