0% found this document useful (0 votes)
10 views

Message 2

Gggg

Uploaded by

Shibi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Message 2

Gggg

Uploaded by

Shibi
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

import tkinter as tk

from tkinter import messagebox


import time
import random

class SudokuSolverGUI:
def __init__(self, master):
self.master = master
self.master.title("Sudoku Solver")

self.grid = [[tk.StringVar() for _ in range(9)] for _ in range(9)]

self.create_widgets()

# Bind the spacebar key to move to the next entry


self.master.bind("<space>", self.focus_next_entry)

def create_widgets(self):
for i in range(9):
for j in range(9):
entry = tk.Entry(self.master, width=3, font=('Arial', 14),
textvariable=self.grid[i][j], justify='center')
entry.grid(row=i, column=j)

solve_button = tk.Button(self.master, text="Solve",


command=self.solve_sudoku)
solve_button.grid(row=9, columnspan=9)

def solve_sudoku(self):
grid = [[0 if cell.get() == '' else int(cell.get()) for cell in row] for
row in self.grid]

start_time = time.time()
if self.solve(grid):
end_time = time.time()
self.display_solution(grid)
messagebox.showinfo("Sudoku Solver", f"Sudoku Solved Successfully!\
nTime taken: {round(end_time - start_time, 4)} seconds")
else:
messagebox.showinfo("Sudoku Solver", "No solution exists.")

def solve(self, grid):


empty_loc = [0, 0]
if not self.find_empty_location(grid, empty_loc):
return True

row, col = empty_loc[0], empty_loc[1]

for num in range(1, 10):


if self.is_safe_location(grid, row, col, num):
grid[row][col] = num
if self.solve(grid):
return True
grid[row][col] = 0

return False

def find_empty_location(self, grid, l):


for row in range(9):
for col in range(9):
if grid[row][col] == 0:
l[0] = row
l[1] = col
return True
return False

def is_safe_location(self, grid, row, col, num):


return not self.used_in_row(grid, row, num) and \
not self.used_in_col(grid, col, num) and \
not self.used_in_box(grid, row - row % 3, col - col % 3, num)

def used_in_row(self, grid, row, num):


return num in grid[row]

def used_in_col(self, grid, col, num):


return num in [grid[i][col] for i in range(9)]

def used_in_box(self, grid, row, col, num):


return num in [grid[row + i][col + j] for i in range(3) for j in range(3)]

def display_solution(self, grid):


for i in range(9):
for j in range(9):
self.grid[i][j].set(grid[i][j])

def focus_next_entry(self, event):


"""Move focus to the next entry when spacebar is pressed."""
current_row, current_col = self.master.focus_get().grid_info()["row"],
self.master.focus_get().grid_info()["column"]
next_col = (current_col + 1) % 9
next_row = current_row + 1 if next_col == 0 else current_row
self.master.children[f"!entry{next_row}.{next_col}"].focus_set()

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

if __name__ == "__main__":
main()

You might also like