210 likes | 341 Views
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)
E N D
AP Case Study:The Magic ofGrid World by Paul Vitale
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()
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
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.
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)
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.
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.
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.
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.
BoundedGrid<E> • The implementation of this class is testable on the AP CS AB exam. • This grid is static. • This class extends AbstractGrid<E>
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>
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()
Actor Methods • setColor(Color) • Color getColor() • setDirection(int) • intgetDirection() • Grid<E> getGrid() • moveTo(Location) //null removes the Actor • Location getLocation()
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)
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.
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.
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 */
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.
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.
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.