Пример #1
0
        // 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>();
        }
Пример #2
0
        // 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>());
        }
Пример #3
0
        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;
 }
Пример #5
0
 public ImmutableSudokuGrid WithoutSquare(SquareCoordinate squareCoordinate)
 {
     int[,] arrayForNewGrid = (int[,])Elements.Clone();
     arrayForNewGrid[squareCoordinate.Row, squareCoordinate.Column] = -1;
     return new ImmutableSudokuGrid(arrayForNewGrid);
 }
Пример #6
0
 public ImmutableSudokuGrid WithoutSquare(SquareCoordinate squareCoordinate)
 {
     int[,] arrayForNewGrid = (int[, ])Elements.Clone();
     arrayForNewGrid[squareCoordinate.Row, squareCoordinate.Column] = -1;
     return(new ImmutableSudokuGrid(arrayForNewGrid));
 }
Пример #7
0
 public void FillInSquare(SquareCoordinate squareCoordinate, int value)
 {
     Elements[squareCoordinate.Row, squareCoordinate.Column] = value;
 }
Пример #8
0
 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;
 }
Пример #9
0
 public void FillInSquare(SquareCoordinate squareCoordinate, int value)
 {
     Elements[squareCoordinate.Row, squareCoordinate.Column] = value;
 }
Пример #10
0
        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);
        }