Exemplo n.º 1
0
        //Generates and returns a new Sudoku grid
        public static SudokuGrid Generate(SudokuGrid.Difficulty difficulty)
        {
            SudokuGrid generatedGrid = new SudokuGrid(ReadySudokus[randGenerator.Next(0, ReadySudokus.Length - 1)], difficulty, false);

            //Do some transformations.
            for (int transposeCount = 0; transposeCount < TransposeLimit; transposeCount++)
            {
                for (int groupSwapCount = 0; groupSwapCount < GroupSwapsLimit; groupSwapCount++)
                {
                    for (int rowSwapCount = 0; rowSwapCount < RowSwapsLimit; rowSwapCount++)
                    {
                        generatedGrid.Transform(SudokuGrid.TransformType.SwapRows);
                    }
                    generatedGrid.Transform(SudokuGrid.TransformType.SwapGroups);
                }
                generatedGrid.Transform(SudokuGrid.TransformType.Transpose);
            }

            #region Remove some numbers.

            int cellsToRemove = 0, emptyRowsLimit = 0;
            switch (difficulty)
            {
            case SudokuGrid.Difficulty.Easy:
                cellsToRemove  = EasyDifficultyInitEmptyCells;
                emptyRowsLimit = EasyDifficultyMaxEmptyRows;
                break;

            case SudokuGrid.Difficulty.Medium:
                cellsToRemove  = MediumDifficultyInitEmptyCells;
                emptyRowsLimit = MediumDifficultyMaxEmptyRows;
                break;

            case SudokuGrid.Difficulty.Hard:
                cellsToRemove  = HardDifficultyInitEmptyCells;
                emptyRowsLimit = HardDifficultyMaxEmptyRows;
                break;
            }
            generatedGrid.EmptyCells = cellsToRemove;

            //Start removing random numbers
            int       cellsRemoved = 0;
            int       rowsRemoved = 0;
            int       row, col;
            const int IndexLimit = SudokuGrid.SudokuDimension - 1;
            while (cellsRemoved <= cellsToRemove)
            {
                row = randGenerator.Next(0, IndexLimit);
                col = randGenerator.Next(0, IndexLimit);

                if (!(generatedGrid.IsSingleInRow(row, col) && rowsRemoved > emptyRowsLimit))
                {
                    generatedGrid.ClearCell(row, col);
                    cellsRemoved++;
                }
            }

            #endregion

            return(generatedGrid);
        }
        //Generates and returns a new Sudoku grid
        public static SudokuGrid Generate(SudokuGrid.Difficulty difficulty)
        {
            SudokuGrid generatedGrid = new SudokuGrid(ReadySudokus[randGenerator.Next(0, ReadySudokus.Length - 1)], difficulty, false);

            //Do some transformations.
            for (int transposeCount = 0; transposeCount < TransposeLimit; transposeCount++)
            {
                for (int groupSwapCount = 0; groupSwapCount < GroupSwapsLimit; groupSwapCount++)
                {
                    for (int rowSwapCount = 0; rowSwapCount < RowSwapsLimit; rowSwapCount++)
                    {
                        generatedGrid.Transform(SudokuGrid.TransformType.SwapRows);
                    }
                    generatedGrid.Transform(SudokuGrid.TransformType.SwapGroups);
                }
                generatedGrid.Transform(SudokuGrid.TransformType.Transpose);
            }

            #region Remove some numbers.

            int cellsToRemove = 0, emptyRowsLimit = 0;
            switch (difficulty)
            {
                case SudokuGrid.Difficulty.Easy:
                    cellsToRemove = EasyDifficultyInitEmptyCells;
                    emptyRowsLimit = EasyDifficultyMaxEmptyRows;
                    break;
                case SudokuGrid.Difficulty.Medium:
                    cellsToRemove = MediumDifficultyInitEmptyCells;
                    emptyRowsLimit = MediumDifficultyMaxEmptyRows;
                    break;
                case SudokuGrid.Difficulty.Hard:
                    cellsToRemove = HardDifficultyInitEmptyCells;
                    emptyRowsLimit = HardDifficultyMaxEmptyRows;
                    break;
            }
            generatedGrid.EmptyCells = cellsToRemove;

            //Start removing random numbers
            int cellsRemoved = 0;
            int rowsRemoved = 0;
            int row, col;
            const int IndexLimit = SudokuGrid.SudokuDimension - 1;
            while (cellsRemoved <= cellsToRemove)
            {
                row = randGenerator.Next(0, IndexLimit);
                col = randGenerator.Next(0, IndexLimit);

                if (!(generatedGrid.IsSingleInRow(row, col) && rowsRemoved > emptyRowsLimit))
                {
                    generatedGrid.ClearCell(row, col);
                    cellsRemoved++;
                }
            }

            #endregion

            return generatedGrid;
        }