Exemplo n.º 1
0
        public static (Sudoku solution, int functionCallCount, double elapsedTimeInMs) Solve(Sudoku initial)
        {
            if (!SudokuValidator.Validate(initial))
            {
                return(null, 0, 0);
            }

            var stopwatch         = Stopwatch.StartNew();
            var functionCallCount = 0;
            var solution          = Solve(initial, ref functionCallCount);

            return(solution, functionCallCount, stopwatch.Elapsed.TotalMilliseconds);
        }
Exemplo n.º 2
0
        private static void SolveSudokuAndDisplayResults(string sudokuPath)
        {
            PrintHeader(sudokuPath);

            Sudoku initial;

            try
            {
                initial = SudokuParser.Parse(sudokuPath);
            }
            catch (Exception e)
            {
                Console.WriteLine();
                Console.WriteLine($"Error: {e.Message}");
                Console.WriteLine();
                return;
            }

            var maxDepth = SudokuInspector.CountEmptyCells(initial);

            PrintSudoku(initial);
            Console.WriteLine($"Solving puzzle '{sudokuPath}'...");

            var(solution, functionCallCount, elapsedTimeInMs) = SudokuSolver.Solve(initial);

            if (solution != null)
            {
                Console.WriteLine("Done! Solution:");
                PrintSudoku(solution);

                Console.WriteLine($"Puzzle '{sudokuPath}' solved in {elapsedTimeInMs:F3}ms.");
                Console.WriteLine($"Solution found at depth {maxDepth}, in {functionCallCount} function calls.");
                Console.WriteLine($"Validity check: {(SudokuValidator.Validate(solution) ? "Passed!" : "Failed.")}");
            }
            else
            {
                Console.WriteLine("Puzzle could not be solved. Please make sure the puzzle was defined properly.");
            }

            Console.WriteLine();
        }
Exemplo n.º 3
0
 public SudokuSolver(SudokuSectionCutter sectionCutter, SudokuPositionCalculator positionCalculator, SudokuValidator validator)
 {
     _sectionCutter      = sectionCutter;
     _positionCalculator = positionCalculator;
     _validator          = validator;
 }