Ejemplo n.º 1
0
 public void GlobalSetup()
 {
     AllPuzzles = new Dictionary <SudokuDifficulty, IList <Core.GrilleSudoku> >();
     foreach (var difficulty in Enum.GetValues(typeof(SudokuDifficulty)).Cast <SudokuDifficulty>())
     {
         AllPuzzles[difficulty] = SudokuHelper.GetSudokus(Difficulty);
     }
 }
Ejemplo n.º 2
0
        private static void SingleSolverTest()
        {
            var solvers = Core.GrilleSudoku.GetSolvers();

            Console.WriteLine("Select difficulty: 1-Easy, 2-Medium, 3-Hard");
            var strDiff = Console.ReadLine();

            int.TryParse(strDiff, out var intDiff);
            SudokuDifficulty difficulty = intDiff switch
            {
                1 => SudokuDifficulty.Easy,
                2 => SudokuDifficulty.Medium,
                _ => SudokuDifficulty.Hard
            };

            var sudokus = SudokuHelper.GetSudokus(difficulty);

            Console.WriteLine($"Choose a puzzle index between 1 and {sudokus.Count}");
            var strIdx = Console.ReadLine();

            int.TryParse(strIdx, out var intIdx);
            var targetSudoku = sudokus[intIdx - 1];

            Console.WriteLine("Chosen Puzzle:");
            Console.WriteLine(targetSudoku.ToString());

            Console.WriteLine("Choose a solver:");
            for (int i = 0; i < solvers.Count(); i++)
            {
                Console.WriteLine($"{(i + 1).ToString(CultureInfo.InvariantCulture)} - {solvers[i].GetType().FullName}");
            }
            var strSolver = Console.ReadLine();

            int.TryParse(strSolver, out var intSolver);
            var solver = solvers[intSolver - 1];

            var cloneSudoku = targetSudoku.CloneSudoku();
            var sw          = Stopwatch.StartNew();

            solver.Solve(cloneSudoku);

            var elapsed = sw.Elapsed;

            if (!cloneSudoku.IsValid(targetSudoku))
            {
                Console.WriteLine($"Invalid Solution : Solution has {cloneSudoku.NbErrors(targetSudoku)} errors");
                Console.WriteLine("Invalid solution:");
            }
            else
            {
                Console.WriteLine("Valid solution:");
            }

            Console.WriteLine(cloneSudoku.ToString());
            Console.WriteLine($"Time to solution: {elapsed.TotalMilliseconds} ms");
        }
    }