Exemplo n.º 1
0
        public void SolverCanSolveHardGrid()
        {
            var grid = new FullGrid();

            short[] values =
            {
                1, 0, 0, 0, 0, 7, 0, 9, 0,
                0, 3, 0, 0, 2, 0, 0, 0, 8,
                0, 0, 9, 6, 0, 0, 5, 0, 0,
                0, 0, 5, 3, 0, 0, 9, 0, 0,
                0, 1, 0, 0, 8, 0, 0, 0, 2,
                6, 0, 0, 0, 0, 4, 0, 0, 0,
                3, 0, 0, 0, 0, 0, 0, 1, 0,
                0, 4, 0, 0, 0, 0, 0, 0, 7,
                0, 0, 7, 0, 0, 0, 3, 0, 0
            };
            FillGrid(grid, values);

            var solver = new Solver(grid);
            var result = solver.TrySolveGrid();

            Assert.IsTrue(result, "Solver didn't succeed");
            Assert.IsTrue(grid.IsComplete(), "Grid is not complete");
            Assert.IsTrue(grid.IsValid(), "Grid is not valid");
        }
Exemplo n.º 2
0
 private void FillGrid(FullGrid grid, short[] values)
 {
     for (int rowId = 0; rowId < 9; rowId++)
     {
         grid.Rows[rowId].SetValues(values.Skip(rowId * 9).Take(9).ToArray());
     }
 }
Exemplo n.º 3
0
        private bool TrySolveGridWithAssumptions()
        {
            var     numberOfPossibleCells = 2;
            short   number = 1;
            SubGrid subGridWithNumberOfPossibleCells = null;

            while (numberOfPossibleCells <= 9)
            {
                while (number <= 9)
                {
                    subGridWithNumberOfPossibleCells = GetSubGridWithNumberOfPossibleCells(_mainGrid.SubGrids3x3, numberOfPossibleCells, number);
                    if (subGridWithNumberOfPossibleCells != null)
                    {
                        break;
                    }
                    subGridWithNumberOfPossibleCells = GetSubGridWithNumberOfPossibleCells(_mainGrid.Rows, numberOfPossibleCells, number);
                    if (subGridWithNumberOfPossibleCells != null)
                    {
                        break;
                    }
                    subGridWithNumberOfPossibleCells = GetSubGridWithNumberOfPossibleCells(_mainGrid.Columns, numberOfPossibleCells, number);
                    if (subGridWithNumberOfPossibleCells != null)
                    {
                        break;
                    }
                    number++;
                }
                if (subGridWithNumberOfPossibleCells != null)
                {
                    break;
                }
                numberOfPossibleCells++;
            }
            foreach (Cell cell in subGridWithNumberOfPossibleCells.Cells.Where((cell) => cell.CanHaveValue(number)))
            {
                cell.Value = number;
                var fullGrid = new FullGrid();
                fullGrid.CopyValues(_mainGrid);
                cell.Value = 0;
                var solver = new Solver(fullGrid);
                try
                {
                    if (solver.TrySolveGrid())
                    {
                        _mainGrid.CopyValues(fullGrid);
                        return(true);
                    }
                }
                catch
                {
                    //All but one of the assumptions should lead to invalid grid and raise an exception
                }
            }
            return(false);
        }
Exemplo n.º 4
0
        // LLena una grilla con la ListaEntidad y Pone Tamanos de columnas para ajustar a pantalla
        private void FillFromLEOTItemsConAnchoDeColumna(FullGrid p_fullGrid, Bel.LEOTItems p_leOTItems)
        {
            p_leOTItems.ChangeCaption(EOTItem.NroagrupadorCmp, "");
            p_leOTItems.ChangeCaption(EOTItem.NroitemCmp, "");
            p_leOTItems.ChangeCaption(EOTItem.ImportecierreCmp, "");
            p_leOTItems.ChangeCaption(EOTItem.EstadoCmp, "");
            p_leOTItems.ChangeCaption(EOTItem.CodcategoriaCmp, "");
            p_leOTItems.ChangeCaption("Oti_kilometraje", "");
            p_leOTItems.ChangeCaption("Oti_categoria", "");
            p_leOTItems.ChangeCaption(EOTItem.ComentariocierreCmp, "");
            p_leOTItems.ChangeCaption(EOTItem.CodreparacionCmp, "");
            p_leOTItems.ChangeCaption(EOTItem.ComentarioCmp, "V1ComentarioCN1");


            p_fullGrid.FillFromLEntidad(p_leOTItems);
            p_fullGrid.ColWitdhs = "64;280;280;80;110;";
        }
Exemplo n.º 5
0
 public Solver(FullGrid mainGrid)
 {
     _mainGrid = mainGrid;
 }