private ImmutableSudokuGrid EmptyGridForSimpleSolve(ImmutableSudokuGrid inputGrid, SudokuSolver solver) { ImmutableSudokuGrid grid = inputGrid; while (true) { bool hasSquareBeenRemoved = false; List<SquareCoordinate> filledSquareList = grid.FindAllFilledSquares(); foreach (SquareCoordinate square in filledSquareList) { ImmutableSudokuGrid gridWithoutSquare = grid.WithoutSquare(square); int numberOfOptions = solver.DetermineValidOptionsForSquare(square, gridWithoutSquare).Count; if (numberOfOptions == 1) { grid = gridWithoutSquare; hasSquareBeenRemoved = true; } } if (!hasSquareBeenRemoved) { return grid; } } }
private ImmutableSudokuGrid EmptyGridForSimpleSolve(ImmutableSudokuGrid inputGrid, SudokuSolver solver) { ImmutableSudokuGrid grid = inputGrid; while (true) { bool hasSquareBeenRemoved = false; List <SquareCoordinate> filledSquareList = grid.FindAllFilledSquares(); foreach (SquareCoordinate square in filledSquareList) { ImmutableSudokuGrid gridWithoutSquare = grid.WithoutSquare(square); int numberOfOptions = solver.DetermineValidOptionsForSquare(square, gridWithoutSquare).Count; if (numberOfOptions == 1) { grid = gridWithoutSquare; hasSquareBeenRemoved = true; } } if (!hasSquareBeenRemoved) { return(grid); } } }
private ImmutableSudokuGrid Solve(ImmutableSudokuGrid grid, SudokuSolver simpleSolver, bool isRandomSolve, int disallowedValue, int disallowedRow, int disallowedColumn) { SudokuGrid mutableGrid = grid.MakeMutableCopy(); bool isGridValid = simpleSolver.TryAndSolveOnce(mutableGrid); grid = new ImmutableSudokuGrid(mutableGrid.Elements); if (!isGridValid) { return(null); } var emptySquareList = grid.FindAllEmptySquares(); if (emptySquareList.Count == 0) { return(grid); } SquareCoordinate emptySquareToFill = emptySquareList[0]; if (isRandomSolve) { var randomEmptySquare = new Random().Next(emptySquareList.Count - 1); emptySquareToFill = emptySquareList[randomEmptySquare]; } var validOptions = simpleSolver.DetermineValidOptionsForSquare(emptySquareToFill, grid); if (emptySquareToFill.Row == disallowedRow && emptySquareToFill.Column == disallowedColumn && disallowedValue != 0) { validOptions.Remove(disallowedValue); } for (int i = 0; i < validOptions.Count; i++) { ImmutableSudokuGrid solvedGrid = Solve(grid.WithExtraSquare(emptySquareToFill, validOptions[i]), isRandomSolve); if (solvedGrid != null) { return(solvedGrid); } } return(null); }
private ImmutableSudokuGrid Solve(ImmutableSudokuGrid grid, SudokuSolver simpleSolver, bool isRandomSolve, int disallowedValue, int disallowedRow, int disallowedColumn) { SudokuGrid mutableGrid = grid.MakeMutableCopy(); bool isGridValid = simpleSolver.TryAndSolveOnce(mutableGrid); grid = new ImmutableSudokuGrid(mutableGrid.Elements); if (!isGridValid) { return null; } var emptySquareList = grid.FindAllEmptySquares(); if (emptySquareList.Count == 0) { return grid; } SquareCoordinate emptySquareToFill = emptySquareList[0]; if (isRandomSolve) { var randomEmptySquare = new Random().Next(emptySquareList.Count - 1); emptySquareToFill = emptySquareList[randomEmptySquare]; } var validOptions = simpleSolver.DetermineValidOptionsForSquare(emptySquareToFill, grid); if (emptySquareToFill.Row == disallowedRow && emptySquareToFill.Column == disallowedColumn && disallowedValue != 0) { validOptions.Remove(disallowedValue); } for (int i = 0; i < validOptions.Count; i++) { ImmutableSudokuGrid solvedGrid = Solve(grid.WithExtraSquare(emptySquareToFill, validOptions[i]), isRandomSolve); if (solvedGrid != null) { return solvedGrid; } } return null; }