Example #1
0
        public void RecursiveGuess(SudokuBoard previous, int previousGuess)
        {
            bool        blocked      = false;
            bool        fullyBlocked = false;
            int         currentGuess = 0;
            SudokuBoard tempBoard    = null;

            double seconds = sw.Elapsed.TotalSeconds;

            if (seconds % 5 < 0.001)
            {
                Console.WriteLine("\n\tElapsed: {0}", sw.Elapsed);
                Console.WriteLine("\t{0} solutions", solutions);
            }

            if (previous.CurrentlyVerified())
            {
                if (!previous.Finished())
                {
                    tempBoard = SudokuBoard.DeepCopy(previous);

                    blankCells = tempBoard.GetPossibleGuesses();
                    if (previousGuess != 0)
                    {
                        currentGuess = blankCells.First().possibilities.Find(x => x > previousGuess);
                        if (currentGuess == 0)
                        {
                            fullyBlocked = true;
                        }
                    }
                    else if (currentGuess < blankCells.First().possibilities.First())
                    {
                        currentGuess = blankCells.First().possibilities.First();
                    }

                    if (currentGuess != 0)
                    {
                        blankCells.First().ForceCellValue(currentGuess);
                    }
                    else
                    {
                        blocked = true;
                    }

                    if (tempBoard.Finished() && !tempBoard.Solved())
                    {
                        blocked = true;
                    }

                    if (!blocked)
                    {
                        RecursiveGuess(tempBoard, 0);
                    }
                }
                else if (previous.Solved())
                {
                    fullyBlocked = true;

                    if (!previous.Equals(tempBoard))
                    {
                        currentBoard = SudokuBoard.DeepCopy(previous);
                    }

                    solutions++;
                }
            }
            else
            {
                fullyBlocked = true;
            }

            if (!fullyBlocked)
            {
                RecursiveGuess(previous, currentGuess);
            }
        }
Example #2
0
        public void RecursiveGuess(SudokuBoard previous, int previousGuess)
        {
            bool blocked = false;
            bool fullyBlocked = false;
            int currentGuess = 0;
            SudokuBoard tempBoard = null;

            double seconds = sw.Elapsed.TotalSeconds;

            if (seconds % 5 < 0.001)
            {
                Console.WriteLine("\n\tElapsed: {0}", sw.Elapsed);
                Console.WriteLine("\t{0} solutions", solutions);
            }

            if (previous.CurrentlyVerified())
            {
                if (!previous.Finished())
                {
                    tempBoard = SudokuBoard.DeepCopy(previous);

                    blankCells = tempBoard.GetPossibleGuesses();
                    if (previousGuess != 0)
                    {
                        currentGuess = blankCells.First().possibilities.Find(x => x > previousGuess);
                        if (currentGuess == 0)
                            fullyBlocked = true;
                    }
                    else if (currentGuess < blankCells.First().possibilities.First())
                    {
                        currentGuess = blankCells.First().possibilities.First();
                    }

                    if (currentGuess != 0)
                        blankCells.First().ForceCellValue(currentGuess);
                    else
                        blocked = true;

                    if (tempBoard.Finished() && !tempBoard.Solved())
                        blocked = true;

                    if (!blocked)
                        RecursiveGuess(tempBoard, 0);
                }
                else if (previous.Solved())
                {
                    fullyBlocked = true;

                    if (!previous.Equals(tempBoard))
                        currentBoard = SudokuBoard.DeepCopy(previous);

                    solutions++;
                }
            }
            else
                fullyBlocked = true;

            if (!fullyBlocked)
                RecursiveGuess(previous, currentGuess);
        }