I have to make a simulator in Java, which will simulate car riding on highway. There should be 3 lanes on highway, in every lane there are cars with constant speed. On this highway, there is one agent, which has to drive through and not crash into any other car. Detailed description can be found in this paper at section 2.5 and picture 5.
This image is from mentioned paper and shows the look of highway:
My goal is to write only a simulator (and GUI), not logic of agent. Now, I would like to design an architecture of this simulator and this is where I need help.
My idea, how the agent's API can look is:
public abstract class BaseAgent {
public abstract void run()
public abstract void onCrash();
}
Agent (car) on highway should be a descendant of this class. In every step, simulator call function run()
where is the agents logic. In this function, agent can call functions like:
goLeft();
goRight();
getNearestCarInLane(int lane_no);
getMySpeed();
So, in every step, agent can decide if he stay in current lane, or if he turns left or right. And that’s all, what can agent do.
So this is agents API, but I do not know, how to design the rest of simulator. My first attempt to simulator architecture was:
class Agent — descendant of BaseAgent, can ride on highway.
class Highway — stores position of all cars on highway.
class Simulator — creates instance of agent and highway; in every step, call agent’s `run()` and monitors any car crash.
This is not a good architecture. In which class should be methods goLeft()
, goRight()
and getNearestCarInLane()
? Because these methods has to be inside of BaseAgent
class, but has to know position of every car on highway. So at the end, I had something like this:
Simulator s = new Simulator();
Highway h = new Highway();
Agent a = new Agent();
s.setAgent(a);
s.setHighway(h);
a.setHighway(h);
h.setAgent(a);
And it is terrible and ugly.
So I need a little help from smarter people here. Can somebody give me a link to book, article, whatever about simulators/architecture? Or explain my what I am doing wrong?
I am not a programmer and this project is part of an optional course at my faculty named Software engineering.