/// <summary> /// Applies <paramref name="singleStepSolution"/> to the puzzle. /// </summary> /// <param name="singleStepSolution">Solution which is applied to the puzzle.</param> public void ApplySingleStepSolution(SingleStepSolution singleStepSolution) { if (singleStepSolution == null) { return; } if (singleStepSolution.Result == null && singleStepSolution.Eliminations.Length == 0) { return; } int[] oldCanBe = new int[] { }; if (singleStepSolution.Result != null) { var cell = Cells[singleStepSolution.Result.IndexOfRow, singleStepSolution.Result.IndexOfColumn]; oldCanBe = cell.CanBe.ToArray(); cell.Value = singleStepSolution.Result.Value; } if (singleStepSolution.Eliminations != null) { foreach (var elimination in singleStepSolution.Eliminations) { Cells[elimination.IndexOfRow, elimination.IndexOfColumn].CanBe.Remove(elimination.Value); } } _steps.Add(new Tuple <SingleStepSolution, int[]>(singleStepSolution, oldCanBe.ToArray())); }
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 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 SingleStepSolutionToStringTest() { var resultSolution = new SingleStepSolution(1, 2, 3, "test"); var expectedResultText = "Row 2 Column 3 Value 3 [test]"; Assert.That(resultSolution.ToString(), Is.EqualTo(expectedResultText)); Assert.That(resultSolution.SolutionDescription, Is.EqualTo(expectedResultText)); var eliminationSolution = new SingleStepSolution(new[] { new SingleStepSolution.Candidate(1, 2, 3), new SingleStepSolution.Candidate(1, 2, 4) }, "test"); var expectedEliminationText = $"Eliminated candidates [test]:{Environment.NewLine}- Row 2 Column 3 Values 3,4"; Assert.That(eliminationSolution.ToString(), Is.EqualTo(expectedEliminationText)); Assert.That(eliminationSolution.SolutionDescription, Is.EqualTo(expectedEliminationText)); }