aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src/com/camilstaps/route66
diff options
context:
space:
mode:
Diffstat (limited to 'Week14 Route 66/src/com/camilstaps/route66')
-rw-r--r--Week14 Route 66/src/com/camilstaps/route66/Driver.java41
-rw-r--r--Week14 Route 66/src/com/camilstaps/route66/Overviewer.java54
2 files changed, 80 insertions, 15 deletions
diff --git a/Week14 Route 66/src/com/camilstaps/route66/Driver.java b/Week14 Route 66/src/com/camilstaps/route66/Driver.java
index f10bb3a..e2e5a7d 100644
--- a/Week14 Route 66/src/com/camilstaps/route66/Driver.java
+++ b/Week14 Route 66/src/com/camilstaps/route66/Driver.java
@@ -5,10 +5,7 @@ package com.camilstaps.route66;
import OO14route66.Car;
import OO14route66.Model;
-import OO14route66.Regelaar;
import java.util.Random;
-import java.util.logging.Level;
-import java.util.logging.Logger;
/**
*
@@ -20,29 +17,43 @@ public class Driver implements Runnable {
private final Random r;
+ private boolean running;
+ private int delay;
+
public Driver(Car car) {
this.car = car;
r = new Random();
}
+
+ public void setRunning(boolean value) {
+ running = value;
+ }
+
+ public void setDelay(int value) {
+ delay = value;
+ }
@Override
@SuppressWarnings("empty-statement")
- public synchronized void run() {
+ public void run() {
+ running = true;
while (true) {
- while (!Regelaar.getInstance().isSafeLocation(car, car.getNewLocation()));
+ while (!running || !Overviewer.getInstance().isSafeLocation(car, car.getNewLocation()));
car.step();
-
- try {
- int bonus = 0;
- if (Math.abs((System.currentTimeMillis() / 1000) % Model.NUMBEROFCARS - car.getNumber()) < 2) {
- bonus = -30;
- }
- Thread.sleep(Math.abs(r.nextInt(50) + 5 + bonus));
- } catch (InterruptedException ex) {
- Logger.getLogger(Driver.class.getName()).log(Level.SEVERE, null, ex);
- }
+
+ pause();
}
}
+ protected void pause() {
+ try {
+ int bonus = 0;
+ if (Math.abs((System.currentTimeMillis() / 1000) % Model.NUMBEROFCARS - car.getNumber()) < 2) {
+ bonus = -50;
+ }
+ Thread.sleep(Math.abs(r.nextInt(50) + delay + bonus));
+ } catch (InterruptedException ex) {}
+ }
+
}
diff --git a/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java b/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java
new file mode 100644
index 0000000..762ded1
--- /dev/null
+++ b/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java
@@ -0,0 +1,54 @@
+/*
+ * 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 java.util.HashMap;
+import java.util.Map;
+
+/**
+ *
+ * @author pieterkoopman
+ */
+public class Overviewer {
+
+ private static Overviewer instance;
+
+ Map<Car, Integer> locations;
+
+ protected Overviewer() {
+ locations = new HashMap<>();
+ }
+
+ public static Overviewer getInstance() {
+ if (instance == null) {
+ instance = new Overviewer();
+ }
+ return instance;
+ }
+
+ public synchronized void setLocation(Car car, int location) {
+ if (locations.containsKey(car)) {
+ locations.remove(car);
+ }
+ locations.put(car, location);
+ }
+
+ 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 &&
+ that_car.getDirection() == car.getDirection() &&
+ that_car.getLocation() > requested_location &&
+ that_car.getLocation() < requested_location + Car.CARLENGTH + Car.MINCARSPACE) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+}