コード例 #1
0
ファイル: Subway.cs プロジェクト: MarkPThomas/HeadFirst-OOAD
 public void AddStation(string stationName)
 {
     if (!HasStation(stationName))
     {
         Station station = new Station(stationName);
         _stations.Add(station);
     }
 }
コード例 #2
0
ファイル: Subway.cs プロジェクト: MarkPThomas/HeadFirst-OOAD
        public void AddConnection(string lineName, string station1Name, string station2Name)
        {
            if (!HasStation(station1Name)) { throw new ArgumentException("Invalid connection! Station " + station1Name + " does not exist."); }
            if (!HasStation(station2Name)) { throw new ArgumentException("Invalid connection! Station " + station2Name + " does not exist."); }

            Station station1 = new Station(station1Name);
            Station station2 = new Station(station2Name);

            // Direction 1
            Connection connection1 = new Connection(lineName, station1, station2);
            _connections.Add(connection1);

            // Direction 2
            Connection connection2 = new Connection(lineName, station2, station1);
            _connections.Add(connection2);
        }
コード例 #3
0
ファイル: Subway.cs プロジェクト: MarkPThomas/HeadFirst-OOAD
 private void AddToNetwork(Station station1, Station station2)
 {
     if (_network.ContainsKey(station1))
     {
         List<Station> connectingStations = _network[station1];
         if (!connectingStations.Contains(station2))
         {
             connectingStations.Add(station2);
         }
     }
     else
     {
         List<Station> connectingStations = new List<Station>() { station2 };
         _network.Add(station1, connectingStations);
     }
 }
コード例 #4
0
ファイル: Subway.cs プロジェクト: MarkPThomas/HeadFirst-OOAD
        public bool HasConnection(string lineName, string station1Name, string station2Name)
        {
            Station station1 = new Station(station1Name);
            Station station2 = new Station(station2Name);

            foreach (Connection connection in _connections)
            {
                if (string.Compare(connection.LineName, lineName) == 0)
                {
                    if ((connection.Station1.Equals(station1)) &&
                        (connection.Station2.Equals(station2)))
                    {
                        return true;
                    }
                }
            }
            return false;
        }
コード例 #5
0
 public Connection(string lineName, Station station1, Station station2)
 {
     LineName = lineName;
     _station1 = station1;
     _station2 = station2;
 }
コード例 #6
0
ファイル: Subway.cs プロジェクト: MarkPThomas/HeadFirst-OOAD
        /// <summary>
        /// This method is based on a well-known bit of code called Dijkstra's Algorithm, which figures out the shortest path between two nodes on a graph.
        /// </summary>
        /// <param name="startStationName"></param>
        /// <param name="endStationName"></param>
        /// <returns></returns>
        public List<Connection> GetDirections(string startStationName,
                                              string endStationName)
        {
            if (!HasStation(startStationName)) { throw new ArgumentException("Invalid connection! Station " + startStationName + " does not exist."); }
            if (!HasStation(endStationName)) { throw new ArgumentException("Invalid connection! Station " + endStationName + " does not exist."); }

            Station start = new Station(startStationName);
            Station end = new Station(endStationName);

            List<Connection> route = new List<Connection>();
            List<Station> reachableStations = new List<Station>();
            Dictionary<Station, Station> previousStations = new Dictionary<Station, Station>();

            List<Station> neighbors = _network[start];
            for (int i = 0; i < neighbors.Count; i++)
            {
                Station station = neighbors[i];
                if (station == end)
                {
                    route.Add(GetConnection(start, end));
                    return route;
                }
                else
                {
                    reachableStations.Add(station);
                    previousStations.Add(station, start);
                }
            }

            List<Station> nextStations = new List<Station>();
            nextStations.AddRange(neighbors);
            Station currentStation = start;

            // searchLoop:
            for (int j = 1; j < _stations.Count; j++)
            {
                List<Station> tmpNextStations = new List<Station>();
                for (int k = 0; k < nextStations.Count; k++)
                {
                    Station searchStation = nextStations[k];
                    reachableStations.Add(searchStation);
                    currentStation = searchStation;

                    List<Station> currentNeighbors = _network[currentStation];
                    for (int l = 0; l < currentNeighbors.Count; l++)
                    {
                        Station neighbor = currentNeighbors[l];
                        if (neighbor == end)
                        {
                            reachableStations.Add(neighbor);
                            previousStations.Add(neighbor, currentStation);
                            break;
                        }
                        else if(!reachableStations.Contains(neighbor))
                        {
                            reachableStations.Add(neighbor);
                            tmpNextStations.Add(neighbor);
                            previousStations.Add(neighbor, currentStation);
                        }
                    }
                }
                nextStations = tmpNextStations; 
            }

            // We've found the path by now
            bool keepLooping = true;
            Station keyStation = end;
            Station loopingStation;

            while (keepLooping)
            {
                loopingStation = previousStations[keyStation];
                route.Add(GetConnection(loopingStation, keyStation));
                //route.Add(0, GetConnection(loopingStation, keyStation));
                if (start == loopingStation)
                {
                    keepLooping = false;
                }
                keyStation = loopingStation;
            }

            return route;
        }
コード例 #7
0
ファイル: Subway.cs プロジェクト: MarkPThomas/HeadFirst-OOAD
 private Connection GetConnection(Station station1, Station station2)
 {
     foreach (Connection connection in _connections)
     {
         Station one = connection.Station1;
         Station two = connection.Station2;
         if ((station1.Name == one.Name) && (station2.Name == two.Name))
         {
             return connection;
         }
     }
     return null;
 }