diff options
Diffstat (limited to 'Week14 Route 66/src/OO14route66/Model.java')
-rw-r--r-- | Week14 Route 66/src/OO14route66/Model.java | 85 |
1 files changed, 79 insertions, 6 deletions
diff --git a/Week14 Route 66/src/OO14route66/Model.java b/Week14 Route 66/src/OO14route66/Model.java index c79aa4e..f4e725f 100644 --- a/Week14 Route 66/src/OO14route66/Model.java +++ b/Week14 Route 66/src/OO14route66/Model.java @@ -1,7 +1,10 @@ package OO14route66; +import com.camilstaps.route66.Crossing; import java.util.ArrayList; import java.util.Observable; +import java.util.logging.Level; +import java.util.logging.Logger; import javax.swing.JFrame; /** @@ -12,13 +15,13 @@ import javax.swing.JFrame; */ public class Model extends Observable { - private final Car [] cars; - public static final int - DIRECTIONS = 2, + private final Car [] cars; + public static final int + DIRECTIONS = 4, NUMBEROFCARS = 5 * DIRECTIONS; // total number of cars in system - private final ArrayList<JFrame> views; - + private final ArrayList<JFrame> views; + private final Crossing crossing = new Crossing(this, Direction.North); /** * Constructor: create all cars @@ -55,9 +58,79 @@ public class Model extends Observable /** * repaint all views */ - public void update() { + public synchronized void update() { for (JFrame view: views) { view.repaint(); } + notifyAll(); + } + + /** + * 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) { + Car that_car = cars[car.getNumber() < DIRECTIONS ? car.getNumber() + NUMBEROFCARS - DIRECTIONS : car.getNumber() - DIRECTIONS]; + + boolean ok; + + System.err.println("Car " + car.getNumber() + " requests safety."); + + do { + ok = !(that_car.getLocation() > requested_location && + that_car.getLocation() < requested_location + Car.CARLENGTH + Car.MINCARSPACE); + +// if (!ok && that_car.getDriver().getWaitingToCross()) { +// crossing.doRequest(); +// } + + if (car.isInFrontOfCrossing() && !crossing.isAllowed(car.getDirection())) { + System.err.println("Requesting switch for car " + car.getNumber()); + ok = false; + //if (!car.getDriver().getWaitingToCross()) { + //car.getDriver().setWaitingToCross(true); + ok = crossing.doRequest(); + //car.getDriver().setWaitingToCross(!ok); + //} + } + + if (!ok) { + System.err.println("Car " + car.getNumber() + " waiting."); + try { + wait(); + } catch (InterruptedException ex) { + } + System.err.println("Car " + car.getNumber() + " acquired lock."); + } + } while (!ok); + + System.err.println("Car " + car.getNumber() + " has safe location."); + + return ok; + } + + public synchronized boolean isNoCarsOnCrossing() { + boolean ok = false; + while (!ok) { + ok = true; + for (Car car : cars) { + if (car.isOnCrossing()) { + System.err.println("Car " + car.getNumber() + " is on crossing."); + ok = false; + } + } + if (!ok) { + System.err.println("Checking crossing waiting"); + try { + wait(); + } catch (InterruptedException ex) { + } + System.err.println("Checking crossing"); + } + } + return true; } } |