コード例 #1
0
        /// <summary>
        /// Generates a new <see cref="SudokuPuzzle"/> by filling it with numbers and then removing numbers according to <c><paramref name="difficulty"/></c>.
        /// </summary>
        /// <param name="size">The number of elements that the new <see cref="SudokuPuzzle"/> can store in each row, column and block.</param>
        /// <param name="difficulty">The difficulty associated with the <see cref="SudokuPuzzle"/>.</param>
        /// <returns>A new <see cref="SudokuPuzzle"/>.</returns>
        /// <exception cref="ArgumentException"><c><paramref name="size"/></c> is not a positive, square integer - or - <c><paramref name="difficulty"/></c> is equal to <see cref="SudokuDifficulty.None"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><c><paramref name="size"/></c> is less than or equal to 0 or greater than <see cref="SudokuPuzzle.MaximumSupportedSize"/>.</exception>
        public static SudokuPuzzle Generate(int size, SudokuDifficulty difficulty)
        {
            SudokuPuzzle sudoku = SudokuGenerator.AddNumbers(size, difficulty);

            SudokuGenerator.RemoveNumbers(sudoku);
            return(sudoku);
        }
コード例 #2
0
 public GameWindow()
 {
     grid = SudokuGenerator.Generate(SudokuGrid.Difficulty.Easy);
     InitWindow();
     InitCells(grid);
     currentMenuItem      = MenuItem.None;
     currentMenuItemIndex = 0;
     gameIsRunning        = true;
 }
コード例 #3
0
        static void Main(string[] args)
        {
            var validator    = new GridValidator();
            var solver       = new SudokuSolver(validator);
            var generator    = new SudokuGenerator(validator, solver);
            var policy       = new HardPuzzlePolicy();
            var sudukoPuzzle = generator.GeneratePuzzle(policy);

            sudukoPuzzle.Print();
            Console.WriteLine($"Num blanks in puzzle: {validator.GetNumBlanks(sudukoPuzzle.puzzleGrid)}");
        }
コード例 #4
0
        /// <summary>
        /// Creates a new <see cref="SudokuPuzzle"/> and fills it with numbers.
        /// </summary>
        /// <param name="size">The number of elements that the new <see cref="SudokuPuzzle"/> can store in each row, column and block.</param>
        /// <param name="difficulty">The difficulty associated with the <see cref="SudokuPuzzle"/>.</param>
        /// <returns>A new <see cref="SudokuPuzzle"/> filled with numbers.</returns>
        /// <exception cref="ArgumentException"><c><paramref name="size"/></c> is not a positive, square integer - or - <c><paramref name="difficulty"/></c> is equal to <see cref="SudokuDifficulty.None"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><c><paramref name="size"/></c> is less than or equal to 0 or greater than <see cref="SudokuPuzzle.MaximumSupportedSize"/>.</exception>
        public static SudokuPuzzle AddNumbers(int size, SudokuDifficulty difficulty)
        {
            if (difficulty == SudokuDifficulty.None)
            {
                throw new ArgumentException(nameof(difficulty));
            }

            SudokuPuzzle sudoku = new SudokuPuzzle(size, difficulty);

            SudokuGenerator.AddNumbers(sudoku);
            return(sudoku);
        }
コード例 #5
0
        private void NewGame(SudokuGrid.Difficulty difficulty)
        {
            grid = SudokuGenerator.Generate(difficulty);

            for (int row = 0; row < 9; row++)
            {
                for (int col = 0; col < 9; col++)
                {
                    cells[row, col].RefreshValues(grid[row, col], grid.IsEditable(row, col));
                }
            }
            RefreshCellValues();
        }
コード例 #6
0
        static void Main(string[] args)
        {
            while (true)
            {
                Console.WriteLine("Would you like to: (Enter 1 or 2) \n1. Generate a Sudoku puzzle \n2. Solve a Sudoku Puzzle \n3. Exit");

                //If selects 1 ---> AFter generate sudoku puzzle, ask if you want to Solve it.
                // If selects 2 --> Ask if wanna generate random puzzle first or Input their own puzzle

                int x = int.Parse(Input.ReadLine());

                if (x == 3)
                {
                    break;
                }

                var annealingSolver = new SimulatedAnnealingSolver();

                if (x == 1)
                {
                    // Generate

                    Console.WriteLine("\nSelect Difficulty level? \n1. Easy \n2. Medium \n3. Hard");
                    int y = int.Parse(Input.ReadLine());

                    SudokuDifficulty difficulty;
                    switch (y)
                    {
                    case 1: difficulty = SudokuDifficulty.Easy; break;

                    case 2: difficulty = SudokuDifficulty.Medium; break;

                    case 3: difficulty = SudokuDifficulty.Hard; break;

                    default: difficulty = SudokuDifficulty.Medium; break;
                    }

                    var sudoku = SudokuGenerator.NewSudoku(difficulty);
                    DisplaySudoku(sudoku);


                    Console.WriteLine("\nWould you like to Solve this puzzle too? (Enter 1, 2 or 3) \n1.Yes, using Backtracking \n2.Yes, using Simulated Annealing \n3. No");
                    var x1 = int.Parse(Input.ReadLine());
                    if (x1 == 1)
                    {
                        ISudokuSolver solver   = new DeepFirstSearchSolver();
                        var           solution = solver.Solve(sudoku);
                        var           isOK     = solution.IsCompleted;
                        Console.WriteLine("\nSolution found. ");
                        DisplaySudoku(solution);
                        Console.WriteLine("\nOriginal puzzle was:");
                        DisplaySudoku(sudoku);
                    }

                    if (x1 == 2)
                    {
                        var temp = sudoku.Clone();

                        annealingSolver.SimulatedAnnealingSolve(temp); Console.WriteLine("\nOriginal puzzle was:");
                        DisplaySudoku(sudoku);
                    }
                    else
                    {
                        Console.WriteLine("\nDone.");
                    }
                }

                else
                { //ie. x==2, that is, Solve
                    Console.WriteLine("\nWould you like to: (Enter 1 or 2) \n1. Input a Sudoku puzzle to Solve \n2. Generate a Sudoku puzzle to Solve");
                    int y = int.Parse(Input.ReadLine());

                    if (y == 1)
                    { //take sudoku Input to Solve
                        var temp = new SudokuBoard();
                        Console.WriteLine("\nEnter rows 1 to 9. Represent blanks as 0s.");
                        String a;
                        for (int i = 0; i < 9; i++)
                        {
                            Console.WriteLine("Enter row " + (i + 1) + ": ");
                            a = Input.ReadLine();
                            for (int j = 0; j < 9; j++)
                            {
                                temp[i, j] = a[j] - '0'; //TEST THIS INPUT METHOD!!!! ~~~~~~~~~~~~~~~~~~~~~~
                            }
                        }

                        var solver   = new DeepFirstSearchSolver();
                        var solution = solver.Solve(temp);

                        Console.WriteLine("\nSolution found. ");
                        DisplaySudoku(solution);
                        Console.WriteLine("\nOriginal puzzle was:");

                        DisplaySudoku(temp);
                    }

                    else
                    { // that is, y==2, ie. Generate puzzle to Solve
                        var sudoku = SudokuGenerator.NewSudoku(SudokuDifficulty.Easy);

                        Console.WriteLine("\nWould you like to Solve this puzzle using: (Enter 1 or 2) \n1. Backtracking \n2. Simulated Annealing");
                        int x1 = int.Parse(Input.ReadLine());
                        if (x1 == 1)
                        {
                            var solver   = new DeepFirstSearchSolver();
                            var solution = solver.Solve(sudoku);
                        }
                        else
                        {
                            annealingSolver.SimulatedAnnealingSolve(sudoku);
                        }
                    }
                }
            }
        }