aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66
diff options
context:
space:
mode:
Diffstat (limited to 'Week14 Route 66')
-rw-r--r--Week14 Route 66/src/OO14route66/Car.java77
-rw-r--r--Week14 Route 66/src/OO14route66/Controller.java56
-rw-r--r--Week14 Route 66/src/OO14route66/KeyHandler.java2
-rw-r--r--Week14 Route 66/src/com/camilstaps/route66/Driver.java41
-rw-r--r--Week14 Route 66/src/com/camilstaps/route66/Overviewer.java (renamed from Week14 Route 66/src/OO14route66/Regelaar.java)13
5 files changed, 101 insertions, 88 deletions
diff --git a/Week14 Route 66/src/OO14route66/Car.java b/Week14 Route 66/src/OO14route66/Car.java
index 6ebb863..0058d8d 100644
--- a/Week14 Route 66/src/OO14route66/Car.java
+++ b/Week14 Route 66/src/OO14route66/Car.java
@@ -1,38 +1,43 @@
package OO14route66;
import com.camilstaps.route66.Driver;
+import com.camilstaps.route66.Overviewer;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
/**
- * OO1route66 initial class
- * @author Pieter Koopman
- *
* Class for an individual car
+ *
+ * @author Pieter Koopman, Camil Staps
*/
-public class Car
-{
+public class Car {
public static final int
- CARWIDTH = 20,
- CARLENGTH = 40,
- MINCARSPACE = 5,
- MINCARSPEED = 2,
- MAXCARSPEED = 4;
- public static final Color[] carColors={Color.red, Color.blue, Color.black, Color.gray, Color.green,
+ CARWIDTH = 20,
+ CARLENGTH = 40,
+ MINCARSPACE = 5,
+ MINCARSPEED = 2,
+ MAXCARSPEED = 4;
+
+ public static final Color[] carColours = {Color.red, Color.blue, Color.black, Color.gray, Color.green,
Color.pink, Color.orange, Color.magenta, Color.lightGray};
private final int speed, number; // speed and car-number
private final Direction direction; // driving direction, used by paint
private int location; // current location on the road
- private final Color color; // color of this car
+ private final Color colour; // colour of this car
private final Random random; // here to ensure that every car gets a new
- private final Driver driver;
- private final Model model;
+ private final Driver driver; // this car's driver
+ private final Model model; // the model the car is in
/**
- * The constructor
+ * The constructor:
+ * - choose a random speed
+ * - set right location, direction and colour
+ * - create new Driver object
+ *
* @param number of the car
+ * @param model the model the car is in
*/
public Car(int number, Model model) {
this.number = number;
@@ -40,38 +45,62 @@ public class Car
speed = MINCARSPEED + random.nextInt(MAXCARSPEED - MINCARSPEED + 1);
direction = Direction.intToDirection(number);
location = RoadView.WINDOWSIZE - 2 - (number/Model.DIRECTIONS) * (CARLENGTH + MINCARSPACE);
- color = carColors[number % carColors.length];
+ colour = carColours[number % carColours.length];
driver = new Driver(this);
this.model = model;
}
+ /**
+ * Get the driver
+ * @return
+ */
public Driver getDriver() {
return driver;
}
+ /**
+ * Get the direction
+ * @return
+ */
public Direction getDirection() {
return direction;
}
+ /**
+ * Get the number of this car
+ * @return
+ */
public int getNumber() {
return number;
}
+
+ /**
+ * Get the current location of this car
+ * @return
+ */
+ public int getLocation() {
+ return location;
+ }
+ /**
+ * Get the location the car would move to if it would move
+ * @return
+ */
public int getNewLocation() {
return (location + speed) % RoadView.WINDOWSIZE;
}
/**
- * move this car one step
+ * Move the car one step
*/
public void step() {
location = getNewLocation();
- Regelaar.getInstance().setLocation(this, location);
+ Overviewer.getInstance().setLocation(this, location);
model.update();
}
/**
- * paint this car
+ * Paint this car
* @param g
*/
public void paint(Graphics g) {
@@ -104,17 +133,9 @@ public class Car
default:
x = y = w = h = 10;
}
- g.setColor(color);
+ g.setColor(colour);
g.fillRect(x, y, w, h);
g.setColor(Color.WHITE);
g.drawString(String.valueOf(number), x + w / 6, y + h / 2 + 4);
}
-
- /**
- * yield the current location of this car
- * @return
- */
- public int getLocation() {
- return location;
- }
}
diff --git a/Week14 Route 66/src/OO14route66/Controller.java b/Week14 Route 66/src/OO14route66/Controller.java
index 3b684dc..83e6098 100644
--- a/Week14 Route 66/src/OO14route66/Controller.java
+++ b/Week14 Route 66/src/OO14route66/Controller.java
@@ -1,21 +1,20 @@
package OO14route66;
+import com.camilstaps.route66.Driver;
import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
- * OO1route66 initial class
- * @author Pieter Koopman
+ * Route66 Controller
+ * @author Pieter Koopman, Camil Staps
*
* The initial controller runs as a single thread
*/
-public class Controller
-{
- private int delay = 200; // average sleep time
+public class Controller {
+ private int delay = 120; // average sleep time
private final Model model; // the model
private final Random random; // a random generator
- private boolean run = true; // car can run in simulation
/**
* The constructor of the controller
@@ -27,53 +26,33 @@ public class Controller
}
/**
- * the run method from Thread
- * forever:
- * move all cars
- * sleep some time
+ * Tell the drivers of all cars in the model to start driving
*/
public void run() {
ExecutorService service = Executors.newCachedThreadPool();
for (Car car : model.getCars()) {
- service.execute(car.getDriver());
+ Driver d = car.getDriver();
+ d.setDelay(delay);
+ service.execute(d);
}
}
/**
- * wait some pseudo random time
- */
- private void pause() {
- try { // sleep can throw an exception
- Thread.sleep(delay / 2 + random.nextInt(delay));
- }
- catch (InterruptedException e) { // catch the exception thrown by sleep
- System.out.println("An exception in Controller: " + e);
- }
- }
-
- /**
- * make one step with all cars and repaint views.
- */
- public void stepAllCars() {
- if (run) {
- for (int c = 0; c < Model.NUMBEROFCARS; c += 1) {
- model.getCar(c).step();
- }
- }
- model.update(); // update only after all cars have stepped
- }
- /**
* stop all cars by setting boolean run to false
*/
public void stopCars() {
- run = false;
+ for (Car car : model.getCars()) {
+ car.getDriver().setRunning(false);
+ }
}
/**
* start all cars by setting boolean run to true
*/
public void resumeCars() {
- run = true;
+ for (Car car : model.getCars()) {
+ car.getDriver().setRunning(true);
+ }
}
public int getDelay() {
@@ -85,6 +64,9 @@ public class Controller
* @param d
*/
public void setDelay(int d) {
- delay = Math.max(50, Math.min (2000, d));
+ delay = Math.max(20, Math.min (2000, d));
+ for (Car car : model.getCars()) {
+ car.getDriver().setDelay(delay);
+ }
}
}
diff --git a/Week14 Route 66/src/OO14route66/KeyHandler.java b/Week14 Route 66/src/OO14route66/KeyHandler.java
index d446d48..ab78676 100644
--- a/Week14 Route 66/src/OO14route66/KeyHandler.java
+++ b/Week14 Route 66/src/OO14route66/KeyHandler.java
@@ -19,7 +19,6 @@ public class KeyHandler extends KeyAdapter {
/**
* on key down 'q' stop program
* on key down 's' stop the cars
- * on key down ' ' all cars one step
* on key down '<' decrease delay
* on key down '>' increase delay
* on any other key activate the cars
@@ -30,7 +29,6 @@ public class KeyHandler extends KeyAdapter {
switch (e.getKeyChar()) {
case 'q': System.exit(0);
case 's': controller.stopCars(); break;
- case ' ': controller.stepAllCars(); break;
case '<': controller.setDelay(controller.getDelay() - 50); break;
case '>': controller.setDelay(controller.getDelay() + 50); break;
default : controller.resumeCars();
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/OO14route66/Regelaar.java b/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java
index bf4cbfd..762ded1 100644
--- a/Week14 Route 66/src/OO14route66/Regelaar.java
+++ b/Week14 Route 66/src/com/camilstaps/route66/Overviewer.java
@@ -4,8 +4,9 @@
* and open the template in the editor.
*/
-package OO14route66;
+package com.camilstaps.route66;
+import OO14route66.Car;
import java.util.HashMap;
import java.util.Map;
@@ -13,19 +14,19 @@ import java.util.Map;
*
* @author pieterkoopman
*/
-public class Regelaar {
+public class Overviewer {
- private static Regelaar instance;
+ private static Overviewer instance;
Map<Car, Integer> locations;
- protected Regelaar() {
+ protected Overviewer() {
locations = new HashMap<>();
}
- public static Regelaar getInstance() {
+ public static Overviewer getInstance() {
if (instance == null) {
- instance = new Regelaar();
+ instance = new Overviewer();
}
return instance;
}