Sudoku.Sudoku.Solve C# (CSharp) Method

Solve() static private method

static private Solve ( int sudoku ) : ].int[
sudoku int
return ].int[
        static int[,] Solve(int[,] sudoku)
        {
            var spot = NextEmpty(sudoku);
            if (spot == null)
            {
                return sudoku;
            }
            for (int i = 1; i < 10; i++)
            {
                if (CanPut(sudoku, spot[0], spot[1], i))
                {
                    sudoku[spot[0], spot[1]] = i;
                    var newSudoku = Solve(sudoku);
                    if (NextEmpty(newSudoku) == null)
                    {
                        return newSudoku; // solution found
                    }
                }
            }

            sudoku[spot[0], spot[1]] = 0; // solution not found, backtrack
            return sudoku;
        }

Usage Example

Beispiel #1
0
        private void SolveButton(object sender, TappedRoutedEventArgs e)
        {
            for (int row = 0; row < 9; row++)
            {
                for (int col = 0; col < 9; col++)
                {
                    TextBlock textBlock = (TextBlock)this.FindName("Block" + (row + 1).ToString() + (col + 1).ToString());
                    if (textBlock.FontSize == 12)
                    {
                        textBlock.Text     = "";
                        textBlock.FontSize = 24;
                    }
                }
            }

            sudoku.Solve(sudoku.Board);
            FillBoard();
            if (sudoku.IsSolved())
            {
                StatusMessage.Text = "Generated solution.";
            }
            else
            {
                StatusMessage.Text = "Block(s) incorrectly filled. Not currently solvable.";
            }
        }
All Usage Examples Of Sudoku.Sudoku::Solve