基于
[color=]python3.11
,其他版本的python可能会报错。
随机刷新一个数独
[Python] 纯文本查看 复制代码import tkinter as tk
from tkinter import messagebox
import random
def is_valid(board, row, col, num):
for x in range(9):
if board[row][x] == num or board[x][col] == num:
return False
start_row, start_col = 3 * (row // 3), 3 * (col // 3)
for i in range(3):
for j in range(3):
if board[start_row + i][start_col + j] == num:
return False
return True
def generate_puzzle():
board = [[0] * 9 for _ in range(9)]
for _ in range(17): # Start with 17 clues
row, col = random.randint(0, 8), random.randint(0, 8)
num = random.randint(1, 9)
while not is_valid(board, row, col, num) or board[row][col] != 0:
row, col = random.randint(0, 8), random.randint(0, 8)
num = random.randint(1, 9)
board[row][col] = num
return board
class SudokuGUI:
def __init__(self, root):
self.root = root
self.root.title("Sudoku")
self.canvas = tk.Canvas(self.root, width=400, height=400, bg="white")
self.canvas.grid(row=0, column=0, columnspan=9)
self.puzzle = generate_puzzle()
self.cells = [[None for _ in range(9)] for _ in range(9)]
self.create_grid()
self.create_check_button()
self.draw_grid()
def create_grid(self):
for row in range(9):
for col in range(9):
cell_value = self.puzzle[row][col]
entry = tk.Entry(self.root, width=3, font=('Arial', 18), justify='center')
entry.place(x=20 + col * 40, y=20 + row * 40, width=40, height=40)
if cell_value != 0:
entry.insert(0, str(cell_value))
entry.config(state='readonly')
self.cells[row][col] = entry
def draw_grid(self):
# Draw thin grid lines
for i in range(10):
color = "black" if i % 3 == 0 else "gray"
x0, y0, x1, y1 = 20 + i * 40, 20, 20 + i * 40, 380
self.canvas.create_line(x0, y0, x1, y1, fill=color)
x0, y0, x1, y1 = 20, 20 + i * 40, 380, 20 + i * 40
self.canvas.create_line(x0, y0, x1, y1, fill=color)
# Draw thick grid lines
for i in range(4):
x0, y0, x1, y1 = 20 + i * 120, 20, 20 + i * 120, 380
self.canvas.create_line(x0, y0, x1, y1, fill="red", width=2)
x0, y0, x1, y1 = 20, 20 + i * 120, 380, 20 + i * 120
self.canvas.create_line(x0, y0, x1, y1, fill="red", width=2)
def create_check_button(self):
check_button = tk.Button(self.root, text="Check", command=self.check_solution)
check_button.grid(row=10, column=0, columnspan=9, pady=10)
def check_solution(self):
for row in range(9):
for col in range(9):
entry = self.cells[row][col]
try:
value = int(entry.get())
if not (1
解数独
[Python] 纯文本查看 复制代码import tkinter as tk
from tkinter import messagebox
def is_valid(board, row, col, num):
for x in range(9):
if board[row][x] == num or board[x][col] == num:
return False
start_row, start_col = 3 * (row // 3), 3 * (col // 3)
for i in range(3):
for j in range(3):
if board[start_row + i][start_col + j] == num:
return False
return True
def solve_sudoku(board):
for row in range(9):
for col in range(9):
if board[row][col] == 0:
for num in range(1, 10):
if is_valid(board, row, col, num):
board[row][col] = num
if solve_sudoku(board):
return True
board[row][col] = 0
return False
return True
class SudokuSolverGUI:
def __init__(self, root):
self.root = root
self.root.title("Sudoku Solver")
self.canvas = tk.Canvas(self.root, width=400, height=400, bg="white")
self.canvas.grid(row=0, column=0, columnspan=9)
self.cells = [[None for _ in range(9)] for _ in range(9)]
self.create_grid()
self.create_buttons()
self.draw_grid()
def create_grid(self):
for row in range(9):
for col in range(9):
entry = tk.Entry(self.root, width=3, font=('Arial', 18), justify='center')
entry.place(x=20 + col * 40, y=20 + row * 40, width=40, height=40)
self.cells[row][col] = entry
def draw_grid(self):
# 画出细的网格线
for i in range(10):
color = "black" if i % 3 == 0 else "gray"
x0, y0, x1, y1 = 20 + i * 40, 20, 20 + i * 40, 380
self.canvas.create_line(x0, y0, x1, y1, fill=color)
x0, y0, x1, y1 = 20, 20 + i * 40, 380, 20 + i * 40
self.canvas.create_line(x0, y0, x1, y1, fill=color)
# 画出粗的网格线
for i in range(4):
x0, y0, x1, y1 = 20 + i * 120, 20, 20 + i * 120, 380
self.canvas.create_line(x0, y0, x1, y1, fill="red", width=2)
x0, y0, x1, y1 = 20, 20 + i * 120, 380, 20 + i * 120
self.canvas.create_line(x0, y0, x1, y1, fill="red", width=2)
def create_buttons(self):
solve_button = tk.Button(self.root, text="Solve", command=self.solve_puzzle)
solve_button.grid(row=10, column=0, columnspan=4, pady=10)
clear_button = tk.Button(self.root, text="Clear", command=self.clear_grid)
clear_button.grid(row=10, column=5, columnspan=4, pady=10)
def solve_puzzle(self):
board = [[0] * 9 for _ in range(9)]
for row in range(9):
for col in range(9):
entry = self.cells[row][col]
value = entry.get()
if value.isdigit():
board[row][col] = int(value)
if solve_sudoku(board):
for row in range(9):
for col in range(9):
self.cells[row][col].delete(0, tk.END)
self.cells[row][col].insert(0, str(board[row][col]))
else:
messagebox.showwarning("Sudoku Solver", "No solution exists for the given input.")
def clear_grid(self):
for row in range(9):
for col in range(9):
self.cells[row][col].delete(0, tk.END)
if __name__ == "__main__":
root = tk.Tk()
solver = SudokuSolverGUI(root)
root.mainloop()