aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src/com/camilstaps/route66/Driver.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/com/camilstaps/route66/Driver.java
parentHack to make it seem to be working (diff)
licensing; reorganisation; javadoc; cleanup
Diffstat (limited to 'Week14 Route 66/src/com/camilstaps/route66/Driver.java')
-rw-r--r--Week14 Route 66/src/com/camilstaps/route66/Driver.java67
1 files changed, 49 insertions, 18 deletions
diff --git a/Week14 Route 66/src/com/camilstaps/route66/Driver.java b/Week14 Route 66/src/com/camilstaps/route66/Driver.java
index 4a323cd..ff603c8 100644
--- a/Week14 Route 66/src/com/camilstaps/route66/Driver.java
+++ b/Week14 Route 66/src/com/camilstaps/route66/Driver.java
@@ -1,14 +1,33 @@
/*
- * Copyright (c) 2015 Camil Staps
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2015 Camil Staps <info@camilstaps.nl>
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
*/
package com.camilstaps.route66;
-import OO14route66.Car;
-import OO14route66.Model;
import java.util.Random;
/**
* A Driver takes care its car doesn't collide
+ *
* @author Camil Staps
*/
public class Driver implements Runnable {
@@ -18,7 +37,7 @@ public class Driver implements Runnable {
private final Random r;
- private boolean running, waitingToCross = false;
+ private boolean running = false;
private int delay;
public Driver(Car car, Model model) {
@@ -28,19 +47,22 @@ public class Driver implements Runnable {
}
/**
- * Set running to false to disable driving altogether
- * @param value
+ * Stop running
*/
- public void setRunning(boolean value) {
- running = value;
- }
-
- public void setWaitingToCross(boolean value) {
- waitingToCross = value;
+ public void stopRunning() {
+ running = false;
}
- public boolean getWaitingToCross() {
- return waitingToCross;
+ /**
+ * Resume running
+ */
+ public void resumeRunning() {
+ if (running == false) {
+ synchronized (this) {
+ running = true;
+ notifyAll();
+ }
+ }
}
/**
@@ -53,11 +75,20 @@ public class Driver implements Runnable {
}
@Override
- @SuppressWarnings("empty-statement")
- public void run() {
+ /**
+ * Forever wait for safety, step and pause for a pseudorandom time.
+ */
+ public synchronized void run() {
running = true;
while (true) {
- while (!running || !model.isSafeLocation(car, car.getNewLocation()));
+ do {
+ while (!running) {
+ try {
+ wait();
+ } catch (InterruptedException ex) {
+ }
+ }
+ } while (!model.isSafeLocation(car, car.getNewLocation()));
car.step();
@@ -74,7 +105,7 @@ public class Driver implements Runnable {
if (Math.abs((System.currentTimeMillis() / 1000) % Model.NUMBEROFCARS - car.getNumber()) < 2) {
bonus = -50;
}
- Thread.sleep(Math.abs(r.nextInt(50) + delay + bonus));
+ Thread.sleep(Math.abs(r.nextInt(100) + delay + bonus));
} catch (InterruptedException ex) {}
}