com.collegeboard.gridworld.actor
Class Critter

java.lang.Object
  extended by com.collegeboard.gridworld.actor.Actor
      extended by com.collegeboard.gridworld.actor.Critter

public class Critter
extends Actor

A critter is an actor that moves through its world, processing its neighbors in some way and then picking a new location. Override this class to obtain critters with interesting behavior.

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

Author:
Cay Horstmann

Constructor Summary
Critter()
           
 
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.
 ArrayList<Location> getMoveLocations()
          Get the possible locations for the next move.
 ArrayList<Actor> getNeighbors()
          Get the neighbors for processing.
 void processNeighbors(ArrayList<Actor> neighbors)
          Process the critter's neighbors.
 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, toString, turnBy
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Critter

public Critter()
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 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 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 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 Location selectMoveLocation(ArrayList<Location> locs)
Selects the location for the next move. Implemented to randomly pick one of the possible locations. 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