Ant Farm - Robert Glen Martin
School for the Talented and Gifted
Dallas I.S.D.

Class WorkerAnt

java.lang.Object
  extended by info.gridworld.actor.Actor
      extended by info.gridworld.actor.Critter
          extended by WorkerAnt
All Implemented Interfaces:
Processable

public class WorkerAnt
extends info.gridworld.actor.Critter
implements Processable

A WorkerAnt is a critter who's mission is to take food from Cake and Cookie objects and to deliver it to a QueenAnt object. Initially it looks for food. After it finds food, it looks for a queen. Worker ants share the location of food and the queen with other ants they encounter. Worker ants with food are red. If they don't have food, they are black.


Field Summary
private  info.gridworld.grid.Location foodLoc
          Location of a Food object
private  int foodQuantity
          Current amount of food being carried
private  info.gridworld.grid.Location queenLoc
          Location of a QueenAnt object
 
Constructor Summary
WorkerAnt()
          Constructs a WorkerAnt critter.
 
Method Summary
private  int getDesiredDirecton()
          Returns the direction that the ant wants to go.
 java.util.ArrayList<info.gridworld.grid.Location> getMoveLocations()
          Gets the possible locations for the next move.
 int giveFood()
          Gives food to the calling QueenAnt.
 void makeMove(info.gridworld.grid.Location loc)
          Moves this critter to the given location, sets its direction, and sets its color (red = has food, black = does not have food).
 void process(WorkerAnt ant)
          Gives current food and queen locations to ant.
 void processActors(java.util.ArrayList<info.gridworld.actor.Actor> actors)
          Processes each of the neighboring Ant Farm actors.
 void shareFoodLocation(info.gridworld.grid.Location fLoc)
          Receives the fLoc food location from a Food object.
 void shareQueenLocation(info.gridworld.grid.Location qLoc)
          Receives the qLoc queen location from a QueenAnt object.
 void takeFood(int fQty)
          Takes fQty amount of food from the calling Food.
 java.lang.String toString()
          Creates a string that describes this actor.
 
Methods inherited from class info.gridworld.actor.Critter
act, getActors, selectMoveLocation
 
Methods inherited from class info.gridworld.actor.Actor
getColor, getDirection, getGrid, getLocation, moveTo, putSelfInGrid, removeSelfFromGrid, setColor, setDirection
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

foodQuantity

private int foodQuantity
Current amount of food being carried


foodLoc

private info.gridworld.grid.Location foodLoc
Location of a Food object


queenLoc

private info.gridworld.grid.Location queenLoc
Location of a QueenAnt object

Constructor Detail

WorkerAnt

public WorkerAnt()
Constructs a WorkerAnt critter. It is originally black (no food) and its direction is chosen randomly from the eight normal cardinal directions.

Method Detail

process

public void process(WorkerAnt ant)
Gives current food and queen locations to ant.

Specified by:
process in interface Processable
Parameters:
ant - the calling WorkerAnt

takeFood

public void takeFood(int fQty)
Takes fQty amount of food from the calling Food.

Parameters:
fQty - the amount of food to take.

giveFood

public int giveFood()
Gives food to the calling QueenAnt.

Returns:
the amound of food to give.

shareFoodLocation

public void shareFoodLocation(info.gridworld.grid.Location fLoc)
Receives the fLoc food location from a Food object. Saves this location if it doesn't already have one.

Parameters:
fLoc - the location of the food.

shareQueenLocation

public void shareQueenLocation(info.gridworld.grid.Location qLoc)
Receives the qLoc queen location from a QueenAnt object. Saves this location if it doesn't already have one.

Parameters:
qLoc - the location of the queen.

processActors

public void processActors(java.util.ArrayList<info.gridworld.actor.Actor> actors)
Processes each of the neighboring Ant Farm actors. Implemented to get food from Cake and Cookie actors, give food to QueenAnt actors, and to share locations with other WorkerAnt actors.
Precondition: All objects in actors are contained in the same grid as this critter.

Overrides:
processActors in class info.gridworld.actor.Critter
Parameters:
actors - the actors to be processed

getMoveLocations

public java.util.ArrayList<info.gridworld.grid.Location> getMoveLocations()
Gets the possible locations for the next move. Implemented to return the empty neighboring locations that are roughly in the direction of the current goal (food or queen). Calles getDesiredDirection to get the direction to the goal. Then it considers locations which are in that direction or +- Location.HALF_RIGHT degrees.
Postcondition: The locations must be valid in the grid of this critter.

Overrides:
getMoveLocations in class info.gridworld.actor.Critter
Returns:
a list of possible locations for the next move

makeMove

public void makeMove(info.gridworld.grid.Location loc)
Moves this critter to the given location, sets its direction, and sets its color (red = has food, black = does not have food). Implemented to call moveTo.
Precondition: loc is valid in the grid of this critter

Overrides:
makeMove in class info.gridworld.actor.Critter
Parameters:
loc - the location to move to (must be valid)

getDesiredDirecton

private int getDesiredDirecton()
Returns the direction that the ant wants to go.

Returns:
the direction to the queen (if there is food and a queen's location is known); the direction to the food (if there is no food and a food's location is known); the current ant's direction otherwise.

toString

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

Overrides:
toString in class info.gridworld.actor.Actor
Returns:
a string with the Actor information plus the current amount of food and any known Food and QueenAnt locations.

© 2007 Robert Glen Martin.
All rights reserved.