private bool Solved(Cell cell)
        {
            var usedDigits = this.GetUsedDigits(cell);

            for (byte digit = 1; digit < this.inputBoard.Length + 1; digit++)
            {
                if (!usedDigits[digit - 1])
                {
                    this.inputBoard[cell] = digit;

                    var nextCell = this.GetNextCell(cell);

                    if (nextCell.Row == this.inputBoard.Length)
                    {
                        return true;
                    }

                    if (this.Solved(nextCell))
                    {
                        return true;
                    }
                }
            }

            this.inputBoard[cell] = EmptyCell;
            return false;
        }
示例#2
0
        private static void DumpBoard(Board board, string header, Cell highlightCell = null)
        {
            Console.WriteLine(header);

            var dump = board.GetDump(highlightCell);
            Console.WriteLine(dump);
        }
 private void GetIfDigitIsUsed(Cell cellToCheck, IList<bool> usedDigits)
 {
     if (this.inputBoard[cellToCheck] != EmptyCell)
     {
         usedDigits[this.inputBoard[cellToCheck] - 1] = true;
     }
 }
        private static IEnumerable<Cell> GetColumnPositions(Board board, Cell value, int gx)
        {
            var result =
                from c in board
                where c.Value == value.Value && c.XSection == gx
                select c;

            return result.ToArray();
        }
        public static List<int> GetValues(IEnumerable<Cell> cells, Cell cell, Orientation orientation)
        {
            var values =
                    cells.Where(
                        c => (orientation == Orientation.Horizontal ? c.Y == cell.Y : c.X == cell.X) && c.Value.HasValue)
                        .Select(c => c.Value.Value).ToList();

            return values;
        }
        private Cell GetNextCell(Cell cell)
        {
            while (cell.Row < this.inputBoard.Length && this.inputBoard[cell.Row, cell.Col] != EmptyCell)
            {
                if (++cell.Col > this.inputBoard.Length - 1)
                {
                    cell.Col = 0;
                    cell.Row++;
                }
            }

            return cell;
        }
示例#7
0
        public MainViewModel()
        {
            var cells = new List<Cell>();
            for (int i = 0; i < 9; i++)
            {
                for (int j = 0; j < 9; j++)
                {
                    Cell cell = new Cell(i, j);
                    cells.Add(cell);
                }
            }

            Cells = cells;
        }
        public static bool IsPossible(IEnumerable<Cell> cells, Cell cell, int value)
        {
            if(cell == null || cell.Value.HasValue)
                throw new ArgumentException("cell");

            // value exist in block
            if (cells.Count(c => c.Block == cell.Block && c.Value.HasValue && c.Value.Value == value) > 0)
                return false;

            // horizontal check
            if(GetValues(cells, cell, Orientation.Horizontal).Contains(value))
                return false;

            // vertical check
            if (GetValues(cells, cell, Orientation.Vertical).Contains(value))
                return false;

            return true;
        }
示例#9
0
        void System.Windows.Markup.IComponentConnector.Connect(int connectionId, object target)
        {
            switch (connectionId)
            {
            case 1:
                this.cell00 = ((SudokuSolver.Cell)(target));
                return;

            case 2:
                this.cell01 = ((SudokuSolver.Cell)(target));
                return;

            case 3:
                this.cell02 = ((SudokuSolver.Cell)(target));
                return;

            case 4:
                this.cell10 = ((SudokuSolver.Cell)(target));
                return;

            case 5:
                this.cell11 = ((SudokuSolver.Cell)(target));
                return;

            case 6:
                this.cell12 = ((SudokuSolver.Cell)(target));
                return;

            case 7:
                this.cell20 = ((SudokuSolver.Cell)(target));
                return;

            case 8:
                this.cell21 = ((SudokuSolver.Cell)(target));
                return;

            case 9:
                this.cell22 = ((SudokuSolver.Cell)(target));
                return;
            }
            this._contentLoaded = true;
        }
        private bool[] GetUsedDigits(Cell cell)
        {
            var tileSize = this.inputBoard.TileSize;
            var usedDigits = new bool[this.inputBoard.Length];

            for (byte index = 0; index < this.inputBoard.Length; index++)
            {
                this.GetIfDigitIsUsed(new Cell { Row = cell.Row, Col = index }, usedDigits);
                this.GetIfDigitIsUsed(new Cell { Row = index, Col = cell.Col }, usedDigits);
                this.GetIfDigitIsUsed(
                    new Cell
                        {
                            Row = (byte)((cell.Row / tileSize) * tileSize + index / tileSize),
                            Col = (byte)((cell.Col / tileSize) * tileSize + index % tileSize)
                        },
                    usedDigits);
            }

            return usedDigits;
        }
示例#11
0
        public static Cell[] GetValueCandidates(Board board, Cell cell)
        {
            var columnValues = board.GetAssignedColumnCells(cell);
            var rowValues =    board.GetAssignedRowCells(cell);
            var gridValues = board.GetGridCells(cell)
                .Where(i => i.IsAssigned())
                .ToArray();

            var all = Cell.All;
            // BUG: "all" keeps bad positional value, corrupting board.

            var missingColumnValues = all.Except(columnValues).ToArray();
            var missingRowValues =    all.Except(rowValues)   .ToArray();
            var missingGridValues =   all.Except(gridValues)  .ToArray();

            var candidates = missingColumnValues
                .Intersect(missingRowValues)
                .Intersect(missingGridValues)
                .ToArray();

            return candidates;
        }
示例#12
0
        private List<int> GetPosibilities(Cell cell)
        {
            var initialps = this.InitialPossibilities.ToList();

            maze.GetRow(cell.Row).ForEach(x => initialps.Remove(x.Value));
            maze.GetColumn(cell.Column).ForEach(x => initialps.Remove(x.Value));
            maze.GetSquare(cell.Row, cell.Column).ForEach(x => initialps.Remove(x.Value));

            return initialps;
        }