public void OverlapTechniqueGivesSolutionWhenHintGivenCompleteInformation_5_2_2()
        {
            // Testing: Finds solution for odd-numbered width
            // Expected Results:
            // Width: 5
            // Hint: 2, 2
            //
            // Solution:
            // XX XX

            const int width = 5;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 2, 2 });

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
        public void IsValidReturnsTrueWhenAllUnknown()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            Assert.AreEqual(true, l.IsValid());
        }
        public void CellLineMaxFunctionsWithSomeBlockedCells()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Hints.AddRange(new int[] { 2, 1 });
            l[2].State = Cell.CellState.Blank;

            var expectedStates = new Cell.CellState[5] {
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Blank, // forced blank
                Cell.CellState.Blank,
                Cell.CellState.Filled
            };

            var expectedFlags = new int[5] {
                0,
                0,
                0,
                1,
                1
            };

            var max = l.Max();

            Assert.IsNotNull(max);

            for (int i = 0; i < 5; ++i)
            {
                Assert.AreEqual(expectedStates[i], max[i].State);
                Assert.AreEqual(expectedFlags[i], max[i].Flag);
            }
        }
Beispiel #4
0
        /// <summary>
        /// Runs the InitSolver on the specified nonogram
        /// </summary>
        /// <param name="ng">Nonogram to work on</param>
        /// <returns>Number of solved tiles or -1 if an error is detected</returns>
        public int Run(Nonogram ng)
        {
            _ng      = ng;
            _results = new List <Result>();
            _solved  = false;
            _marked  = new bool?[ng.Height][];
            for (int i = 0; i < ng.Height; i++)
            {
                _marked[i] = new bool?[ng.Width];
            }
            Stopwatch sw = Stopwatch.StartNew();

            for (int i = 0; i < ng.Height; i++)
            {
                if (!InitRow(i))
                {
                    _results = new List <Result>();
                    return(-1);
                }
            }
            for (int i = 0; i < ng.Width; i++)
            {
                if (!InitColumn(i))
                {
                    _results = new List <Result>();
                    return(-1);
                }
            }
            _solved = _marked.All(x => x.All(y => y.HasValue));
            sw.Stop();
            _time = sw.Elapsed;
            return(_results.Count);
        }
        public void DistinctGroupsReturnsZeroWithAllUnknowns()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            Assert.AreEqual(0, l.DistinctGroups());
        }
        public void CellLineMinFunctionsWithAllUnknownCells_5_2_1()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Hints.AddRange(new int[] { 2, 1 });

            var expectedStates = new Cell.CellState[5] {
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Blank
            };

            var expectedFlags = new int[5] {
                0,
                0,
                0,
                1,
                1
            };

            var min = l.Min();

            Assert.IsNotNull(min);

            for (int i = 0; i < 5; ++i)
            {
                Assert.AreEqual(expectedStates[i], min[i].State);
                Assert.AreEqual(expectedFlags[i], min[i].Flag);
            }
        }
        public void FillingPartOfALineThatHasStateOutsideThatPartWithStateAndFlagFillsTheSectionWithThatStateAndFlagAndReturnsFalse()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l[0].State = Cell.CellState.Blank;

            Assert.IsTrue(l.Fill(1, 2, Cell.CellState.Filled, 42));

            var expectedStates = new Cell.CellState[5] {
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown
            };

            var expectedFlags = new int[5] {
                0,
                42,
                42,
                0,
                0
            };

            for (int i = 0; i < 5; i++)
            {
                Assert.AreEqual(expectedStates[i], l[i].State);
                Assert.AreEqual(expectedFlags[i], l[i].Flag);
            }
        }
        public void CellLineMaxFunctionsWithSomeKnownCells_5_3_last()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Hints.AddRange(new int[] { 3 });
            l[4].State = Cell.CellState.Filled;

            var expectedStates = new Cell.CellState[5] {
                Cell.CellState.Blank,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Filled
            };

            var expectedFlags = new int[5] {
                0,
                1,
                0,
                0,
                0
            };

            var max = l.Max();

            Assert.IsNotNull(max);

            for (int i = 0; i < 5; ++i)
            {
                Assert.AreEqual(expectedStates[i], max[i].State);
                Assert.AreEqual(expectedFlags[i], max[i].Flag);
            }
        }
        public void OverlapTechniqueFindsSolutionWhenGivenPartialInformation_5_3_last()
        {
            // Testing: Finds information when a partial solution is in place
            // Expected Results:
            // Width: 5
            // Hint: 3
            // Pre-given information: ????X
            // Solution: __XXX

            const int width = 5;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 3 });

            line[4].State = Cell.CellState.Filled;

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Blank,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Filled
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
        public void OverlapTechniqueGivesNoInformationWhenHintGivenIncompleteInformation_5_1_1()
        {
            // Testing: Returns no information when given hints that don't overlap
            // Expected Results:
            // Width: 5
            // Hint: 1, 1
            //
            // Min/Max:
            // X X??
            // ??X X
            //
            // Overlap:
            // ?????

            const int width = 5;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 1, 1 });

            var t = new OverlapTechnique();

            t.Apply(line);

            // All are unknown
            Assert.IsTrue(line.All(x => x.State == Cell.CellState.Unknown));
        }
        public void OverlapTechniqueDoesNothingWhenGivenConflictingInformation()
        {
            // Testing: Finds information when a partial solution is in place
            // Expected Results:
            // Width: 5
            // Hint: 3
            // Pre-given information: X???X

            const int width = 5;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 3 });

            line[0].State = Cell.CellState.Filled;
            line[4].State = Cell.CellState.Filled;

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Filled,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Unknown,
                Cell.CellState.Filled
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
        public void OverlapTechniqueGivesSolutionWhenHintGivenCompleteInformation_1_1()
        {
            // Testing: Finds solution for simple problem
            // Expected Results:
            // Width: 1
            // Hint: 1
            //
            // Solution:
            // X

            const int width = 1;
            var       n     = new Nonogram(width, 1);
            var       line  = n.Row(0);

            line.Hints.AddRange(new int[] { 1 });

            Cell.CellState[] expected = new Cell.CellState[width] {
                Cell.CellState.Filled
            };

            var t = new OverlapTechnique();

            t.Apply(line);

            for (int i = 0; i < width; ++i)
            {
                Assert.AreEqual(expected[i], line[i].State);
            }
        }
Beispiel #13
0
        public void NonogramGetLinesInRow_1()
        {
            var origin = new Nonogram(null, null)
            {
                row = new List <Line> [1]
            };

            origin.board               = new bool?[8, 11];
            origin.width               = 11;
            origin.board[0, 0]         =
                origin.board[0, 1]     =
                    origin.board[0, 2] = origin.board[0, 5] = origin.board[0, 6] = origin.board[0, 8] = false;
            var result = new List <bool?[]> {
                new bool?[] { null, null }, new bool?[] { null }, new bool?[] { null, null }
            };
            var resultF = origin.GetLinesInRow(0);

            Assert.AreEqual(resultF.Count, result.Count);
            for (var i = 0; i < resultF.Count; i++)
            {
                for (var j = 0; j < resultF[i].Length; j++)
                {
                    Assert.AreEqual(resultF[i][j], result[i][j]);
                }
            }
        }
Beispiel #14
0
        public void NonogramGetLinesInRow_8()
        {
            var origin = new Nonogram(null, null)
            {
                row = new List <Line> [1]
            };

            origin.width  = 3;
            origin.height = origin.width;
            origin.board  = new bool?[origin.height, origin.width];

            origin.board[0, 0] = true;
            var result = new List <bool?[]> {
                new bool?[] { true, null, null }
            };
            var resultF = origin.GetLinesInRow(0);

            Assert.AreEqual(resultF.Count, result.Count);
            for (var i = 0; i < resultF.Count; i++)
            {
                for (var j = 0; j < resultF[i].Length; j++)
                {
                    Assert.AreEqual(resultF[i][j], result[i][j]);
                }
            }
        }
Beispiel #15
0
    private string[] Solved()
    {
        READER reader            = new READER(path);
        List <List <String> > Da = reader.GetData();
        string x = "";

        for (int i = 0; i < Da[0].Count; i++)
        {
            x = x + Da[0][i];
            if (i < Da[0].Count - 1)
            {
                x = x + ' ';
            }
        }
        string y = "";

        for (int i = 0; i < Da[1].Count; i++)
        {
            y = y + Da[1][i];
            if (i < Da[1].Count - 1)
            {
                y = y + ' ';
            }
        }
        Stopwatch watch = new Stopwatch();

        watch.Start();
        string result = Nonogram.Solve(x, y);

        watch.Stop();
        UnityEngine.Debug.Log("Tiempo de ejecución = " + watch.Elapsed);
        Matrix = new List <GameObject>();
        return(result.Split('\n'));
    }
        public void IsValidReturnsTrueWhenHintMatchesContents_Hint0_Contents0()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Fill(Cell.CellState.Blank);

            Assert.AreEqual(true, l.IsValid());
        }
        public void DistinctGroupsReturnsOneWithAllFilled()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Fill(Cell.CellState.Filled);

            Assert.AreEqual(1, l.DistinctGroups());
        }
        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);
        }
        public void DistinctGroupsReturnsZeroWithAllBlank()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Fill(Cell.CellState.Blank);

            Assert.AreEqual(0, l.DistinctGroups());
        }
        public void IsValidReturnsFalseWhenHintIsNotPossible_Hint32()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Hints.AddRange(new int[] { 3, 2 });

            Assert.AreEqual(false, l.IsValid());
        }
        public void ReversingACellLineReversesItsContents()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l[0].State = Cell.CellState.Blank;
            l.Reverse();

            Assert.AreEqual(Cell.CellState.Blank, l[4].State);
        }
        public void IsValidReturnsFalseWhenHintsExistButAllBlank()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Fill(Cell.CellState.Blank);
            l.Hints.AddRange(new int[] { 1 });

            Assert.AreEqual(false, l.IsValid());
        }
        public void DistinctGroupsReturnsActualNumberOfGroupsWithTwoGroupsOfTwo()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Fill(Cell.CellState.Filled);
            l[2].State = Cell.CellState.Blank;

            Assert.AreEqual(2, l.DistinctGroups());
        }
        public void DistinctGroupsReturnsActualNumberOfGroupsWithThreeGroupsOfOne()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l[0].State = l[2].State = l[4].State = Cell.CellState.Filled;
            l[1].State = l[3].State = Cell.CellState.Blank;

            Assert.AreEqual(3, l.DistinctGroups());
        }
        public void RunTest()
        {
            Nonogram ng = NonoGramFactory.ParseFromString(Simple);

            Assert.AreEqual(20, _ts.Run(ng),
                            "Unexpected amount of resolved tiles reported");
            Assert.IsTrue(_ts.Solved());
            Assert.AreNotEqual(TimeSpan.Zero.TotalMilliseconds,
                               _ts.BenchTime().TotalMilliseconds);
        }
Beispiel #26
0
        private static void Main(string[] args)
        {
            TimeSpan ts;
            string   path = Environment.CurrentDirectory;

            if (path.Contains("NonogramSolver"))
            {
                path = Regex.Replace(path, "NonogramSolver.*", @"NonogramSolver\\Data");
                if (!Directory.Exists(path))
                {
                    Console.WriteLine("No nonograms found in " + path);
                    path = Path.Combine(Directory.GetCurrentDirectory(), "Data");
                }
            }
            else
            {
                path = Path.Combine(Directory.GetCurrentDirectory(), "Data");
            }
            if (!Directory.Exists(path))
            {
                Console.WriteLine("No nonograms found in " + path);
                Console.WriteLine("Any key to terminate.");
                Console.ReadKey();
            }
            StringBuilder sb = new StringBuilder();

            foreach (string file in Directory.GetFiles(path))
            {
                Console.WriteLine("\nBenchmarking file: " + Path.GetFileName(file));
                sb.AppendLine("\nBenchmarking file: " + Path.GetFileName(file));
                Nonogram ng = NonoGramFactory.ParseFromFile(file);
                ISolver  s  = new SerialSolver(true);
                s.Run(ng);
                Console.WriteLine("Solvable with SerialSolver: " + s.Solved());
                sb.AppendLine("Solvable with SerialSolver: " + s.Solved());
                if (s.Solved())
                {
                    ts = TimeSpan.Zero;
                    for (int i = 0; i < 50; i++)
                    {
                        s.Run(ng);
                        ts = ts.Add(s.BenchTime());
                    }
                    Console.WriteLine("Average solving time: " + (ts.TotalMilliseconds / 50) + "ms");
                    sb.AppendLine("Average solving time: " + (ts.TotalMilliseconds / 50) + "ms");
                }
            }
            using (StreamWriter sw = new StreamWriter("Output.txt", false))
            {
                sw.Write(sb.ToString());
                Console.WriteLine("Output written to Output.txt");
            }
            Console.WriteLine("Any key to terminate.");
            Console.ReadKey();
        }
        public void IsValidReturnsTrueWhenNotEnoughGroups_Hint111_Contents11()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l[0].State = l[2].State = Cell.CellState.Filled;
            l[1].State = l[3].State = Cell.CellState.Blank;
            l.Hints.AddRange(new int[] { 1, 1, 1 });

            Assert.AreEqual(true, l.IsValid());
        }
        public void IsValidReturnsFalseWhenGroupIsTooLarge_Hint3_Contents4_right()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l[0].State = Cell.CellState.Blank;
            l.Fill(1, 4, Cell.CellState.Filled);
            l.Hints.AddRange(new int[] { 3 });

            Assert.AreEqual(false, l.IsValid());
        }
        public void IsValidReturnsFalseWhenNoPossibleSolutionsExist()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Fill(0, 3, Cell.CellState.Filled);
            l[4].State = Cell.CellState.Filled;
            l.Hints.AddRange(new int[] { 3 });

            Assert.AreEqual(false, l.IsValid());
        }
        public void IsValidReturnsTrueWhenPossibleSolutionsExist()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

            l.Fill(0, 3, Cell.CellState.Blank);
            l[4].State = Cell.CellState.Blank;
            l.Hints.AddRange(new int[] { 1 });

            Assert.AreEqual(true, l.IsValid());
        }
Beispiel #31
-1
 public override object Read(ES2Reader reader)
 {
     Nonogram data = new Nonogram();
     Read(reader, data);
     return data;
 }
Beispiel #32
-1
    private void ClearBoard(int size)
    {
        for (int i = 0; i < size; i++) {
            Destroy(tips_row_go[i].gameObject);
            Destroy(tips_col_go[i].gameObject);
        }

        foreach (GameObject go in tiles)
            Destroy(go);

        tiles.Clear();
        tips_row_go.Clear();
        tips_col_go.Clear();

        nonogram = null;
        solution = null;
    }
Beispiel #33
-2
    public bool InitEmptyGrid(int size = EditorData.default_grid_size)
    {
        if(nonogram != null)
            ClearBoard(nonogram.size);

        if(size > 16) {
            InfoController.Instance.info.text = "ERROR: ---Nono Size is too big---";
            return false;
        }
        nonogram = new Nonogram(size);
        solution = new Nonogram(size);

        // Set Grid Size
        grid.GetComponent<GridLayoutGroup>().constraintCount = size;

        // Instantiate grid tiles
        for(int x = 0; x < size; x++){
            for(int y = 0; y < size; y++){
                GameObject tile = Instantiate(Resources.Load<GameObject>("Prefabs/Tile"));

                tile.name = "Tile " + x +"; " +y;
                tile.GetComponent<Tile>().cords = new Vector2(x, y);

                tile.transform.SetParent(grid);
                tiles.Add(tile);
            }
        }
        InitTips(size);

        return true;
    }