public void ApplyValidSingleStepSolutionTest()
        {
            var sudoku = (int[, ])_sudoku.Clone();

            sudoku[4, 7] = 0;

            var sudokuPuzzle = new SudokuPuzzle(sudoku);
            var strategyWhichFindsNothing      = new NakedTriple();
            var strategyWhichReducesCandidates = new BasicElimination();
            var strategyWhichFindsResult       = new HiddenSingle();

            sudokuPuzzle.ApplySingleStepSolution(strategyWhichFindsNothing.SolveSingleStep(sudokuPuzzle));
            Assert.That(sudokuPuzzle.Steps, Is.Empty);
            Assert.That(sudokuPuzzle.NumberOfSteps, Is.Zero);

            sudokuPuzzle.ApplySingleStepSolution(strategyWhichReducesCandidates.SolveSingleStep(sudokuPuzzle));
            Assert.That(sudokuPuzzle.Steps.Count, Is.EqualTo(1));
            Assert.That(sudokuPuzzle.NumberOfSteps, Is.EqualTo(1));
            Assert.That(sudokuPuzzle.Steps.Last().Strategy, Is.EqualTo(strategyWhichReducesCandidates.StrategyName));
            Assert.That(sudokuPuzzle.Steps.Last().SolutionDescription, Is.Not.Empty);

            sudokuPuzzle.ApplySingleStepSolution(strategyWhichFindsResult.SolveSingleStep(sudokuPuzzle));
            Assert.That(sudokuPuzzle.Steps.Count, Is.EqualTo(2));
            Assert.That(sudokuPuzzle.NumberOfSteps, Is.EqualTo(2));
            Assert.That(sudokuPuzzle.Steps.Last().Strategy, Is.EqualTo(strategyWhichFindsResult.StrategyName));
            Assert.That(sudokuPuzzle.Cells[4, 7].Value, Is.EqualTo(5));
            Assert.That(sudokuPuzzle.Steps.Last().SolutionDescription, Is.Not.Empty);
        }
        public void ApplyInvalidSingleStepSolutionTest()
        {
            var sudokuPuzzle = new SudokuPuzzle(_sudoku);

            Assert.That(() => sudokuPuzzle.ApplySingleStepSolution(null), Throws.Nothing);
            Assert.That(sudokuPuzzle.Steps, Is.Empty);
            Assert.That(sudokuPuzzle.NumberOfSteps, Is.Zero);

            var solutionWithNoEliminations = new SingleStepSolution(new SingleStepSolution.Candidate[0], "test");

            Assert.That(() => sudokuPuzzle.ApplySingleStepSolution(solutionWithNoEliminations), Throws.Nothing);
            Assert.That(sudokuPuzzle.Steps, Is.Empty);
            Assert.That(sudokuPuzzle.NumberOfSteps, Is.Zero);
        }
        private SingleStepSolution ExecuteStep(SudokuPuzzle sudokuPuzzle, ISudokuSolverStrategy sudokuSolverStrategy)
        {
            var elimination = _basicElimination.SolveSingleStep(sudokuPuzzle);

            if (elimination != null)
            {
                sudokuPuzzle.ApplySingleStepSolution(elimination);
            }
            return(sudokuSolverStrategy.SolveSingleStep(sudokuPuzzle));
        }
        public SudokuPuzzle SolveUsingStrategy(SudokuPuzzle sudokuPuzzle, ISudokuSolverStrategy sudokuSolverStrategy)
        {
            var step = ExecuteStep(sudokuPuzzle, sudokuSolverStrategy);

            while (step != null)
            {
                sudokuPuzzle.ApplySingleStepSolution(step);
                step = ExecuteStep(sudokuPuzzle, sudokuSolverStrategy);
            }

            return(sudokuPuzzle);
        }
        private void OnKeyDown(object sender, KeyEventArgs e)
        {
            // if arrow key was pressed, do navigation
            if (ProcessArrowKey(e.Key))
            {
                return;
            }

            // if there is no active cell or active cell already has value
            if (_activeCell == null || _activeCell.Cell.HasValue)
            {
                return;
            }

            // if a non-valid number was entered
            var enteredValue = GetKeyValue(e.Key);

            if (enteredValue <= 0)
            {
                return;
            }

            // if value is not correct or solver cannot solve the puzzle (so we do not know if the value is correct), we do not allow it
            var solvedValue = SolvedSudokuPuzzle.Cells[_activeCell.Cell.RowIndex, _activeCell.Cell.ColumnIndex].Value;

            var mainViewModel = (MainViewModel)DataContext;

            if (solvedValue == 0)
            {
                mainViewModel.AppendMessage("Cannot enter value because solver cannot solve the puzzle");
                return;
            }
            if (enteredValue != solvedValue)
            {
                mainViewModel.AppendMessage($"Row {_activeCell.Cell.RowIndex + 1} Column {_activeCell.Cell.ColumnIndex + 1} Value {enteredValue} [Wrong value entered by user]");
                return;
            }

            var solution = new SingleStepSolution(_activeCell.Cell.RowIndex, _activeCell.Cell.ColumnIndex, enteredValue, "Entered by user");

            SudokuPuzzle.ApplySingleStepSolution(solution);
            _activeCell.NotifyCellValueChanged();
            SetSelection(_activeCell);
            mainViewModel.AppendMessage(solution.SolutionDescription);
            mainViewModel.UpdateStatusMessage();
        }
        public void UndoSingleStepSolutionsWhichReturnsEliminations_UndoesEliminations_Test()
        {
            var sudoku = (int[, ])_sudoku.Clone();

            sudoku[4, 7] = 0;

            var sudokuPuzzle = new SudokuPuzzle(sudoku);
            var strategyWhichReducesCandidates = new BasicElimination();

            sudokuPuzzle.ApplySingleStepSolution(strategyWhichReducesCandidates.SolveSingleStep(sudokuPuzzle));
            Assert.That(sudokuPuzzle.Cells[4, 7].CanBe.Single(), Is.EqualTo(5));

            var step = sudokuPuzzle.UndoLastSingleStepSolution();

            Assert.That(step.Eliminations, Is.Not.Empty);
            Assert.That(step.Result, Is.Null);
            Assert.That(step.Strategy, Is.EqualTo(strategyWhichReducesCandidates.StrategyName));
            Assert.That(step.SolutionDescription, Is.Not.Empty);
            CollectionAssert.AreEqual(sudokuPuzzle.Cells[4, 7].CanBe, new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
        }
Пример #7
0
        /// <inheritdoc />
        public SudokuPuzzle Solve(int[,] sudoku)
        {
            var sudokuPuzzle = new SudokuPuzzle(sudoku);

            while (!sudokuPuzzle.IsSolved())
            {
                var solution = SolveSingleStep(sudokuPuzzle);
                if (solution == null)
                {
                    // Sudoku cannot be solved :(
                    sudokuPuzzle = _sudokuPuzzleAfterFailedSolveSingleStep;
                    break;
                }
                else
                {
                    sudokuPuzzle.ApplySingleStepSolution(solution);
                    _sudokuPuzzleAfterFailedSolveSingleStep = sudokuPuzzle;
                }
            }

            return(sudokuPuzzle);
        }
        public void UndoSingleStepSolutionsWhichReturnsResult_UndoesResult_Test()
        {
            var sudoku = (int[, ])_sudoku.Clone();

            sudoku[4, 7] = 0;

            var sudokuPuzzle             = new SudokuPuzzle(sudoku);
            var strategyWhichFindsResult = new HiddenSingle();

            sudokuPuzzle.ApplySingleStepSolution(strategyWhichFindsResult.SolveSingleStep(sudokuPuzzle));
            Assert.That(sudokuPuzzle.Cells[4, 7].Value, Is.EqualTo(5));

            var step = sudokuPuzzle.UndoLastSingleStepSolution();

            Assert.That(step.Eliminations, Is.Null);
            Assert.That(step.Result, Is.Not.Null);
            Assert.That(step.Result.IndexOfRow, Is.EqualTo(4));
            Assert.That(step.Result.IndexOfColumn, Is.EqualTo(7));
            Assert.That(step.Strategy, Is.EqualTo(strategyWhichFindsResult.StrategyName));
            Assert.That(step.SolutionDescription, Is.Not.Empty);
            Assert.That(sudokuPuzzle.Cells[4, 7].Value, Is.EqualTo(0));
        }