コード例 #1
0
 public SudokuViewModel() : this(
         IoCContainer.Resolve <ISudokuFileLoader>(),
         IoCContainer.Resolve <ISudokuGenerator>())
 {
     _board = new int?[9][];
     CurrentSudokuDifficultyLevel = SudokuDifficultyLevel.Easy;
 }
コード例 #2
0
ファイル: Sudoku.cs プロジェクト: 3391916/Sudoku
        public Sudoku(List <SudokuCell> cells)
        {
            if (cells == null)
            {
                throw new ArgumentNullException("cells can't be equal to null");
            }

            _cells = cells;

            _sudokuSolver = ServiceLocator.Current.GetInstance <ISudokuSolver>();
            _sudokuComplexityEstimator = ServiceLocator.Current.GetInstance <ISudokuComplexityEstimator>();
            _sudokuDifficultyLevel     = _sudokuComplexityEstimator.EstimateComplexityLevel(this);
        }
コード例 #3
0
        public int GetNumberOfFreeCellsByComplexityLevel(SudokuDifficultyLevel sudokuDifficultyLevel)
        {
            switch (sudokuDifficultyLevel)
            {
            case SudokuDifficultyLevel.Easy:
                return(49);

            case SudokuDifficultyLevel.Medium:
                return(53);

            case SudokuDifficultyLevel.Hard:
                return(57);

            case SudokuDifficultyLevel.Samurai:
            default:
                return(58);
            }
        }
コード例 #4
0
        public Sudoku GenerateSudoku(SudokuDifficultyLevel difficultyLevel)
        {
            var newSudokuMatrix      = GenerateMatrix();
            var nullableSudokuMatrix = new int?[9][];

            for (int i = 0; i < 9; i++)
            {
                if (nullableSudokuMatrix[i] == null)
                {
                    nullableSudokuMatrix[i] = new int?[9];
                }

                for (int j = 0; j < 9; j++)
                {
                    nullableSudokuMatrix[i][j] = newSudokuMatrix[i][j];
                }
            }

            var symetry = SudokuSymetry.VerticalHorizontal;

            if (difficultyLevel == SudokuDifficultyLevel.Hard || difficultyLevel == SudokuDifficultyLevel.Samurai)
            {
                symetry = SudokuSymetry.Diagonal;
            }

            // fetching number of empty cells per level
            int emptyCellsRequiredForLevel = _sudokuComplexityEstimator.GetNumberOfFreeCellsByComplexityLevel(difficultyLevel);
            int currentEmptyCellsCount     = 0;

            var generatedEmptyCellsPossibleCoordinates = GenerateSymeticCoordinates(symetry);

            while (currentEmptyCellsCount < emptyCellsRequiredForLevel)
            {
                if (!generatedEmptyCellsPossibleCoordinates.Any())
                {
                    generatedEmptyCellsPossibleCoordinates = GenerateSymeticCoordinates(symetry);
                }

                var coord = generatedEmptyCellsPossibleCoordinates.Pop();

                if (nullableSudokuMatrix[coord.X][coord.Y] == null)
                {
                    continue;
                }

                nullableSudokuMatrix[coord.X][coord.Y] = null;
                currentEmptyCellsCount++;
            }

            var sudokuCells = new List <SudokuCell>();

            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    var cell = new SudokuCell();
                    cell.X = j;
                    cell.Y = i;

                    cell.Value   = nullableSudokuMatrix[i][j];
                    cell.Initial = nullableSudokuMatrix[i][j] != null;

                    cell.Block = SudokuCell.GetBlock(cell.X, cell.Y);

                    sudokuCells.Add(cell);
                }
            }

            return(new Sudoku(sudokuCells));
        }