public static SudokuPuzzle RandomGrid(int size) { var puzzle = new SudokuPuzzle(size); while (true) { var unsolvedCellIndexes = puzzle.Cells .Select((cands, index) => new {cands, index}) .Where(t => t.cands.Length >= 2) .Select(u => u.index) .ToArray(); var cellIndex = unsolvedCellIndexes[Random.Next(unsolvedCellIndexes.Length)]; var candidateValue = puzzle.Cells[cellIndex][Random.Next(puzzle.Cells[cellIndex].Length)]; var workingPuzzle = puzzle.PlaceValue(cellIndex, candidateValue); if (workingPuzzle == null) continue; var solutions = MultiSolve(workingPuzzle, 2); switch (solutions.Count) { case 0: continue; case 1: return solutions.Single(); default: puzzle = workingPuzzle; break; } } }
public static SudokuPuzzle RandomGrid(int size) { var puzzle = new SudokuPuzzle(size); while (true) { var unsolvedCellIndexes = puzzle.Cells .Select((cands, index) => new { cands, index }) .Where(t => t.cands.Length >= 2) .Select(u => u.index) .ToArray(); var cellIndex = unsolvedCellIndexes[Random.Next(unsolvedCellIndexes.Length)]; var candidateValue = puzzle.Cells[cellIndex][Random.Next(puzzle.Cells[cellIndex].Length)]; var workingPuzzle = puzzle.PlaceValue(cellIndex, candidateValue); if (workingPuzzle == null) { continue; } var solutions = MultiSolve(workingPuzzle, 2); switch (solutions.Count) { case 0: continue; case 1: return(solutions.Single()); default: puzzle = workingPuzzle; break; } } }
private static List <int> FindSingularizedCells(SudokuPuzzle puzzle1, SudokuPuzzle puzzle2, int cellIndex) { return (puzzle1.Peers(cellIndex) .Where(i => puzzle1.Cells[i].Length > 1 && puzzle2.Cells[i].Length == 1) .ToList()); }
private 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 object Clone() { var clone = new SudokuPuzzle(Length) { Cells = new int[Cells.Length][] }; for (var i = 0; i < Cells.Length; i++) { clone.Cells[i] = new int[Cells[i].Length]; Buffer.BlockCopy(Cells[i], 0, clone.Cells[i], 0, Buffer.ByteLength(Cells[i])); } return(clone); }
public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); app.Run(async context => { context.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" }); context.Response.Headers.Add("Access-Control-Allow-Headers", new[] { "*" }); context.Response.Headers.Add("Access-Control-Allow-Methods", new[] { "*" }); context.Response.ContentType = "application/json"; var json = SudokuPuzzle.RandomGrid(9).ToString(); await context.Response.WriteAsync(json); }); }
public object Clone() { var clone = new SudokuPuzzle(Length) {Cells = new int[Cells.Length][]}; for (var i = 0; i < Cells.Length; i++) { clone.Cells[i] = new int[Cells[i].Length]; Buffer.BlockCopy(Cells[i], 0, clone.Cells[i], 0, Buffer.ByteLength(Cells[i])); } return clone; }
private 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; }
private static List<int> FindSingularizedCells(SudokuPuzzle puzzle1, SudokuPuzzle puzzle2, int cellIndex) { return puzzle1.Peers(cellIndex) .Where(i => puzzle1.Cells[i].Length > 1 && puzzle2.Cells[i].Length == 1) .ToList(); }