Monday, 15 June 2015

c# - Removing cells from a Sudoku solution to make it a puzzle -



c# - Removing cells from a Sudoku solution to make it a puzzle -

i writing sudoku application , working on game generation algorithm. managed figure out how generate solution (not solve). stumped on how remove of numbers create puzzle, though. first inclination randomly remove number of cells based on difficulty, not right algorithm, because renders puzzle unsolvable or has multiple solutions. might generate puzzles don't reflect requested difficulty.

here code have far. removed of irrelevant code, if see isn't implemented used below, please allow me know. can provide effort @ puzzlefy method if like, opted out of posting since it's blatantly wrong (even though "works").

using system; using system.collections.generic; using system.linq; namespace sudoku { public class game { public enum difficulty { veryeasy, easy, medium, difficult, evil } private readonly int?[,] _currentitems = new int?[9,9]; private readonly int?[,] _solution = new int?[9,9]; private readonly int?[,] _startingitems = new int?[9,9]; private readonly difficulty _difficulty; public game(difficulty difficulty) { _difficulty = difficulty; generatesolution(); puzzlefy(); } private void generatesolution() { var random = new random(); var availablenumbers = new stack<list<int?>>(81); var x = 0; var y = 0; availablenumbers.push(allowablenumbers(_solution, 0, 0).tolist()); while (x < 9 && y < 9) { var currentavailablenumbers = allowablenumbers(_solution, x, y).tolist(); availablenumbers.push(currentavailablenumbers); // trace if board in invalid state while (currentavailablenumbers.count == 0) { _solution[x, y] = null; availablenumbers.pop(); currentavailablenumbers = availablenumbers.peek(); x -= y >= 1 ? 0 : 1; y = y >= 1 ? y - 1 : 8; } var index = random.next(currentavailablenumbers.count); _solution[x, y] = currentavailablenumbers[index]; currentavailablenumbers.removeat(index); x += y < 8 ? 0 : 1; y = y < 8 ? y + 1 : 0; } } private void puzzlefy() { copycells(_solution, _startingitems); // remove stuff _startingitems copycells(_startingitems, _currentitems); } } }

i not looking code, rather algorithm. how go removing numbers solution create puzzle?

here paper on sudoku generation

i think need sudoku solver count number of solutions available, substract numbers in such way there 1 available solution.

you apply same method adding numbers grid, check number of possible solution , maintain adding when number of solution greater 1 , backtracking when number of solutions 0

c# .net algorithm sudoku

No comments:

Post a Comment