Skip to content

Gridworld Case Study Solutions Manual

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.

QuickCrab

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) locs.add(loc); } } }

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

One of the four free response problems on each AP Computer Science Exam references the GridWorld Case Study. Previous problems have required students to interact with the Grid interface and the Location class in complex ways. This problem is intended as practice with commonly tested topics.

Each part of this problem is representative of a typical exam problem; however, this problem includes more parts than are typical of a real exam problem. Do not use this problem to check your timing.

Problem description

This question involves reasoning about the GridWorld case study. Reference materials are provided in the Quick Reference.

In this question, you will write four methods of the GridTools class that will process a BoundedGrid<Actor> object.

The declaration of the GridTools class is shown below.

public class GridTools { /** The grid; guaranteed never to be null */ private BoundedGrid<Actor> gr; /** @return a list of all Color.BLUE actors that are neighbors of loc *  @param loc a valid location in the grid gr */ public List<Actor> blueNeighbors(Location loc) { /* to be implemented in part (a) */ } /** @return a list of all actors in the grid gr with at least *  one Color.BLUE neighbor */ public List<Actor> actorsWithBlueNeighbors() { /* to be implemented in part (b) */ } /** @return an Actor in the grid gr with the most Color.BLUE neighbors; *  null if no Actor with at least one Color.BLUE neighbor */ public Actor actorWithMostBlueNeighbors() { /* to be implemented in part (c) */ } /** @return a list of all empty locations in grid gr within *  spots rows and spots columns of center *  @param center a valid location in the grid gr *  @param spots a positive number */ public List<Location> getEmptyWithinSpots(Location center, int spots) { /* to be implemented in part (d) */ } // There may be variables, constructors, and methods that are not shown. }

Part (a)

The method blueNeighbors returns a list containing all Color.BLUE Actor objects that are neighbors of the parameter loc. The object references in the returned list may appear in any order. If there are no Color.BLUE Actor objects neighboring loc, the method returns an empty list.

Complete method blueNeighbors below.

/** @return a list of all Color.BLUE actors that are neighbors of loc *  @param loc a valid location in the grid gr */ public List<Actor> blueNeighbors(Location loc)

Part (b)

The method actorsWithBlueNeighbors returns a list containing all Actor objects in the grid gr that have at least one Color.BLUE neighbor. The object references in the returned list may appear in any order. If no Actor has at least one Color.BLUE neighbor, the method returns an empty list.

Assume that blueNeighbors works as specified, regardless of what you wrote in part (a).

Complete method actorsWithBlueNeighbors below.

/** @return a list of all actors in the grid gr with at least *  one Color.BLUE neighbor */ public List<Actor> actorsWithBlueNeighbors()

Part (c)

The method actorWithMostBlueNeighbors returns an Actor from the grid gr that has the most Color.BLUE neighbors. If there are multiple actors that have the maximum number of blue neighbors, any of them may be returned. If no Actor has at least one Color.BLUE neighbor, the method returns null.

Assume that blueNeighbors and actorsWithBlueNeighbors work correctly regardless of what you wrote in parts (a) & (b).

Complete method actorWithMostBlueNeighbors below.

/** @return an Actor in the grid gr with the most Color.BLUE neighbors; *  null if no Actor with at least one Color.BLUE neighbor */ public Actor actorWithMostBlueNeighbors()

Part (d)

The method getEmptyWithinSpots returns a list containing all empty locations in grid gr within spots rows and spots columns of center. If there are no such locations, the method returns an empty list. The object references in the returned list may appear in any order. The returned list does not include center, even if it is empty.

Complete method getEmptyWithinSpots below.

/** @return a list of all empty locations in grid gr within *  spots rows and spots columns of center *  @param center a valid location in the grid gr *  @param spots a positive number */ public List<Location> getEmptyWithinSpots(Location center, int spots)

See the GridTools solution or review it with AP CS Tutor Brandon Horn.