aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src/com/camilstaps
diff options
context:
space:
mode:
Diffstat (limited to 'Week14 Route 66/src/com/camilstaps')
-rw-r--r--Week14 Route 66/src/com/camilstaps/route66/Crossing.java61
-rw-r--r--Week14 Route 66/src/com/camilstaps/route66/Driver.java32
-rw-r--r--Week14 Route 66/src/com/camilstaps/route66/Overviewer.java64
3 files changed, 88 insertions, 69 deletions
diff --git a/Week14 Route 66/src/com/camilstaps/route66/Crossing.java b/Week14 Route 66/src/com/camilstaps/route66/Crossing.java
new file mode 100644
index 0000000..eba5bfe
--- /dev/null
+++ b/Week14 Route 66/src/com/camilstaps/route66/Crossing.java
@@ -0,0 +1,61 @@
+/*
+ * Copyright (c) 2015 Camil Staps
+ */
+package com.camilstaps.route66;
+
+import OO14route66.Direction;
+import OO14route66.Model;
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+/**
+ *
+ * @author camilstaps
+ */
+public class Crossing {
+
+ private final Model model;
+ private Direction allowed;
+ private boolean waiting = false;
+ private int requests = 0;
+
+ public Crossing(Model model, Direction allowed) {
+ this.model = model;
+ this.allowed = allowed;
+ }
+
+ public Direction getAllowed() {
+ return allowed;
+ }
+
+ public synchronized void doSwitch() {
+ waiting = true;
+ while (!model.isNoCarsOnCrossing());
+ System.err.println("Switching");
+ if (allowed == Direction.East || allowed == Direction.West) {
+ allowed = Direction.North;
+ } else {
+ allowed = Direction.East;
+ }
+ requests = 0;
+ System.err.println("Switched to " + allowed);
+ waiting = false;
+ //notifyAll();
+ }
+
+ public boolean isAllowed(Direction direction) {
+ return !waiting && (direction == allowed || Direction.opposite(direction) == allowed);
+ }
+
+ public boolean doRequest() {
+ //if (++requests >= 2) {
+ if (waiting) {
+ return false;
+ }
+ doSwitch();
+ return true;
+ //}
+ //return false;
+ }
+
+}
diff --git a/Week14 Route 66/src/com/camilstaps/route66/Driver.java b/Week14 Route 66/src/com/camilstaps/route66/Driver.java
index e2e5a7d..4a323cd 100644
--- a/Week14 Route 66/src/com/camilstaps/route66/Driver.java
+++ b/Week14 Route 66/src/com/camilstaps/route66/Driver.java
@@ -8,27 +8,46 @@ import OO14route66.Model;
import java.util.Random;
/**
- *
- * @author camilstaps
+ * A Driver takes care its car doesn't collide
+ * @author Camil Staps
*/
public class Driver implements Runnable {
private final Car car;
+ private final Model model;
private final Random r;
- private boolean running;
+ private boolean running, waitingToCross = false;
private int delay;
- public Driver(Car car) {
+ public Driver(Car car, Model model) {
this.car = car;
+ this.model = model;
r = new Random();
}
+ /**
+ * Set running to false to disable driving altogether
+ * @param value
+ */
public void setRunning(boolean value) {
running = value;
}
+ public void setWaitingToCross(boolean value) {
+ waitingToCross = value;
+ }
+
+ public boolean getWaitingToCross() {
+ return waitingToCross;
+ }
+
+ /**
+ * Set some approximate delay for {@link Driver#pause()}. The actual delay
+ * is still pseudorandom.
+ * @param value
+ */
public void setDelay(int value) {
delay = value;
}
@@ -38,7 +57,7 @@ public class Driver implements Runnable {
public void run() {
running = true;
while (true) {
- while (!running || !Overviewer.getInstance().isSafeLocation(car, car.getNewLocation()));
+ while (!running || !model.isSafeLocation(car, car.getNewLocation()));
car.step();
@@ -46,6 +65,9 @@ public class Driver implements Runnable {
}
}
+ /**
+ * Pause for some pseudorandom time
+ */
protected void pause() {
try {
int bonus = 0;
diff --git a/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java b/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java
deleted file mode 100644
index 844f40c..0000000
--- a/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java
+++ /dev/null
@@ -1,64 +0,0 @@
-/*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
-
-package com.camilstaps.route66;
-
-import OO14route66.Car;
-import OO14route66.Model;
-
-/**
- * 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;
-
- Car[] cars = new Car[Model.NUMBEROFCARS];
-
- /**
- * This is a singleton
- * @return the instance
- */
- public static Overviewer getInstance() {
- if (instance == null) {
- instance = new Overviewer();
- }
- return instance;
- }
-
- /**
- * 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 (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) {
- return false;
- }
- }
- return true;
- }
-
-}