現在 AP コンピューター サイエンスでは、gridworld のケース スタディ、特に AnnoyingCritter というラボに取り組んでいます。要件は次のとおりです。
Critter クラスを拡張して、新しい AnnoyingCritter を作成します。AnnoyingCritter は無作為にアクターを選択して、永久にベスト フレンド (bFF) にします。選択されたアクターは、移動するアクターでなければなりません。AnnoyingCritter は、bFF の近くにいることを期待して、bFF をたどったり、bFF に向かって移動したりします。BFF が大好きです。AnnoyingCritter は動くものを好まないので、石と花だけを食べます。AnnoyingCritter は、常に 1 つのセルをその 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()から、nullポインター例外を取得します。理由はわかりませんが、実験を通じて、世界自体がゼロであると判断しました。さらにコンテキストが必要な場合:
拡張中のクリッター クラス:
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 を返します。理由は不明です。
ご意見をお待ちしております。お時間をいただきありがとうございます。