public SetCellValueResponse SetCellValue([FromBody] List <List <List <int> > > currentData, [FromQuery] int rowIndex, [FromQuery] int columnIndex, [FromQuery] int cellValue, [FromQuery] bool solve) { try { var abc = new Classes.Sudoku(solve); int currentRowIndex = 0; int currentColumnIndex = 0; foreach (var row in currentData) { currentRowIndex++; currentColumnIndex = 0; foreach (var cell in row) { currentColumnIndex++; if (cell.Count == 1) { abc.SetValue(currentRowIndex, currentColumnIndex, cell[0]); } } } abc.SetValue(rowIndex, columnIndex, cellValue); return(new SetCellValueResponse { Status = "OK", NewData = abc.ToArray() }); } catch (Exception e) { return(new SetCellValueResponse { Status = $"Error: {e.Message}", NewData = currentData }); } }
/// <summary> /// Button witch solves given Sudoku and shows it on Canvas /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void SolveButton(object sender, RoutedEventArgs e) { randomSudokuClass = new Pages.RandomSudoku(); Classes.Sudoku sudokuClass = new Classes.Sudoku(Extract); // Returns solved Sudoku int[,] solvedSudoku = await sudokuClass.SudokuMain(notSolvedSudoku); this._SolvedSudoku = solvedSudoku; if (solvedSudoku == null) { MessageBox.Show("Sudoku has no solution"); } else { // Check if Sudoku has been completed because of pressing button more than once bool condition = randomSudokuClass.Completed(this._SudokuArray); if (condition) { MessageBox.Show("Sudoku has been solved"); } else { // Display completed Sudoku on Canvas int temp = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { Label l = (Label)this._SudokuArray.Children[temp]; l.Content = this._SolvedSudoku[i, j].ToString(); temp++; } } } } }
/// <summary> /// Algorithm for random generation of Sudoku /// </summary> /// <returns> Output is randomly generated Sudoku </returns> private int[,] SudokuRandomGenerator() { Classes.Sudoku sudokuClass = new Classes.Sudoku(Extract); int[,] sudoku = new int[9, 9]; // Array filling with zeros for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { sudoku[i, j] = 0; } } Random rand = new Random(); for (int i = 0; i < 9; i++) { // Random number of numbers in one row int quantityOfNumbersInRow = rand.Next(0, 9); for (int j = 0; j < quantityOfNumbersInRow; j++) { // Index where will number be int index = rand.Next(0, 9); int randNumber = rand.Next(0, 10); sudoku[i, index] = randNumber; // Check for duplicates. If duplicate is finded number on specified index is set to zero bool cond = sudokuClass.Check(sudoku, i, index); if (!cond) { sudoku[i, index] = 0; } } } return(sudoku); }
/// <summary> /// Button that start a Visualization of algorithm by steps /// </summary> /// <param name="sender"></param> /// <param name="e"></param> /// <remarks> /// Users set how many steps will program goes through. Boxes through algorithm goes are red. /// </remarks> private async void Visualization(object sender, RoutedEventArgs e) { Extract.Visibility = Visibility.Visible; try { int.Parse(numberOfStepsBox.Text); } catch { MessageBox.Show("Write a number in the box"); } if (numberOfStepsBox.Text == "" || numberOfStepsBox.Text == "0") { MessageBox.Show("Write a number in the box"); } else { int[,] sudokuCopy = this.notSolvedSudoku.Clone() as int[, ]; Classes.Sudoku viz = new Classes.Sudoku(Extract); try { for (int i = 0; i < int.Parse(numberOfStepsBox.Text); i++) { // Finds first empty cell in given sudoku Extract.Text = "Finding empty cell."; await Task.Delay(500); Tuple <int, int> indexes = viz.EmptyCell(this.notSolvedSudoku, sudokuCopy); if (indexes == null) { bool condition = viz.Completed(sudokuCopy); if (!condition) { throw new Exception("Error"); } else { MessageBox.Show("Solved"); break; } } int row = indexes.Item1; int column = indexes.Item2; // Calculation of index on canvas by row and column int canvasIndex = row * 9 + column; Label l = (Label)this._SudokuArray.Children[canvasIndex]; l.Background = new SolidColorBrush(Colors.Red); // Filler method fill right number in empty cell by row and column Extract.Text = "Try number\n from 1 to 9\n and fill empty cell\n with the right one."; await Task.Delay(700); sudokuCopy = await viz.Filler(this.notSolvedSudoku, sudokuCopy, row, column); if (sudokuCopy == null) { throw new Exception("Error"); } l.Background = new SolidColorBrush(Colors.Transparent); // Display Sudoku on Canvas int temp = 0; for (int k = 0; k < 9; k++) { for (int j = 0; j < 9; j++) { l = (Label)this._SudokuArray.Children[temp]; l.Content = sudokuCopy[k, j].ToString(); temp++; } } await Task.Delay(500); } Extract.Visibility = Visibility.Hidden; stackPanelViz.Visibility = Visibility.Hidden; solvedButton.Visibility = Visibility.Visible; } catch { MessageBox.Show("Sudoku has no solution"); stackPanelViz.Visibility = Visibility.Hidden; } } }