// Method establishes which values can fill an empty square by eliminating any values already occurring in row/column/box. public List<int> DetermineValidOptionsForSquare(SquareCoordinate squareCoordinate, IImmutableSudokuGrid grid) { HashSet<int> validSquareOptions = new HashSet<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (int offset = 1; offset < 9; offset++) { validSquareOptions.Remove(grid.Elements[(squareCoordinate.Row + offset) % 9, squareCoordinate.Column]); validSquareOptions.Remove(grid.Elements[(squareCoordinate.Row), (squareCoordinate.Column + offset) % 9]); validSquareOptions.Remove(grid.Elements[(3 * (squareCoordinate.Row / 3)) + offset % 3, (3 * (squareCoordinate.Column / 3)) + (offset - offset % 3) / 3]); } return validSquareOptions.ToList<int>(); }
// Method establishes which values can fill an empty square by eliminating any values already occurring in row/column/box. public List <int> DetermineValidOptionsForSquare(SquareCoordinate squareCoordinate, IImmutableSudokuGrid grid) { HashSet <int> validSquareOptions = new HashSet <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; for (int offset = 1; offset < 9; offset++) { validSquareOptions.Remove(grid.Elements[(squareCoordinate.Row + offset) % 9, squareCoordinate.Column]); validSquareOptions.Remove(grid.Elements[(squareCoordinate.Row), (squareCoordinate.Column + offset) % 9]); validSquareOptions.Remove(grid.Elements[(3 * (squareCoordinate.Row / 3)) + offset % 3, (3 * (squareCoordinate.Column / 3)) + (offset - offset % 3) / 3]); } return(validSquareOptions.ToList <int>()); }
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 FillInSquareWithValue(int value) { SquareCoordinate newSquare = new SquareCoordinate(0, 0); ImmutableSudokuGrid newGrid = sudokuGrid.WithExtraSquare(newSquare, value); return newGrid; }
public ImmutableSudokuGrid WithoutSquare(SquareCoordinate squareCoordinate) { int[,] arrayForNewGrid = (int[,])Elements.Clone(); arrayForNewGrid[squareCoordinate.Row, squareCoordinate.Column] = -1; return new ImmutableSudokuGrid(arrayForNewGrid); }
public ImmutableSudokuGrid WithoutSquare(SquareCoordinate squareCoordinate) { int[,] arrayForNewGrid = (int[, ])Elements.Clone(); arrayForNewGrid[squareCoordinate.Row, squareCoordinate.Column] = -1; return(new ImmutableSudokuGrid(arrayForNewGrid)); }
public void FillInSquare(SquareCoordinate squareCoordinate, int value) { Elements[squareCoordinate.Row, squareCoordinate.Column] = value; }
public bool CanBeSolvedWithDifferentValueInSquare(ImmutableSudokuGrid grid, SquareCoordinate square, int disallowedValue) { ImmutableSudokuGrid differentValuedGrid = Solve(grid, disallowedValue: disallowedValue, disallowedRow: square.Row, disallowedColumn : square.Column); return differentValuedGrid == null ? false : true; }
public bool CanBeSolvedWithDifferentValueInSquare(ImmutableSudokuGrid grid, SquareCoordinate square, int disallowedValue) { ImmutableSudokuGrid differentValuedGrid = Solve(grid, disallowedValue: disallowedValue, disallowedRow: square.Row, disallowedColumn: square.Column); return(differentValuedGrid == null ? false : true); }