Mar-30-2018, 11:38 AM
Hi,
I am new to Python and programming in general. What I want to do is build a kind of "sudoku-solver" which is capable of doing the following tasks:
- let the user enter a random sudoku
- solve the sudoku via backtracking if it has a solution
- save sudoku and solution to a .txt file
- import sudokus from .txt file
...
This is how it looks like:
![[Image: sudokusolvervls8o.jpg]](https://abload.de/img/sudokusolvervls8o.jpg)
When pressing the solve button, this function is called:
I am new to Python and programming in general. What I want to do is build a kind of "sudoku-solver" which is capable of doing the following tasks:
- let the user enter a random sudoku
- solve the sudoku via backtracking if it has a solution
- save sudoku and solution to a .txt file
- import sudokus from .txt file
...
This is how it looks like:
![[Image: sudokusolvervls8o.jpg]](https://abload.de/img/sudokusolvervls8o.jpg)
When pressing the solve button, this function is called:
def solveSUDOKU(self): self.getinput(self.sudoku) # get user input from GUI and write it into sudoku arrey if not self.checkSUDOKU(self.sudoku)[0]: # check if user input makes sense self.message_txt.set("This sudoku does not have a solution") # ----- save time how long the solving took ----- # else: start = time.time() solve(0) # start backtracking end = time.time() self.message_txt.set("%s seconds, %s backtracks"%(round((end-start),3) ,self.backtrack_ctr)) self.backtrack_ctr = 0 # reset backtrack counter # ----- fill all fields with the solution ----- # for row in range(9): for col in range(9): self.values[row,col].delete(0,END) self.values[row,col].insert(0,self.solution[row][col])solve(0) is calling this recursive function:
def solve(num): SudokuSolver.backtrack_ctr += 1 if num==81: print("Solution found!") SudokuSolver.solution = SudokuSolver.copy(SudokuSolver,SudokuSolver.sudoku) return True else: row = int(num / 9) col = num % 9 if SudokuSolver.sudoku[row][col]!=' ': solve(num+1) else: for value in range(1,10): if SudokuSolver.consistent(SudokuSolver,SudokuSolver.sudoku, row,col,str(value)): SudokuSolver.sudoku[row][col]=str(value) if solve(num+1): return True SudokuSolver.sudoku[row][col]=' ' return FalseThis backtracking can take some time (depending on the sudoku). After a few seconds tkinter freezes and you cant do anything. Is there any possibility to not "crash" the GUI while waiting for the solution? I have looked into multiprocessing and multithreading but I didnt really understand how to do it.