Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import info.gridworld.actor.Actor;
- import info.gridworld.actor.Critter;
- import info.gridworld.actor.Rock;
- import info.gridworld.grid.Grid;
- import info.gridworld.grid.Location;
- import java.util.ArrayList;
- public class SeekerCritter extends Critter
- {
- private boolean atLocation;
- private Location randomLocation;
- /** Old instance variables, to delete **/
- //private int row;
- //private int col;
- public SeekerCritter()
- {
- super();
- //Start atLocation at true so that a random location is generated,
- //and 0,0 isn't actually used
- atLocation = true;
- randomLocation = new Location(0, 0);
- }
- /** FOR DEBUGGING PURPOSES **/
- public Location getLoc() { return randomLocation; }
- /**////////////////////////**/
- public ArrayList<Actor> getActors()
- {
- ArrayList<Actor> list = new ArrayList<Actor>();
- Grid<Actor> gr = getGrid();
- Location loc = getLocation();
- atLocation = atRandomLocation();
- if(!atLocation)
- {
- setDirection(loc.getDirectionToward(randomLocation));
- Actor a = gr.get(getLocation());
- if(a != null)
- list.add(a);
- } else
- getRandomLocation();
- return list;
- }
- public void processActors(ArrayList<Actor> actors)
- {
- for(Actor a : actors)
- a.removeSelfFromGrid();
- }
- public ArrayList<Location> getMoveLocations()
- {
- ArrayList<Location> locs = new ArrayList<Location>();
- Grid<Actor> gr = getGrid();
- Location loc = getLocation();
- Location newLoc = loc.getAdjacentLocation(getDirection());
- if(gr.isValid(newLoc))
- locs.add(newLoc);
- return locs;
- }
- public Location selectMoveLocation(ArrayList<Location> locs)
- {
- Grid<Actor> gr = getGrid();
- int x = locs.size();
- if(x != 0)
- {
- for(Location newLoc : locs)
- if(!gr.isValid(newLoc))
- locs.remove(newLoc);
- int r = (int) (Math.random() * locs.size());
- return locs.get(r);
- } else
- return super.selectMoveLocation(locs);
- }
- public void makeMove(Location loc)
- {
- Grid<Actor> gr = getGrid();
- if(!atLocation && gr != null && gr.isValid(loc))
- super.makeMove(loc);
- else
- {
- getRandomLocation();
- return;
- }
- }
- public void getRandomLocation()
- {
- int r = getGrid().getNumRows();
- int c = getGrid().getNumCols();
- int row = (int) (Math.random() * r);
- int col = (int) (Math.random() * c);
- randomLocation = new Location(row, col);
- //Make sure to reset the atLocation variable so
- //the methods work properly
- atLocation = atRandomLocation();
- }
- //Check to see if the current location of the critter
- //is the ranodm location
- public boolean atRandomLocation()
- {
- int row = randomLocation.getRow();
- int col = randomLocation.getCol();
- int r = getLocation().getRow();
- int c = getLocation().getCol();
- if((row == r) && (col == c))
- return true;
- else
- return false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement