com.collegeboard.gridworld.actor
Class Actor

java.lang.Object
  extended by com.collegeboard.gridworld.actor.Actor
Direct Known Subclasses:
Critter, Flower, Rock

public abstract class Actor
extends Object


Constructor Summary
Actor()
          Constructs a black actor that is facing north.
 
Method Summary
abstract  void act()
          Override this method with the action that your actor carries out.actor needs to
 Color getColor()
          Gets the color of this actor.
 int getDirection()
          Gets the current direction of this actor.
 Grid<Actor> getGrid()
          Gets the grid in which this actor is located
 Location getLocation()
          Gets the location of this actor
Precondition: This actor is in a grid
 ArrayList<Location> getValidNeighborLocations()
          Gets the valid locations of the neighbors in all directions (north, northeast, east, southeast, south, southwest, west, and northwest)
Precondition: This actor is in a grid
 void moveTo(Location newLocation)
          Moves this actor to a new location.
 void putSelfInGrid(Grid<Actor> gr, Location loc)
          Puts this actor into a grid.
 void removeSelfFromGrid()
          Removes an actor from a grid.
 void setColor(Color newColor)
          Sets the color of this actor.
 String toString()
           
 void turn(int angle)
          Turns the direction of this actor.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Actor

public Actor()
Constructs a black actor that is facing north.

Method Detail

getLocation

public Location getLocation()
Gets the location of this actor
Precondition: This actor is in a grid

Returns:
this actor's location

getValidNeighborLocations

public ArrayList<Location> getValidNeighborLocations()
Gets the valid locations of the neighbors in all directions (north, northeast, east, southeast, south, southwest, west, and northwest)
Precondition: This actor is in a grid

Returns:
an array list of the valid locations of loc in all eight directions

getGrid

public Grid<Actor> getGrid()
Gets the grid in which this actor is located

Returns:
the grid, or null if this actor is not contained in a grid

putSelfInGrid

public void putSelfInGrid(Grid<Actor> gr,
                          Location loc)
Puts this actor into a grid. If there is another actor at the given location, it is removed.
Precondition: This actor is not in a grid

Parameters:
gr - the grid into which this actor should be placed
loc - the location into which the actor should be placed (must be valid)

removeSelfFromGrid

public void removeSelfFromGrid()
Removes an actor from a grid.
Precondition: This actor has been put in a grid.


moveTo

public void moveTo(Location newLocation)
Moves this actor to a new location. If there is another actor at the given location, it is removed.
Precondition: This actor has been put in a grid

Parameters:
newLocation - the new location (must be valid)

act

public abstract void act()
Override this method with the action that your actor carries out.actor needs to


turn

public void turn(int angle)
Turns the direction of this actor.

Parameters:
angle - the angle (clockwise) by which to turn

getDirection

public int getDirection()
Gets the current direction of this actor.

Returns:
the direction in { 0, 1, ..., 359 }

getColor

public Color getColor()
Gets the color of this actor.

Returns:
the color

setColor

public void setColor(Color newColor)
Sets the color of this actor.

Parameters:
newColor - the new color

toString

public String toString()
Overrides:
toString in class Object