1 / 21

AP Case Study: The Magic of Grid World

AP Case Study: The Magic of Grid World. by Paul Vitale. Class Hierarchy. Location. The API of this class is testable on the AP CS A and AB exams. Simple class that holds a location. Holds Row and Column variables as well as Direction Noteworthy Methods boolean equals(Object)

bobby
Download Presentation

AP Case Study: The Magic of Grid World

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. AP Case Study:The Magic ofGrid World by Paul Vitale

  2. Class Hierarchy

  3. Location • The API of this class is testable on the AP CS A and AB exams. • Simple class that holds a location. • Holds Row and Column variables as well as Direction • Noteworthy Methods • boolean equals(Object) • Location getAdjacentLocation(int direction) • intgetCol() • intgetRow() • String toString()

  4. Location Constants • AHEAD 0 • FULL_CIRCLE 360 • HALF_CIRCLE 180 • HALF_LEFT -45 • HALF_RIGHT 45 • LEFT -90 • RIGHT 90 • NORTH 0 • NORTHEAST 45 • NORTHWEST 315 • SOUTH 180 • SOUTHEAST 225 • SOUTHWEST 135 • WEST 270 • EAST 90

  5. Java Templates • This is not tested on the AP exam. • Java templates are classes that take any data-type. • Java templates use the syntax • class Template<T> { /* class definition */ } • Type T is any type. • Java classes such as ArrayList<T> use T and cast it to an Object and it holds the data in a variable and when an implementer of the requests an index the data is cast back to type T.

  6. Grid<E> • This interface is testable on the AP CS A and AB exams. • Abstract class that defines basic functions for a Grid. • E get(Location) return E at passed Location. • ArrayList<Location> getEmptyAdjacentLocations(Location) • ArrayList<E> getNeighbors(Location) • intgetNumCol() • intgetNumRow() • ArrayList<Location> getOccupiedAdjacentLocations(Location) • ArraList<Location> getOccupiedLocations() • ArrayList<Location> getValidAdjacentLocations(Location) • booleanisValid(Location) • E put(Location, E) • E remove(Location, E)

  7. Grid<E> Functions Explained • getEmptyAdjacentLocations • Takes a Location • Returns an ArrayList of Locations • This function only returns valid Locations. • getNeighbors • Takes a Location • Returns an ArrayList of Locations • This function only returns valid Locations.

  8. Grid<E> Functions Explained • getOccupiedAdjacentLocations • Takes a Location • Returns an ArrayList of Locations • This function returns only valid Locations. • isValid • Takes a Location • Returns a boolean • This functions only checks if Location is on the Grid.

  9. Grid<E> Functions Explained • getValidAdjacentLocations • Takes a Location. • Returns an ArrayList of Locations. • This returns all nearby locations that are not out of bounds. • getOccupiedLocations • Takes nothing. • Returns an ArrayList of Locations • This returns all Locations that are occupied.

  10. AbstractGrid<E> • The implementation of this class is testable on the AP CS AB exam. • Abstract class that represents a grid for Grid World. • AbstractGrid<E> implements Grid<E>. • Same methods as Grid<E> with definition for abstract methods.

  11. BoundedGrid<E> • The implementation of this class is testable on the AP CS AB exam. • This grid is static. • This class extends AbstractGrid<E>

  12. UnboundedGrid<E> • The implementation of this class is testable on the AP CS AB exam. • This grid is dynamic. • This class extends AbstractGrid<E> • getNumRows and getNumCol return -1 when using UnboundedGrid<E>

  13. Actor • The API of this class is testable on the AP CS A and AB exams. • An Actor is an entity with a color and direction that can act. • An Actor’s act method simply reverses the Actor’s facing. • An Actor puts itself in a Grid<E> using putSelfInGrid(Grid<E>, Location) • An Actor removes itself in a Grid<E> using removeSelfFromGrid()

  14. Actor Methods • setColor(Color) • Color getColor() • setDirection(int) • intgetDirection() • Grid<E> getGrid() • moveTo(Location) //null removes the Actor • Location getLocation()

  15. Bug • The implementation of this class is testable on the AP CS A and AB exams. • The Bug class extends Actor. • The Bugact method moves the Bug in the current direction and places a Flower in the last place it was at. If the Bug cannot move it changes direction. Right 45o. • A Bug can test it if can move with boolean canMove() • A Bug can move() • A Bug can turn() • Color is specified at construction and is set with setColor(Color)

  16. BoxBug • The implementation of this class is testable on the AP CS A and AB exams. • A BoxBug traces out a square box of a given size. • A BoxBug’s constructor takes an int to specify box length. • The BoxBug overrides act by turning twice after acting the number of times specified in the constructor.

  17. Critter • The implementation of this class is testable on the AP CS A and AB exams. • The Critter class extends Actor. • A Critter moves randomly. • A Critter removes all nearby Actors that are Flowers and non-Critters from the Grid<E>. • A class derived from Critter can be defined by overriding methods. The act method can be left the same.

  18. Critter Methods • void makeMove (Location) //null removes the Critter • Location selectMoveLocation(ArrayList<Location>) /* This randomly selects a Location from the ArrayList */ • ArrayList<Location> getMoveLocations() /* default behavior returns all nearby empty Locations */ • ArrayList<Actor> getActors() /* returns all nearby Actors */ • Grid<E> getGrid() /* returns the Grid<E> associated with the Critter */

  19. ChameleonCritter • The implementation of this class is testable on the AP CS A and AB exams. • A ChameleonCritter takes on the color of neighboring actors as it moves through the grid. • A ChameleonCritter moves to the Location of the Actor it is eating and changes to its color. Selection is random. • makeMove is overridden to move towards the Actor it ate. The new makeMove calls Critter’s makeMove.

  20. Flower • The API of this class is testable on the AP CS A and AB exams. • A Flower is an actor that darkens over time. Some actors drop flowers as they move. • A Flower’s act method darkens the Flower. • A Flower’s color is set to pink by default. A Flower’s color can be set at construction.

  21. Rock • A Rock is an actor that does nothing. It is commonly used to block other actors from moving. • The API of this class is testable on the AP CS A and AB exams. • A Rock’s act method does nothing. • A Rock’s color may be specified at construction or by calling setColor(Color). The default color is black.

More Related