From a6183f57e9d432feae273c0b294cf90d3e97d835 Mon Sep 17 00:00:00 2001 From: Camil Staps Date: Fri, 29 May 2015 08:16:55 +0200 Subject: comments; cleanup --- Week14 Route 66/src/OO14route66/Car.java | 77 ++++++++++++++-------- Week14 Route 66/src/OO14route66/Controller.java | 56 ++++++---------- Week14 Route 66/src/OO14route66/KeyHandler.java | 2 - Week14 Route 66/src/OO14route66/Regelaar.java | 53 --------------- .../src/com/camilstaps/route66/Driver.java | 41 +++++++----- .../src/com/camilstaps/route66/Overviewer.java | 54 +++++++++++++++ 6 files changed, 148 insertions(+), 135 deletions(-) delete mode 100644 Week14 Route 66/src/OO14route66/Regelaar.java create mode 100644 Week14 Route 66/src/com/camilstaps/route66/Overviewer.java (limited to 'Week14 Route 66') 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 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 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; - } - -} 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 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 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; + } + +} -- cgit v1.2.3