public void SolveSudoku() { // TraceOut.Enter(); // get a solution for the sudoku if not already available if (_creationMode == SudokuCreationMode.Manual) { var sudoku = _sudokuView.GetSudoku(); _solution = new ScoreSudokuPuzzle(new Algorithms.v2.SudokuSolver().SolveSudoku(sudoku.Deserialize()) ?? sudoku.Deserialize()); } // check if this is still necessary for (int row = 0; row < 9; row++) { for (int column = 0; column < 9; column++) { var puzzle = _sudokuView.GetSudoku(); _solution.fields[row * 9 + column].isFix = puzzle.fields[row * 9 + column].isFix; } } // apply the solution to the view _sudokuView.ApplySudoku(_solution); // TraceOut.Leave(); }
public async void GenerateSudoku() { // TraceOut.Enter(); await Task.Run(() => { if (!_isGeneratorRunning) { _isGeneratorRunning = true; // set creation mode to automatic _creationMode = SudokuCreationMode.Automatic; NotifyOfPropertyChange(() => IsChecked_Automatic); NotifyOfPropertyChange(() => IsChecked_Manual); // create a new sudoku puzzle and solve it var sudoku = new Algorithms.v2.SudokuGenerator().GenerateSudoku(_selectedDifficulty); var sudokuAsScore = new ScoreSudokuPuzzle(sudoku); _solution = new ScoreSudokuPuzzle(new Algorithms.v2.SudokuSolver().SolveSudoku(sudoku)); // apply the sudoku to the view _sudokuView.ClearSudoku(); _sudokuView.ApplySudoku(sudokuAsScore); _sudokuView.MarkSetFieldsAsFix(); // apply the changes to the history ApplyChangesToHistory(); _isGeneratorRunning = false; } }); // TraceOut.Leave(); }
public ScoreSudokuPuzzle GetSudoku() { // TraceOut.Enter(); var score = new ScoreSudokuPuzzle(); for (int row = 0; row < 9; row++) { for (int column = 0; column < 9; column++) { // TODO: check if values are correct int i = row / 3; int j = row % 3; int k = column / 3; int l = column % 3; var fieldSwitch = _boxes[i, j].Fields[k, l] as SudokuFieldViewModel; if (fieldSwitch.Mode == SudokuInputMode.Determined) { var field = fieldSwitch.DeterminedView; score.fields.Add(new ScoreSudokuField() { row = row, column = column, isFix = field.IsFix, value = field.GetValue() }); } else // if (fieldViewModel.Mode == SudokuFieldMode.Notes) { var field = fieldSwitch.NotesView; score.fields.Add(new ScoreSudokuField() { row = row, column = column, isFix = false, value = 0, possibilities = field.GetPossibilities() }); } } } // TraceOut.Leave(); return(score); }
public void ApplySudoku(ScoreSudokuPuzzle sudoku) { // TraceOut.Enter(); for (int row = 0; row < 9; row++) { for (int column = 0; column < 9; column++) { // TODO: check if values are correct int i = row / 3; int j = row % 3; int k = column / 3; int l = column % 3; var field = _boxes[i, j].Fields[k, l].DeterminedView; field.SetValue(sudoku.fields[row * 9 + column].value); field.IsFix = sudoku.fields[row * 9 + column].isFix; } } // TraceOut.Leave(); }