Ejemplo 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);
                }
            }
        }
Ejemplo n.º 2
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;
                }
            }
        }
Ejemplo n.º 3
0
        public void TryToSolveValidSudoku_CheckSolveReturnsCompletedGrid()
        {
            ImmutableSudokuGrid grid = new ImmutableSudokuGrid(CreateSudokuArray());
            var returnFromSolve = sudokuSolver.Solve(grid);
            var numberEmptySquares = returnFromSolve.FindAllEmptySquares().Count;

            Assert.IsNotNull(grid);
            Assert.AreEqual(numberEmptySquares, 0);
        }
Ejemplo 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);
        }
Ejemplo n.º 5
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;
        }
Ejemplo n.º 6
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;
        }
Ejemplo n.º 7
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);
        }
Ejemplo n.º 8
0
        public void DetermineValidEntriesWorksCorrectly()
        {
            var grid = new ImmutableSudokuGrid(CreateNewMostlyEmptyArray());
            var validOptionsFor00 = sudokuSolver.DetermineValidOptionsForSquare(new SquareCoordinate(0, 0), grid);
            var validOptionsFor14 = sudokuSolver.DetermineValidOptionsForSquare(new SquareCoordinate(1, 4), grid);

            Assert.AreEqual(validOptionsFor00.Count, 6);
            for (int i = 4; i <= 9; i++)
            {
                Assert.IsTrue(validOptionsFor00.Contains(i));
            }

            Assert.AreEqual(validOptionsFor14.Count, 7);
            for (int i = 1; i <= 9; i++)
            {
                if (i != 3 && i != 4)
                {
                    Assert.IsTrue(validOptionsFor14.Contains(i));
                }
            }
        }
Ejemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("Do you want me to try and solve an Easy Sudoku (type 1) or a Very Hard Sudoku (type 2)?");
            string difficulty = Console.ReadLine();
            if (difficulty == "1") { difficulty = "Easy" ; }
            else if (difficulty == "2") { difficulty = "VeryHard"; }
            else
            {
                Console.WriteLine("Stop being difficult.  I'm going to solve an easy one then...");
                difficulty = "Easy";
            }

            ImmutableSudokuGrid grid = new ImmutableSudokuGrid(@"..\..\..\" + difficulty + @"Sudoku.txt");

            if (grid.Elements == null)
            {
                Console.WriteLine("Error, cannot read grid from file.");
                return;
            }

            HarderSudokuSolver sudokuSolver = new HarderSudokuSolver();
            sudokuSolver.Solve(grid).PrintGrid();
        }
Ejemplo n.º 10
0
 private ImmutableSudokuGrid CreateFilledGrid(HarderSudokuSolver solver)
 {
     ImmutableSudokuGrid sudokuGrid = new ImmutableSudokuGrid();
     return solver.Solve(sudokuGrid, true);
 }
Ejemplo n.º 11
0
 public ImmutableSudokuGrid EmptyGridForSimpleSolve(ImmutableSudokuGrid grid)
 {
     return EmptyGridForSimpleSolve(grid, new SudokuSolver());
 }
Ejemplo n.º 12
0
 public ImmutableSudokuGrid EmptyGridForHardSolve(ImmutableSudokuGrid grid)
 {
     return EmptyGridForHardSolve(grid, new HarderSudokuSolver());
 }
Ejemplo n.º 13
0
 public void InitializeTest()
 {
     sudokuGrid = new ImmutableSudokuGrid(@"..\..\..\VeryHardSudoku.txt");
 }
Ejemplo n.º 14
0
 public ImmutableSudokuGrid Solve(ImmutableSudokuGrid grid, bool isRandomSolve = false, int disallowedValue = 0, int disallowedRow = -1, int disallowedColumn = -1)
 {
     return(Solve(grid, new SudokuSolver(), isRandomSolve, disallowedValue, disallowedRow, disallowedColumn));
 }
Ejemplo n.º 15
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);
        }
Ejemplo n.º 16
0
        private ImmutableSudokuGrid CreateFilledGrid(HarderSudokuSolver solver)
        {
            ImmutableSudokuGrid sudokuGrid = new ImmutableSudokuGrid();

            return(solver.Solve(sudokuGrid, true));
        }
Ejemplo n.º 17
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;
 }
Ejemplo n.º 18
0
 public void TryToSolveInvalidSudoku_CheckSolveReturnsNull()
 {
     ImmutableSudokuGrid grid = new ImmutableSudokuGrid(CreateInvalidSudokuArray());
     var returnFromSolve = sudokuSolver.Solve(grid);
     Assert.IsNull(returnFromSolve);
 }
Ejemplo n.º 19
0
 public ImmutableSudokuGrid EmptyGridForSimpleSolve(ImmutableSudokuGrid grid)
 {
     return(EmptyGridForSimpleSolve(grid, new SudokuSolver()));
 }
Ejemplo n.º 20
0
 public ImmutableSudokuGrid EmptyGridForHardSolve(ImmutableSudokuGrid grid)
 {
     return(EmptyGridForHardSolve(grid, new HarderSudokuSolver()));
 }
Ejemplo n.º 21
0
 public ImmutableSudokuGrid Solve(ImmutableSudokuGrid grid, bool isRandomSolve = false, int disallowedValue = 0, int disallowedRow = -1, int disallowedColumn = -1)
 {
     return Solve(grid, new SudokuSolver(), isRandomSolve, disallowedValue, disallowedRow, disallowedColumn);
 }