Ejemplo n.º 1
0
        public void Solve(Nonogram n)
        {
            var rowsPossibleStates   = Utils.PossibleStatesForRows(n.Width, n.RowDescriptors);
            var colsPossibleStates   = Utils.PossibleStatesForRows(n.Height, n.ColumnDescriptors);
            var rowSolvingCandidates = rowsPossibleStates.Select(rowStates => FindCommonCells(rowStates).ToList()).ToList();
            var colSolvingCandidates = colsPossibleStates.Select(rowStates => FindCommonCells(rowStates).ToList()).ToList();

            bool changeFound = true;

            while (changeFound)
            {
                changeFound = false;
                for (int i = 0; i < rowSolvingCandidates.Count; i++)
                {
                    var rowCandidate = rowSolvingCandidates[i];

                    int rowsCandidatesRemoved = RemoveConflictingStates(rowCandidate, i, colsPossibleStates);
                    if (rowsCandidatesRemoved > 0)
                    {
                        changeFound = true;
                    }
                }
                colSolvingCandidates = colsPossibleStates.Select(rowStates => FindCommonCells(rowStates).ToList()).ToList();
                for (int i = 0; i < colSolvingCandidates.Count; i++)
                {
                    var rowCandidate = colSolvingCandidates[i];

                    int rowsCandidatesRemoved = RemoveConflictingStates(rowCandidate, i, rowsPossibleStates);
                    if (rowsCandidatesRemoved > 0)
                    {
                        changeFound = true;
                    }
                }
                rowSolvingCandidates = rowsPossibleStates.Select(rowStates => FindCommonCells(rowStates).ToList()).ToList();
            }

            for (int i = 0; i < n.Height; i++)
            {
                var row         = n.getRow(i);
                var commonCells = rowSolvingCandidates[i];
                for (int j = 0; j < commonCells.Count; j++)
                {
                    row[j].State = commonCells[j];
                }
            }
            for (int i = 0; i < n.Width; i++)
            {
                var col         = n.getColumn(i);
                var commonCells = colSolvingCandidates[i];
                for (int j = 0; j < commonCells.Count; j++)
                {
                    col[j].State = commonCells[j];
                }
            }
        }
Ejemplo n.º 2
0
        public static Nonogram MakeNonogram(int rows, int columns)
        {
            var nonogram = new Nonogram(columns, rows);

            var rand = new Random();
            var a    = (Array)(new int[] { 5 });


            foreach (var r in nonogram.Cells)
            {
                foreach (var cell in r)
                {
                    cell.State = rand.NextDouble() > 0.5
                        ? CellState.Filled
                        : CellState.Empty;
                }
            }

            var rowsList = new List <RowDescriptor>(rows);

            for (int i = 0; i < rows; i++)
            {
                var           row           = nonogram.getRow(i);
                RowDescriptor rowDescriptor = MakeRowDescriptorFor(row);
                rowsList.Add(rowDescriptor);
            }
            nonogram.RowDescriptors = rowsList;

            var columnList = new List <RowDescriptor>(rows);

            for (int i = 0; i < columns; i++)
            {
                var           column        = nonogram.getColumn(i);
                RowDescriptor rowDescriptor = MakeRowDescriptorFor(column);
                columnList.Add(rowDescriptor);
            }
            nonogram.ColumnDescriptors = columnList;
            return(nonogram);
        }
Ejemplo n.º 3
0
        public void SolveWithOneGuess(Nonogram n)
        {
            var rowsPossibleStates = Utils.PossibleStatesForRows(n.Width, n.RowDescriptors);
            var colsPossibleStates = Utils.PossibleStatesForRows(n.Height, n.ColumnDescriptors);
            List <List <CellState> > rowSolvingCandidates = null;
            List <List <CellState> > colSolvingCandidates = null;

            bool fullySolved = false;

            while (rowsPossibleStates.All(rowStates => rowStates.Count > 0) &&
                   colsPossibleStates.All(columnStates => columnStates.Count > 0) &&
                   !fullySolved)
            {
                rowSolvingCandidates = rowsPossibleStates.Select(rowStates => FindCommonCells(rowStates).ToList()).ToList();
                colSolvingCandidates = colsPossibleStates.Select(rowStates => FindCommonCells(rowStates).ToList()).ToList();
                bool changeFound = true;
                while (changeFound)
                {
                    changeFound = false;
                    for (int i = 0; i < rowSolvingCandidates.Count; i++)
                    {
                        var rowCandidate = rowSolvingCandidates[i];

                        int rowsCandidatesRemoved = RemoveConflictingStates(rowCandidate, i, colsPossibleStates);
                        if (rowsCandidatesRemoved > 0)
                        {
                            changeFound = true;
                        }
                    }
                    if (colsPossibleStates.Any(columnStates => columnStates.Count == 0))
                    {
                        break;
                    }
                    colSolvingCandidates = colsPossibleStates.Select(rowStates => FindCommonCells(rowStates).ToList()).ToList();
                    for (int i = 0; i < colSolvingCandidates.Count; i++)
                    {
                        var rowCandidate = colSolvingCandidates[i];

                        int rowsCandidatesRemoved = RemoveConflictingStates(rowCandidate, i, rowsPossibleStates);
                        if (rowsCandidatesRemoved > 0)
                        {
                            changeFound = true;
                        }
                    }
                    if (rowsPossibleStates.Any(rowStates => rowStates.Count == 0))
                    {
                        break;
                    }
                    rowSolvingCandidates = rowsPossibleStates.Select(rowStates => FindCommonCells(rowStates).ToList()).ToList();
                }
                fullySolved = rowSolvingCandidates.All(rowState => rowState.All(cell => cell != CellState.Undefined)) &&
                              colSolvingCandidates.All(rowState => rowState.All(cell => cell != CellState.Undefined));
                if (!fullySolved)
                {
                    //find row with several candidates left and remove all others
                    foreach (var colStates in colsPossibleStates)
                    {
                        if (colStates.Count > 1)
                        {
                            colStates.RemoveRange(1, colStates.Count - 1);
                            break;
                        }
                    }
                }
            }

            for (int i = 0; i < n.Height; i++)
            {
                var row         = n.getRow(i);
                var commonCells = rowSolvingCandidates[i];
                for (int j = 0; j < commonCells.Count; j++)
                {
                    row[j].State = commonCells[j];
                }
            }
            for (int i = 0; i < n.Width; i++)
            {
                var col         = n.getColumn(i);
                var commonCells = colSolvingCandidates[i];
                for (int j = 0; j < commonCells.Count; j++)
                {
                    col[j].State = commonCells[j];
                }
            }
        }