Ejemplo n.º 1
0
 /// <summary>
 /// This function takes in a sudoku and checks if it's valid and returns a boolean based on the result
 /// </summary>
 /// <param name="sudoku"></param>
 /// <returns>true if the sudoku is valid</returns>
 private bool IsValidGrid(int[,] sudoku)
 {
     if (!CheckGrid.Check(sudoku))
     {
         MessageBox.Show("De sudoku is niet oplosbaar");
         return(false);
     }
     return(true);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// This function takes in the sudoku as a parameter
        /// It will search for the first unfilled spot and fills it
        /// Then it will check if the solution is still valid. If so perform a recursive call
        /// Else try another number
        /// </summary>
        /// <param name="_sudoku"></param>
        /// <returns>A boolean based on the fact if the sudoku is solvable</returns>
        public bool Solve(int[,] _sudoku)
        {
            // Make a clone of the array and use it in this function
            int[,] sudoku = new int[size, size];
            sudoku        = (int[, ])_sudoku.Clone();

            // Initialize a few variables
            int row, column = 0;
            int number = 0;

            // Find the first empty box in the sudoku
            for (row = 0; number != -1 && row < size; row++)
            {
                for (column = 0; number != -1 && column < size; column++)
                {
                    number = sudoku[row, column];
                }
            }
            column--;
            row--;
            int  i      = 1;
            bool solved = false;

            // Try to solve the sudoku, if it tries to fill in a number greater than the size or it is already solved it stops.
            while (i < size + 1 && !solved)
            {
                sudoku[row, column] = i;
                if (CheckGrid.Check(sudoku))
                {
                    if (CheckGrid.IsSolved(sudoku))
                    {
                        solution = sudoku;
                        return(true);
                    }
                    else
                    {
                        solved = Solve(sudoku);
                    }
                }

                i++;
            }
            // Return if the sudoku is solved or not
            return(solved);
        }