Solving is actually pretty fast due to Sudoku being an exact cover problem, which can be solved with Knuth's Algorithm X and then optimized with the "Dancing Links" optimization (which is very gross to implement. Thanks, Donald).
For my purposes the regular ol' X, implemented naively, was fast enough and can solve Sudokus in about 150ms on my desktop computer from 2015.
Generation can be done with the same algorithm: if you start with a blank field and "solve" it while choosing rows of the constraint matrix randomly, the result is going to be a random Sudoku. Upon generating a Sudoku, you can then backtrack until you have a minimum of numbers while only allowing one valid solution.
Evaluating the difficulty of a generated Sudoku is probably harder.
Why would you refuse to show your code until someone agrees to help? That just slows the whole process down. If you show your code upfront, someone can jump right in with giving advice.
Since sudoku states cannot be statically knowable to be solvable, you always need to run the solver to verify. Perhaps you forgot to discard an invalid board state and got stuck in an infinite loop?
Otherwise it's probably your algorithm, such as forgetting to check for the cell with the lowest number of options and creating an exponential complexity.
like "no visible progress in 30 entire seconds" slow.
The search runs on Tkinter’s main thread, so the GUI is blocked and can’t show progress or respond until generation finishes. That explains the "no visible progress", but not necessarily why generation takes so long. Which difficulty were you generating, and did this happen at startup or after clicking “New game”? That would help me reproduce the slowdown and investigate it.
yeah that was what i was thinking. I was trying to make a program to play and generate really difficult sudokus (up to AIC difficulty), but it failed to generate anything past locked candidates difficulty.
And for up to naked singles, it just gave a sudoku with one missing number.
12
u/SCD_minecraft 8d ago
6
u/schoolmonky 8d ago
Why would you refuse to show your code until someone agrees to help? That just slows the whole process down. If you show your code upfront, someone can jump right in with giving advice.
2
u/LayotFctor 8d ago
How slow are we talking here?
1
u/Kevdog824_ 8d ago
Can you describe the steps you do to generate boards and verify solutions? We might be able to point out an issue with your approach
1
u/QueasyCarBoy 8d ago
edit: i think i got a solution, i'm deleting this in like a wek
0
u/[deleted] 8d ago
[deleted] — view deleted comment