SE450: Project: Lights [11/13] Previous pageContentsNext page

Now, how do you add lights to this?

Think first about a stoplight that sits just between two road segments (no intersection).

One idea:

class Light implements CarAcceptor {
  {GREEN,RED} state;
  CarAcceptor nextRoad;
  public double distanceToObstacle(double fromPosition) {
    if state==GREEN
      return nextRoad.distanceToObstacle(fromPosition);
      return 0;
  void setState(...) // set the state

Then set up the objects as follows:

CarGenerator -> Road1 -> Light -> Road2 -> Sink

How do you coordinate two lights then?

One idea:

class TwoWayLightController implements Agent {
   Light[] lights;
   public void run() {
     // change the state of lights...

Previous pageContentsNext page