Python数独游戏
时间: 2023-12-14 17:33:21 浏览: 114
以下是使用Python和Tkinter实现数独游戏的示例代码:
```python
import tkinter as tk
class Sudoku:
def __init__(self, board):
self.board = board
self.solution = None
def solve(self):
self.solution = self.board.copy()
self._solve_helper(0, 0)
def _solve_helper(self, row, col):
if row == 9:
return True
next_row = row if col < 8 else row + 1
next_col = (col + 1) % 9
if self.board[row][col] != 0:
return self._solve_helper(next_row, next_col)
for num in range(1, 10):
if self._is_valid(row, col, num):
self.solution[row][col] = num
if self._solve_helper(next_row, next_col):
return True
self.solution[row][col] = 0
return False
def _is_valid(self, row, col, num):
for i in range(9):
if self.solution[row][i] == num or self.solution[i][col] == num:
return False
subgrid_row = (row // 3) * 3
subgrid_col = (col // 3) * 3
for i in range(3):
for j in range(3):
if self.solution[subgrid_row + i][subgrid_col + j] == num:
return False
return True
class SudokuGUI:
def __init__(self, board):
self.board = board
self.sudoku = Sudoku(board)
self.sudoku.solve()
self.window = tk.Tk()
self.window.title("Sudoku")
self.canvas = tk.Canvas(self.window, width=450, height=450)
self.canvas.pack()
self._draw_grid()
self._draw_numbers()
self.window.mainloop()
def _draw_grid(self):
for i in range(10):
width = 3 if i % 3 == 0 else 1
x0 = 50 + i * 40
y0 = 50
x1 = x0
y1 = 410
self.canvas.create_line(x0, y0, x1, y1, width=width)
x0 = 50
y0 = 50 + i * 40
x1 = 410
y1 = y0
self.canvas.create_line(x0, y0, x1, y1, width=width)
def _draw_numbers(self):
for i in range(9):
for j in range(9):
if self.board[i][j] != 0:
x = 70 + j * 40
y = 70 + i * 40
self.canvas.create_text(x, y, text=str(self.board[i][j]), font=("Arial", 20))
for i in range(9):
for j in range(9):
if self.board[i][j] == 0:
x = 70 + j * 40
y = 70 + i * 40
self.canvas.create_text(x, y, text=str(self.sudoku.solution[i][j]), font=("Arial", 20), tags="number")
self.canvas.bind("<Button-1>", self._on_click)
def _on_click(self, event):
x, y = event.x, event.y
col = (x - 50) // 40
row = (y - 50) // 40
if 0 <= row < 9 and 0 <= col < 9:
self.canvas.delete("selected")
x0 = 50 + col * 40
y0 = 50 + row * 40
x1 = x0 + 40
y1 = y0 + 40
self.canvas.create_rectangle(x0, y0, x1, y1, outline="blue", tags="selected")
def run(self):
self.window.mainloop()
board = [
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0]
]
# 在这里填入数独题目的初始状态
# 例如:
# board = [
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0]
# ]
gui = SudokuGUI(board)
gui.run()
```
运行代码后,将会弹出一个窗口,显示数独游戏的界面。玩家可以点击空白格子,然后输入数字,程序会自动判断是否符合数独规则,并在正确的情况下填入数字。当所有空白格子都被填满时,程序会自动判断答案是否正确。
阅读全文