diff options
| author | Camil Staps | 2015-05-29 08:27:18 +0200 | 
|---|---|---|
| committer | Camil Staps | 2015-05-29 08:27:18 +0200 | 
| commit | 52796bb5693dfd6ad1fda9a06cbc4c427a683f0d (patch) | |
| tree | 5cf334c832ce70f58bffea241f01166dceb45263 /Week14 Route 66 | |
| parent | comments; cleanup (diff) | |
Overviewer without Map
Diffstat (limited to 'Week14 Route 66')
| -rw-r--r-- | Week14 Route 66/src/OO14route66/Car.java | 3 | ||||
| -rw-r--r-- | Week14 Route 66/src/com/camilstaps/route66/Overviewer.java | 46 | 
2 files changed, 30 insertions, 19 deletions
| diff --git a/Week14 Route 66/src/OO14route66/Car.java b/Week14 Route 66/src/OO14route66/Car.java index 0058d8d..97d7b33 100644 --- a/Week14 Route 66/src/OO14route66/Car.java +++ b/Week14 Route 66/src/OO14route66/Car.java @@ -35,6 +35,7 @@ public class Car {       *  - choose a random speed       *  - set right location, direction and colour       *  - create new Driver object +     *  - say hello to the Overviewer       *        * @param number of the car       * @param model the model the car is in @@ -48,6 +49,7 @@ public class Car {          colour = carColours[number % carColours.length];          driver = new Driver(this);          this.model = model; +        Overviewer.getInstance().hello(this);      }      /** @@ -95,7 +97,6 @@ public class Car {       */      public void step() {          location = getNewLocation(); -        Overviewer.getInstance().setLocation(this, location);          model.update();      } diff --git a/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java b/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java index 762ded1..844f40c 100644 --- a/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java +++ b/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java @@ -7,23 +7,26 @@  package com.camilstaps.route66;  import OO14route66.Car; -import java.util.HashMap; -import java.util.Map; +import OO14route66.Model;  /** - * - * @author pieterkoopman + * The Overviewer (always only one instance) tells Drivers when they can drive + *  + * @author Camil Staps   */  public class Overviewer { -     +  +    /** +     * This is a singleton +     */      private static Overviewer instance; -    Map<Car, Integer> locations; -     -    protected Overviewer() { -        locations = new HashMap<>(); -    } +    Car[] cars = new Car[Model.NUMBEROFCARS]; +    /** +     * This is a singleton +     * @return the instance +     */      public static Overviewer getInstance() {          if (instance == null) {              instance = new Overviewer(); @@ -31,17 +34,24 @@ public class Overviewer {          return instance;      } -    public synchronized void setLocation(Car car, int location) { -        if (locations.containsKey(car)) { -            locations.remove(car); -        }  -        locations.put(car, location); +    /** +     * Add a car to the list of cars +     * @param car  +     */ +    public synchronized void hello(Car car) { +        cars[car.getNumber()] = car;      } +    /** +     * Check if a location is safe for a car to go to. This should always be  +     * checked by a driver before actually driving. +     * @param car +     * @param requested_location +     * @return  +     */      public synchronized boolean isSafeLocation(Car car, int requested_location) { -        for (Map.Entry<Car, Integer> location : locations.entrySet()) { -            Car that_car = location.getKey(); -            if (that_car != car &&  +        for (Car that_car : cars) { +            if (that_car != null && that_car != car &&                       that_car.getDirection() == car.getDirection() &&                      that_car.getLocation() > requested_location &&                      that_car.getLocation() < requested_location + Car.CARLENGTH + Car.MINCARSPACE) { | 
