Esempio n. 1
0
        static void Main(string[] args)
        {
            var n         = ReadNonogramFromFile(args[0]);
            var s         = new NonogramSolver();
            var stepTime  = new Stopwatch();
            var totalTime = new Stopwatch();

            Console.WriteLine("Starting position:");
            PrintNonogram(n);
            Console.WriteLine();
            Console.WriteLine();

            int currentStep = 0;

            totalTime.Start();
            stepTime.Start();

            while (s.SolveStep(n))
            {
                stepTime.Stop();
                ++currentStep;
                Console.WriteLine($"Step {currentStep} (took {stepTime.Elapsed}):");
                PrintNonogram(n);
                Console.WriteLine();
                Console.WriteLine();
                stepTime.Restart();
            }

            totalTime.Stop();
            Console.WriteLine("Can not make any more solve steps.");
            Console.WriteLine($"Solved: {n.IsComplete()}");
            Console.WriteLine($"Took: {totalTime.Elapsed}");
        }
Esempio n. 2
0
 public static NonogramSolver getInstance()
 {
     if (nonogramSolver == null || nonogramSolver.getNonogram().Length == 0)
     {
         nonogramSolver = new NonogramSolver();
     }
     return(nonogramSolver);
 }
        public void OneByOneNonogramWithNoHintsIsSolvable()
        {
            var n = new Nonogram(1, 1);
            var s = new NonogramSolver();

            Assert.IsTrue(s.Solve(n));

            Assert.AreEqual(Cell.CellState.Blank, n[0, 0].State);
        }
    void ResolverNonogram()
    {
        // Empezar a medir tiempo

        this.Contador.Start();
        // Resolver nonogram
        NonogramSolver.ResolverNonogram(DataManager.Instance.tablero, DataManager.Instance.infoMono, 0);
        this.Contador.Stop();
        Debug.Log(this.Contador.Elapsed.ToString());
    }
        public void OneByOneNonogramWithHintsIsSolvable()
        {
            var hHints = new List <int>[] { new List <int>(new int[] { 1 }) };
            var vHints = new List <int>[] { new List <int>(new int[] { 1 }) };
            var n      = new Nonogram(1, 1, hHints, vHints);
            var s      = new NonogramSolver();

            Assert.IsTrue(s.Solve(n));

            Assert.AreEqual(Cell.CellState.Filled, n[0, 0].State);
        }
        public void SimpleNonogramWithHintsIsSolvable()
        {
            var hHints = new List <int>[] {
                new List <int>(new int[] { 2 }),
                new List <int>(new int[] { 4 }),
                new List <int>(new int[] { 5 }),
                new List <int>(new int[] { 4 }),
                new List <int>(new int[] { 2 }),
            };
            var vHints = new List <int>[] {
                new List <int>(new int[] { 3 }),
                new List <int>(new int[] { 5 }),
                new List <int>(new int[] { 5 }),
                new List <int>(new int[] { 3 }),
                new List <int>(new int[] { 1 }),
            };

            var n = new Nonogram(5, 5, hHints, vHints);
            var s = new NonogramSolver();

            Assert.IsTrue(s.Solve(n));

            Assert.AreEqual(n[0, 0].State, Cell.CellState.Blank);
            Assert.AreEqual(n[1, 0].State, Cell.CellState.Filled);
            Assert.AreEqual(n[2, 0].State, Cell.CellState.Filled);
            Assert.AreEqual(n[3, 0].State, Cell.CellState.Filled);
            Assert.AreEqual(n[4, 0].State, Cell.CellState.Blank);

            Assert.AreEqual(n[0, 1].State, Cell.CellState.Filled);
            Assert.AreEqual(n[1, 1].State, Cell.CellState.Filled);
            Assert.AreEqual(n[2, 1].State, Cell.CellState.Filled);
            Assert.AreEqual(n[3, 1].State, Cell.CellState.Filled);
            Assert.AreEqual(n[4, 1].State, Cell.CellState.Filled);

            Assert.AreEqual(n[0, 2].State, Cell.CellState.Filled);
            Assert.AreEqual(n[1, 2].State, Cell.CellState.Filled);
            Assert.AreEqual(n[2, 2].State, Cell.CellState.Filled);
            Assert.AreEqual(n[3, 2].State, Cell.CellState.Filled);
            Assert.AreEqual(n[4, 2].State, Cell.CellState.Filled);

            Assert.AreEqual(n[0, 3].State, Cell.CellState.Blank);
            Assert.AreEqual(n[1, 3].State, Cell.CellState.Filled);
            Assert.AreEqual(n[2, 3].State, Cell.CellState.Filled);
            Assert.AreEqual(n[3, 3].State, Cell.CellState.Filled);
            Assert.AreEqual(n[4, 3].State, Cell.CellState.Blank);

            Assert.AreEqual(n[0, 4].State, Cell.CellState.Blank);
            Assert.AreEqual(n[1, 4].State, Cell.CellState.Blank);
            Assert.AreEqual(n[2, 4].State, Cell.CellState.Filled);
            Assert.AreEqual(n[3, 4].State, Cell.CellState.Blank);
            Assert.AreEqual(n[4, 4].State, Cell.CellState.Blank);
        }
        public void TwoByTwoNonogramWithNotEnoughHintsIsNotSolvable()
        {
            var hHints = new List <int>[] {
                new List <int>(new int[] { 1 }),
                new List <int>(new int[] { 1 })
            };
            var vHints = new List <int>[] {
                new List <int>(new int[] { 1 }),
                new List <int>(new int[] { 1 })
            };

            var n = new Nonogram(2, 2, hHints, vHints);
            var s = new NonogramSolver();

            Assert.IsFalse(s.Solve(n));
        }
        public void SimpleNonogramWithHintsIsSolvable_NFP()
        {
            string picture = @"
_XXX_
XXXXX
XXXXX
_XXX_
__X__
";
            var    n       = NonogramFromPicture(picture);
            var    s       = new NonogramSolver();

            Assert.IsTrue(s.Solve(n));

            EnsureNonogramIsSameAsPicture(n, picture);
        }
Esempio n. 9
0
        public NonogramGUI(NonogramSolver solver)
        {
            InitializeComponent();
            this.solver = solver;

            this.AutoSize     = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.Padding      = new Padding(100);

            row    = solver.GetRows();
            column = solver.GetColumns();
            picBox = new PictureBox[row, column];

            Console.WriteLine("column = " + column);
            Console.WriteLine("row = " + row);

            tableLayoutPanel1.AutoSize     = true;
            tableLayoutPanel1.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            tableLayoutPanel1.Padding      = new Padding(0, 0, 0, 0);
            tableLayoutPanel1.Margin       = new Padding(0, 0, 0, 0);
            tableLayoutPanel1.ColumnCount  = column + 1;
            tableLayoutPanel1.RowCount     = row + 1;
            string textClues;

            tableLayoutPanel1.Controls.Add(new Label());

            for (int i = 1; i <= column; i++)
            {
                textClues = "";
                foreach (Clue clue in solver.GetColumn(i).Clues)
                {
                    textClues += "\n" + clue.Value;
                }

                tableLayoutPanel1.Controls.Add(new Label {
                    Text = textClues, AutoSize = true, Font = new Font("Arial", 18)
                });
            }

            for (int i = 0; i < row; i++)
            {
                textClues = "";
                foreach (Clue clue in solver.GetRow(i + 1).Clues)
                {
                    textClues += clue.Value + "  ";
                }
                tableLayoutPanel1.Controls.Add(new Label {
                    Text = textClues, AutoSize = true, Font = new Font("Arial", 18)
                });;

                for (int j = 0; j < column; j++)
                {
                    Image      image;
                    PictureBox tempBox;
                    if (row >= 12 || column >= 12)
                    {
                        Image imager = Properties.Resources.Blank;
                        image = new Bitmap(imager, new Size(25, 25));

                        tempBox = new PictureBox()
                        {
                            Image = image
                        };
                    }
                    else
                    {
                        image = Properties.Resources.Blank;
                    }

                    tempBox = new PictureBox()
                    {
                        Image = image
                    };
                    tempBox.Size = image.Size;
                    tableLayoutPanel1.Controls.Add(tempBox);

                    picBox[i, j]    = tempBox;
                    solver.Updater += Updated;
                }
            }

            solver.Updater += Updated;
            this.Show();
            Application.DoEvents();
            Thread.Sleep(5000);
            solver.Run();
            if (solver.IsFinished())
            {
                label1.Visible = true;
            }
        }
Esempio n. 10
0
        private void button1_Click(object sender, EventArgs e)
        {
            String rowString    = "";
            String columnString = "";

            string[] temp;
            label2.Visible = false;
            label3.Visible = false;

            foreach (TextBox box in rowbox)
            {
                temp = box.Text.Split(' ');
                int num   = 0;
                int total = 0;

                foreach (string c in temp)
                {
                    if (!Int32.TryParse(c, out num))
                    {
                        label2.Visible = true;
                    }
                    else
                    {
                        Int32.TryParse(c, out num);
                        total += num;
                    }
                }

                total += temp.Length - 1;
                if (total > column)
                {
                    label3.Visible = true;
                }
            }

            foreach (TextBox box in columnbox)
            {
                temp = box.Text.Split(' ');
                int num   = 0;
                int total = 0;

                foreach (string c in temp)
                {
                    if (!Int32.TryParse(c, out num))
                    {
                        label2.Visible = true;
                    }
                    else
                    {
                        Int32.TryParse(c, out num);
                        total += num;
                    }
                }

                total += temp.Length - 1;
                Console.WriteLine(total);
                if (total > row)
                {
                    label3.Visible = true;
                }
            }

            if (!label2.Visible && !label3.Visible)
            {
                foreach (TextBox box in rowbox)
                {
                    rowString += box.Text + "; ";
                }

                foreach (TextBox box in columnbox)
                {
                    columnString += box.Text + "; ";
                }

                save();

                NonogramSolver solver = new NonogramSolver(rowString, columnString);
                Console.WriteLine(rowString);
                this.Hide();
                NonogramGUI form = new NonogramGUI(solver);
            }
        }
        public void HardestNonogramWithHintsIsSolvable()
        {
            var horizontalHints = new List <int>[]
            {
                new int[] { 10 }.ToList(),
                new int[] { 11, 1 }.ToList(),
                new int[] { 13, 9 }.ToList(),
                new int[] { 33 }.ToList(),
                new int[] { 35 }.ToList(),
                new int[] { 7, 27 }.ToList(),
                new int[] { 5, 6, 8 }.ToList(),
                new int[] { 5, 2, 1, 7 }.ToList(),
                new int[] { 4, 2, 2, 7 }.ToList(),
                new int[] { 5, 3, 2, 2, 3 }.ToList(),
                new int[] { 6, 2, 3, 2, 3 }.ToList(),
                new int[] { 7, 1, 3, 2, 2, 2, 2 }.ToList(),
                new int[] { 7, 1, 3, 1, 2, 1, 2, 1 }.ToList(),
                new int[] { 8, 1, 3, 1, 2, 1 }.ToList(),
                new int[] { 8, 1, 2, 1, 1, 1 }.ToList(),
                new int[] { 8, 1, 11, 1, 1, 1 }.ToList(),
                new int[] { 8, 1, 3, 1, 2, 1 }.ToList(),
                new int[] { 8, 3, 1, 3, 1, 2, 1 }.ToList(),
                new int[] { 7, 4, 2, 2, 2, 1 }.ToList(),
                new int[] { 3, 1, 3, 3, 2, 2 }.ToList(),
                new int[] { 3, 3, 2, 1, 2 }.ToList(),
                new int[] { 3, 2, 2, 2 }.ToList(),
                new int[] { 3, 2, 1, 2 }.ToList(),
                new int[] { 3, 2 }.ToList(),
                new int[] { 4, 3 }.ToList(),
                new int[] { 3, 4, 7 }.ToList(),
                new int[] { 4, 26 }.ToList(),
                new int[] { 12, 9 }.ToList(),
                new int[] { 10, 1 }.ToList(),
                new int[] { 10 }.ToList(),
            };

            var verticalHints = new List <int>[]
            {
                new int[] { 13 }.ToList(),
                new int[] { 21 }.ToList(),
                new int[] { 24 }.ToList(),
                new int[] { 17, 6 }.ToList(),
                new int[] { 17, 4 }.ToList(),
                new int[] { 7, 10, 3 }.ToList(),
                new int[] { 5, 8, 2 }.ToList(),
                new int[] { 5, 5, 2 }.ToList(),
                new int[] { 4, 2 }.ToList(),
                new int[] { 4, 3 }.ToList(),
                new int[] { 5, 3 }.ToList(),
                new int[] { 5, 3 }.ToList(),
                new int[] { 6, 8, 4 }.ToList(),
                new int[] { 6, 2, 4 }.ToList(),
                new int[] { 6, 2, 4, 4 }.ToList(),
                new int[] { 7, 4, 5, 2, 1 }.ToList(),
                new int[] { 1, 10, 4, 1, 1 }.ToList(),
                new int[] { 1, 7, 1, 1 }.ToList(),
                new int[] { 1, 3, 3, 1, 3, 1, 1 }.ToList(),
                new int[] { 1, 3, 4, 1, 4, 1, 1 }.ToList(),
                new int[] { 1, 3, 4, 1, 4, 1, 1 }.ToList(),
                new int[] { 1, 4, 1, 2, 1 }.ToList(),
                new int[] { 1, 4, 1, 2, 1 }.ToList(),
                new int[] { 1, 4, 1, 2, 1 }.ToList(),
                new int[] { 6, 1, 4 }.ToList(),
                new int[] { 4, 1, 2 }.ToList(),
                new int[] { 4, 3, 3, 2 }.ToList(),
                new int[] { 4, 8, 2 }.ToList(),
                new int[] { 4, 7, 2 }.ToList(),
                new int[] { 5, 2, 3 }.ToList(),
                new int[] { 5, 1, 2 }.ToList(),
                new int[] { 16, 2 }.ToList(),
                new int[] { 9, 2 }.ToList(),
                new int[] { 6, 6, 2 }.ToList(),
                new int[] { 6, 2, 2, 3 }.ToList(),
                new int[] { 5, 4 }.ToList(),
                new int[] { 5, 4 }.ToList(),
                new int[] { 3, 2 }.ToList(),
                new int[] { 3, 2 }.ToList(),
                new int[] { 10 }.ToList(),
            };

            Assert.AreEqual(horizontalHints.Length, 30);
            Assert.AreEqual(verticalHints.Length, 40);

            var n = new Nonogram(30, 40, horizontalHints, verticalHints);
            var s = new NonogramSolver();

            Assert.IsTrue(s.Solve(n));
        }