info.gridworld.actor
Class Actor

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

public class Actor
extends Object

An Actor is an entity with a color and direction that can act.
The API of this class is testable on the AP CS A and AB exams.


Constructor Summary
Actor()
          Constructs a blue actor that is facing north.
 
Method Summary
 void act()
          Reverses the direction of this actor.
 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.
 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 this actor from its grid.
 void setColor(Color newColor)
          Sets the color of this actor.
 void setDirection(int newDirection)
          Sets the current direction of this actor.
 String toString()
          Creates a string that describes 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 blue actor that is facing north.

Method Detail

getColor

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

Returns:
the color of this actor

setColor

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

Parameters:
newColor - the new color

getDirection

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

Returns:
the direction of this actor, an angle between 0 and 359 degrees

setDirection

public void setDirection(int newDirection)
Sets the current direction of this actor.

Parameters:
newDirection - the new direction. The direction of this actor is set to the angle between 0 and 359 degrees that is equivalent to newDirection.

getGrid

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

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

getLocation

public Location getLocation()
Gets the location of this actor.

Returns:
the location of this actor, 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: (1) This actor is not contained in a grid (2) loc is valid in gr

Parameters:
gr - the grid into which this actor should be placed
loc - the location into which the actor should be placed

removeSelfFromGrid

public void removeSelfFromGrid()
Removes this actor from its grid.
Precondition: This actor is contained 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: (1) This actor is contained in a grid (2) newLocation is valid in the grid of this actor

Parameters:
newLocation - the new location

act

public void act()
Reverses the direction of this actor. Override this method in subclasses of Actor to define types of actors with different behavior


toString

public String toString()
Creates a string that describes this actor.

Overrides:
toString in class Object
Returns:
a string with the location, direction, and color of this actor