예제 #1
0
    /// <summary>
    ///
    /// </summary>
    /// <param name="currentRoom"></param>
    /// <param name="exitRoom"></param>
    /// <param name="routeLength"></param>
    /// <param name="visited"></param>
    /// <returns></returns>
    private int CalculateShortestRoute(BaseRoom currentRoom, BaseRoom exitRoom, int routeLength, HashSet <BaseRoom> visited)
    {
        var nextRooms = new Dictionary <BaseRoom, bool>();

        foreach (var direction in RoomConnection.Directions)
        {
            if (!currentRoom.HasConnection(direction))
            {
                continue;
            }

            var door = currentRoom.GetConnector(direction) as DoorConnector;
            if (door == null)
            {
                continue;
            }

            var nextRoom = GetAdjacentRoom(currentRoom, direction);
            if (nextRoom == null)
            {
                continue;
            }

            if (visited.Contains(nextRoom))
            {
                continue;
            }

            if (nextRoom == exitRoom)
            {
                return(routeLength + (door.IsLocked ? 1 : 0));
            }

            nextRooms.Add(nextRoom, door.IsLocked);
        }

        visited.Add(currentRoom);
        foreach (var roomEntry in nextRooms)
        {
            var room       = roomEntry.Key;
            var doorLocked = roomEntry.Value;

            var length = CalculateShortestRoute(room, exitRoom, routeLength + (doorLocked ? 1 : 0), visited);
            if (length < int.MaxValue)
            {
                return(length);
            }
        }

        return(int.MaxValue);
    }