0

現在 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.getOc​​cupiedLocations();」で発生します。おそらくworld.getOc​​cupiedLocations()から、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 を返します。理由は不明です。

ご意見をお待ちしております。お時間をいただきありがとうございます。

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());最初に新しい Location と新しい AnnoyingCritter を作成し、それ をグリッドに追加する必要があります。そのため、AnnoyingCritter が最初に作成されたとき、グリッドにはありません。

pickFriends()また、フィールドから へ の呼び出しを の先頭に移動する必要がありますfigureDirection()。その方法worldを初期化することができ、別の NullPointerException を回避できます。

また、 Ifに Actor が含まれていないと呼び出すことができないというNullPointerException場合もあります。world.get(next).removeSelfFromGrid();next.removeSelfFromGrid().

于 2014-03-28T15:30:05.783 に答える