我必须用Java制作一个模拟器,它将模拟在高速公路上行驶的汽车。高速公路上应该有3条车道,每条车道都有匀速行驶的汽车。在这条高速公路上,有一个特工,他必须开车通过,不能撞到任何其他汽车。详细说明见本文第 2.5 节和图 5。
这张图片来自上述论文,显示了高速公路的外观:
我的目标是只编写一个模拟器(和 GUI),而不是代理的逻辑。现在,我想设计这个模拟器的架构,这就是我需要帮助的地方。
我的想法是,代理的 API 的外观是:
public abstract class BaseAgent {
public abstract void run()
public abstract void onCrash();
}
高速公路上的代理(汽车)应该是这个类的后代。在每一步中,模拟器调用函数run()
,代理逻辑在哪里。在这个函数中,代理可以调用如下函数:
goLeft();
goRight();
getNearestCarInLane(int lane_no);
getMySpeed();
因此,在每一步中,代理都可以决定他是否留在当前车道,或者他是左转还是右转。仅此而已,代理可以做什么。
所以这是代理API,但我不知道如何设计模拟器的其余部分。我第一次尝试模拟架构是:
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.
这不是一个好的架构。方法应该在哪个类中goLeft()
,goRight()
以及getNearestCarInLane()
?因为这些方法必须在BaseAgent
课堂内,但必须知道高速公路上每辆车的位置。所以最后,我有这样的事情:
Simulator s = new Simulator();
Highway h = new Highway();
Agent a = new Agent();
s.setAgent(a);
s.setHighway(h);
a.setHighway(h);
h.setAgent(a);
这是可怕和丑陋的。
所以我需要一些聪明人的帮助。有人可以给我一个关于模拟器/架构的书籍、文章的链接吗?或者解释我做错了什么?
我不是程序员,这个项目是我所在学院的一门名为Software Engineering的选修课程的一部分。