Esempio n. 1
0
        public SudokuGrid(int?[] grid)
        {
            if (grid.Count() != 9 * 9)
                throw new ArgumentException();

            _cells = new List<Cell>(9 * 9);

            for (int y = 0; y < 9; y++)
            {
                for (int x = 0; x < 9; x++)
                {
                    int? value = grid[y * 9 + x];
                    Cell cell = new Cell(this, value, x, y);
                    _cells.Add(cell);
                }
            }
        }
        int? investigateCell(Cell cell)
        {
            if (!cell.IsEmpty)
                return cell.Value;

            List<int> pValues = _grid.PossibleValues(cell);

            if (pValues.Count == 1)
                return (int?)pValues.First();

            List<Cell> block = _grid.BlockOf(cell);
            block.Remove(cell);

            foreach (int value in pValues)
            {
                int otherPossibleCellsCount = block.Where(cell2 => _grid.PossibleValues(cell2).Contains(value)).Count();
                if (otherPossibleCellsCount == 0)
                    return value;
            }

            return null;
        }
Esempio n. 3
0
        public SudokuGrid(SudokuGrid grid)
        {
            if (grid == null)
                throw new ArgumentNullException();

            _cells = new List<Cell>(9 * 9);

            for (int x = 0; x < 9; x++)
            {
                for (int y = 0; y < 9; y++)
                {
                    Cell cell = new Cell(this, grid.Get(x, y).Value, x, y);
                    _cells.Add(cell);
                }
            }
        }
Esempio n. 4
0
 public List<Cell> RowOf(Cell cell)
 {
     List<Cell> row = _cells.Where(cell2 => cell2.Y == cell.Y).ToList();
     return row;
 }
Esempio n. 5
0
        public List<int> PossibleValues(Cell cell)
        {
            if(!cell.IsEmpty)
                return new List<int>(new int[]{(int)cell.Value});

            List<Cell> column = ColumnOf(cell).Where(cell2 => !cell2.IsEmpty).ToList();
            List<Cell> row = RowOf(cell).Where(cell2 => !cell2.IsEmpty).ToList();
            List<Cell> block = BlockOf(cell).Where(cell2 => !cell2.IsEmpty).ToList();

            List<int> fValues = new List<int>();
            fValues.AddRange(column.ConvertAll(cell2 => (int)cell2.Value).ToList());
            fValues.AddRange(row.ConvertAll(cell2 => (int)cell2.Value).ToList());
            fValues.AddRange(block.ConvertAll(cell2 => (int)cell2.Value).ToList());

            List<int> pValues = new List<int>();
            for (int i = 1; i <= 9; i++)
            {
                if (fValues.Contains(i) == false)
                    pValues.Add(i);
            }

            return pValues;
        }
Esempio n. 6
0
 public List<Cell> ColumnOf(Cell cell)
 {
     List<Cell> column = _cells.Where(cell2 => cell2.X == cell.X).ToList();
     return column;
 }
Esempio n. 7
0
        public bool? CantAccept(Cell cell, int value)
        {
            if(!PossibleValues(cell).Contains(value))
                return true;

            return null;
        }
Esempio n. 8
0
        public List<Cell> BlockOf(Cell cell)
        {
            int blockX = cell.X - (cell.X % 3);
            int blockY = cell.Y - (cell.Y % 3);
            List<Cell> block =new List<Cell>();

            for (int x = blockX; x < blockX + 3; x++)
            {
                for (int y = blockY; y < blockY + 3; y++)
                {
                    block.Add(Get(x, y));
                }
            }

            return block;
        }