Other 2011 FR Questions FR other years Be Prepared Home
A-2
public class AttractiveCritter extends Critter
{
  public ArrayList<Actor> getActors()
  {
    Grid<Actor> gr = getGrid();
    ArrayList<Location> locs = gr.getOccupiedLocations();
    ArrayList<Actor> actors = new ArrayList<Actor>();

    for (Location loc : locs)
    {
      Actor a = gr.get(loc);
      if (a != this)
        actors.add(a);
    } 1

    return actors;
  }

  public void processActors(ArrayList<Actor> actors)
  {
    Grid<Actor> gr = getGrid();
    for (Actor a : actors)
    {
      Location loc = a.getLocation();
      int dir = loc.getDirectionToward(getLocation());
      Location newLoc = loc.getAdjacentLocation(dir);
      if (gr.isValid(newLoc) && gr.get(newLoc) == null)
        a.moveTo(newLoc);
    }
  }
}
Notes:
  1. Or:
        for (Location loc : locs)
          actors.add(gr.get(loc));
        actors.remove(this);
    
    While List's remove(Object obj) method is not in the AP subset, you can use it if you do it correctly.

Other 2011 FR Questions | Back to Contents

Copyright © 2011 by Skylight Publishing
support@skylit.com