Example #1
0
 public Road(int length, Crossroad firstCrossroad, Crossroad secondCrossroad, int laneCount, Orientation orientation)
 {
     Id = GetUniqueId();
     Length = length;
     First = firstCrossroad;
     Second = secondCrossroad;
     IncreasingLaneCount = laneCount;
     DecreasingLaneCount = laneCount;
     Orientation = orientation;
     IncreasingLanes = new LinkedList<CarDistance>[laneCount];
     DecreasingLanes = new LinkedList<CarDistance>[laneCount];
     for (int i = 0; i < laneCount; i++)
     {
         IncreasingLanes[i] = new LinkedList<CarDistance>();
         DecreasingLanes[i] = new LinkedList<CarDistance>();
     }
 }
Example #2
0
 public static void AddRoad(List<Road> list, Direction direction, Crossroad nextCrossroad)
 {
     switch (direction)
     {
         case Direction.North:
             list.Add(nextCrossroad.North);
             break;
         case Direction.East:
             list.Add(nextCrossroad.East);
             break;
         case Direction.West:
             list.Add(nextCrossroad.West);
             break;
         case Direction.South:
             list.Add(nextCrossroad.South);
             break;
         default:
             Console.WriteLine("Could not determine Direction");
             break;
     }
 }
        private void SwitchLights(Crossroad crossroad)
        {
            var lights = crossroad.Lights;
            var moment = CurrentMoment;
            while (moment > lights.CycleDuration)
            {
                moment -= lights.CycleDuration;
            }

            var secondDuration = lights.WestEastDuration;
            Orientation computed = crossroad.LightsState;
            switch (lights.StartingLightingState)
            {
                case Orientation.NorthSouth:
                    secondDuration = lights.WestEastDuration;
                    break;
                case Orientation.EastWest:
                    secondDuration = lights.NorthSouthDuration;
                    break;
            }
            if (moment < lights.TimeShift || moment >= lights.TimeShift + secondDuration)
            {
                if (computed != lights.StartingLightingState)
                    crossroad.SwitchLightState();
            }
            else
            {
                if (computed == lights.StartingLightingState)
                    crossroad.SwitchLightState();
            }
        }
        private void ProcessCars(LinkedList<CarDistance> lane,Crossroad crossroad, List<Car> finishedCars, bool fromIncreasingLane )
        {
            List<CarDistance> toDelete = new List<CarDistance>();
            foreach (var carDistance in lane)
            {
                if (carDistance.Distance > 0 && carDistance.Distance > ProblemInstance.CarSpeed)
                    //check if exists empty space to move a car
                    if (lane.Any(x => x.Distance + Car.Length > carDistance.Distance +
                                                         ProblemInstance.CarSpeed &&
                                                         x.Distance < carDistance.Distance))
                    {
                        carDistance.Stopped = true;
                        //korek,wyznaczyc nowy distance
                    }
                    else
                    {
                        if (carDistance.Stopped)
                            //w tej fazie jedynie ruszamy
                            carDistance.Stopped = false;
                        else
                            //samochod sie porusza i konczy na tej samej drodze
                            carDistance.Distance -= ProblemInstance.CarSpeed;
                    }
                else
                {
                    var leftoverSpeed = ProblemInstance.CarSpeed - carDistance.Distance;
                    // zmniejsz distance do 0 lub todo:najmniejszej mozliwej wartosci
                    carDistance.Distance = 0;
                    //result == true if car left road after movement
                    var result = MoveDependingOnLights(crossroad, carDistance, leftoverSpeed, finishedCars, fromIncreasingLane);

                    if (result)
                        toDelete.Add(carDistance);
                }
            }

            foreach (var element in toDelete)
            {
                lane.Remove(element);
            }
        }
        private bool MoveDependingOnLights(Crossroad crossroad, CarDistance carDistance, int leftoverSpeed, List<Car> finishedCars, bool fromIncreasingLane )
        {
            //koniec mapy
            if (crossroad == null)
            {
                finishedCars.Add(carDistance.Car);
                return true;
            }

            //czerwone
            if (crossroad.LightsState != carDistance.Road.Orientation)
            {
                return false;
            }

            if (carDistance.Delay > 0)
            {
                carDistance.Delay--;
                return false;
            }

            var road = carDistance.Car.NextRoad;

            //koniec, samochod dojechal do konca skrzyzowania
            if (road == null)
            {
                finishedCars.Add(carDistance.Car);
                return true;
            }

            Direction direction = DetermineDirection(crossroad,road);

            //setDelay
            int delay = 0;
            if (carDistance.Car.Route.Roads.Count >= carDistance.Car.RoadProgress + 2)
            {
                var delayRoad = carDistance.Car.Route.Roads[carDistance.Car.RoadProgress + 1];
                if (delayRoad != null)
                {
                    var delayCrossroad = road.First;
                    if (road.First != null && road.First.Id == crossroad.Id)
                        delayCrossroad = road.Second;

                    if (delayCrossroad != null)
                    {
                        var delayDirection = DetermineDirection(delayCrossroad, delayRoad);
                        delay = ComputeDelay(direction, delayDirection);
                    }
                }
            }

            switch (direction)
            {
                case Direction.North:
                    return road.AddCarToDecreasingLane(carDistance.Car, road.Length - leftoverSpeed - Car.Length, delay);
                case Direction.East:
                    return road.AddCarToIncreasingLane(carDistance.Car, road.Length - leftoverSpeed - Car.Length, delay);
                case Direction.South:
                    return road.AddCarToIncreasingLane(carDistance.Car, road.Length - leftoverSpeed - Car.Length, delay);
                case Direction.West:
                    return road.AddCarToDecreasingLane(carDistance.Car, road.Length - leftoverSpeed - Car.Length, delay);
            }

            return false;
        }
 private Direction DetermineDirection(Crossroad crossroad, Road road)
 {
     if (crossroad.North != null && crossroad.North.Id == road.Id)
         return Direction.North;
     if (crossroad.South != null && crossroad.South.Id == road.Id)
         return Direction.South;
     if (crossroad.East != null && crossroad.East.Id == road.Id)
         return Direction.East;
     if (crossroad.West != null && crossroad.West.Id == road.Id)
         return Direction.West;
     throw new Exception("Cannot determine direction");
 }