QuickCrab solution

Note: GridWorld will not be featured on the 2015 and subsequent AP CS Exams.

QuickCrab moves differently than CrabCritter; however, this does not imply that it should override makeMove. Instead, the postconditions require that QuickCrab override getMoveLocations to select a different set of move locations if possible. The requirement that QuickCrab move like CrabCritter if it cannot move as desired is met by returning the result of calling the superclass method.

Review the QuickCrab solution with AP CS Tutor Brandon Horn.


public class QuickCrab extends CrabCritter
  public ArrayList<Location> getMoveLocations()
    ArrayList<Location> locs = new ArrayList<Location>();
    this.addIfPath(this.getDirection() + Location.LEFT, locs);
    this.addIfPath(this.getDirection() + Location.RIGHT, locs);
    if(locs.size() > 0)
      return locs;
    return super.getMoveLocations();
   * Adds the location 2 spaces from this QuickCrab in the specified direction to
   * locs if and only if it and the intervening location are valid and empty. 
  private void addIfPath(int direction, ArrayList<Location> locs)
    Grid<Actor> gr = this.getGrid();
    Location loc = this.getLocation().getAdjacentLocation(direction);
    if(gr.isValid(loc) && gr.get(loc) == null)
      loc = loc.getAdjacentLocation(direction);
      if(gr.isValid(loc) && gr.get(loc) == null)

Get AP CS Help

AP CS GridWorld Case Study Solutions

Recommended Practice Problems