Note: GridWorld is no longer featured on the AP CS A Exam (since the 2015 Exam).

One of the four free response problems on each AP Computer Science Exam referenced the GridWorld Case Study. Previous problems required students to interact with the Grid interface and the Location class in complex ways.

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) blueNeighbors method

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.

/**
 * @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) actorsWithBlueNeighbors method

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.

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

Part (c) actorWithMostBlueNeighbors method

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 part (a) & part (b).

Complete method actorWithMostBlueNeighbors.

/**
 * @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) getEmptyWithinSpots method

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.

/**
 * @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.