com.collegeboard.gridworld.actor
Class AbstractCritter

java.lang.Object
  extended by com.collegeboard.gridworld.actor.Actor
      extended by com.collegeboard.gridworld.actor.AbstractCritter
Direct Known Subclasses:
SimpleCritter

public abstract class AbstractCritter
extends Actor

A critter is an actor that moves through its world, processing its neighbors in some way and then picking a new location.

copyright© 2005 Cay S. Horstmann (http://horstmann.com)

Author:
Cay Horstmann

Constructor Summary
AbstractCritter()
           
 
Method Summary
 void act()
          A critter acts by getting a list of its neighbors, processing them, getting locations to move to, selecting one of them, and moving to the selected location.
abstract  ArrayList<Location> getMoveLocations()
          Get the possible locations for the next move.
abstract  ArrayList<Actor> getNeighbors()
          Get the neighbors for processing.
abstract  void makeMove(Location loc)
          Moves this critter to the given location.
abstract  void processNeighbors(ArrayList<Actor> neighbors)
          Process the critter's neighbors.
abstract  Location selectMoveLocation(ArrayList<Location> locs)
          Selects the location for the next move.
 
Methods inherited from class com.collegeboard.gridworld.actor.Actor
getColor, getDirection, getGrid, getLocation, moveTo, putSelfInGrid, removeSelfFromGrid, setColor, setDirection, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AbstractCritter

public AbstractCritter()
Method Detail

act

public void act()
A critter acts by getting a list of its neighbors, processing them, getting locations to move to, selecting one of them, and moving to the selected location.

Specified by:
act in class Actor

getNeighbors

public abstract ArrayList<Actor> getNeighbors()
Get the neighbors for processing. Implemented to return the actors that occupy neighboring grid locations. Override this method for subclasses that look elsewhere for neighbors.

Returns:
a list of actors that are neighbors of this critter

processNeighbors

public abstract void processNeighbors(ArrayList<Actor> neighbors)
Process the critter's neighbors. This method is implemented to do nothing. Override it in subclasses that process neighbors, for example by examining or removing them.

Parameters:
neighbors - the neighbors to be processed

getMoveLocations

public abstract ArrayList<Location> getMoveLocations()
Get the possible locations for the next move. Implemented to return the empty neighboring locations. Override this method for subclasses that look elsewhere for move locations.

Returns:
a list of possible locations for the next move

selectMoveLocation

public abstract Location selectMoveLocation(ArrayList<Location> locs)
Selects the location for the next move. Implemented to randomly pick one of the possible locations, or to return the current location if locs has size 0. Override this method for subclasses that have another mechanism for selecting the next move location.

Parameters:
locs - the possible locations for the next move
Returns:
the location that was selected for the next move.

makeMove

public abstract void makeMove(Location loc)
Moves this critter to the given location. Implemented to call moveTo. Override this method for subclasses that want to carry out other actions for moving (for example, turning or leaving traces).

Parameters:
loc - the location to move to (must be valid)