I need to make a Java simulator that will simulate driving a car on a highway. There should be 3 lanes on the highway, in each lane - cars with a constant speed. There is one agent on this highway who must drive and not crash into any other car. A detailed description can be found in this article in section 2.5 and figure 5.
This image is from the mentioned article and shows the view of the highway:
My goal is to write only a simulator (and GUI), not an agent logic. Now I would like to design the architecture of this simulator, and here I need help.
My idea is what the agent API looks like:
public abstract class BaseAgent { public abstract void run() public abstract void onCrash(); }
The agent (car) on the highway must be a descendant of this class. At each step, the function of calling the simulator
run()
, where the agent logic is located. In this function, an agent can call functions such as:
goLeft(); goRight(); getNearestCarInLane(int lane_no); getMySpeed();
So, at each step, the agent can decide whether he will remain in the current lane, or if he turns left or right. And all the agent can do.
So these are API agents, but I donβt know how to develop other simulators. My first attempt at modeling a simulator 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. What class should goLeft()
, goRight()
and getNearestCarInLane()
methods be? Because these methods must be inside the BaseAgent
class, but must know the position of each car on the highway. So in 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's awful and ugly.
So I need a little help from smart people here. Can someone give me a link to a book, article, regardless of simulators / architecture? Or explain what I'm doing wrong?
I am not a programmer, and this project is part of an additional course at my faculty of software development.