Ejemplo n.º 1
0
  private void moveThruIntersection(Vehicle vehicle, int nextPos) {

    lockIntersection(nextPos);
    vehicle.setPos(nextPos);
    vehicle.setStoppedByPolicy(false);
    unlockIntersection(nextPos);
  }
Ejemplo n.º 2
0
  public void advance(Vehicle vehicle) {

    vehicle.setRunning(true);
    // Verify Intersection
    int nextPos = 1 + (vehicle.getPos());
    boolean intersect = false;
    Vehicle otherVehicle;
    for (int pos : adjacentStreets.keySet()) {

      //	System.out.print("Pos:"+pos + "==" + "nextPos"+nextPos);
      if (nextPos == pos) {

        intersect = true;
        break;
      }
    }
    System.out.println(vehicle.getName() + ":");
    // IF intersect
    if (intersect) {
      // IF free intersect is available
      if (freeIntersection(nextPos))
        // If nobody is waiting for the intersect. Vehicle arrive first
        // so
        // apply FIFO
        if ((otherVehicle = waitingVehicle(nextPos)) == null) { // nobody

          System.out.println(" Nobody is waiting");
          // is
          // waiting
          moveThruIntersection(vehicle, nextPos);
        } else if (IntersectPolicy.policy(vehicle, otherVehicle)) {
          moveThruIntersection(vehicle, nextPos);
          System.out.println("Policy Say Move First");
        } else {
          // Policy say I must stop
          vehicle.setRunning(false);
          vehicle.setStoppedByPolicy(true);
          System.out.println("Policy say stop");
        }
    } else if (carsInFront(nextPos)) {
      vehicle.setRunning(false);
      System.out.println("Car in front");
    } else {
      System.out.println("Move");
      vehicle.setPos(nextPos);
    }
  }