目前在AP计算机科学领域我们正在开展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.getOccupiedLocations();”我得到一个空指针异常,大概来自world.getOccupiedLocations()。通过实验,我已经确定世界本身是空的,虽然我不明白为什么。如果需要更多背景:
正在扩展的生物类:
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,不确定原因。
感谢你的时间,感谢任何意见。
答案 0 :(得分: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().