An algorithm for solving Sudokus which uses the Tkinter module to create a graphical user interface. The idea for this project came in an Information-Management-lecture where we solved Sudokus by hand in preparation for the lecture on sorting algorithms.
It just works for easy and medium Sudokus when only one step involes guessing.
For the user inputs, I used the Canvas object from the Tkinter module. It displays an empty Sudoku grid and generates 81 input fields with 2 for-loops.
When the solve button is pressed all the data is fetched and added to a list of dictionaries. These dictionaries contain the index, the value, the quadrant, the row, the column, and a list of possible values. If a field has a value, the possible value list will be empty.
The solve-function starts by checking the quadrant, rows, and columns for each field. All the found values in the same quarter, row or column are then removed from the possible-values list.
If a field has just one possible value this value will be assigned to the field. Unfortunately, that only works for Sudokus if there is no guessing involved.
If there were no values assigned to any field and the game is not over, all the data will be backed up before guessing. Then all the fields with 2 possible values are checked.
This only works for easy and medium difficult Sudokus and very rarely for hard ones, because after the first guess all other steps must be assigned directly otherwise the program will revert to the backed-up data and try again. The checked values that didn’t lead to a result are saved in a list as well.
In the future, I want to implement the saving of multiple versions so that the guessing can be repeated. For harder Sudokus, it also maybe required to guess for fields which have more than 2 possible values.