public static void main(String[] args) {
    Random rng = new Random();

    TrafficSignal trafficSignal = new TrafficSignal();
    trafficSignal.start();

    while (true) {
      int delay = rng.nextInt(10);
      try {
        Thread.sleep(delay * 1000);
      } catch (InterruptedException e) {
      }
      trafficSignal.pressButton();
    }
  }
示例#2
0
 private Intersection findNextInter() {
   Road r = getRoad();
   if (getDir() == r.getDir()) nextInter = r.getEndIntersection();
   else nextInter = r.getStartIntersection();
   TrafficControl tc = nextInter.getTrafficControl();
   if (tc != null && tc.getType() == 0) {
     int oppDir = RoadGrid.getOppDir(getDir());
     synchronized (tc) {
       ((TrafficSignal) tc).addListener(this, oppDir);
     }
     watch_red = ((TrafficSignal) tc).getSignalState(oppDir) == 0;
   }
   return nextInter;
 }
示例#3
0
 private void make_Turn(int dir) {
   TrafficControl traf_cont = nextInter.getTrafficControl();
   int oppDir = RoadGrid.getOppDir(getDir());
   boolean listen = false;
   if (traf_cont != null && traf_cont.getType() == 0) listen = true;
   if (!watch_red && !nextInter.isOccupied()) {
     if (listen)
       synchronized (traf_cont) {
         ((TrafficSignal) traf_cont).removeListener(this, oppDir);
       }
     synchronized (nextInter) {
       crossIntersection(nextInter, dir);
       this.accelerate((getRoad().getSpeedLimit() - getSpeed()) * 10, 0);
     }
     findNextInter();
   }
 }