Exemplo n.º 1
0
    /// <summary>
    /// Solves the puzzle as best it can.  This is the method portraying the logical order of solving.
    /// </summary>
    /// <param name="s"></param>
    /// <param name="c"></param>
    /// <returns>integer type board</returns>
    public static int[,] Solve(SNode[,] s, Controller c)
    {
        //untested.Remove(tested[0]);
        controller = c;

        Load(s); // Loads initial state with predetermined obvious "not-possibles"

        while (!IsSolved())
        {
            // Precedence 0: Solve spaces when only 1 value is possible.
            if (NakedSingle())
            {
                failCount = 0;
                continue;
            }

            // Precedence 1: Remove some possibles with this solve technique.  Line/Box Reduction.
            if (LineBoxReduction())
            {
                failCount = 0;
                continue;
            }

            // Precedence 2: Remove some possibles with this solve technique.  Naked, Single, Pair, Triplet, Quadruplet, etc...
            if (SetLoop())
            {
                failCount = 0;
                continue;
            }

            // Precedence 3: Remove some possibles with this solve technique.  XWing[Row->Col] or XWing[Col->Row]
            if (XWingLoop())
            {
                failCount = 0;
                continue;
            }

            // Precedence 4: Guess a number into a SNode.  Hope that it causes contradiction or completed the puzzle.
            //if (!guessPlaced)
            //{
            //    BackupSudokuBoard();
            //    GuessValue();
            //}
            //else
            //{
            //    if (Contradiction())
            //    {
            //        attemptedGuesses.Clear();
            //
            //        RetractSudokuBoardState();
            //
            //        guessedNode = board.Sudoku[guessedNode.columnIndex, guessedNode.rowIndex];
            //
            //        guessedNode.Possibles.Remove(guessedValue);
            //    }
            //    else    // we didn't gain any knowledge....try a different number on same SNode or different SNode entirely.
            //    {
            //        string coord = "" + guessedNode.columnIndex + guessedNode.rowIndex;
            //
            //        if (!attemptedGuesses.ContainsKey(coord))
            //            attemptedGuesses.Add(coord, new OrderedSet<int>());
            //
            //        attemptedGuesses[coord].Add(guessedValue);
            //
            //        RetractSudokuBoardState();
            //        GuessValue();
            //    }
            //}

            if (failCount > 2)
            {
                return(SNode.ConvertToOutput(board));
            }

            failCount++;
        }

        if (controller != null)
        {
            string output = "Puzzle Solved!";
            controller.PrintStatement(output);
        }

        return(SNode.ConvertToOutput(board));
    } // Solve()