Several weeks ago, I created a post about Sudoku and shared my MATLAB code for solving Sudoku puzzle. Sadly, it turns out that my code was a failure. It can solve up to several hard-difficulty puzzle in websudoku.com but it fails for most of hard-difficulty puzzles and all evil-difficulty puzzle (Yes! Evil difficulty. I wonder why they naming it evil. Why not Super Hard or Very Hard?). Seeing this, my obssessive compulsif disorder kicks in and I decided to improve my code. Osh!
After reviewing the code, I knew that the algorithm was a mess and it does not allow me to implement guessing scheme (step 4 in the previous Sudoku! post). I also found out that several block was repeated for several time but I didn’t make it as a local function. Man, What the hell was I thinking when I created this code? This is a complete mess. So, I decided to throw away this code and start again from the scratch.
So, yesterday, I woke up 9AM and started re-creating my Sudoku Solver with the guessing scheme. After a litre of tears and sweats, it is 5PM and I finally finished the code, Yay! (dancing happily). Then, I tested this code against the evil-difficulty puzzle in websudoku.com and guess what, it works (Yay!). I have tested this for 4 or 5 of the evil-diffuclty puzzle and my code worked for all of them. Thanks to 8 hours well wasted in creating this code, now I’m listed as top 1% for evil-difficulty puzzle in websudoku.com.
The cool thing about this new code is that since the guessing scheme is implemented to solve very hard puzzle, it can also be used to generate a random sudoku puzzle. Yeah, by inserting a completely blank board as the input for this code, the code will create a random completed Sudoku puzzle. Thus, with this code, I can also generate my own puzzle simply by removing several numbers from the completed Sudoku board. It’s like killing two birds with one stone:D.
In the night (still yesterday), I created GUI for my Sudoku solver code. This GUI can be used to start a Sudoku game or to solve a puzzle that you cannot solve (no offense:D). Here are several screenshot of the GUI.
MATLAB Sudoku – Screenshot 01
MATLAB Sudoku – Screenshot 03
As usual, the Sudoku solver code (SudokuSolver.m) and its GUI (Sudoku.m) is shared to all of you and you can download the m-files from my MATLAB Fun Toolbox page. You are welcomed to download the m-files and if you do, please leave me any comments or critics or suggestion so that I can improve my code:D. Hope this is useful for you (and also fun).