public void makeDecision() { // съехали со старой дороги if (currentRoad != null) { currentRoad.relatedCars.Remove(this); } // заехали на новую дорогу if (currentRoad == null) { var sortedRoads = to.relatedRoads.OrderBy(road => road.azimuth).ToList<Road>(); var randNumber = rand.Next(1, 11); if (randNumber <= 7 || sortedRoads.Count == 1) { currentRoad = sortedRoads[0]; } else { currentRoad = sortedRoads[rand.Next(1, sortedRoads.Count)]; } } else { var sortedRoads = to.relatedRoads.OrderBy(road => Math.Abs(currentRoad.azimuth - road.azimuth)).ToList<Road>(); var randNumber = rand.Next(1, 11); if (randNumber <= 7 || sortedRoads.Count == 1) { currentRoad = sortedRoads[0]; } else { currentRoad = sortedRoads[rand.Next(1, sortedRoads.Count)]; } } currentRoad.relatedCars.Add(this); var newJunction = currentRoad.junc1 == to ? currentRoad.junc2 : currentRoad.junc1; from = to; to = newJunction; // расчет длины и времени speed = currentRoad.maxSpeed * Math.Pow(2, -1 * currentRoad.relatedCars.Count / currentRoad.halfLoad); departureTime = Form1.CurrentTime; arrivalTime = Form1.CurrentTime + currentRoad.Length / speed; }
public Road(Junction junc1_, Junction junc2_, int maxSpeed_, int halfLoad_) { if (junc1_.relatedRoads.Count == 4 || junc2_.relatedRoads.Count == 4) { throw new ArgumentException("In Road(Junction. Junction): one of junctions already has 4 roads!"); } amountOfCars = 0; junc1 = junc1_; junc1_.relatedRoads.Add(this); junc2 = junc2_; junc2_.relatedRoads.Add(this); maxSpeed = maxSpeed_; halfLoad = halfLoad_; }
public Car() { to = Form1.junctions[rand.Next(Form1.junctions.Count)]; this.makeDecision(); }