Example #1
0
        internal List <Location> FindAdjacent(List <int> allowedDelta, List <Location> excludedComb, List <Location> excludedOddRowComb, List <Location> excludedEvenRowComb, bool wrap, List <int> gridSize)
        {
            // for every dimension, we must allow for -1, 0, +1 states
            // this is really a variations / repitition problem, for the mapping (-1,0,-1) select "dimensions" of them
            List <Location> outList = new List <Location>();

            JPD.Combinatorics.Variations <int> var = new Variations <int>((uint)index.Count, 3, true, allowedDelta);

            for (var.First(); !var.AtEnd; var.Next())
            {
                bool exclude = (excludedComb != null && excludedComb.Contains(new Location(var.Current.ToList()))) ||
                               ((index[0] % 2 == 1) && excludedOddRowComb != null && excludedOddRowComb.Contains(new Location(var.Current.ToList()))) ||
                               ((index[0] % 2 == 0) && excludedEvenRowComb != null && excludedEvenRowComb.Contains(new Location(var.Current.ToList())));

                if (!exclude)
                {
                    Location l = new Location();
                    for (int d = 0; d < index.Count; d++)
                    {
                        int target = index[d] + var.Current[d];
                        if (wrap)
                        {
                            target = (target + gridSize[d]) % gridSize[d];
                        }
                        l.Add(d, target);
                    }
                    outList.Add(l);
                }
            }
            return(outList);
        }
        /// <summary>
        /// Test Next() method
        /// </summary>
        public void TestNext()
        {
            // Case 1: check all variations are returned
            // {2,1,0}'s variations are: {2}, {1}, {0}, {2,1}, {2,0}, {1,2}, {1,0}, {0,2}, {0,1},
            // {2,1,0}, {2,0,1}, {1,2,0}, {1,0,2}, {0,2,1}, {0,1,2}
            // With "-1" as padding, and consider ends with 0 situation, then expected result should be {-1,-1,2}, {-1,-1,1}, {-1,-1,0}, {-1,2,1}, {-1,2,0},
            // {-1,1,2}, {-1,1,0}, {2,1,0}, {2,0,1}, {1,2,0}, {1,0,2}
            List <int[]> expected = new List <int[]>();

            expected.Add(new int[] { -1, -1, 0 });
            expected.Add(new int[] { -1, -1, 1 });
            expected.Add(new int[] { -1, -1, 2 });
            expected.Add(new int[] { -1, 1, 0 });
            expected.Add(new int[] { -1, 1, 2 });
            expected.Add(new int[] { -1, 2, 0 });
            expected.Add(new int[] { -1, 2, 1 });
            expected.Add(new int[] { 1, 0, 2 });
            expected.Add(new int[] { 1, 2, 0 });
            expected.Add(new int[] { 2, 0, 1 });
            expected.Add(new int[] { 2, 1, 0 });

            Variations   varians = new Variations(3);
            List <int[]> result  = new List <int[]>();

            while (true)
            {
                if (!varians.Next())
                {
                    break;
                }

                result.Add((int[])varians.Indices.Clone());
            }

            Assert.AreEqual(result.Count, expected.Count);

            for (int i = 0; i < result.Count; i++)
            {
                CollectionAssert.AreEqual(result[i], expected[i]);
            }
        }