public static List <SudokuPuzzle> MultiSolve(SudokuPuzzle input, int MaximumSolutions = -1) { var Solutions = new List <SudokuPuzzle>(); input.Solve(p => { Solutions.Add(p); return(Solutions.Count() < MaximumSolutions || MaximumSolutions == -1); }); return(Solutions); }
public static int[] CreateClues(SudokuPuzzle input, int maxClues = 0) { //Get a unique solution for the input puzzle, in case it wasn't already var puzzle = input.Solve(); if (puzzle == null) { throw new ArgumentException("Can't create clues from an unsolvable puzzle!"); } //This is the list of clues we work on. It can be reconstituted into a new puzzle object by way of a constructor int[] Clues = puzzle.Cells.Select(c => c[0]).ToArray(); var rand = new Random(); while (true) { //Pick a random cell to blank int ClueCell = rand.Next(Clues.Length); if (Clues[ClueCell] == 0) { continue; } var workingClues = Clues.ToArray(); workingClues[ClueCell] = 0; if (MultiSolve(new SudokuPuzzle(workingClues), 2).Count() > 1) { if (maxClues == 0) { return(Clues); } else { continue; } } Clues = workingClues; if (Clues.Count(c => c != 0) <= maxClues) { return(Clues); } } }