info.gridworld.actor
Class Critter

java.lang.Object
  extended by info.gridworld.actor.Actor
      extended by info.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.


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> findValidNeighborLocations(int[] directions)
          Finds the valid neighbor locations of this critter in different directions
 ArrayList<Actor> getActors()
          Get the actors for processing.
 ArrayList<Location> getMoveLocations()
          Get the possible locations for the next move.
 void makeMove(Location loc)
          Moves this critter to the given location.
 void processActors(ArrayList<Actor> actors)
          Process the actors.
 Location selectMoveLocation(ArrayList<Location> locs)
          Selects the location for the next move.
 
Methods inherited from class info.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

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.

Overrides:
act in class Actor

getActors

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

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

processActors

public void processActors(ArrayList<Actor> actors)
Process the actors. This method is implemented to "eat" (i.e. remove) all actors that are not rocks or critters. Override it in subclasses that process neighbors in a different way.

Parameters:
actors - the actors 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, 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 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)

findValidNeighborLocations

public ArrayList<Location> findValidNeighborLocations(int[] directions)
Finds the valid neighbor locations of this critter in different directions

Parameters:
directions - - an array of directions (which are relative to the current direction)
Returns:
a set of valid locations that are neighbors of the current location in the given directions