/// <summary>
 /// Initializes a new instance of the <see cref="RoutePathElement"/> class.
 /// </summary>
 /// <param name="room">The room of this element.</param>
 /// <param name="previous">The previous <see cref="RoutePathElement"/>.</param>
 public RoutePathElement([NotNull] RoomViewModel room, [NotNull] RoutePathElement previous)
 {
     Assert.ArgumentNotNull(room, "room");
     Assert.ArgumentNotNull(previous, "previous");
     Room     = room;
     Previous = previous;
 }
Beispiel #2
0
        public static IEnumerable <RoomViewModel> FindRouteToRoom([NotNull] RoomViewModel currentRoom, [NotNull] RoomViewModel roomToNavigateTo, IEnumerable <RoomColor> roomColorsToSkip = null)
        {
            Assert.ArgumentNotNull(currentRoom, "currentRoom");
            Assert.ArgumentNotNull(roomToNavigateTo, "roomToNavigateTo");

            if (currentRoom == roomToNavigateTo)
            {
                return(Enumerable.Empty <RoomViewModel>());
            }

            var visitedRooms   = new HashSet <RoomViewModel>();
            var pathQueue      = new Queue <RoutePathElement>();
            var currentElement = new RoutePathElement(currentRoom);

            while (true)
            {
                if (currentElement.Room == roomToNavigateTo)
                {
                    break;
                }

                foreach (var exit in currentElement.Room.Exits)
                {
                    if (exit.Room == null)
                    {
                        continue;
                    }

                    if (!visitedRooms.Contains(exit.Room))
                    {
                        bool skipRoom = roomColorsToSkip?.Any(roomColor => exit.Room.Color == roomColor) == true;

                        if (skipRoom)
                        {
                            continue;
                        }

                        pathQueue.Enqueue(new RoutePathElement(exit.Room, currentElement));
                        visitedRooms.Add(exit.Room);
                    }
                }

                if (pathQueue.Count == 0)
                {
                    return(Enumerable.Empty <RoomViewModel>());
                }

                currentElement = pathQueue.Dequeue();
            }

            var result = new List <RoomViewModel>();

            while (currentElement.Previous != null)
            {
                result.Add(currentElement.Room);
                currentElement = currentElement.Previous;
            }

            result.Reverse();
            return(result);
        }