public static SudokuPuzzle RandomGrid(int size) { SudokuPuzzle puzzle = new SudokuPuzzle(size); var rand = new Random(); while (true) { int[] UnsolvedCellIndexes = puzzle.Cells .Select((cands, index) => new { cands, index }) //Project to a new sequence of candidates and index (an anonymous type behaving like a tuple) .Where(t => t.cands.Length >= 2) //Filter to cells with at least 2 candidates .Select(u => u.index) //Project the tuple to only the index .ToArray(); int cellIndex = UnsolvedCellIndexes[rand.Next(UnsolvedCellIndexes.Length)]; int candidateValue = puzzle.Cells[cellIndex][rand.Next(puzzle.Cells[cellIndex].Length)]; SudokuPuzzle workingPuzzle = puzzle.PlaceValue(cellIndex, candidateValue); if (workingPuzzle != null) { var Solutions = MultiSolve(workingPuzzle, 2); switch (Solutions.Count) { case 0: continue; case 1: //Console.WriteLine(Solutions[0].ToString()); return(Solutions.Single()); default: puzzle = workingPuzzle; break; } } } }
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 void Output(SudokuPuzzle puzzle) { StringBuilder s = new StringBuilder(); for (int i = 0; i < 81; i++) { s.Append(puzzle.Cells[i][0].ToString()); } Console.WriteLine(s.ToString()); }
public object Clone() { var clone = new SudokuPuzzle(this.Length); clone.Cells = new int[this.Cells.Length][]; for (int i = 0; i < this.Cells.Length; i++) { clone.Cells[i] = new int[this.Cells[i].Length]; Buffer.BlockCopy(this.Cells[i], 0, clone.Cells[i], 0, Buffer.ByteLength(this.Cells[i])); } return(clone); }
public static List <int> FindSingularizedCells(SudokuPuzzle puzzle1, SudokuPuzzle puzzle2, int cellIndex) { //Debug.Assert(puzzle1.Length == puzzle2.Length); var result = new List <int>(); foreach (int i in puzzle1.Peers(cellIndex)) { if (puzzle1.Cells[i].Length > 1 && puzzle2.Cells[i].Length == 1) { result.Add(i); } } return(result); }
public virtual SudokuPuzzle ApplyConstraints(int cellIndex, int value) { SudokuPuzzle puzzle = (SudokuPuzzle)this.Clone(); //Standard Sudoku constraint logic: Set this cell to one and only one candidate, and remove this value from the candidate list of all its peers puzzle.Cells[cellIndex] = new int[] { value }; foreach (int peerIndex in puzzle.Peers(cellIndex)) { var newPeers = puzzle.Cells[peerIndex].Except(new int[] { value }).ToArray(); if (!newPeers.Any()) { return(null); } puzzle.Cells[peerIndex] = newPeers; } return(puzzle); }