diff options
Diffstat (limited to 'Week14 Route 66/src/OO14route66/Controller.java')
| -rw-r--r-- | Week14 Route 66/src/OO14route66/Controller.java | 56 | 
1 files changed, 19 insertions, 37 deletions
| diff --git a/Week14 Route 66/src/OO14route66/Controller.java b/Week14 Route 66/src/OO14route66/Controller.java index 3b684dc..83e6098 100644 --- a/Week14 Route 66/src/OO14route66/Controller.java +++ b/Week14 Route 66/src/OO14route66/Controller.java @@ -1,21 +1,20 @@  package OO14route66; +import com.camilstaps.route66.Driver;  import java.util.Random;  import java.util.concurrent.ExecutorService;  import java.util.concurrent.Executors;  /** - * OO1route66 initial class - * @author Pieter Koopman + * Route66 Controller + * @author Pieter Koopman, Camil Staps   *   * The initial controller runs as a single thread   */ -public class Controller -{ -    private int delay = 200;                // average sleep time +public class Controller { +    private int delay = 120;                // average sleep time      private final Model model;              // the model      private final Random random;            // a random generator -    private boolean run = true;             // car can run in simulation      /**       * The constructor of the controller @@ -27,53 +26,33 @@ public class Controller      }      /** -     * the run method from Thread -     * forever: -     *      move all cars -     *      sleep some time +     * Tell the drivers of all cars in the model to start driving       */      public void run() {          ExecutorService service = Executors.newCachedThreadPool();          for (Car car : model.getCars()) { -            service.execute(car.getDriver()); +            Driver d = car.getDriver(); +            d.setDelay(delay); +            service.execute(d);          }      }      /** -     * wait some pseudo random time -     */ -    private void pause() { -        try { // sleep can throw an exception  -            Thread.sleep(delay / 2 + random.nextInt(delay)); -        } -        catch (InterruptedException e) { // catch the exception thrown by sleep -            System.out.println("An exception in Controller: " + e); -        } -    } -     -    /** -     * make one step with all cars and repaint views. -     */ -    public void stepAllCars() { -        if (run) { -            for (int c = 0; c < Model.NUMBEROFCARS; c += 1) { -                model.getCar(c).step(); -            } -        } -        model.update(); // update only after all cars have stepped -    } -    /**       * stop all cars by setting boolean run to false       */      public void stopCars() { -        run = false; +        for (Car car : model.getCars()) { +            car.getDriver().setRunning(false); +        }      }      /**       * start all cars by setting boolean run to true       */      public void resumeCars() { -        run = true; +        for (Car car : model.getCars()) { +            car.getDriver().setRunning(true); +        }      }      public int getDelay() { @@ -85,6 +64,9 @@ public class Controller       * @param d        */      public void setDelay(int d) { -        delay = Math.max(50, Math.min (2000, d)); +        delay = Math.max(20, Math.min (2000, d)); +        for (Car car : model.getCars()) { +            car.getDriver().setDelay(delay); +        }      }  } | 
