Advertisement
Guest User

Untitled

a guest
Apr 19th, 2013
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.53 KB | None | 0 0
  1. import info.gridworld.actor.Actor;
  2. import info.gridworld.actor.Critter;
  3. import info.gridworld.actor.Rock;
  4.  
  5. import info.gridworld.grid.Grid;
  6. import info.gridworld.grid.Location;
  7.  
  8. import java.util.ArrayList;
  9.  
  10. public class SeekerCritter extends Critter
  11. {
  12.     private boolean atLocation;
  13.     private Location randomLocation;
  14.    
  15.     /** Old instance variables, to delete **/
  16.     //private int row;
  17.     //private int col;
  18.    
  19.     public SeekerCritter()
  20.     {
  21.         super();
  22.        
  23.         //Start atLocation at true so that a random location is generated,
  24.         //and 0,0 isn't actually used
  25.         atLocation = true;
  26.         randomLocation = new Location(0, 0);
  27.     }
  28.    
  29.     /** FOR DEBUGGING PURPOSES **/
  30.     public Location getLoc() { return randomLocation; }
  31.     /**////////////////////////**/
  32.    
  33.     public ArrayList<Actor> getActors()
  34.     {
  35.         ArrayList<Actor> list = new ArrayList<Actor>();
  36.         Grid<Actor> gr = getGrid();
  37.         Location loc = getLocation();
  38.            
  39.         atLocation = atRandomLocation();
  40.            
  41.         if(!atLocation)
  42.         {
  43.             setDirection(loc.getDirectionToward(randomLocation));
  44.             Actor a = gr.get(getLocation());
  45.            
  46.             if(a != null)
  47.                 list.add(a);
  48.            
  49.         } else
  50.             getRandomLocation();
  51.        
  52.         return list;
  53.     }
  54.    
  55.     public void processActors(ArrayList<Actor> actors)
  56.     {
  57.         for(Actor a : actors)
  58.             a.removeSelfFromGrid();
  59.     }
  60.    
  61.     public ArrayList<Location> getMoveLocations()
  62.     {
  63.         ArrayList<Location> locs = new ArrayList<Location>();
  64.         Grid<Actor> gr = getGrid();
  65.        
  66.         Location loc = getLocation();
  67.         Location newLoc = loc.getAdjacentLocation(getDirection());
  68.        
  69.         if(gr.isValid(newLoc))
  70.             locs.add(newLoc);
  71.            
  72.         return locs;
  73.     }
  74.    
  75.     public Location selectMoveLocation(ArrayList<Location> locs)
  76.     {
  77.         Grid<Actor> gr = getGrid();
  78.         int x = locs.size();
  79.        
  80.         if(x != 0)
  81.         {
  82.             for(Location newLoc : locs)
  83.                 if(!gr.isValid(newLoc))
  84.                     locs.remove(newLoc);
  85.            
  86.             int r = (int) (Math.random() * locs.size());
  87.            
  88.             return locs.get(r);
  89.         } else
  90.             return super.selectMoveLocation(locs);
  91.     }
  92.    
  93.     public void makeMove(Location loc)
  94.     {
  95.         Grid<Actor> gr = getGrid();
  96.    
  97.         if(!atLocation && gr != null && gr.isValid(loc))
  98.             super.makeMove(loc);
  99.         else
  100.         {
  101.             getRandomLocation();
  102.             return;
  103.         }
  104.     }
  105.    
  106.     public void getRandomLocation()
  107.     {
  108.         int r = getGrid().getNumRows();
  109.         int c = getGrid().getNumCols();
  110.         int row = (int) (Math.random() * r);
  111.         int col = (int) (Math.random() * c);
  112.        
  113.         randomLocation = new Location(row, col);
  114.        
  115.         //Make sure to reset the atLocation variable so
  116.         //the methods work properly
  117.         atLocation = atRandomLocation();
  118.     }
  119.    
  120.     //Check to see if the current location of the critter
  121.     //is the ranodm location
  122.     public boolean atRandomLocation()
  123.     {
  124.         int row = randomLocation.getRow();
  125.         int col = randomLocation.getCol();
  126.         int r = getLocation().getRow();
  127.         int c = getLocation().getCol();
  128.        
  129.         if((row == r) && (col == c))
  130.             return true;
  131.         else
  132.             return false;
  133.     }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement