Java APCS Gridworld-烦扰生物空指针
目前,在美联社《计算机科学》杂志上,我们正在进行gridworld案例研究,特别是一个名为“烦扰生物”的实验室。要求如下: 扩展Critter类以生成新的烦扰生物。烦人的小动物会随机挑选一个演员作为它永远最好的朋友(bFF)——被选中的演员必须是一个会移动的演员。烦人的小动物会跟随/移动到它的bFF,希望靠近它的bFF——它喜欢它的bFF!烦人的小动物只吃石头和花,因为它不喜欢吃可以移动的东西。烦人的生物总是沿着它的bFF方向移动一个细胞。如果烦扰生物不能沿着其BFF的方向移动到该细胞,它会像正常生物一样移动到任何空的相邻细胞 下面的代码是我试图完成的Java APCS Gridworld-烦扰生物空指针,java,nullpointerexception,gridworld,Java,Nullpointerexception,Gridworld,目前,在美联社《计算机科学》杂志上,我们正在进行gridworld案例研究,特别是一个名为“烦扰生物”的实验室。要求如下: 扩展Critter类以生成新的烦扰生物。烦人的小动物会随机挑选一个演员作为它永远最好的朋友(bFF)——被选中的演员必须是一个会移动的演员。烦人的小动物会跟随/移动到它的bFF,希望靠近它的bFF——它喜欢它的bFF!烦人的小动物只吃石头和花,因为它不喜欢吃可以移动的东西。烦人的生物总是沿着它的bFF方向移动一个细胞。如果烦扰生物不能沿着其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();
}
}
import info.gridworld.actor.actor;
导入info.gridworld.actor.Rock;
导入info.gridworld.actor.Flower;
导入info.gridworld.actor.Critter;
导入info.gridworld.grid.Location;
导入info.gridworld.grid.grid;
导入java.awt.Color;
导入java.util.Random;
导入java.util.ArrayList;
公共类骚扰生物扩展生物
{
Random gen=新的Random();
网格世界=getGrid();
Actor bFF=pickFriend();
公共演员pickFriend(){
ArrayList locs=world.getOccuppiedLocations();
int pick=gen.nextInt(locs.size());
Actor temp=world.get(locs.get(pick));
while(岩石的临时实例| |花的临时实例){
pick=gen.nextInt(locs.size());
temp=世界获取(本地获取(拾取));
}
返回温度;
}
public void figureDirection(){
int bFFCol=bFF.getLocation().getCol();
int bFFRow=bFF.getLocation().getRow();
int=getLocation().getRow();
int row=getLocation().getCol();
位置next=null;
如果(bFFCol>COL){
下一步=新位置(行、列+1);
}否则如果(bFFCol Row){
下一步=新位置(行+1,列);
}否则如果(bFFRow|
问题出现在第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);
}
}
package info.gridworld.actor;
导入info.gridworld.grid.Location;
导入java.util.ArrayList;
公共类生物扩展演员
{
公共无效法
{
如果(getGrid()==null)
返回;
ArrayList actors=getActors();
过程参与者(参与者);
ArrayList moveLocs=getMoveLocations();
位置定位=选择移动位置(移动定位);
makeMove(loc);
}
公共数组列表getActors()
{
返回getGrid().getNeights(getLocation());
}
public void processActors(ArrayList actors)
{
(演员a:演员)
{
如果(!(岩石的实例)和(!(生物的实例))
a、 removeSelfFromGrid();
}
}
公共阵列列表getMoveLocations()
{
返回getGrid().getEmptyAjacentLocations(getLocation());
}
公共位置选择移动位置(ArrayList locs)
{
int n=位置大小();
如果(n==0)
返回getLocation();
int r=(int)(Math.random()*n);
返回locs.get(r);
}
公共空间移动(位置loc)
{
如果(loc==null)
removeSelfFromGrid();
其他的
移动到(loc);
}
}
最后是runner类:
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();
}
}
导入java.awt.Color;
导入info.gridworld.actor.Rock;
导入info.gridworld.actor.actor;
导入info.gridworld.actor.Flower;
导入info.gridworld.actor.Bug;
导入info.gridworld.grid.Location;
导入info.gridworld.grid.BoundedGrid;
导入info.gridworld.actor.ActorWorld;
公共级APlusCritterRunner
{
公共静态void main(字符串[]args)
{
ActorWorld=新ActorWorld(新边界网格(8,8));
添加(新位置(3,1),新岩石();
添加(新位置(5,2),新角色();
添加(新位置(7,6),新花());
添加(新位置(6,6),新角色();
添加(新位置(0,5),新角色());
添加(新位置(3,5),新角色();
添加(新位置(1,1),新烦扰生物();
world.show();
}
}
tl;dr Grid返回null,不确定原因
非常感谢您的宝贵时间。请尝试:
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();
}
公共类烦扰生物扩展生物
{
Random gen=新的Random();
网格世界=空;
参与者bFF=null;
公共演员pickFriend(){
world=getGrid();
if(world==null)
返回;
ArrayList locs=world.getOccuppiedLocations();
int pick=gen.nextInt(locs.size());
Actor temp=world.get(locs.get(pick));
while(岩石的临时实例| |花的临时实例){
pick=gen.nextInt(locs.size());
temp=世界获取(本地获取(拾取));
}
返回温度;
}
public void figureDirection(){
bFF=pickFriend();
...
if(world.get(next)!=null&(next!=bFF.getLocation()| | world.get(next)instanceof Rock)){
get(next.removeSelfFromGrid();
移动到(下一个);
}
...
}
公共无效法{
图方向();
}
我认为您的问题在于您在
骚扰生物被放入网格。