diff options
Diffstat (limited to 'Week14 Route 66/src/OO14route66/Model.java')
-rw-r--r-- | Week14 Route 66/src/OO14route66/Model.java | 102 |
1 files changed, 51 insertions, 51 deletions
diff --git a/Week14 Route 66/src/OO14route66/Model.java b/Week14 Route 66/src/OO14route66/Model.java index f4e725f..ee9da42 100644 --- a/Week14 Route 66/src/OO14route66/Model.java +++ b/Week14 Route 66/src/OO14route66/Model.java @@ -1,10 +1,7 @@ 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; /** @@ -21,7 +18,12 @@ public class Model extends Observable NUMBEROFCARS = 5 * DIRECTIONS; // total number of cars in system private final ArrayList<JFrame> views; - private final Crossing crossing = new Crossing(this, Direction.North); + //private final Crossing crossing = new Crossing(this, Direction.North); + + private Direction allowed; + private boolean crossingWaiting = false; + private long lastCrossingChange = 0; + private static final int MIN_CROSS_TIME = 1000; /** * Constructor: create all cars @@ -75,62 +77,60 @@ public class Model extends Observable 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); + boolean 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."); + if (car.isInFrontOfCrossing() && !isCrossingAllowed(car.getDirection())) { + ok = doCrossingRequest(); + } + + if (!ok) { + try { + wait(); + } catch (InterruptedException ex) { } - } 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; - } + public synchronized boolean isCarsOnCrossing() { + for (Car car : cars) { + if (car.isOnCrossing()) { + return true; } - if (!ok) { - System.err.println("Checking crossing waiting"); - try { - wait(); - } catch (InterruptedException ex) { - } - System.err.println("Checking crossing"); + } + return false; + } + + public synchronized void doSwitchCrossing() { + crossingWaiting = true; + while (isCarsOnCrossing()) { + try { + wait(); + } catch (InterruptedException ex) { } } + System.err.println("Switching"); + if (allowed == Direction.East || allowed == Direction.West) { + allowed = Direction.North; + } else { + allowed = Direction.East; + } + System.err.println("Switched to " + allowed); + crossingWaiting = false; + lastCrossingChange = System.currentTimeMillis(); + } + + public synchronized boolean isCrossingAllowed(Direction direction) { + return !crossingWaiting && (direction == allowed || Direction.opposite(direction) == allowed); + } + + public synchronized boolean doCrossingRequest() { + if (crossingWaiting || System.currentTimeMillis() - lastCrossingChange < MIN_CROSS_TIME) { + return false; + } + doSwitchCrossing(); return true; } } |