Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.awt.*;
- import info.gridworld.grid.*;
- import info.gridworld.actor.*;
- public class LooseCannon extends Critter
- {
- private Actor selectTarget(ArrayList<Actor> array)
- {
- Actor target = new Actor();
- Location location = getLocation();
- double distance = Integer.MAX_VALUE;
- int row = location.getRow();
- int col = location.getCol();
- for(Actor actor: array)
- {
- Location actLoc = actor.getLocation();
- int actRow = actLoc.getRow();
- int actCol = actLoc.getCol();
- double actDistance = Math.sqrt((actRow - row)^2 + (actCol - col)^2);
- if(actDistance < distance && (actor instanceof Critter && !(actor instanceof LooseCannon)))
- {
- target = actor;
- distance = actDistance;
- }
- }
- return target;
- }
- public ArrayList<Actor> getActors()
- {
- ArrayList<Actor> actors = new ArrayList<Actor>();
- Grid<Actor> grid = getGrid();
- int row = grid.getNumRows();
- int col = grid.getNumCols();
- for(int rowCount = 0; rowCount < row; rowCount++)
- {
- for(int colCount = 0; colCount < col; colCount++)
- {
- Location location = new Location(rowCount, colCount);
- Actor actor = grid.get(location);
- if(actor != null && (actor instanceof Critter && !(actor instanceof LooseCannon)))
- {
- actors.add(grid.get(location));
- }
- }
- }
- return actors;
- }
- public void processActors(ArrayList<Actor> array)
- {
- Actor target = selectTarget(array);
- if(target != null && target.getGrid() != null)
- {
- target.removeSelfFromGrid();
- }
- }
- public ArrayList<Location> getMoveLocations()
- {
- Grid<Actor> grid = getGrid();
- ArrayList<Actor> array = getActors();
- Actor target = selectTarget(array);
- System.out.println("getMoveLocations:" + target.toString());
- Location targetLoc = target.getLocation();
- ArrayList<Location> arrayLoc = new ArrayList<Location>();
- if(targetLoc != null)
- {
- arrayLoc = grid.getEmptyAdjacentLocations(targetLoc);
- int size = arrayLoc.size();
- while(size == 0)
- {
- int direction = targetLoc.getDirectionToward(getLocation());
- targetLoc = targetLoc.getAdjacentLocation(direction);
- arrayLoc = grid.getEmptyAdjacentLocations(targetLoc);
- size = arrayLoc.size();
- if(targetLoc.equals(getLocation()) && size == 0)
- {
- arrayLoc.add(getLocation());
- size = arrayLoc.size();
- }
- }
- }
- else
- {
- arrayLoc = super.getMoveLocations();
- }
- return arrayLoc;
- }
- public Location selectMoveLocation(ArrayList<Location> array)
- {
- int size = array.size();
- if(size != 0)
- {
- System.out.println("selectMoveLocation:" + array.get(size/2));
- return array.get(size/2);
- }
- else
- {
- System.out.println("selectMoveLocation: null");
- return null;
- }
- }
- public void makeMove(Location location)
- {
- super.makeMove(location);
- if(location != null && getLocation() != null)
- {
- System.out.println("makeMove:" + location.getRow() + " " + location.getCol());
- Location currentLoc = getLocation();
- int direction = currentLoc.getDirectionToward(location);
- setDirection(direction);
- Grid grid = getGrid();
- ArrayList<Location> array = grid.getEmptyAdjacentLocations(currentLoc);
- Bug bugOne = new Bug();
- Bug bugTwo = new Bug();
- if(array.size() >= 1)
- {
- grid.put(array.get(0), bugOne);
- System.out.println("makeMove array(0):" + array.get(0).getRow() + " " + array.get(0).getCol());
- bugOne.setDirection(currentLoc.getDirectionToward(array.get(0)));
- }
- if(array.size() >= 2)
- {
- grid.put(array.get(array.size() - 1), bugTwo);
- System.out.println("makeMove array(size-1):" + array.get(array.size() - 1).getRow() + " " + array.get(array.size() - 1).getCol());
- bugTwo.setDirection(currentLoc.getDirectionToward(array.get(array.size() - 1)));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement