Many times I have been writing a set of algorithms for solving Sudoku, but it has always overgrowing me and I was giving up. Today at night, without any thinking and planning, I just did it. Ok, it is not finished yet, it is rather a very good beginning – I have done something that works and helps in Sudoku solving, I have many ideas for new features and the most important – I got the bug. Download jar archive here.
select: arrows, WSAD, left mouse button
erase: delete, backspace, Q
The program is really simple at this point. User cannot specify the strength of auto-solving algorithms or turn them all off (and switch to the game mode). Also user has no control over suggestions. The program always start with an empty grid.
It all is going to change. The aim is to implement a full Sudoku game/solver on a grid of any size. The user should be able to define how much help he/she is going to receive by specifying the strength of auto-solving algorithms and making the suggestions automatical or manual.
When I have done it today at night, it was all in one class and most options where hard-coded. I spent all day to separate it into two classes, even though they are still strongly connected and the back end class contains elements of GUI. The reason for that is that GUI is updated while the algorithm is filling grid. Otherwise the program would have to wait for the algorithm to stop working and then update everything – passing changes to GUI would be much more difficult than it is now.
I have implement two solving algorithms so far. The first one checks all cells’ suggestions, if any cell’s suggestion contain only one number, the cell is filled with this number. The second is more complex:
for all cells for all digits if a digit is not in the suggestions of any other cell in current row, column or square fill current cell with this digit
This algorithm contains however a minor bug – it does not do this for all cells, but only for neighbour cells (the cells that are in the same row, column or square as a last filled cell). Moreover, my current structure does not allow me to easily change it, because cells do not have access to all the other cells, but only to neighbour ones. What I am going to do is introducing a class called Grid in between my current classes Sudoku with GUI and Cell with the main functionality.
The other important change I have made while rewriting the code today was making many hard-coded parameters a constant ones. The most important one is responsible for the sizes of the grid and is fully functional. However my current key listener does not allow to enter two-digit numbers into fields. So the only working sizes are 9×9 and 4×4. Well, 1×1 too, but it does not really look like Sudoku, does it?
I have also learned today something I always wanted to know – using for each loop on my own objects. It was not that hard as I always thought.
52 seconds long “gameplay”*:
*it is over 30MB large so it may take a while to load, but it is really here