Other 2013 FR Questions FR other years Be Prepared Home
A-3
Part (a)
  public static ArrayList<Location> getEmptyLocations(Grid grid)
  {
    ArrayList<Location> emptyLocs = new ArrayList<Location>();

    for (int r = 0; r < grid.getNumRows(); r++)
    {
      for (int c = 0; c < grid.getNumCols(); c++)
      {
        Location loc = new Location(r, c);
        if (grid.get(loc) == null)
          emptyLocs.add(loc);
      }
    }
    return emptyLocs;
  }


Part (b)
public class JumpingCritter extends Critter
{
  public ArrayList<Location> getMoveLocations()
  {
    return GridWorldUtilities.getEmptyLocations(getGrid());
  }

  public Location selectMoveLocation(ArrayList<Location> locs)
  {
    if (locs.size() == 0)
      return null; 1
    return super.selectMoveLocation(locs);
  } 2
}
Notes:
  1. You cannot remove this JumpingCritter from the grid right here, because this would violate selectMoveLocation's precondition #2.  Leave this job to the makeMove method.
  2. Or:
      public Location selectMoveLocation(ArrayList<Location> locs)
      {
        int n = locs.size();
        if (n == 0)
          return null;
        int r = (int)(Math.random() * n);
        return locs.get(r);
      }

Other 2013 FR Questions | Back to Contents

Copyright © 2013 by Skylight Publishing
support@skylit.com