public static SudokuGrid Generate(SudokuGrid.Difficulty difficulty)
        {
            SudokuGrid generatedGrid = new SudokuGrid(ReadySudokus[randGenerator.Next(0, ReadySudokus.Length - 1)], difficulty, false);

            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;

            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);
        }
Beispiel #2
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();
        }