Я нахожусь в APCS, и мне нужно сделать расширение класса critter, которое заставит critter случайным образом выбирать любого актера в сетке, а затем перемещаться к его местоположению; тем самым «следуя» за ним. Мой учитель дал нам начальные имена классов и методов для начала, которые нельзя изменить. Вот бегун, который нельзя изменить.
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 AnnoyingCritterRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld(new BoundedGrid<Actor>(8,8));
world.add(new Location(1, 1), new AnnoyingCritter());
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(2, 6), new Bug(Color.GREEN));
world.add(new Location(3, 5), new Actor());
world.show();
}
}
Класс AnnoyingCritter должен содержать все методы, необходимые твари, чтобы найти своего «лучшего друга» и «следовать за ним».
import info.gridworld.actor.Actor;
import info.gridworld.actor.Critter;
import info.gridworld.grid.Location;
import java.awt.Color;
import java.util.ArrayList;
public class AnnoyingCritter extends Critter
{
/* instance variables will be needed
* one for the bFF and one to set whether this Critter has a bFF (a boolean)
*/
public AnnoyingCritter()
{
/* make an AnnoyingCritter constructor that sets a color and starts with the
* boolean variable above being "false"
*/
Critter AnnoyingCritter = new Critter();
AnnoyingCritter.setColor(Color.green);
}
private void pickBFF()
{
/* you'll need the grid, occupied locations, and some randomness to pick a friend
*/
}
public void processActors( ArrayList<Actor> actors)
{
/* this can be simple or complicated. the idea is to pick a BFF if
* one is needed
*/
if( !hasFriend )
{
}
//and it eats flowers and rocks
for( Actor dude : actors )
{
}
}
public Location selectMoveLocation( ArrayList<Location> locs )
{
//you need a Grid
//you need a location
//you need to know where to go and if it's clear to move and then go there
}
}
AnnoyingCritter должен переместиться к случайному выбранному актеру и съесть цветы и камни, оказавшиеся на пути. Он также должен перемещаться к местонахождению актера по одному пространству за раз. Я не могу понять, как заставить надоедливую тварь найти актеров, а затем случайным образом найти их. Импорт также не может быть изменен.
Пожалуйста, помогите, так как я застрял на этом в течение нескольких часов.