Reputation: 1
I'm in APCS and I have to make an extension of the critter class that will make a critter randomly select any actor in the grid and then move to its location; thereby "following" it. My teacher gave us beginning class and method names to start with that cannot be altered. Here is the runner which cannot be changed.
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();
}
}
The AnnoyingCritter class must contain all the methods required for the critter to find its "Best Friend" and "follow it".
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
}
}
The AnnoyingCritter must move to the location of the actor it randomly selects and eat the flowers and rocks that are in the way. It also needs to move to the location of the actor one space at a time. I cannot figure out how to get the annoying critter to find where the actors are and then randomly find one. The imports cannot be altered, either.
Please help as I have been stuck on this for hours.
Upvotes: 0
Views: 2937
Reputation: 82
The Location class has some powerful methods. Try this:
int direction=getLocation().getDirectionToward(BFF.getLocation);
moveTo(getLocation().getAdjacentLocation(direction));
This should move you towards the BFF one space at a time.
To find a BFF:
Grid<Actor> grid=getGrid();
ArrayList<Location> locList=grid.getOccupiedLocations();
BFF=grid.get(locList.get(0));
Just make sure that locList(0) is not your own critter.
Try reading the Quick Reference guide, too.
Upvotes: 0
Reputation: 23
I'm pretty sure you have to use the getLocation() to find where the actor is. Or... you can use a loop and pick random number for columns and rows, until you find an actor. Shoot, that sounds like a good idea. I'm going to try it.
Upvotes: 0