コード例 #1
0
        public SudokuPuzzle GenerateSudoku(SudokuDifficuty difficulty, int length = 9)
        {
            // get randomly filled out sudoku
            SudokuPuzzle solution = SolveSudoku(new SudokuPuzzle());
            SudokuPuzzle sudoku   = (SudokuPuzzle)solution.Clone();

            // check how many fields need to be removed (depending on desired difficulty)
            int fieldsToRemove = ((length * length) - (int)difficulty);

            while (fieldsToRemove > 0)
            {
                int freeFieldsCount = sudoku.GetFreeFields().Count;

                foreach (var field in sudoku.GetSetFields().Shuffle())
                {
                    if (fieldsToRemove > 0 && isFieldDetermined(sudoku, solution, field.RowIndex, field.ColumnIndex))
                    {
                        field.SetValue(0);
                        fieldsToRemove--;
                    }
                }

                // check if progress was made (if not => try with another sudoku puzzle)
                if ((freeFieldsCount - sudoku.GetFreeFields().Count) == 0)
                {
                    sudoku = GenerateSudoku(difficulty, length);
                    break;
                }
            }

            // check if the generated sudoku is unique. if not create a new one.
            return((HasSudokuUniqueSolution(sudoku)) ? sudoku : GenerateSudoku(difficulty, length));
        }
コード例 #2
0
        public SudokuPuzzle GenerateSudoku(SudokuDifficuty difficulty, int length = 9)
        {
            // get filled out random sudoku
            var solver = new SudokuSolver();
            var sudoku = solver.SolveSudoku(new SudokuPuzzle());

            // reset several values (depending on desired difficulty)
            for (int i = 0; i < (length * length) - (int)difficulty; i++)
            {
                var field = sudoku.GetSetFields().ChooseRandom();
                field.SetValue(0);
            }

            // check if sudoku has a unique solution (if not, generate a new one and repeat procedure)
            sudoku = (!solver.HasSudokuUniqueSolution(sudoku)) ? GenerateSudoku(difficulty) : sudoku;

            return(sudoku);
        }
コード例 #3
0
        public SudokuPuzzle GenerateSudoku(SudokuDifficuty difficulty, int length = 9)
        {
            // get randomly filled out sudoku
            SudokuPuzzle source = SolveSudoku(new SudokuPuzzle());
            SudokuPuzzle result;

            int i;
            int fieldsToRemove = ((length * length) - (int)difficulty);

            //DateTime genStart = DateTime.Now;
            //Console.WriteLine($"====================================================");

            do
            {
                result = (SudokuPuzzle)source.Clone();
                result.GetFields1D().ChooseRandom().SetValue(0);

                for (i = 0; i < fieldsToRemove; i++)
                {
                    //DateTime start = DateTime.Now;

                    if (!removeField(result, source))
                    {
                        break;
                    }

                    //DateTime end = DateTime.Now;
                    //Console.WriteLine($"iteration { i } took { (end - start).TotalMilliseconds } ms");
                }

                //if (i < fieldsToRemove) { Console.WriteLine($"generator failed"); }
                //else { Console.WriteLine($"generator successful (total time elapsed: { (DateTime.Now - genStart).TotalMilliseconds } ms)"); }
            }while (i < fieldsToRemove);

            //Console.WriteLine($"====================================================");

            return(result);
        }