Esempio n. 1
0
        public void CellLineMinFunctionsWithSomeKnownCells_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 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);
            }
        }
        // Determines if any particular group of cells must overlap with itself
        // for both the min and max case

        /*public void Apply(CellLine line)
         * {
         *  int length = line.Length;
         *  int minimumWidth = line.Hints.Sum() + line.Hints.Count - 1;
         *  int remainder = length - minimumWidth;
         *
         *  if (remainder >= line.Hints.Max())
         *  {
         *      return;
         *  }
         *
         *  int offset = 0;
         *  for (int group = 0; group < line.Hints.Count; group++)
         *  {
         *      int numberKnown = Math.Max(line.Hints[group] - remainder, 0);
         *      int numberUnknown = line.Hints[group] - numberKnown;
         *      offset += numberUnknown;
         *
         *      for (int i = 0; i < numberKnown; i++)
         *      {
         *          line[offset + i].State = Cell.CellState.Filled;
         *      }
         *
         *      // Special case: hints give a complete picture of the line
         *      // I.E. remainder = 0, there are no unknown cells
         *      // We can fill in the rest of the 'unknowns' with Blanks.
         *      if (remainder == 0 && group + 1 != line.Hints.Count)
         *      {
         *          line[offset + numberKnown].State = Cell.CellState.Blank;
         *      }
         *
         *      offset += numberKnown + 1;
         *  }
         * }*/

        public bool Apply(CellLine line)
        {
            var min = line.Min();
            var max = line.Max();

            if (min == null || max == null)
            {
                // Min or Max was given conflicting information and cannot be filled.
                // Can't do anything, so return now.
                return(false);
            }

            bool madeChanges = false;

            for (int i = 0; i < line.Length; i++)
            {
                var minCell = min[i];
                var maxCell = max[i];

                if (minCell == maxCell)
                {
                    if (line[i].State != minCell.State)
                    {
                        madeChanges = true;
                    }

                    line[i].State = minCell.State;
                }
            }

            return(madeChanges);
        }
Esempio n. 3
0
        public void CellLineMinFunctionsWithSomeBlockedCells()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

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

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

            var expectedFlags = new int[5] {
                0,
                0,
                1,
                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);
            }
        }
Esempio n. 4
0
        public void CellLineMinReturnsNullWhenNotPossibleToFill()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

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

            var min = l.Min();

            Assert.IsNull(min);
        }
Esempio n. 5
0
        public void CellLineMinReturnsNullWhenGivenConflictingInformation()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

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

            var min = l.Min();

            Assert.IsNull(min);
        }
Esempio n. 6
0
        public void CellLineMinDoesNotModifyOriginalLine()
        {
            Nonogram n = new Nonogram(5, 1);
            CellLine l = n.Row(0);

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

            var min = l.Min();

            for (int i = 0; i < 5; ++i)
            {
                Assert.AreNotSame(l[i], min[i]);
                Assert.AreEqual(Cell.CellState.Unknown, l[i].State);
                Assert.AreEqual(0, l[i].Flag);
            }
        }
Esempio n. 7
0
        public void CellLineMinFunctionsWithTrickyKnownCells()
        {
            // Test pitfall of greedy algorithm.
            // The first spot that the 3 hint can fit into leads to an invalid state.
            //
            // Hint: 2 3 4 2
            // Given: ???????XX???XXXX????
            //        ^^ ^^^
            // Greedy fills these 5 cells incorrectly.
            Nonogram n    = new Nonogram(20, 1);
            CellLine line = n.Row(0);

            line.Hints.AddRange(new int[] { 2, 3, 4, 2 });
            line[7].State                      =
                line[8].State                  =
                    line[12].State             =
                        line[13].State         =
                            line[14].State     =
                                line[15].State =
                                    Cell.CellState.Filled;

            Cell.CellState[] expectedStates = new Cell.CellState[20] {
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Blank,
                Cell.CellState.Blank,
                Cell.CellState.Blank,

                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Blank,

                Cell.CellState.Blank,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Filled,

                Cell.CellState.Filled,
                Cell.CellState.Blank,
                Cell.CellState.Filled,
                Cell.CellState.Filled,
                Cell.CellState.Blank
            };

            var expectedFlags = new int[20] {
                0,
                0,
                0,
                1,
                2,

                3,
                2,
                2,
                2,
                4,

                5,
                6,
                3,
                3,
                3,

                3,
                7,
                4,
                4,
                8
            };

            var min = line.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);
            }
        }