Beispiel #1
0
 private void overwriteFields(SudokuBoard valuesToWrite) /* PLEASE TEST ME  -----------------------------------------!*/
 {
     /* Update array */
     this.boardArray = null;
     this.boardArray = valuesToWrite.boardArray;
 }
Beispiel #2
0
        private bool solveSudokuRecursive()
        {
            /* This is it, the main backtracking-based solving engine... */


            /* Traverse the entire sudokuBoard searching for empty cells */
            for (int x = 0; x < SUDOKU_SIZE; x++)
            {
                for (int y = 0; y < SUDOKU_SIZE; y++)
                {
                    /* Look for Emtpy cells */
                    if (this.boardArray[x, y].getValue() == 0)
                    {
                        /* Try each of the possible values of the current emtpy cell */
                        foreach (Int16 testValue in this.boardArray[x, y].validValues)
                        {
                            bool tempConstraintPResult;

                            /* CLONE */
                            SudokuBoard currentSudokuBranch = this.clone();
                            /* WRITE */
                            currentSudokuBranch.writeCell(x, y, testValue);

                            if (SudokuBoard.currentSolvingOptimization == SolvingOptimizations.UNIQUE_UPDATE)
                            {
                                /* OPTIMIZE */
                                tempConstraintPResult = currentSudokuBranch.constraintPropagation();
                            }

                            if (currentSudokuBranch.checkStucked() == true)
                            {
                                tempConstraintPResult = false;
                                SudokuBoard.backtrackCounter++;
                            }
                            else if (currentSudokuBranch.checkSolved() == true)
                            {
                                tempConstraintPResult = true;
                            }
                            else if (currentSudokuBranch.checkRepeatedValues() == false)
                            {
                                tempConstraintPResult = false;
                                SudokuBoard.backtrackCounter++;
                            }
                            else
                            {
                                tempConstraintPResult = currentSudokuBranch.solveSudokuRecursive();
                            }


                            if (tempConstraintPResult == true)
                            {
                                /* Sudoku Solved!!! */
                                /* Transfer values from tempSudoku to current object */
                                this.overwriteFields(currentSudokuBranch);
                                return(true);
                            }
                        }
                        return(false);
                    }
                }
            }
            return(false);
        }