aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCamil Staps2015-05-28 23:50:19 +0200
committerCamil Staps2015-05-28 23:50:19 +0200
commita8281523e2bbee4a1b4688f175896fe9c6b30c55 (patch)
treef519960e5c48f215edf8a4f1a75917200f329eb4
parentStart w14 (diff)
This does seem to work a bit... kindof...
-rw-r--r--Week14 Route 66/src/OO14route66/Car.java27
-rw-r--r--Week14 Route 66/src/OO14route66/Controller.java8
-rw-r--r--Week14 Route 66/src/OO14route66/Model.java13
-rw-r--r--Week14 Route 66/src/OO14route66/Regelaar.java40
-rw-r--r--Week14 Route 66/src/com/camilstaps/route66/Driver.java48
5 files changed, 125 insertions, 11 deletions
diff --git a/Week14 Route 66/src/OO14route66/Car.java b/Week14 Route 66/src/OO14route66/Car.java
index 4448a27..6ebb863 100644
--- a/Week14 Route 66/src/OO14route66/Car.java
+++ b/Week14 Route 66/src/OO14route66/Car.java
@@ -1,5 +1,6 @@
package OO14route66;
+import com.camilstaps.route66.Driver;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;
@@ -26,25 +27,47 @@ public class Car
private int location; // current location on the road
private final Color color; // color of this car
private final Random random; // here to ensure that every car gets a new
+ private final Driver driver;
+ private final Model model;
/**
* The constructor
* @param number of the car
*/
- public Car(int number) {
+ public Car(int number, Model model) {
this.number = number;
random = new Random();
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];
+ driver = new Driver(this);
+ this.model = model;
+ }
+
+ public Driver getDriver() {
+ return driver;
+ }
+
+ public Direction getDirection() {
+ return direction;
+ }
+
+ public int getNumber() {
+ return number;
+ }
+
+ public int getNewLocation() {
+ return (location + speed) % RoadView.WINDOWSIZE;
}
/**
* move this car one step
*/
public void step() {
- location = (location + speed) % RoadView.WINDOWSIZE;
+ location = getNewLocation();
+ Regelaar.getInstance().setLocation(this, location);
+ model.update();
}
/**
diff --git a/Week14 Route 66/src/OO14route66/Controller.java b/Week14 Route 66/src/OO14route66/Controller.java
index c1542b9..3b684dc 100644
--- a/Week14 Route 66/src/OO14route66/Controller.java
+++ b/Week14 Route 66/src/OO14route66/Controller.java
@@ -1,6 +1,8 @@
package OO14route66;
import java.util.Random;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
/**
* OO1route66 initial class
@@ -31,9 +33,9 @@ public class Controller
* sleep some time
*/
public void run() {
- while (true) {
- stepAllCars();
- pause();
+ ExecutorService service = Executors.newCachedThreadPool();
+ for (Car car : model.getCars()) {
+ service.execute(car.getDriver());
}
}
diff --git a/Week14 Route 66/src/OO14route66/Model.java b/Week14 Route 66/src/OO14route66/Model.java
index 10c413f..c79aa4e 100644
--- a/Week14 Route 66/src/OO14route66/Model.java
+++ b/Week14 Route 66/src/OO14route66/Model.java
@@ -14,9 +14,8 @@ public class Model extends Observable
{
private final Car [] cars;
public static final int
-// DIRECTIONS = 4, // for a crossing
- DIRECTIONS = 2, // for a single road
- NUMBEROFCARS = 5 * DIRECTIONS; // total number of cars in system
+ DIRECTIONS = 2,
+ NUMBEROFCARS = 5 * DIRECTIONS; // total number of cars in system
private final ArrayList<JFrame> views;
@@ -25,10 +24,10 @@ public class Model extends Observable
* Constructor: create all cars
*/
public Model() {
- views = new ArrayList<JFrame>();
+ views = new ArrayList<>();
cars = new Car [NUMBEROFCARS];
for (int c = 0; c < NUMBEROFCARS; c += 1) {
- cars[c] = new Car(c);
+ cars[c] = new Car(c, this);
}
}
@@ -39,6 +38,10 @@ public class Model extends Observable
public void addView(JFrame view) {
views.add(view);
}
+
+ public Car[] getCars() {
+ return cars;
+ }
/**
* get a car from the model
diff --git a/Week14 Route 66/src/OO14route66/Regelaar.java b/Week14 Route 66/src/OO14route66/Regelaar.java
index 1fc336f..bf4cbfd 100644
--- a/Week14 Route 66/src/OO14route66/Regelaar.java
+++ b/Week14 Route 66/src/OO14route66/Regelaar.java
@@ -6,10 +6,48 @@
package OO14route66;
+import java.util.HashMap;
+import java.util.Map;
+
/**
*
* @author pieterkoopman
*/
-class Regelaar {
+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;
+ }
}
diff --git a/Week14 Route 66/src/com/camilstaps/route66/Driver.java b/Week14 Route 66/src/com/camilstaps/route66/Driver.java
new file mode 100644
index 0000000..f10bb3a
--- /dev/null
+++ b/Week14 Route 66/src/com/camilstaps/route66/Driver.java
@@ -0,0 +1,48 @@
+/*
+ * Copyright (c) 2015 Camil Staps
+ */
+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;
+
+/**
+ *
+ * @author camilstaps
+ */
+public class Driver implements Runnable {
+
+ private final Car car;
+
+ private final Random r;
+
+ public Driver(Car car) {
+ this.car = car;
+ r = new Random();
+ }
+
+ @Override
+ @SuppressWarnings("empty-statement")
+ public synchronized void run() {
+ while (true) {
+ while (!Regelaar.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);
+ }
+ }
+ }
+
+}