0

In response to the locking of my last question here due to lack of information I will now try explain further to eliminate confusion.

Ok first left get some background info on what I'm doing.
I started on a personal project of making a sudoku game to learn about object oriented programming, ArrayLists, algorithms, model/control/design layer and generally expanding my knowledge programming.
I have come a very far way in making this game and it is nearing completion but I have hit a smaller problem that I need help to solve.

I hit the problem when I generated 3 sudokus, one easy, one medium and one hard.
Easy and medium difficulty sudokus was solvable but the hard sudoku was unsolvable.

How it works:
First I have an algorithm using random number generation and validation to generate a valid sudoku board, then I pass it through another algorithm that goes through all numbers on the 9x9 board and removes them on a percent chance, this percent chance is specified when calling the method, for example 50% chance of removing a number for easy and 65% chance for hard.

My issue:
Ok so my issue is that I generated a sudoku at "hard" difficulty and found that it was unsolvable. Right now I have no validation method to check if the puzzle is solvable in any way, so long story short its by chance if its solvable.

What I need:
I need an algorithm or a method to validate if the puzzle is solvable cause as it is right now I only have a random chance of it being solvable due to the random number removal chance. This should not be done using brute force(Backtracking) but instead it should look at the puzzle and take decisions on what numbers go where, basically just as you and I would solve it. This way I can verify not only if it has a single solve but also if you and I can solve it.

A small graphical view of variables and how the classes is connected:

Class diagram

A visual representation of how the sudoku is structured using the above example:

Sudoku representation

The 1 to9 numbers in the sudoku is the cells 1 to 9.

If you need any more detailed information on the program please tell me and I will add it to this form, I just tried to keep it as short and simple as possible while still trying to cover everything relevant for this issue.

Community
  • 1
  • 1
Simon Jensen
  • 488
  • 1
  • 3
  • 19
  • 4
    Just a note, generally you're expected to edit your last question instead of asking a new one about the same thing... Also, you should make your question clearer. It's buried in the middle of things and is somewhat easy to miss. – awksp Jun 24 '14 at 01:37
  • I don't think that one can state that if a Sudoku puzzle as a solution that this implies that the solution obtained is the one unique solution. So your test of solubility may not be adequate. Please correct me if I'm wrong. – Hovercraft Full Of Eels Jun 24 '14 at 01:40
  • Are you saying that unsolvable can still be solvable? If so, you have an algorithm error. – PM 77-1 Jun 24 '14 at 01:40
  • @user3580294 I know but i judged that question to be beyond rescuing. – Simon Jensen Jun 24 '14 at 01:50
  • Also thank you for stating that I did not explain clearly enough what I need, I will add a section under "my issue" explaining it in detail – Simon Jensen Jun 24 '14 at 01:55
  • 1
    When solving Sudoku puzzles, I sometimes use a *try and see further* method with pencil and eraser which **is** backtracking ... And I thing I'm not alone to do that ! – Serge Ballesta Jun 24 '14 at 08:37
  • The train of thought followed by human beings to solve puzzles such as Sudoku differ from person to person. Beginners might follow methods that are similar to depth first search with backtracking. When dealing with hard problems that is not the most efficient method for human beings. In games such as chess, that is simply not a sustainable method. Experts have a tendency to recognize patterns by having a global view of the board. I am certain that a similar process is followed by expert Sudoku human players. See http://theinvisiblegorilla.com/blog/2012/02/15/how-experts-recall-chess-positions/ – Tarik Jun 24 '14 at 11:03

4 Answers4

2

I will attempt to answer the questions you asked either explicitly or implicitly:

"First I have an algorithm using random number generation and validation to generate a valid sudoku board"

If your algorithm does generate a valid Sudoku board, it should therefore be solvable. If this is not what you mean by solvable please elaborate.

"I need an algorithm or a method to validate if the puzzle is solvable cause as it is right now I only have a random chance of it being solvable due to the random number removal chance."

Any algorithm that solves Sudoku will validate that it is indeed solvable. Any number removal might actually make the game harder to solve and probably increase the number of valid solutions, thereby making it invalid (see tactful comment made by Serge Ballesta). I would also like to mention that: "Sudoku puzzles are generally classified as easy, medium or hard with puzzles having more starting clues generally but not always easier to solve. But quantifying the difficulty mathematically is hard.". See Mathematics of Sudoku Leads To "Richter Scale" of Puzzle Hardness

"This should not be done using brute force(Backtracking) but instead it should look at the puzzle and take decisions on what numbers go where, basically just as you and I would solve it."

You are asking for an algorithm that emulates human behavior and thinking in solving this problem. I do not think this is a feasible request and would probably fall into the research area of artificial intelligence. The train of thought followed by human beings to solve puzzles such as Sudoku differ from person to person. Beginners might follow methods that are similar to depth first search with backtracking. When dealing with hard problems that is not the most efficient method for human beings. In games such as chess, that is simply not a sustainable method. Experts have a tendency to recognize patterns by having a global view of the board. I am quite certain that a similar process is followed by expert Sudoku human players. See How experts recall chess positions

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • You are wrong on one point : if more than one solution exists for a given Sudoku puzzle, the puzzle is considered "invalid". – Serge Ballesta Jun 24 '14 at 08:24
  • @SergeBallesta Updated my answer as per your comment. – Tarik Jun 24 '14 at 08:35
  • "If your algorithm does generate a valid Sudoku board, it should therefore be solvable. If this is not what you mean by solvable please elaborate." Take out too many numbers and it will become unsolvable by the player. Brute force on the other hand will always find a solution taken the fact that it tries every possible combination. – Simon Jensen Jun 24 '14 at 13:17
  • @Simon Jensen Technically if you take too many numbers it is still solvable by player. At some point player must decide what number to put from given possibilities, this might be dead end and if it is dead end then player must rollback all moves from that point and try another number. It is solvable but not just simple as "put here only one possible number". – bary Jun 24 '14 at 13:44
  • @SimonJensen "Take out too many numbers and it will become unsolvable by the player." Incorrect, the user would just have to put back the number that has been removed. In general terms, if a given Sudoku game is solvable, then any game obtained by removing any number from any position is solvable albeit that it might not be a valid Sudoku anymore due to the possibility of having introduced more valid solutions while valid games require a unique solution. – Tarik Jun 24 '14 at 13:51
  • @Tarik In short, this was what I was referring to, basically if you remove too many numbers or the wrong numbers the player would no longer be able to calculate his/her way to solving the puzzle and will end out in guessing. I don't want it to be so hard that the player ends out having to guess, which also could lead to too many possible solves. – Simon Jensen Jun 24 '14 at 16:52
  • "if you remove too many numbers or the wrong numbers the player would no longer be able to calculate his/her way to solving the puzzle" No. It might be harder but he will always be able to find his way to a solution, except that removing too many numbers might lead to have more than one possible solution thereby rendering the game invalid. – Tarik Jun 24 '14 at 20:21
  • @Tarik that was the case with the sudoku I generated. I want the player to be able to calculate his/her way through the whole sudoku without having to guess, this also means single solutions only. – Simon Jensen Jun 25 '14 at 01:41
  • If this is what you mean by "guess" then the leadt I can say is that we would have to guess that this was what you meant. – Tarik Jun 25 '14 at 01:45
  • I apologize that I might not have formulated my question properly, I tried my best to explain it. – Simon Jensen Jun 25 '14 at 09:41
1

I second that what Tarik said in his answer.

Possible algorithm I can think of (it is only idea and is not optimal but can be improved):

First part - straightforward solving:

  • 1) Assign to each empty cell list of possible numbers to insert (based on sudoku rules).
  • 2) Iterate over each empty cell and check if list of possible numbers to insert contains only one element.

If contains only one possible number to insert then insert this number and repeat step (1). If you cant insert more numbers go to second part.

Second part - branching:

  • 3) Iterate over each empty cell in ascending order based on number of possible numbers to insert (from first part).
  • 4) Here comes the recursion. For each of possibe number to fill in given cell create separate sudoku board and try to solve it in same way starting from step (1)

This recursive approach (Depth-first search) will allow you to find all possible solutrions.

This is good excercise to increase your programing skills :)

bary
  • 1,699
  • 2
  • 15
  • 24
0

The first part you have to do is non programmatic : you have to find an exhaustive (to a point ...) list of solving methods. Then just apply them in turn until either you solve the puzzle and declare it solvable by your program or keep stuck and decide to reject it.

But this is not enough. If you can find a solution, you still have to verify by brute force that this solution is unique. If you do not, you could generate puzzle having multiple solutions and those are declared invalid by most players.

It is less important to reject valid puzzles not solvable by your methods, because nobody will ever notice them (as long as you do not let user enter their own puzzles).

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Provided the solving methods are correct (a cell content is determined only if it can be uniquely determined) there is only one solution possible. – Henry Jun 24 '14 at 08:38
  • "you have to find an exhaustive (to a point ...) list of solving methods." One solving method should do. Solving programmatically Sudoku is relatively trivial. You could use a constraint programming library which usually provides Sudoku as a toy problem sample. – Tarik Jun 24 '14 at 10:26
0

If you're looking for logical algorithms for solving sudoku then this would be helpful:

http://hodoku.sourceforge.net/en/techniques.php

http://diuf.unifr.ch/pai/people/juillera/Sudoku/Sudoku.html

Danio
  • 186
  • 1
  • 5