Example #1
0
        public IEnumerable <SudokuBoard> Solve()
        {
            ResetSolutions();
            SudokuProgress simplify = SudokuProgress.PROGRESS;

            while (simplify == SudokuProgress.PROGRESS)
            {
                simplify = Simplify();
            }

            if (simplify == SudokuProgress.FAILED)
            {
                yield break;
            }

            // Find one of the values with the least number of alternatives, but that still has at least 2 alternatives
            var query = from rule in rules
                        from tile in rule
                        where tile.PossibleCount > 1
                        orderby tile.PossibleCount ascending
                        select tile;

            SudokuTile chosen = query.FirstOrDefault();

            if (chosen == null)
            {
                // We solved it!
                yield return(this);

                yield break;
            }

            foreach (var value in Enumerable.Range(1, _maxValue))
            {
                // Iterate through all the valid possibles on the chosen square and pick a number for it
                if (!chosen.IsPossibleValue(value))
                {
                    continue;
                }
                var copy = new SudokuBoard(this);
                copy.Tile(chosen.X, chosen.Y).Fix(value, "Trial and error");
                foreach (var innerSolution in copy.Solve())
                {
                    yield return(innerSolution);
                }
            }
            yield break;
        }
Example #2
0
        internal SudokuProgress Simplify()
        {
            SudokuProgress result = SudokuProgress.NO_PROGRESS;
            bool           valid  = CheckValid();

            if (!valid)
            {
                return(SudokuProgress.FAILED);
            }

            foreach (SudokuRule rule in rules)
            {
                result = SudokuTile.CombineSolvedState(result, rule.Solve());
            }

            return(result);
        }