0

目前在 AP Computer Science 中,我们正在研究 gridworld 案例研究,特别是一个名为 AnnoyingCritter 的实验室。要求是这样的:

扩展 Critter 类以创建一个新的 AnnoyingCritter。AnnoyingCritter 会随机选择一个演员作为它永远最好的朋友(bFF)——选择的演员必须是一个会移动的演员。AnnoyingCritter 将跟随/走向它的 bFF,希望靠近它的 bFF - 它喜欢它的 bff!AnnoyingCritter 只会吃石头和鲜花,因为它不喜欢吃会移动的东西。AnnoyingCritter 将始终向其 bFF 方向移动一个单元格。如果 AnnoyingCritter 无法移动到其 BFF 方向的单元格,它将像普通小动物一样移动到其任何空的相邻单元格。

以下代码是我试图完成的。

import info.gridworld.actor.Actor;  
import info.gridworld.actor.Rock;
import info.gridworld.actor.Flower;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;
import info.gridworld.grid.Grid;
import java.awt.Color;
import java.util.Random;

import java.util.ArrayList;

public class AnnoyingCritter extends Critter
{
Random gen = new Random();
Grid<Actor> world = getGrid();
Actor bFF = pickFriend();

public Actor pickFriend() {
    ArrayList<Location> locs = world.getOccupiedLocations();
    int pick = gen.nextInt(locs.size());
    Actor temp = world.get(locs.get(pick));
    while(temp instanceof Rock || temp instanceof Flower) {
        pick = gen.nextInt(locs.size());
        temp = world.get(locs.get(pick));
    }
    return temp;
}
public void figureDirection() {
    int bFFCol = bFF.getLocation().getCol();
    int bFFRow = bFF.getLocation().getRow();
    int annoyCol = getLocation().getRow();
    int annoyRow = getLocation().getCol();
    Location next = null;
    if(bFFCol > annoyCol) {
        next = new Location(annoyRow, annoyCol+1);
    } else if(bFFCol < annoyCol) {
        next = new Location(annoyRow, annoyCol-1);
    } else if(bFFCol == annoyCol) {
        if(bFFRow > annoyRow) {
            next = new Location(annoyRow+1, annoyCol);
        } else if(bFFRow < annoyRow) {
            next = new Location(annoyRow-1, annoyCol);
        }
    }
    if(next != bFF.getLocation() || world.get(next) instanceof Rock) {
        world.get(next).removeSelfFromGrid();
        moveTo(next);
    } else {
        super.makeMove(super.selectMoveLocation(super.getMoveLocations()));
    }
}
public void act() {
    figureDirection();
}
}

问题出现在第 25 行“ArrayList locs = world.getOc​​cupiedLocations();” 我得到一个空指针异常,大概来自 world.getOc​​cupiedLocations()。通过实验,我确定世界本身是空的,尽管我不明白为什么。如果需要更多上下文:

正在扩展的小动物类:

package info.gridworld.actor;

import info.gridworld.grid.Location;

import java.util.ArrayList;

 public class Critter extends Actor
 {

public void act()
{
    if (getGrid() == null)
        return;
    ArrayList<Actor> actors = getActors();
    processActors(actors);
    ArrayList<Location> moveLocs = getMoveLocations();
    Location loc = selectMoveLocation(moveLocs);
    makeMove(loc);
}
public ArrayList<Actor> getActors()
{
    return getGrid().getNeighbors(getLocation());
}
public void processActors(ArrayList<Actor> actors)
{
    for (Actor a : actors)
    {
        if (!(a instanceof Rock) && !(a instanceof Critter))
            a.removeSelfFromGrid();
    }
}
public ArrayList<Location> getMoveLocations()
{
    return getGrid().getEmptyAdjacentLocations(getLocation());
}
public Location selectMoveLocation(ArrayList<Location> locs)
{
    int n = locs.size();
    if (n == 0)
        return getLocation();
    int r = (int) (Math.random() * n);
    return locs.get(r);
}
public void makeMove(Location loc)
{
    if (loc == null)
        removeSelfFromGrid();
    else
        moveTo(loc);
}
}

最后是跑步者类:

import java.awt.Color; 
import info.gridworld.actor.Rock;
import info.gridworld.actor.Actor;
import info.gridworld.actor.Flower;
import info.gridworld.actor.Bug;
import info.gridworld.grid.Location;
import info.gridworld.grid.BoundedGrid;
import info.gridworld.actor.ActorWorld;

public class APlusCritterRunner
{
public static void main(String[] args) 
{
    ActorWorld world = new ActorWorld(new BoundedGrid<Actor>(8,8));
    world.add(new Location(3, 1), new Rock());
    world.add(new Location(5, 2), new Actor());
    world.add(new Location(7, 6), new Flower());
    world.add(new Location(6, 6), new Actor());
    world.add(new Location(0, 5), new Actor());
    world.add(new Location(3, 5), new Actor());
    world.add(new Location(1, 1), new AnnoyingCritter());
    world.show(); 
}
}

tl;dr Grid 返回 null,不知道为什么。

任何意见表示赞赏,感谢您的时间。

4

1 回答 1

0

尝试:

public class AnnoyingCritter extends Critter
{
    Random gen = new Random();
    Grid<Actor> world = null;
    Actor bFF = null;

    public Actor pickFriend() {
        world = getGrid();
        if (world == null)
            return;
        ArrayList<Location> locs = world.getOccupiedLocations();
        int pick = gen.nextInt(locs.size());
        Actor temp = world.get(locs.get(pick));
        while(temp instanceof Rock || temp instanceof Flower) {
            pick = gen.nextInt(locs.size());
            temp = world.get(locs.get(pick));
        }
        return temp;
    }

    public void figureDirection() {
        bFF = pickFriend();
        ...
        if(world.get(next) != null && (next != bFF.getLocation() || world.get(next) instanceof Rock)) {
            world.get(next).removeSelfFromGrid();
            moveTo(next);
        }
        ...
    }

    public void act() {
        figureDirection();
    }

我认为您的问题是您world在将 AnnoyingCritter 放入网格之前声明和初始化。world.add(new Location(1, 1), new AnnoyingCritter());首先必须创建一个新位置和一个新的 AnnoyingCritter ,然后 将其添加到网格中。因此,当 AnnoyingCritter 首次创建时,它不在网格中。

您还必须将呼叫pickFriends()从您的字段移至 figureDirection(). 这种方式world可以被初始化,并且您将避免另一个 NullPointerException。

还有一个NullPointerException当你说world.get(next).removeSelfFromGrid(); 如果next不包含你不能调用的演员.removeSelfFromGrid().

于 2014-03-28T15:30:05.783 回答