-4

The past few weeks I've been working on a sudoku game. The game as a lot of features such as: play game, print sudoku, solve sudoku.

The solve function uses conventional backtracking but thats not the issue, the issue is that I need the game to be able to produce a humanely solvable sudoku, for that I need a method that will be able to solve a sudoku as a human would do it.

If anyone could help me work out the mechanics of how this could be done I would greatly appreciate it.

Simon Jensen
  • 488
  • 1
  • 3
  • 19
  • 2
    It would be useful if we knew what your code was. Do you use pure backtracking (known commonly as brute force) or do you have a logical solving algorithm that uses backtracking only as a last resort? – k_g Jun 22 '14 at 18:55
  • If a sudoku (as all should) has a unique solution, the solution can be solved by pure logic and can be solved by a human. So what are you really looking for? – Don Roby Jun 22 '14 at 18:57
  • Can't you just create a random matrix which obeys Sudoku's rules, and then remove radomnly one number, solve it using the backtracker to make sure there exist a solution and it is unique, if it is solvable then you remove another one and repit, till there is no solution or solution it is not unique, then you just keep the previous sudoku. I think this would be O(n^3)? – questing Jun 22 '14 at 19:01
  • @k_g I have a solve method that uses standard backtracking(brute force) but what i need is a method with logical solving algorithm as you mentioned. – Simon Jensen Jun 22 '14 at 19:21
  • @DonRoby Don Roby, yes a normal sudoku is solvable, but not if you randomly removed the wrong numbers, making it unsolvable using common logic. – Simon Jensen Jun 22 '14 at 19:22
  • @mancuernita I did make random matrix obaying sudoku rules, with that I take a 50%, 55% and 65% chance of removing a number (depending on deficulty), running through all numbers. Wouldn't the backtrack you spoke of always return a solve tho? – Simon Jensen Jun 22 '14 at 19:22

2 Answers2

3

An overwhelming collection of Sudoku solving strategies for human players is nicely presented and explained on Andrew Stuart's Sudoku page:

**Show Possibles**         
1: Hidden Singles      
2: Naked Pairs/Triples     
3: Hidden Pairs/Triples    
4: Naked Quads     
5: Pointing Pairs      
6: Box/Line Reduction      
**Tough Strategies**  
7: X-Wing      
8: Simple Colouring        
9: Y-Wing      
10: Sword-Fish         
11: XYZ Wing       
**Diabolical Strategies** 
12: X-Cycles       
13: XY-Chain       
14: 3D Medusa      
15: Jelly-Fish         
16: Unique Rectangles      
17: Extended Unique Rect.      
18: Hidden Unique Rect's       
19: WXYZ Wing      
20: Aligned Pair Exclusion         
**Extreme Strategies**    
21: Grouped X-Cycles       
22: Empty Rectangles       
23: Finned X-Wing      
24: Finned Sword-Fish      
25: Altern. Inference Chains   
26: Sue-de-Coq         
27: Digit Forcing Chains       
28: Nishio Forcing Chains  
29: Cell Forcing Chains        
30: Unit Forcing Chains        
31: Almost Locked Sets         
32: Death Blossom      
33: Pattern Overlay Method         
34: Quad Forcing Chains        
**"Trial and Error"** 
35: Bowman's Bingo

As a fairly frequent player, I would judge everything beyond strategy 11 as "no fun anymore". But that is probably a matter of taste.

Axel Kemper
  • 10,544
  • 2
  • 31
  • 54
1

If you just need a quick random sudoku, you can use a particular way of creating a valid sudoku pattern with the following algorithm I figured out a while ago:

You initialize an array with a randomized set of the numbers 1 to 9, 
technically it's easier if you initialize 3 arrays each with 3 length.
You can have these numbers be randomized, thus create a different sudoku.

[1 2 3] [4 5 6] [7 8 9]
Then you shift these:
[7 8 9] [1 2 3] [4 5 6]
[4 5 6] [7 8 9] [1 2 3]

Then you shift the numbers inside the arrays:

[3 1 2] [6 4 5] [9 7 8]
Then you shift the arrays themselves again:
[9 7 8] [3 1 2] [6 4 5]
[6 4 5] [9 7 8] [3 1 2]

Then you shift the numbers inside the arrays:

[2 3 1] [5 6 4] [8 9 7]
Then you shift the arrays again:
[8 9 7] [2 3 1] [5 6 4]
[5 6 4] [8 9 7] [2 3 1]

And you'll have the final set of sudoku table:

[1 2 3] [4 5 6] [7 8 9]
[7 8 9] [1 2 3] [4 5 6]
[4 5 6] [7 8 9] [1 2 3]
[3 1 2] [6 4 5] [9 7 8]
[9 7 8] [3 1 2] [6 4 5]
[6 4 5] [9 7 8] [3 1 2]
[2 3 1] [5 6 4] [8 9 7]
[8 9 7] [2 3 1] [5 6 4]
[5 6 4] [8 9 7] [2 3 1]

Which is valid. Afterwards, you can take out some numbers, and you can check with the algorithm you already have whether it has a single solution, or multiple. If removing a certain number produces multiple, then either undo it and end the removal, or try removing another.

EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • I already have an algorithm which can produce any random sudoku board i ask it for, all being valid sudoku boards. – Simon Jensen Jun 22 '14 at 20:42
  • Oh, okay, I thought this was what you were looking for. So you already had the valid board, but you needed a way to remove numbers and keep it solvable. Apart from picking out numbers at random and checking how many solutions there are, and if there is only one solution then keep the removal, if not then undo the removal and try another is the only way I can think of at the moment. – EpicPandaForce Jun 22 '14 at 20:46
  • Also I have a method to run through the board removing random numbers at a specified percentage(fx 50% chance of removing a number). What I need is an example or an explanation of how to verify if the puzzly generated is humanly solvable, meaning brute force(backtracking) is not useful here. – Simon Jensen Jun 22 '14 at 20:48
  • Apart from some form of clever iterative bruteforcing, I'm not sure what you can do. – EpicPandaForce Jun 23 '14 at 09:56
  • Technically if you maintain the list of what are usable numbers at given cells in a given state and iterate those lists to bruteforce, it significantly reduces the number of tries you need to check (from 9^n). – EpicPandaForce Jun 27 '14 at 09:07