Exemplo n.º 1
0
        public RoadWayLocation(RoadWay roadWay, IDictionary <long, Car> cars)
        {
            RoadWay = roadWay ?? throw new ArgumentNullException(nameof(roadWay));
            Cars    = cars ?? throw new ArgumentNullException(nameof(cars));

            if (!IsValid())
            {
                throw new ArgumentOutOfRangeException();
            }
        }
Exemplo n.º 2
0
        public IRoadWayLocation GetNextRoadLocationAfterTick(IRoadWayLocation currentRoadWayLocation)
        {
            IRoadWayLocation nextRoadWayLocation = new RoadWayLocation(currentRoadWayLocation.RoadWay, new Dictionary <long, Car>());

            IDictionary <long, Car> currentCars = currentRoadWayLocation.Cars;

            // [TODO]: Process car to be out from the Road
            long       forwardCarFreeDistance = currentRoadWayLocation.RoadWay.Distance + 1;
            CarContext forwardCarContext      = new CarContext(currentRoadWayLocation.RoadWay, forwardCarFreeDistance);

            foreach (var item in currentCars.OrderByDescending(c => c.Key))
            {
                RoadWay roadWay         = currentRoadWayLocation.RoadWay;
                long    currentDistance = item.Key;

                Car car = item.Value;
                var currentCarContext = new CarContext(roadWay, currentDistance);

                long nextCarDistance = GetNextCarDistanceAfterTick(car, currentCarContext, forwardCarContext);

                if (nextCarDistance > currentRoadWayLocation.RoadWay.Distance)
                {
                    // [TODO]: When car is out of the Road
                    continue;
                }

                if (nextCarDistance == currentDistance)
                {
                    // [TODO]: When car does not move
                }

                nextRoadWayLocation.Cars.Add(nextCarDistance, car);

                forwardCarContext = currentCarContext;
            }

            return(nextRoadWayLocation);
        }
Exemplo n.º 3
0
 public bool IsValid()
 {
     return(RoadWay.IsValid() &&
            Cars.All(item => item.Value.IsValid() && item.Key >= 0 && item.Key <= RoadWay.Distance) &&
            Cars.Keys.Count == Cars.Keys.Distinct().Count());
 }