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 other actors in some way and then picking a new location.
The implementation of this class is testable on the AP CS A and AB exams.


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<Actor> getActors()
          Gets the actors for processing.
 ArrayList<Location> getMoveLocations()
          Gets the possible locations for the next move.
 void makeMove(Location loc)
          Moves this critter to the given location.
 void processActors(ArrayList<Actor> actors)
          Processes selected 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()
Gets the actors for processing. Implemented to return the actors that occupy neighboring grid locations. Override this method in subclasses to look elsewhere for actors to process.
Postcondition: (1) The selected actors are contained in the same grid as this critter. (2) The state of all actors is unchanged.

Returns:
a list of actors that this critter wishes to process.

processActors

public void processActors(ArrayList<Actor> actors)
Processes selected actors. Implemented to "eat" (i.e. remove) selected actors that are not rocks or critters. Override this method in subclasses to process actors in a different way.
Precondition: All elements of actors are contained in the same grid as this critter.
Postcondition: (1) The state of all actors other than this critter and the elements of actors is unchanged. (2) The location of this critter is unchanged.

Parameters:
actors - the actors to be processed

getMoveLocations

public ArrayList<Location> getMoveLocations()
Gets the possible locations for the next move. Implemented to return the empty neighboring locations. Override this method in subclasses to look elsewhere for move locations.
Postcondition: (1) The move locations are valid in the grid of this critter. (2) The state of all actors is unchanged.

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 in subclasses that have another mechanism for selecting the next move location.
Precondition: All locations in locs are valid in the grid of this critter.
Postcondition: (1) loc is an element of locs, this Critter's current location, or null. (2) The state of all actors is unchanged.

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 remove this critter from its grid if loc is null, and to move to loc otherwise. Override this method in subclasses that want to carry out other actions (for example, turning this critter or adding occupants in empty locations).
Precondition: loc is valid in the grid of this critter or null.
Postcondition: (1) The critter's location is loc. (2) The state of all other actors is unchanged.

Parameters:
loc - the location to move to