Programming Sudoku



The FreeSudoku Program

Programming an algorithm to solve Sudoku games is not really difficult, specially after some minutes of Internet search. What is more interesting is to generate good Sudoku games and evaluate the difficulty.

This is why I programmed the FreeSudoku game, because I find it a challenge and I hope that other people will enjoy playing Sudokus, programming or just exploring and investigating the secrets of this game. There are still plenty of open problems to be solved and solutions to be discoreved.
FreeSudoku example board.
This is a generated FreeSudoku symmetric game where some posible numbers are filled.
With CTRL-SHIFT-CLICK on a cell the posible numbers are authomatically calculated for that cell.


FreeSudoku can generate games with four levels of difficulty and has the option to enter several small number in the cells, the same way you would do on paper. In the training mode it has even some tricks to help you to fill authomatically the posible numbers in the cells.
Top

Download FreeSudoku!




Types of Sudoku games

A Sudoku game should have one and only one solution. I consider Sudoku games with several solutions as not valid games, because you can not solve them using only logic and deduction until the end. For the same reason I prefer the games where you don't have to use try and error techniques to solve them, you can fill every number just by applying logical rules. The problem is that nobody knows if all rules are already discovered, in other words, when you think that is impossible to continue without guessing, maybe is just because you don't know all the logic techniques, maybe nobody knows because probably some of them are not yet discovered.

We can divide the games in valid and not valid. Not valid are the games with no solution or with more than one solution. I directly discard them, FreeSudoku generates games with one and only one solution.
Some people could consider that a game that is so hard that the only way to finish it is by guessing is not interesting to play. Well, is an option, but maybe someday someone will solve such a games with a new technique.
We can also divide the valid Sudokus into symmetrics and not symmetrics. In the FreeSudoku options you can select symmetric or not. I don't know if it has any influence in the difficulty of the game or in making it more interesting or funny. Maybe is just an stetic option, maybe not.

So, the first challenge is to generate good Sudoku games with only one solution. It would be nice if eventually we are able to generate symmetric games and games with less than 30 initial numbers.

Then we find the second problem, how can we generate games with the desired level of difficulty? 'easy', 'medium', 'hard', 'crazy'... Well, probably nobody knows how to do it directly, the approach I know is to generate a game, check the difficulty and see if it fits with the desired level, if not generate another one. This a little bit radical but works.
Then arises the next question: how to evaluate the difficulty of a generated game? This is not easy, is doesn't really depend on the amount of initial numbers. A game with less initial numbers can be easier than other one with more.
To check how long takes to the computer to solve it is not the solution, the computer probably finds pretty boring and stupid the game and the way to solve it, a waste of nano-seconds, because the way a computer takes to solve problems is very different from the way the humans take (at least most of the humans). So the only solution is to simulate the human way of solving Sudokus to check how difficult it is for us, poor living beings.
Top

Simulating the human approach

The system I use to evaluate the dificulty of a game is try to solve it by simulating the way a human would follow. I have programmed the most common and some of the advanced technics and I just apply them from the easier to harder. When one of the rules works and is posible to fill a cell or remove a posible candidate from a cell, the program increments a counter with a certain value depending on how complex the used rule is. Then I start over from the easiest rule.

So, if the program applys a lot of times only the 3 easiest rules, the game is easy. But if it applied a few of the hard rules then the game is harder. The program only applies one hard rule when there is no other way, when all the easier ones didn't work.

When I say 'hard rule' I mean hard for the humans. For a computer would be hard to write a poem or explain a joke, but I guess that all this rules are similar and stupid for it.
So, the easiest rule is just to check a cell a see if only one number fit in that row, column and group, the first thing a person would check with a pencil and a sudoku from the newspaper. The harder rules involve checking a lot of columns and rows just to eliminate a canditate number from a cell.

There is a polemic rule: 'guessing', but finally I have included it because is something that a human would try when is very clear, so only one or two levels deep. It means to put a number in a cell and check if you get an invalid board after a few movements. If this is the case you can eliminate that number from the candidates in that cell. If not, you can't do nothing. I don't like guessing to solve a sudoku game, but I recognize that near the end of the game it can help.
Top

Generating valid Sudoku Games

Coming soon...
Top