Example #1
0
        private SudokuSolver GuessFork()
        {
            Point position = GetSmallestOptionPosition();

            if (position.X < 0 || position.Y < 0)
            {
                return(null);
            }

            foreach (var opt in Data[position.X, position.Y].Notes)
            {
                SudokuSolver fork = SudokuSolver.Clone(this);

                // Set the guess
                fork.Data[position.X, position.Y].Value = opt;

                // Try to solve
                if (fork.Solve(true))
                {
                    return(fork);
                }
            }

            return(null);
        }
Example #2
0
        public bool Solve(bool canFork = true)
        {
            int count;

            do
            {
                count = GetOptionsCount();

                foreach (Technique technique in Techniques)
                {
                    technique.ReduceOptions();
                    FillValuesWithOneOption();
                }
            } while (count != GetOptionsCount());

            if (Data.IsCorrect())
            {
                return(true);
            }

            if (IsInvalid())
            {
                return(false);
            }

            if (!canFork)
            {
                return(false);
            }

            SudokuSolver solution = GuessFork();

            if (solution == null)
            {
                return(false);
            }

            this.Data = solution.Data;

            return(true);
        }
Example #3
0
        public static SudokuSolver Setup(Sudoku data)
        {
            SudokuSolver solver = new SudokuSolver(data);

            for (int x = 0; x < Sudoku.BOARDSIZE; x++)
            {
                for (int y = 0; y < Sudoku.BOARDSIZE; y++)
                {
                    if (solver.Data[x, y].HasValue)
                    {
                        solver.Data[x, y].Notes.Add(solver.Data[x, y]);
                    }
                    else
                    {
                        foreach (var note in DefaultOptions)
                        {
                            solver.Data[x, y].Notes.Add((byte)note);
                        }
                    }
                }
            }

            return(solver);
        }
Example #4
0
 public static SudokuSolver Clone(SudokuSolver subject)
 {
     return(new SudokuSolver(subject.Data.Clone()));
 }