Пример #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)
            {
                // The board has been completed, we're done!
                yield return(this);

                yield break;
            }

            Console.WriteLine("SudokuTile: " + chosen.ToString());

            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.IsValuePossible(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;
        }
Пример #2
0
 private static void CompleteSolve(SudokuBoard board)
 {
     Console.WriteLine("Rules:");
     board.OutputRules();
     Console.WriteLine("Board:");
     board.Output();
     var solutions = board.Solve().ToList();
     Console.WriteLine("Base Board Progress:");
     board.Output();
     Console.WriteLine("--");
     Console.WriteLine("--");
     Console.WriteLine("All " + solutions.Count + " solutions:");
     var i = 1;
     foreach (var solution in solutions)
     {
         Console.WriteLine("----------------");
         Console.WriteLine("Solution " + i++.ToString() + " / " + solutions.Count + ":");
         solution.Output();
     }
 }
Пример #3
0
        private static void CompleteSolve(SudokuBoard board)
        {
            Console.WriteLine("Rules:");
            board.OutputRules();
            Console.WriteLine("Board:");
            board.Output();
            var solutions = board.Solve().ToList();

            Console.WriteLine("Base Board Progress:");
            board.Output();
            Console.WriteLine("--");
            Console.WriteLine("--");
            Console.WriteLine("All " + solutions.Count + " solutions:");
            var i = 1;

            foreach (var solution in solutions)
            {
                Console.WriteLine("----------------");
                Console.WriteLine("Solution " + i++.ToString() + " / " + solutions.Count + ":");
                solution.Output();
            }
        }
Пример #4
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)
            {
                // The board has been completed, we're done!
                yield return this;
                yield break;
            }

            Console.WriteLine("SudokuTile: " + chosen.ToString());

            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.IsValuePossible(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;
        }