aznishboy

WorkerAnt

Jan 30th, 2012
1,120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 6.13 KB | None | 0 0
  1. /**
  2.  * WorkerAnt.java  05/10/07
  3.  *
  4.  * @author - Jane Doe
  5.  * @author - Period n
  6.  * @author - Id nnnnnnn
  7.  *
  8.  * @author - I received help from ...
  9.  *
  10.  */
  11.  
  12. import info.gridworld.actor.*;
  13. import info.gridworld.grid.*;
  14.  
  15.  
  16. import java.awt.Color;
  17. import java.util.ArrayList;
  18.  
  19. /**
  20.  * A <code>WorkerAnt</code> is a critter who's mission is
  21.  * to take food from <code>Cake and Cookie</code> objects and to
  22.  * deliver it to a <code>QueenAnt</code> object.
  23.  * Initially it looks for food.  After it finds food, it looks for
  24.  * a queen.
  25.  * Worker ants share the location of food and the queen
  26.  * with other ants they encounter.
  27.  * Worker ants with food are red.  If they don't have food,
  28.  * they are black.
  29.  */
  30. public class WorkerAnt extends Critter implements Processable
  31. {
  32.     private int foodCarried;
  33.     private Location foodLoc;
  34.     private Location queenLoc;
  35.     /** Current amount of food being carried */
  36.  
  37.  
  38.     /** Location of a <code>Food</code> object */
  39.  
  40.  
  41.     /** Location of a <code>QueenAnt</code> object */
  42.  
  43.  
  44.     /**
  45.      * Constructs a <code>WorkerAnt</code> critter.
  46.      * It is originally black (no food) and
  47.      * its direction is chosen randomly from the
  48.      * eight normal cardinal directions.
  49.      */
  50.     public WorkerAnt()
  51.     {
  52.         setColor(Color.BLACK);
  53.         int r = ((int)(Math.random() * 8)) * 45;
  54.         setDirection(r);
  55.         foodCarried = 0;
  56.         foodLoc = null;
  57.         queenLoc = null;
  58.     }
  59.  
  60.     /**
  61.      * Gives current food and queen locations to <code>ant</code>.
  62.      * @param ant the calling <code>WorkerAnt</code>
  63.      */
  64.     public void process(WorkerAnt ant)
  65.     {
  66.        if(foodLoc != null)
  67.           ant.shareFoodLocation(foodLoc);
  68.        if(queenLoc != null)
  69.            ant.shareQueenLocation(queenLoc);
  70.     }
  71.  
  72.     /**
  73.      * Takes <code>fQty</code> amount of food from the
  74.      * calling <code>Food</code>.
  75.      * @param fQty the amount of food to take.
  76.      */
  77.     public void takeFood(int fQty)
  78.     {
  79.         foodCarried = fQty;
  80.     }
  81.  
  82.     /**
  83.      * Gives food to the calling <code>QueenAnt</code>.
  84.      * @return the amound of food to give.
  85.      */
  86.     public int giveFood()
  87.     {
  88.         int tempFood = foodCarried;
  89.         foodCarried = 0;
  90.         return tempFood;
  91.     }
  92.  
  93.     /**
  94.      * Receives the <code>fLoc</code> food location from a
  95.      * <code>Food</code> object.  Saves this location if
  96.      * it doesn't already have one.
  97.      * @param fLoc the location of the food.
  98.      */
  99.     public void shareFoodLocation(Location fLoc)
  100.     {
  101.         foodLoc = fLoc;
  102.     }
  103.  
  104.     /**
  105.      * Receives the <code>qLoc</code> queen location from a
  106.      * <code>QueenAnt</code> object.  Saves this location if
  107.      * it doesn't already have one.
  108.      * @param qLoc the location of the queen.
  109.      */
  110.     public void shareQueenLocation(Location qLoc)
  111.     {
  112.         queenLoc = qLoc;
  113.     }
  114.  
  115.     /**
  116.      * Processes each of the neighboring Ant Farm actors.
  117.      * Implemented to get food from <code>Cake and Cookie</code> actors,
  118.      * give food to <code>QueenAnt</code> actors, and to share locations
  119.      * with other <code>WorkerAnt</code> actors.<br />
  120.      * Precondition: All objects in <code>actors</code>
  121.      * are contained in the same grid as this critter.
  122.      * @param actors the actors to be processed
  123.      */
  124.     @Override
  125.     public void processActors(ArrayList<Actor> actors)
  126.     {
  127.         for (Actor a: actors)
  128.             ((Processable)a).process(this);
  129.     }
  130.  
  131.     /**
  132.      * Gets the possible locations for the next move.
  133.      * Implemented to return the empty neighboring locations
  134.      * that are roughly in the direction of the current goal
  135.      * (food or queen).  Calles getDesiredDirection to get the
  136.      * direction to the goal.  Then it considers locations which
  137.      * are in that direction or +- Location.HALF_RIGHT degrees.<br />
  138.      * Postcondition: The locations must be valid in the grid
  139.      * of this critter.
  140.      * @return a list of possible locations for the next move
  141.      */
  142.     @Override
  143.     public ArrayList<Location> getMoveLocations()
  144.     {
  145.         int dir = getDesiredDirecton();
  146.         ArrayList<Location> validLoc = new ArrayList<Location>();
  147.         for (int i = dir + Location.HALF_LEFT; i <= dir + Location.HALF_RIGHT; i+= 45)
  148.         {
  149.             Location adloc = this.getLocation().getAdjacentLocation(i);
  150.             if(getGrid().isValid(adloc) && getGrid().get(adloc) == null)
  151.             {
  152.                 validLoc.add(adloc);
  153.             }
  154.         }
  155.         return validLoc;
  156.     }
  157.  
  158.     /**
  159.      * Moves this critter to the given location, sets its direction,
  160.      * and sets its color (red = has food, black = does not have food).
  161.      * Implemented to call moveTo.<br />
  162.      * Precondition: <code>loc</code> is valid in the grid of this critter
  163.      * @param loc the location to move to (must be valid)
  164.      */
  165.     @Override
  166.     public void makeMove(Location loc)
  167.     {
  168.         Location selectedLocation = selectMoveLocation(getMoveLocations());
  169.         if (selectedLocation != null && selectedLocation != getLocation())
  170.         {
  171.                 setDirection(getLocation().getDirectionToward(selectedLocation));
  172.                 moveTo(selectedLocation);
  173.         }
  174.         else
  175.         {
  176.             double rand = Math.random();
  177.             if (rand <= .5)
  178.                 setDirection(Location.HALF_LEFT);
  179.             else
  180.                 setDirection(Location.HALF_RIGHT);
  181.         }
  182.         if (foodCarried > 0)
  183.             setColor(Color.RED);
  184.         else
  185.             setColor(Color.BLACK);     
  186.     }
  187.  
  188.     /**
  189.      * Returns the direction that the ant wants to go.
  190.      * @return the direction to the queen (if there is food
  191.      * and a queen's location is known); the direction to the
  192.      * food (if there is no food and a food's location is known);
  193.      * the current ant's direction otherwise.
  194.      */
  195.     private int getDesiredDirecton()
  196.     {
  197.         if(queenLoc != null && foodCarried != 0)
  198.            return getLocation().getDirectionToward(queenLoc);
  199.         else if(foodLoc != null && foodCarried == 0)
  200.            return getLocation().getDirectionToward(foodLoc);
  201.         else
  202.             return getDirection();
  203.     }
  204.  
  205.     /**
  206.      * Creates a string that describes this actor.
  207.      * @return a string with the <code>Actor</code> information
  208.      * plus the current amount of food and any known
  209.      * <code>Food</code> and <code>QueenAnt</code> locations.
  210.      */
  211.     @Override
  212.     public String toString()
  213.     {
  214.         return super.toString() + "FQty" + foodCarried + "FLoc" + foodLoc + "QLoc" + queenLoc;
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment