Esempio n. 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;
                }
            }
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            SudokuWriter sudokuWriter = new SudokuWriter();
            ImmutableSudokuGrid filledGrid = sudokuWriter.CreateFilledGrid();
            filledGrid.PrintGrid();

            Console.WriteLine("\n");

            ImmutableSudokuGrid emptiedGrid = sudokuWriter.EmptyGridForHardSolve(filledGrid);
            emptiedGrid.PrintGrid();

            Console.WriteLine("\n");

            SudokuGrid mutableEmptiedGrid = emptiedGrid.MakeMutableCopy();
            SudokuSolver sudokuSolver = new SudokuSolver();
            sudokuSolver.Solve(mutableEmptiedGrid);
            mutableEmptiedGrid.PrintGrid();

            if (mutableEmptiedGrid.FindAllEmptySquares().Count != 0)
            {
                Console.WriteLine("\n");
                HarderSudokuSolver harderSolver = new HarderSudokuSolver();
                ImmutableSudokuGrid solvedGrid = harderSolver.Solve(emptiedGrid);
                solvedGrid.PrintGrid();
            }
        }
Esempio n. 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);
        }
Esempio n. 4
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;
        }
Esempio n. 5
0
 public void InitializeTests()
 {
     sudokuSolver = new SudokuSolver();
 }
Esempio n. 6
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);
                }
            }
        }