Gridworld计划中的位置错误?

时间:2014-04-11 03:57:54

标签: location gridworld

在我的网格世界计划中,我有一个像Bug一样的公民,以及一个将公民变成受害者的罪犯。我的警察演员虽然没有完成,但目前正在帮助受害者。但是,在有界网格中,它无法识别下一个位置无效。这是我的代码。

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

1 个答案:

答案 0 :(得分:0)

尝试在if语句后移动Actor neighbor = gr.get(next);

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)。没有理由创建变量而不使用它。

相关问题