public void TestCheckSolvable()
        {
            Assert.IsTrue(SudokuSolver.CheckSolvable(this.Sudoku, out bool multipleSolutions));
            Assert.IsFalse(multipleSolutions);
            SudokuPuzzle unsolvablePuzzle = new SudokuPuzzle(SudokuPuzzle.MaximumSupportedSize, SudokuDifficulty.None);
            Random       random           = new Random();
            int          upperBound       = unsolvablePuzzle.Size - 1;

            for (int i = 0; i < upperBound; i++)
            {
                for (int j = 0; j < upperBound; j++)
                {
                    unsolvablePuzzle[i, j] = random.Next(unsolvablePuzzle.Size) + 1;
                }
            }

            unsolvablePuzzle[upperBound, 0] = unsolvablePuzzle[upperBound, 1] = 7;
            Assert.IsFalse(SudokuSolver.CheckSolvable(unsolvablePuzzle, out multipleSolutions));
            Assert.IsFalse(multipleSolutions);
            Assert.IsTrue(SudokuSolver.CheckSolvable(new SudokuPuzzle(SudokuPuzzle.MaximumSupportedSize, SudokuDifficulty.None), out multipleSolutions));
            Assert.IsTrue(multipleSolutions);
        }
예제 #2
0
        private static int Enumerate(String filename)
        {
            String text;

            if (!File.Exists(filename))
            {
                Console.WriteLine(Program.FileNotFoundErrorMessage);
                return(Program.FileNotFoundError);
            }

            try
            {
                text = File.ReadAllText(filename);
            }

            catch
            {
                Console.WriteLine(Program.FileFormatIncorrectErrorMessage);
                return(Program.FileFormatIncorrectError);
            }

            SudokuPuzzle sudoku;

            try
            {
                sudoku = SudokuPuzzle.Parse(text);
            }

            catch (FormatException)
            {
                Console.WriteLine(Program.FileFormatIncorrectErrorMessage);
                return(Program.FileFormatIncorrectError);
            }

            bool solvable = SudokuSolver.CheckSolvable(sudoku, out bool multipleSolutions);

            if (solvable)
            {
                Console.WriteLine("The specified puzzle is solvable.");

                if (multipleSolutions)
                {
                    Console.WriteLine("The specified puzzle has multiple possible solutions.");
                }

                else
                {
                    Console.WriteLine("The specified puzzle has a single valid solution.");
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    SudokuSolver.RecursiveSolve(sudoku);
                    stopwatch.Stop();
                    Console.WriteLine(sudoku);
                    Console.WriteLine($"Puzzle solved recursively in {stopwatch.ElapsedMilliseconds}ms");
                }
            }

            else
            {
                Console.WriteLine("The specified puzzle is NOT solvable.");
            }

            return(0);
        }