The post Sudoku Library appeared first on Furcino.com.

]]>For this project I use Java. It is not that I cannot use any other object-oriented language, but Java is the language I currently work with. C was the mandatory language in the class. I think using C helped us to understand concepts like working directly with the memory and it is useful for programming microcontrollers. However, Java brings with it very useful tools, that make the job easier. To create and manage the project I use Maven. Also, I use Eclipse IDE for development and testing instead of the text editors I used in the university, which did not offer more than syntax highlighting.

Another useful habit is using Test-driven development. For a long time, I was not a fan of tests. I considered them to be a waste of time and could not understand how they would be of help. It was not until I saw them working on real projects and saving the developers a lot of time and tedious searching through code, that I became a believer. The Sudoku solver project is a great opportunity to write a few tests and see them helping you to achieve your goal. All you need is some input data in the form of unsolved puzzles and you can test your creation with a few clicks in the IDE of your choice. I do not write tests for everything and maybe I should, but this was supposed to be fun.

I have actually solved the original problem in a shorter time than what I have spent on it in the university. First, I wrote a solver, which attempts to input all possible numbers starting from one. This solver can work in a lot of cases, but eventually, you will find puzzles, where it takes too long to find a solution. Then I have created a solver, which prioritizes which cells to solve first based on the number of possible solutions. This solver takes a reasonable time for all puzzles. You can read about it more here. After I was done, I have created a solver that attempts to solve puzzles like a human. This enabled me to create a generator, which creates puzzles that can be solved by people. After all that, I wanted to see the produced Sudokus on paper and I added PDF output.

You can see the PDF output of my Sudoku generator here. The generator is still a work in progress. Although the PDF file contains the Sudokus of the lowest difficulty, it takes a long time to solve them for a human. However, I am very confident that every puzzle can be solved by a hand and has only one solution. In addition to generating PDFs, I decided to share some Sudokus in string format as test data, so you can attempt to write your own solution. Finally, I decided to upload some of the code to github for inspiration.

I have managed to do the assignment in a fraction of the time I spent on it in the university. The main advantage is the tests, which enable me to see the impact of my changes to the code right away. Also, using the object-oriented programming has its own benefits. It was a fun project and it assured me, that I have come a long way since the university.

The post Sudoku Library appeared first on Furcino.com.

]]>The post Solving Sudokus appeared first on Furcino.com.

]]>Sudoku is a game in which no number can be repeated in a cell within the same grid, row or column. This rule helps us to narrow the number of possible solutions significantly. Therefore, it is important to keep a record of values we can use in a cell. Let us imagine we would like to place a value X=5 in a cell in the 6th row in the 6th column in an empty Sudoku. All other cells are empty. The following example shows where we could place the value X in other cells in next steps. So ‘1’ represents cells where X could be placed or is already placed, and ‘0’ represents where X cannot be placed anymore.

111|110|111 111|110|111 111|110|111 ----------- 111|000|111 111|000|111 000|001|000 ----------- 111|110|111 111|110|111 111|110|111

We can assign values, and query if we can use a value for a cell by using the Sudoku class functions. We can also mark a value as not valid for a cell.

The first thing that comes to mind when solving a Sudoku with a computer is to try every possible value for each cell until you solve a puzzle. It is the easiest approach. It is just a few lines of code, and you get a result eventually (given you have an eternity to get there). In my Sudoku library, the **SudokuDeterministicSolver** class does just that, and it works on a lot of puzzles. However, sooner or later you find a puzzle that takes a long time to be solved. Even some of the test Sudokus I have made available take a rather long time to be solved with this approach. That is why it was necessary to do things differently.

In the **SudokuPossibilityBasedSolver** class, I first check how many values are valid candidates for each cell. I start with the cells that have the least number of possible candidates. Once I have used up all the possibilities for a cell, I return zero to indicate that the combination of tested values cannot produce a solution. This works much faster because since I am trying the cells with less possible values first, it is more probable I fail or succeed sooner. I am not the first one to use this approach, and I would like to recommend Peter Norvig’s article on solving Sudokus. The author goes into much more detail and demonstrates in numbers how this method performs.

Finally, I wrote a **SudokuHumanSolver** class. It uses some basic methods used by humans to solve Sudokus. I have included a search for unsolved values that have only one possible position in a grid, row, or column. I have also implemented the locked candidates method for grids, rows, and columns. Lastly, I made an attempt at X Wing and Skyscraper solving methods.

I have tested the **SudokuPossibilityBasedSolver **on the hard and hardest sudokus from Peter Norvig’s website and repeated the experiment ten times. I think the big difference in results is due to the difference in hardware. However, the results show that this approach is valid.

After that I have tested each of the solvers on the easy sudokus generated by my library:

It becomes very apparent how much slower the deterministic solver performs.

Feel free to check out my code on github.

The post Solving Sudokus appeared first on Furcino.com.

]]>The post WordPress Blog appeared first on Furcino.com.

]]>The post WordPress Blog appeared first on Furcino.com.

]]>