aboutsummaryrefslogtreecommitdiff
path: root/Week14 Route 66/src/OO14route66
diff options
context:
space:
mode:
Diffstat (limited to 'Week14 Route 66/src/OO14route66')
-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/OO14route66/Regelaar.java53
4 files changed, 68 insertions, 120 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/OO14route66/Regelaar.java b/Week14 Route 66/src/OO14route66/Regelaar.java
deleted file mode 100644
index bf4cbfd..0000000
--- a/Week14 Route 66/src/OO14route66/Regelaar.java
+++ /dev/null
@@ -1,53 +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 OO14route66;
-
-import java.util.HashMap;
-import java.util.Map;
-
-/**
- *
- * @author pieterkoopman
- */
-public class Regelaar {
-
- private static Regelaar instance;
-
- Map<Car, Integer> locations;
-
- protected Regelaar() {
- locations = new HashMap<>();
- }
-
- public static Regelaar getInstance() {
- if (instance == null) {
- instance = new Regelaar();
- }
- 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;
- }
-
-}