1

i'm not a Java-Newbie but I can't get my head around a problem occured recently.

I have to simulate a road system in Java. For sake of proper OOP, I've got a class Car and a class Street (and several others to manage the whole simulation of course^^). I already managed to simulate a jam on one road and have had no problem doing so.

OK, here comes the Question: I want to extend my simulation from one lonely street to a road system. So i thought of a class called something like "RoadSystem" which might have an array of streets and some sort of connection (I thought of 'knots') allowing cars to know where they can drive as they reach the end of the street they are driving on.

The problem is that I have no idea how to implement these knots. The car has to be able to ask the street "hey bro, I'm at the end of you, where can I drive now?" and the street should somehow know which knot has a reference to it and ask it for the streets that are also connected to this particular knot. How do I do this reference? I thought of an ID but this might get extremely slow for bigger road systems if the street has to search through the street-IDs of every knot in order to find its own ID there. Or am I missing an obvious solution to my problem?

Every help highly appreciated!

Greetings from Germany,

Ruffy

  • The ultimate answer depends on the detail you want to put into the simulation. Does it have to consider lane-directions and crossing vehicles on intersections (and the like)? Or is a intersection jsut an abstract thing to "switch" from road to road? – dognose Jun 04 '15 at 12:57
  • 1
    streets (and their intersections) can be modeled as a GRAPH http://introcs.cs.princeton.edu/java/45graph/ – Hector Jun 04 '15 at 12:59
  • look at this SO answer http://stackoverflow.com/questions/51574/good-java-graph-algorithm-library – Hector Jun 04 '15 at 13:01
  • @dognose The solution should be easily extendable to more details. – Ruffy x Nami Jun 04 '15 at 13:12

1 Answers1

0

You should have a Look at the SourceCode of LinkedList and maybe adapt this principle. A road has 2 connection points, while a intersection maybe 2 to 4?

Abstract class RoadElement{
  //abstract for simulation purpose, maybe randomized
  //calculation of next direction, etc.
}

class Road extends RoadElement{
  private RoadElement previous = null;
  private RoadElement next = null;
}

class Intersection extends RoadElement{
    private RoadElement northernConnection = null;
    private RoadElement easternConnection = null;
    private RoadElement southernConnection = null;
    private RoadElement westernConnection = null;
}

Finally, you can design your Road-Network and linking up RoadElements as requried. During the simulation you don't have to care about concrete instaces, cause they will be connected logically.

(You could later on improve this with additional RoadElements, such as "Curves" with limited velocity, person-crossings with stopping time etc.)

Example:

   List<RoadElement> RoadMap = new LinkedList<RoadElement>();
   Road r1 = new Road();
   Intersection i1 = new Intersection();
   r1.setPrevious(i1);
   i1.setNorthernConnection(r1);
   ....

Then, during the simulation, you just can do something like:

Car currentCar = getCurrentCar();
RoadElement re = currentCar.getLocation();
if (re instanceof Road){
  //can we drive "forward and backward?"
  if ((Road)re).getPrevious() != null){

  }

  if ((Road)re).getNext() != null){

  }
}else if (re instanceof Intersection){
   //check available outgoing roads
   if ((Intersection)re).getNorthernConnection() != null){

   }
   ...
}
dognose
  • 20,360
  • 9
  • 61
  • 107
  • Thanks for this answer. Helped me! :) – Ruffy x Nami Jun 04 '15 at 13:54
  • How could one implement the Car.getLocation-method? I don't need code, just the idea.. – Ruffy x Nami Jun 05 '15 at 12:55
  • Of course e road element can have a collection of cars, but I just don't get the connection from a car upwards to its street/road element... – Ruffy x Nami Jun 05 '15 at 13:37
  • @RuffyxNami The Location method would just be getter for a RoadElement. - And everytime your simulation decides to move a car, update the attribute to the latest RoadElement visited. – dognose Jun 05 '15 at 16:20
  • One more, last thing. As cars have to react to the cars in front of them and behind them, every road element should have a collection of cars in order for cars to easily find out what cars are in front/behind of them. If a car changes its road element, I have to remove exactly this car from the previous element and add it to the element the card drives to. Adding is easy but how do I remove exactly this car from a collection? I thought about iterating over the collection and checking somewhat like if (car = carToRemove), does this work? Thanks in advance. – Ruffy x Nami Jun 06 '15 at 16:44
  • @RuffyxNami this does work, ofc. Just take into account to use an `iterator`, else you can't modify the list you are iterating over. Anoter way would be to implement `equals()` and `hashcode()` on your car and simple use `list.remove(carInstance)`. https://docs.oracle.com/javase/7/docs/api/java/util/List.html#remove(java.lang.Object) (`Collection.remove` uses `equals()` to find the correct element) http://tutorials.jenkov.com/java-collections/hashcode-equals.html – dognose Jun 07 '15 at 11:09