在我的 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();
}