private IEnumerable<int> GetColumnCadidates(Position position)
 {
     var columnCadidates = new List<int>();
     for (var i = 0; i < SudokuBoard.EdgeSize; i++)
     {
         columnCadidates.Add(_sudokuBoard.Get(new Position(i, position.YPos)).Value);
     }
     columnCadidates.RemoveAt(position.XPos);
     return columnCadidates;
 }
        private IEnumerable<int> GetRowCadidates(Position position)
        {
            var rowCadidates = new List<int>();
            for (var y = 0; y < SudokuBoard.EdgeSize; y++)
            {

                rowCadidates.Add(_sudokuBoard.Get(new Position(position.XPos, y)).Value);
            }
            rowCadidates.RemoveAt(position.YPos);
            return rowCadidates;
        }
        public List<int> GetCandidates(Position position)
        {
            var cadidates = new List<int>();

            if (_sudokuBoard.Get(position).Locked) return cadidates;

            var rowCadidates = GetRowCadidates(position);
            var columnCadidates = GetColumnCadidates(position);
            var squareCadidates = GetSquareCadidates(position);

            var valuesSet = rowCadidates.Union(columnCadidates).Union(squareCadidates).Distinct();
            return SudokuBoard.Scopes.Except(valuesSet).ToList();
        }
Exemple #4
0
        public bool Set(Position position, int value, bool locked)
        {
            if (value == 0)
            {
                _cells[position.Index].Value = value;
                return true;
            }

            if (!_candidateRule.GetCandidates(position).Contains(value)) return false;

            _cells[position.Index].Value = value;
            _cells[position.Index].Locked = locked;
            return true;
        }
        private IEnumerable<int> GetSquareCadidates(Position position)
        {
            var ints = new List<int>();
            var squareOriginX = position.SquareOriginX();
            var squareOriginY = position.SquareOriginY();

            for (var x = squareOriginX; x < squareOriginX + SudokuBoard.SquareEdgeSize; x++)
            {
                for (var y = squareOriginY; y < squareOriginY + SudokuBoard.SquareEdgeSize; y++)
                {
                    ints.Add(_sudokuBoard.Get(new Position(x,y)).Value);
                }
            }
            return ints;
        }
        public void should_get_candidates_for_unlocked_cell()
        {
            var sudokuBoard = new SudokuBoard();
            var position = new Position(0,0);
            var candidates = new GeneralCandidateRule(sudokuBoard).GetCandidates(position);
            Assert.Equal(9, candidates.Count);

            sudokuBoard.Set(new Position(0, 1), 2, false);
            sudokuBoard.Set(new Position(0, 2), 3, false);
            sudokuBoard.Set(new Position(0, 3), 4, false);
            sudokuBoard.Set(new Position(0, 4), 5, false);
            sudokuBoard.Set(new Position(0, 5), 6, false);
            sudokuBoard.Set(new Position(0, 6), 7, false);
            sudokuBoard.Set(new Position(0, 7), 8, false);
            sudokuBoard.Set(new Position(0, 8), 9, false);
            candidates = new GeneralCandidateRule(sudokuBoard).GetCandidates(position);
            Assert.Equal(1, candidates.Count);
            Assert.Equal(1, candidates.First());
        }
Exemple #7
0
        public void Solve()
        {
            for (var i = 0; i < BoardSize; i++)
            {
                var position = new Position(i);
                _cells[i].Candidates = new Stack<int>(_candidateRule.GetCandidates(position));
            }
            for (var i = 0; i < BoardSize;)
            {
                if (_cells[i].Set())
                {
                    if (++i == BoardSize) break;

                    _cells[i].Candidates = new Stack<int>(_candidateRule.GetCandidates(new Position(i)));
                    continue;
                }
                while (--i != 0)
                {
                    if (i < 0)
                    {
                        Console.Error.WriteLine("No Answer");
                        break;
                    }
                    if (!_cells[i].Locked) break;
                }
            }
        }
Exemple #8
0
 public Cell Get(Position position)
 {
     return _cells[position.Index].Clone;
 }
        public void should_get_empty_candidates_for_locked_cell()
        {
            var sudokuBoard = new SudokuBoard();
            var position = new Position(0, 0);
            sudokuBoard.Set(position, 2, true);

            var candidates = new GeneralCandidateRule(sudokuBoard).GetCandidates(position);
            Assert.Equal(0, candidates.Count);
        }
        public void should_set_the_cell_value_for()
        {
            var sudokuBoard = new SudokuBoard();
            var position = new Position(0, 0);
            sudokuBoard.Set(position, 9, false);
            Assert.Equal(9, sudokuBoard.Get(position).Value);

            position = new Position(8, 8);
            sudokuBoard.Set(position, 1, true);
            Assert.Equal(1, sudokuBoard.Get(position).Value);
            Assert.Equal(true, sudokuBoard.Get(position).Locked);
        }
        public void should_not_break_the_unique_rule()
        {
            var sudokuBoard = new SudokuBoard();
            var position = new Position(0, 0);
            sudokuBoard.Set(position, 1, false);
            Assert.Equal(1,sudokuBoard.Get(position).Value);

            position = new Position(0, 1);
            sudokuBoard.Set(position, 1, false);
            Assert.NotEqual(1, sudokuBoard.Get(position).Value);
        }