Note: GridWorld is no longer featured on the AP CS A Exam (since the 2015 Exam).
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.
QuickCrab
class
public class QuickCrab extends CrabCritter
{
public ArrayList<Location> getMoveLocations()
{
ArrayList<Location> locs = new ArrayList<Location>();
addIfPath(getDirection() + Location.LEFT, locs);
addIfPath(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 = getGrid();
Location loc = getLocation().getAdjacentLocation(direction);
if(gr.isValid(loc) && gr.get(loc) == null)
{
loc = loc.getAdjacentLocation(direction);
if(gr.isValid(loc) && gr.get(loc) == null)
locs.add(loc);
}
}
}