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.
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.
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.
来源:https://stackoverflow.com/questions/24354568/sudoku-solver-not-backtracking-solver