aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src/OO14route66/Model.java
diff options
context:
space:
mode:
authorCamil Staps2015-05-29 13:17:53 +0200
committerCamil Staps2015-05-29 13:17:53 +0200
commit3ccaa674fdbd3408d4cbea3ed9abb02c14433b9c (patch)
treec51dbe974566b4c437500fbf6894b626b203d265 /Week14 Route 66/src/OO14route66/Model.java
parentHack to make it seem to be working (diff)
licensing; reorganisation; javadoc; cleanup
Diffstat (limited to 'Week14 Route 66/src/OO14route66/Model.java')
-rw-r--r--Week14 Route 66/src/OO14route66/Model.java136
1 files changed, 0 insertions, 136 deletions
diff --git a/Week14 Route 66/src/OO14route66/Model.java b/Week14 Route 66/src/OO14route66/Model.java
deleted file mode 100644
index ee9da42..0000000
--- a/Week14 Route 66/src/OO14route66/Model.java
+++ /dev/null
@@ -1,136 +0,0 @@
-package OO14route66;
-
-import java.util.ArrayList;
-import java.util.Observable;
-import javax.swing.JFrame;
-
-/**
- * OO1route66 initial class
- * @author Pieter Koopman
- *
- * The class model holds all cars in the simulation
- */
-public class Model extends Observable
-{
- 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 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
- */
- public Model() {
- views = new ArrayList<>();
- cars = new Car [NUMBEROFCARS];
- for (int c = 0; c < NUMBEROFCARS; c += 1) {
- cars[c] = new Car(c, this);
- }
- }
-
- /**
- * add the view to this model. It will be repainted upon an update
- * @param view
- */
- public void addView(JFrame view) {
- views.add(view);
- }
-
- public Car[] getCars() {
- return cars;
- }
-
- /**
- * get a car from the model
- * @param i numbers of required car
- * @return the car itself (not a copy)
- */
- public Car getCar(int i) {
- return cars[i];
- }
-
- /**
- * repaint all views
- */
- 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 = !(that_car.getLocation() > requested_location &&
- that_car.getLocation() < requested_location + Car.CARLENGTH + Car.MINCARSPACE);
-
- if (car.isInFrontOfCrossing() && !isCrossingAllowed(car.getDirection())) {
- ok = doCrossingRequest();
- }
-
- if (!ok) {
- try {
- wait();
- } catch (InterruptedException ex) {
- }
- }
-
- return ok;
- }
-
- public synchronized boolean isCarsOnCrossing() {
- for (Car car : cars) {
- if (car.isOnCrossing()) {
- return true;
- }
- }
- 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;
- }
-}