|
| 1 | +''' |
| 2 | +GUI which allows you to solve any Sudoku puzzle. |
| 3 | +
|
| 4 | +The GUI has been created using Tkinter and a backtracking algorithm has been used. |
| 5 | +''' |
| 6 | + |
| 7 | +from tkinter import * |
| 8 | + |
| 9 | +root = Tk() |
| 10 | +root.geometry('330x370') |
| 11 | + |
| 12 | +# Sudoku solver class |
| 13 | +class SudokuSolver(): |
| 14 | + |
| 15 | + def __init__(self): |
| 16 | + self.setZero() |
| 17 | + self.solve() |
| 18 | + |
| 19 | + # Set the empty cells to 0 (i.e. the cells you have not filled in) |
| 20 | + def setZero(self): |
| 21 | + for i in range(9): |
| 22 | + for j in range(9): |
| 23 | + if filledBoard[i][j].get() not in ['1','2','3','4','5','6','7','8','9']: |
| 24 | + filledBoard[i][j].set(0) |
| 25 | + |
| 26 | + # Solve using backtracking |
| 27 | + def solve(self): |
| 28 | + # Find next empty cell |
| 29 | + findEmpty = self.emptyCell() |
| 30 | + |
| 31 | + if not findEmpty: |
| 32 | + return True |
| 33 | + else: |
| 34 | + row, column = findEmpty |
| 35 | + |
| 36 | + for i in range(1,10): |
| 37 | + if self.isValid(i, (row, column)): |
| 38 | + filledBoard[row][column].set(i) |
| 39 | + |
| 40 | + if self.solve(): |
| 41 | + return True |
| 42 | + |
| 43 | + filledBoard[row][column].set(0) |
| 44 | + |
| 45 | + return False |
| 46 | + |
| 47 | + # Check row, column and subgrid(3x3 square) to see if number can be placed in cell |
| 48 | + def isValid (self, num, pos): |
| 49 | + # Check Row |
| 50 | + for i in range(9): |
| 51 | + if filledBoard[pos[0]][i].get() == str(num): |
| 52 | + return False |
| 53 | + # Check Column |
| 54 | + for i in range(9): |
| 55 | + if filledBoard[i][pos[1]].get() == str(num): |
| 56 | + return False |
| 57 | + |
| 58 | + # Check Sub Grid |
| 59 | + row = pos[0] // 3 |
| 60 | + column = pos[1] // 3 |
| 61 | + |
| 62 | + for i in range(row * 3, (row * 3) + 3): |
| 63 | + for j in range(column * 3, (column * 3) + 3): |
| 64 | + if filledBoard[i][j].get() == str(num): |
| 65 | + return False |
| 66 | + return True |
| 67 | + |
| 68 | + # Find empty cells, defined as cells filled with a zero |
| 69 | + def emptyCell(self): |
| 70 | + for row in range(9): |
| 71 | + for column in range(9): |
| 72 | + if filledBoard[row][column].get() == '0': |
| 73 | + return (row,column) |
| 74 | + return None |
| 75 | + |
| 76 | +# GUI class |
| 77 | +class Interface(): |
| 78 | + def __init__(self, window): |
| 79 | + self.window = window |
| 80 | + window.title("Sudoku Solver") |
| 81 | + |
| 82 | + font = ('Arial', 20) |
| 83 | + color = 'white' |
| 84 | + |
| 85 | + # Create solve and clear button and link them to Solve and Clear methods |
| 86 | + solve = Button(window, text = 'Solve', command = self.Solve) |
| 87 | + solve.grid(column=3,row=20) |
| 88 | + clear = Button(window, text = 'Clear', command = self.Clear) |
| 89 | + clear.grid(column = 5,row=20) |
| 90 | + |
| 91 | + # Initialise empty 2D list |
| 92 | + self.board = [] |
| 93 | + for row in range(9): |
| 94 | + self.board += [["","","","","","","","",""]] |
| 95 | + |
| 96 | + for row in range(9): |
| 97 | + for col in range(9): |
| 98 | + # Change color of cells depending on position in grid |
| 99 | + if (row < 3 or row > 5) and (col < 3 or col > 5): |
| 100 | + color = 'white' |
| 101 | + elif (row >= 3 and row < 6) and (col >=3 and col < 6): |
| 102 | + color = 'white' |
| 103 | + else: |
| 104 | + color = 'grey' |
| 105 | + |
| 106 | + # Make each cell of grid a entry box and store each user entry into the filledBoard 2D list |
| 107 | + self.board[row][col] = Entry(window, width = 2, font = font, bg = color, cursor = 'arrow', borderwidth = 2, |
| 108 | + highlightcolor = 'yellow', highlightthickness = 0, highlightbackground = 'black', |
| 109 | + textvariable = filledBoard[row][col]) |
| 110 | + self.board[row][col].bind('<FocusIn>', self.gridChecker) # Link to better understand binding statements: https://www.python-course.eu/tkinter_events_binds.php#:~:text=Tkinter%20provides%20a%20mechanism%20to,and%20methods%20to%20an%20event.&text=If%20the%20defined%20event%20occurs,describing%20the%20event. |
| 111 | + self.board[row][col].bind('<Motion>', self.gridChecker) |
| 112 | + self.board[row][col].grid(row = row, column = col) |
| 113 | + |
| 114 | + # Function to check if user enters a value which is not an int between 1 and 9 (valid numbers in Sudoku game). |
| 115 | + # If entry is not valid, clear value |
| 116 | + def gridChecker(self, event): |
| 117 | + for row in range(9): |
| 118 | + for col in range(9): |
| 119 | + if filledBoard[row][col].get() not in ['1','2','3','4','5','6','7','8','9']: |
| 120 | + filledBoard[row][col].set('') |
| 121 | + |
| 122 | + # Call Sudoku solver class (called by solve button) |
| 123 | + def Solve(self): |
| 124 | + SudokuSolver() |
| 125 | + |
| 126 | + # Function to clear board (called by clear button) |
| 127 | + def Clear(self): |
| 128 | + for row in range(9): |
| 129 | + for col in range(9): |
| 130 | + filledBoard[row][col].set('') |
| 131 | + |
| 132 | +# Global 2D list which saved in the values the user enters on the GUI |
| 133 | +# Each value in the 2D list is set as a StringVar(), a class in Tkinter |
| 134 | +# which allows you to save values users enter into the Entry widget |
| 135 | +filledBoard = [] |
| 136 | +for row in range(9): |
| 137 | + filledBoard += [["","","","","","","","",""]] |
| 138 | +for row in range(9): |
| 139 | + for col in range(9): |
| 140 | + filledBoard[row][col] = StringVar(root) |
| 141 | + |
| 142 | +# Main Loop |
| 143 | +Interface(root) |
| 144 | +root.mainloop() |
| 145 | + |
| 146 | + |
| 147 | + |
0 commit comments