Example #1
0
        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);
                }
            }
        }
Example #2
0
        private ImmutableSudokuGrid EmptyGridForHardSolve(ImmutableSudokuGrid inputGrid, HarderSudokuSolver solver)
        {
            ImmutableSudokuGrid grid = EmptyGridForSimpleSolve(inputGrid);
            var listOfFilledSquares  = grid.FindAllFilledSquares();

            for (int i = 0; i < listOfFilledSquares.Count; i++)
            {
                var  square           = listOfFilledSquares[i];
                bool isNotUniqueValue = solver.CanBeSolvedWithDifferentValueInSquare(grid, square, grid.Elements[square.Row, square.Column]);

                if (!isNotUniqueValue)
                {
                    grid = grid.WithoutSquare(square);
                    return(EmptyGridForHardSolve(grid));
                }
            }

            return(grid);
        }