private void IsValidPath(Map map, MoveDirection[] path, int expectedPathLength)
        {
            var chestTaken = false;
            var chestSet   = new HashSet <Point>(map.Chests);
            var walker     = new Walker(map.InitialPosition);

            foreach (var step in path)
            {
                walker = walker.WalkInDirection(map, step);
                if (walker.PointOfCollision.HasValue)
                {
                    Assert.Fail($"Collided with wall at {walker.PointOfCollision.Value}");
                }
                if (chestSet.Contains(walker.Position))
                {
                    chestTaken = true;
                }
            }

            Assert.True(chestTaken, "Player did not take any chest.");
            Assert.AreEqual(map.Exit, walker.Position, "Player did not reach the exit.");
            Assert.GreaterOrEqual(path.Length, expectedPathLength,
                                  "Hmm.... Seems to be an error in the checker. Please notify us in the comments below.");
            Assert.AreEqual(expectedPathLength, path.Length, "Path must be shortest.");
        }
Beispiel #2
0
        public static IEnumerable <SinglyLinkedList <Point> > FindPaths(Map map, Point start, Point[] chests)
        {
            var queue = new Queue <SinglyLinkedList <Point> >();
            var roads = new HashSet <Point> {
                start
            };
            var hashChests = chests.ToHashSet();
            var directions = new[] { 0, 1, 2, 3 };

            queue.Enqueue(new SinglyLinkedList <Point>(start));
            while (queue.Count != 0)
            {
                var currentPath = queue.Dequeue();
                var walker      = new Walker(currentPath.Value);
                foreach (var newPosition in
                         directions
                         .Select(x => walker.WalkInDirection(map, (MoveDirection)x))
                         .Where(x => x.PointOfCollision == null && !roads.Contains(x.Position))
                         .Select(x => x.Position))
                {
                    var path = new SinglyLinkedList <Point>(newPosition, currentPath);
                    queue.Enqueue(path);
                    roads.Add(newPosition);
                    if (hashChests.Contains(newPosition))
                    {
                        yield return(path);
                    }
                }
            }
        }
Beispiel #3
0
        public static IEnumerable <SinglyLinkedList <Point> > FindPaths(Map map, Point start, Point[] chests)
        {
            var queue   = new Queue <SinglyLinkedList <Point> >();
            var visited = new HashSet <Point> {
                start
            };
            var chestsHashSet = chests.ToHashSet();

            queue.Enqueue(new SinglyLinkedList <Point>(start));

            while (queue.Count != 0)
            {
                var currentPosition = queue.Dequeue();
                var walker          = new Walker(currentPosition.Value);

                var nextPositions = directions
                                    .Select(direction => walker.WalkInDirection(map, direction))
                                    .Where(w => w.PointOfCollision == null && !visited.Contains(w.Position))
                                    .Select(w => w.Position);

                foreach (var nextPosition in nextPositions)
                {
                    var nextPath = new SinglyLinkedList <Point>(nextPosition, currentPosition);
                    queue.Enqueue(nextPath);
                    visited.Add(nextPosition);
                    if (chestsHashSet.Contains(nextPosition))
                    {
                        yield return(nextPath);
                    }
                }
            }
        }
        private IEnumerable <Point> TransformPath(Map map, MoveDirection[] path)
        {
            var walker = new Walker(map.InitialPosition);

            yield return(map.InitialPosition);

            foreach (var direction in path)
            {
                walker = walker.WalkInDirection(map, direction);
                yield return(walker.Position);

                if (walker.PointOfCollision.HasValue)
                {
                    break;
                }
            }
        }