直接上代码
1 #!/usr/bin/python3 2 #coding=GB2312 3 import tkinter as tk 4 import threading 5 import time 6 import random 7 import copy 8 import sys 9 10 class Sudoku(threading.Thread): 11 winw = 500 12 winh = 510 13 14 def __init__(self): 15 threading.Thread.__init__(self) 16 self.ui = tk.Tk() 17 self.centerDisplay() 18 self.cells = [] 19 self.createSudoku() 20 21 self.initUi() 22 def checkRow(self, row, value): 23 if value in self.tmpCells[row]: 24 return False 25 return True 26 def checkCol(self, col, value): 27 for row in range(9): 28 if value == self.tmpCells[row][col]: 29 return False 30 return True 31 def checkBlock(self, row, col, value): 32 for i in range(3): 33 for j in range(3): 34 if value == self.tmpCells[int(row / 3) * 3 + i][int(col / 3) * 3 + j]: 35 return False 36 return True 37 def solveSudoku(self): 38 for i in range(9): 39 for j in range(9): 40 if 0 == self.tmpCells[i][j]: 41 for tmp in range(1, 10): 42 if(self.checkRow(i, tmp) and 43 self.checkCol(j, tmp) and 44 self.checkBlock(i, j, tmp)): 45 self.tmpCells[i][j] = tmp 46 if (self.solveSudoku()): 47 return True 48 else: 49 self.updateUi() 50 self.tmpCells[i][j] = self.cells[i][j] 51 continue 52 else: 53 continue 54 return False 55 return True 56 57 def createSudoku(self): 58 #self.cells = [[None for i in range(9)] for i in range(9)] 59 self.cells = [ 60 [0, 0, 5, 3, 0, 0, 0, 0, 0], 61 [8, 0, 0, 0, 0, 0, 0, 2, 0], 62 [0, 7, 0, 0, 1, 0, 5, 0, 0], 63 [4, 0, 0, 0, 0, 5, 3, 0, 0], 64 [0, 1, 0, 0, 7, 0, 0, 0, 6], 65 [0, 0, 3, 2, 0, 0, 0, 8, 0], 66 [0, 6, 0, 5, 0, 0, 0, 0, 9], 67 [0, 0, 4, 0, 0, 0, 0, 3, 0], 68 [0, 0, 0, 0, 0, 9, 7, 0, 0]] 69 self.tmpCells = copy.deepcopy(self.cells) 70 71 def initUi(self): 72 self.cs = tk.Canvas(self.ui, bg = '#7fb081') 73 self.cs.pack(fill = tk.BOTH, expand = 1) 74 w = self.winw / 9.0 75 h = self.winh / 9.0 76 for n in range(9): 77 if 0 == n % 3: 78 width = 4 79 else: 80 width = 1 81 self.cs.create_line(0, n * h, self.winw, n * h, 82 width = width, fill = '#2b4490') 83 self.cs.create_line(n * w, 0, n * w, self.winh, 84 width = width, fill = '#2b4490') 85 for i in range(9): 86 for j in range(9): 87 if self.cells[i][j]: 88 self.cs.create_text(30 + j * 55, 30 + i * 57, 89 font = ('Times -55 bold'), text = self.cells[i][j]) 90 def updateUi(self): 91 self.cs.delete('test') 92 for i in range(9): 93 for j in range(9): 94 if not self.cells[i][j] and self.tmpCells[i][j]: 95 self.cs.create_text(30 + j * 55, 30 + i * 57, 96 font = ('Times -55 bold'), text = self.tmpCells[i][j], 97 tag = 'test', fill = '#aa363d') 98 99 100 def centerDisplay(self): 101 screenw = self.ui.winfo_screenwidth() 102 screenh = self.ui.winfo_screenheight() 103 self.ui.geometry('{}x{}+{}+{}'.format( 104 self.winw, self.winh, int((screenw - self.winw)/2), 105 int((screenh - self.winh)/2))) 106 self.ui.title('Sudoku by jianc') 107 108 def run(self): 109 print(self.solveSudoku()) 110 111 self.updateUi() 112 113 sys.setrecursionlimit(100000) 114 sudoku = Sudoku() 115 sudoku.start() 116 tk.mainloop()