在我的Gridworld程序中,我有一个表现得像虫子的公民,以及一个把公民变成受害者的罪犯。我的警察演员,虽然还没有完全完成,但目前正在帮助受害者。但是,在有界网格中,它无法识别下一个位置是无效的。这是我的代码。
public void act()
{
Grid<Actor> gr = getGrid();
Location loca = getLocation();
Location next = loca.getAdjacentLocation(getDirection());
Actor neighbor = gr.get(next);
if (gr.isValid(next))
{
ArrayList<Location> locs = getGrid().getOccupiedLocations();
for(Location loc: locs)
{
if (getGrid().get(loc) instanceof Victim)
{
Location prev = loc.getAdjacentLocation(getDirection()-180);
moveTo(prev);
}
else if( neighbor instanceof Victim || neighbor instanceof Citizen)
turn();
else
moveTo(next);
}
}
else
turn();
}发布于 2014-04-12 01:04:46
尝试将Actor neighbor = gr.get(next);移到if语句之后。
if (gr.isValid(next))
{
Actor neighbor = gr.get(next);
ArrayList<Location> locs = getGrid().getOccupiedLocations();
for(Location loc: locs)
...这样,在尝试从网格中获取next之前,系统会检查您的Actor位置,以确保它在网格中。
另一件事是,在第二个if语句中,您调用了getGrid().get(next),但是您已经将gr作为当前网格,因此可以只使用gr.get(next)。没有理由创建一个变量而不使用它。
https://stackoverflow.com/questions/23003205
复制相似问题