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(); } }
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); }
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()); }