2009 Stockpile Critter
<< 2009NumberCube | APQuestionsTrailIndex | 2009BatteryCharger >>
I made a 48 x 48 gif file which can be downloaded and placed in the same folder as your class files.

StockpileCritter.java
import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.util.ArrayList;
/**
* @author Chris Thiel
* @version 11 May 2009
*/
public class StockpileCritter extends Critter
{
private int stockpile;
public StockpileCritter()
{
stockpile=0;
}
/**
* getStockpile returns the number in
* the stockpile and is not necesary
* for a solution, but is here to
* see if your solution is working
* properly
*/
public int getStocpile()
{
return stockpile;
}
public void processActors(ArrayList<Actor> actors)
{
stockpile+=actors.size();
for (Actor a : actors) a.removeSelfFromGrid();
}
public void makeMove(Location loc)
{
stockpile--;
if (stockpile == 0 || loc==null)
removeSelfFromGrid();
else moveTo(loc);
}
}
StockpileCritterRunner
import info.gridworld.actor.*;
import info.gridworld.grid.*;
import java.awt.Color;
/**
* This class runs a world that contains critters.
* This class is not tested on the AP CS A and AB exams.
*/
public class StockPileCritterRunner
{
public static void main(String[] args)
{
ActorWorld world = new ActorWorld();
world.add(new Location(0, 1), new Rock());
world.add(new Location(1, 3), new Rock());
world.add(new Location(2, 1), new Flower(Color.BLUE));
world.add(new Location(2, 2), new StockpileCritter());
world.add(new Location(2, 3), new Rock());
world.add(new Location(5, 3), new Flower(Color.RED));
world.show();
}
}
