Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Mar 22nd, 2010 | Syntax: None | Size: 2.59 KB | Hits: 102 | Expires: Never
Copy text to clipboard
  1. /**
  2.  * A class that symbolizes a spy in search for the "top secret file" object. It will fight with the other spy if encountered. It will also become hurt if it touches with traps.
  3.  *
  4.  * @author Juan Alvarez
  5.  * @version 3/18/2010
  6.  */
  7. import info.gridworld.actor.Actor;
  8. import info.gridworld.actor.Critter;
  9. import info.gridworld.grid.Location;
  10. import info.gridworld.grid.Grid;
  11. import info.gridworld.actor.Bug;
  12.  
  13. import java.awt.Color;
  14. import java.util.ArrayList;
  15. import java.util.*;
  16.  
  17. public class WhiteSpy extends Critter
  18. {
  19. public int wHealth;
  20.  
  21.     public WhiteSpy()
  22.     {
  23.         wHealth = 100;
  24.         setColor(Color.WHITE);
  25.     }
  26.      /**
  27.      * A spy gets the actors in front of it
  28.      * Removes health if it is in front and adds 10 to wHealth
  29.      * - 5 wHealth if Trap is in front
  30.      * - 15 wHealth if superTrap is in front
  31.      * -20 wHealth if BlackSpy is in front
  32.      * @return a list of actors occupying these locations
  33.      */
  34.     public ArrayList<Actor> getActor()
  35.     {
  36.         ArrayList<Actor> actors = new ArrayList<Actor>();
  37.         int[] dirs =
  38.             { Location.AHEAD};
  39.             for (Location loc : getLocationsInDirections(dirs))
  40.         {
  41.             Actor a = getGrid().get(loc);
  42.             if (a instanceof Health){
  43.                 Health.removeSelfFromGrid();
  44.                 wHealth = wHealth + 10;}
  45.                
  46.             if (a instanceof BlackSpy)
  47.                 bHealth = bHealth - 20;
  48.                
  49.             if (a instanceof Trap)
  50.                 wHealth = wHealth - 5;
  51.                
  52.             if (a instanceof SuperTrap)
  53.                 wHealth = wHealth - 15;
  54.                
  55.             if (a instanceof TopSecretFile)
  56.                 System.out.println("THE WHITE SPY WINS!");
  57.                
  58.         }
  59.  
  60.         return actors;
  61.     }
  62.     /**
  63.      * Finds the valid adjacent locations of this critter in different
  64.      * directions.
  65.      * @param directions - an array of directions (which are relative to the
  66.      * current direction)
  67.      * @return a set of valid locations that are neighbors of the current
  68.      * location in the given directions
  69.      */
  70.     public ArrayList<Location> getLocationsInDirections(int[] directions)
  71.     {
  72.         ArrayList<Location> locs = new ArrayList<Location>();
  73.         Grid gr = getGrid();
  74.         Location loc = getLocation();
  75.    
  76.         for (int d : directions)
  77.         {
  78.             Location neighborLoc = loc.getAdjacentLocation(getDirection() + d);
  79.             if (gr.isValid(neighborLoc))
  80.                 locs.add(neighborLoc);
  81.         }
  82.         return locs;
  83.     }