Exemplo n.º 1
0
        public void InValidSolved()
        {
            string puzzleString = "876549321543182769291763854385471692964329175127695438752816943419237586638954217";

            SudokuPuzzle puzzle = new SudokuPuzzle(puzzleString);

            bool res = m_Validator.ValidateSudoku(puzzle);

            Assert.IsFalse(res);
        }
Exemplo n.º 2
0
        public void Easy()
        {
            ISudokuPuzzle puzzle = m_Generator.Generate(SudokuDifficulty.easy);

            bool valid = m_Validator.ValidateSudoku(puzzle);

            Assert.IsTrue(valid);
        }
Exemplo n.º 3
0
        public ISudokuPuzzle Generate(SudokuDifficulty config, int?seed = null)
        {
            if (seed == null)
            {
                seed = new Random().Next();
            }

            SudokuPuzzle puzzle = new SudokuPuzzle((int)seed);

            m_AvailableNumbers.Clear();

            int i = 0;

            while (i < 81)
            {
                if (!TryGetNextNumber(i, (int)seed, out int val))
                {
                    puzzle.Cells[i].Value = 0;
                    i--;
                    continue;
                }

                puzzle.Cells[i].Value = val;

                if (m_Validator.ValidateSudoku(puzzle))
                {
                    i++;
                }
            }

            AdjustPuzzleToConfig(puzzle, config, (int)seed);

            return(puzzle);
        }
Exemplo n.º 4
0
        //TODO: Solve from front and back to see if multiple solutions exist (throw exception)
        public ISudokuPuzzle Solve()
        {
            if (!m_Validator.ValidateSudoku(m_Puzzle))
            {
                throw new InvalidPuzzleException();
            }

            int i = 0;

            Cell[] userCells = m_Puzzle.Cells.Where(c => c is UserCell).ToArray();

            while (i < userCells.Length)
            {
                if (i < 0)
                {
                    throw new UnsolvablePuzzleException();
                }

                UserCell cell = userCells[i] as UserCell;

                int val = cell.Value + 1;

                if (val > 9)
                {
                    cell.Value = 0;
                    i--;
                    continue;
                }

                cell.Value = val;

                if (m_Validator.ValidateCell(m_Puzzle, cell))
                {
                    i++;
                }
            }

            return(m_Puzzle);
        }