Beispiel #1
0
        /// <summary>
        /// First unboard, then board passengers
        /// </summary>
        /// <param name="tram"></param>
        /// <returns></returns>
        public (int pOut, int pIn, List <int> entrances) UnboardAndBoard(Tram tram, int maxUnboard)
        {
            // First empty the tram
            int pOut = tram.EmptyPassengers(maxUnboard);

            // Then board the passengers
            var(pIn, entrances) = BoardPassengers(tram);

            return(pOut, pIn, entrances);
        }
Beispiel #2
0
        /// <summary>
        /// Get platform corresponding to occupant
        /// </summary>
        /// <param name="tram">Tram</param>
        /// <returns>Platform at which the tram is stationed</returns>
        private Platform PlatformForOccupant(Tram tram)
        {
            if (tram == occupant)
            {
                return(Platform.A);
            }
            else if (tram == occupant2)
            {
                return(Platform.B);
            }

            throw new Exception($"Tried to find platform corresponding to tram {tram.id}, but none was found.");
        }
Beispiel #3
0
 /// <summary>
 /// Try enter the station
 /// </summary>
 /// <param name="tram"></param>
 /// <returns>True if tram could arrive at station</returns>
 public void Occupy(Tram tram)
 {
     if (occupant is null)
     {
         occupant = tram;
         Trace.Assert(lastOccupantId < 0 || occupant.id - 1 == lastOccupantId || occupant.id == 6001,
                      $"Tram arrived in wrong order, {occupant.id} arrived after {lastOccupantId} at {name} ({direction})");
         lastOccupantId = occupant.id;
     }
     else
     {
         throw new Exception($"{tram.id} tried to occupy {name} in direction {direction}, but the station was full.");
     }
 }
Beispiel #4
0
        /// <summary>
        /// Remove passengers from station
        /// </summary>
        /// <param name="maxPassengers">Maximum amount that will still fit in the tram</param>
        /// <returns>The amount of boarded passengers, and their arrival times</returns>
        public (int, List <int>) BoardPassengers(Tram tram)
        {
            List <int> entrances     = new List <int>();
            int        maxPassengers = tram.PassengerSpace();

            int count = 0;

            while (count < maxPassengers && waitingPassengers.Count > 0)
            {
                entrances.Add(waitingPassengers.Dequeue());
                count++;
            }

            tram.AddPassengers(count);
            return(count, entrances);
        }
Beispiel #5
0
        internal Tram OccupyFromDepotQueue(Platform platform)
        {
            Trace.Assert(IsFree(platform), $"Tried to occupy {platform} from queue, but platform was already taken.");

            Tram tram = depotQueue.Dequeue();

            if (platform == Platform.A)
            {
                occupant = tram;
            }
            else
            {
                occupant2 = tram;
            }

            // No tram yet, so first arrival
            if (first == Platform.None)
            {
                first = platform;
            }

            return(tram);
        }
Beispiel #6
0
        /// <summary>
        /// Occupy a platform of the station
        /// </summary>
        /// <param name="tram">The new occupant</param>
        /// <param name="platform">The target platform</param>
        internal void Occupy(Tram tram, Platform platform)
        {
            if (platform == Platform.A)
            {
                Trace.Assert(occupant == null, $"Tram {tram.id} tried to occupy {name} with platform {platform} but that platform was already occupied by {occupant?.id}");
                occupant = tram;
            }
            else if (platform == Platform.B)
            {
                Trace.Assert(occupant2 == null, $"Tram {tram.id} tried to occupy {name} with platform {platform} but that platform was already occupied by {occupant2?.id}");
                occupant2 = tram;
            }
            else
            {
                throw new Exception($"Unknown platform {platform}.");
            }

            // No tram yet, so first arrival
            if (first == Platform.None)
            {
                first = platform;
            }
        }
Beispiel #7
0
 // Put tram in station queue
 public void Enqueue(Tram tram)
 {
     incomingTrams.Enqueue(tram);
 }