예제 #1
0
        public async void GenerateSudoku()
        {
            // TraceOut.Enter();

            await Task.Run(() =>
            {
                if (!_isGeneratorRunning)
                {
                    _isGeneratorRunning = true;

                    // set creation mode to automatic
                    _creationMode = SudokuCreationMode.Automatic;
                    NotifyOfPropertyChange(() => IsChecked_Automatic);
                    NotifyOfPropertyChange(() => IsChecked_Manual);

                    // create a new sudoku puzzle and solve it
                    var sudoku        = new Algorithms.v2.SudokuGenerator().GenerateSudoku(_selectedDifficulty);
                    var sudokuAsScore = new ScoreSudokuPuzzle(sudoku);
                    _solution         = new ScoreSudokuPuzzle(new Algorithms.v2.SudokuSolver().SolveSudoku(sudoku));

                    // apply the sudoku to the view
                    _sudokuView.ClearSudoku();
                    _sudokuView.ApplySudoku(sudokuAsScore);
                    _sudokuView.MarkSetFieldsAsFix();

                    // apply the changes to the history
                    ApplyChangesToHistory();

                    _isGeneratorRunning = false;
                }
            });

            // TraceOut.Leave();
        }
        public void TestSudokuGeneratorPerformance_v2()
        {
            Console.WriteLine("==========================");
            Console.WriteLine("Generator Performance Test");
            Console.WriteLine("==========================");

            int      count = 10;
            DateTime start = DateTime.Now;

            // generate 10 sudokus, check if they have a unique solution and find out the solution
            for (int i = 0; i < count; i++)
            {
                var genSudoku = new Algorithms.v2.SudokuGenerator().GenerateSudoku(SudokuDifficuty.Extreme);
                Console.WriteLine("\r\n" + genSudoku.ToString());
                Console.WriteLine(new Algorithms.v2.SudokuSolver().HasSudokuUniqueSolution(genSudoku).ToString());
                Console.WriteLine("\r\n" + new Algorithms.v2.SudokuSolver().SolveSudoku(genSudoku));
            }

            DateTime end = DateTime.Now;

            Console.WriteLine("");
            Console.WriteLine($"generating { count } sudokus took { (end - start).TotalSeconds }s");
            Console.WriteLine("");
        }