Other 2009 FR Questions FR other years Be Prepared Home
A-2
public class StockpileCritter extends Critter
{
  private int stockpile = 0; // 0 by default

  public void processActors(ArrayList<Actor> actors)
  {
    for (Actor a : actors)
    {
      a.removeSelfFromGrid();
      stockpile++;
    }
  }

  public Location selectMoveLocation(ArrayList<Location> locs) 1
  {
    if(stockpile == 0)
      return null;
    return super.selectMoveLocation(locs);
  }

  public void makeMove(Location loc) 2
  {
    super.makeMove(loc);
    if (stockpile > 0)
      stockpile--;
  }
} 3
Notes:
  1. We have no choice but to override selectMoveLocation to indicate when this StockpileCritter must be removed from the grid.  We can't remove it exlicitly in makeMove, because that would violate makeMove's postcondition getLocation() == loc.

  2. We override makeMove only to decrement stockpile.  It would be possible to decrement stockpile in processActors; then we would have to override only two methods, processActors and selectMoveLocation.

  3. The question states that stockpile "keeps track of actors." This solution assumes that it is OK for stockpile to be simply an int that keeps track of the number of actors.  The following alternative solution keeps track of actors in a list:
    public class StockpileCritter extends Critter
    {
      private ArrayList<Actor> stockpile;
    
      public StockpileCritter()
      {
        stockpile = new ArrayList<Actor>();
      }
    
      public void processActors(ArrayList<Actor> actors)
      {
        for (Actor a : actors)
        {
          a.removeSelfFromGrid();
          stockpile.add(a);
        }
      }
    
      public Location selectMoveLocation(ArrayList<Location> locs)
      {
        if(stockpile.isEmpty())
          return null;
        return super.selectMoveLocation(locs);
      }
    
      public void makeMove(Location loc)
      {
        super.makeMove(loc);
        if (!stockpile.isEmpty())
          stockpile.remove(0);  // or stockpile.remove(stockpile.size() - 1);
      }
    }

Other 2009 FR Questions | Back to Contents

Copyright © 2009 by Skylight Publishing
support@skylit.com