Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import aicapn.agents.ShipLogic;
- import aicapn.bot.Bot;
- import aicapn.exceptions.OutOfThisWorldException;
- import aicapn.resources.Resources;
- import aicapn.world.ShipState;
- import java.awt.Point;
- import java.util.ArrayList;
- import java.util.Iterator;
- public class ABot extends Bot{
- public static boolean getPriorityShip(ShipState priorityShip, ShipState shipState, ShipLogic ship) {
- double shipStateDistance = Math.sqrt(Math.pow(shipState.getX() - ship.getX(), 2) + Math.pow(shipState.getY() - ship.getY(), 2));
- double priorityShipDistance = Math.sqrt(Math.pow(priorityShip.getX() - ship.getX(), 2) + Math.pow(priorityShip.getY() - ship.getY(), 2));
- if (shipStateDistance < priorityShipDistance || shipStateDistance == priorityShipDistance && shipState.getHealth() < priorityShip.getHealth()) {
- return true;
- }
- return false;
- }
- @Override
- public void action() {
- int priorityShip = -1;
- int x = 0, y = 0, myX = 0, myY = 0;
- while (ship.isAlive()) {
- ArrayList<ShipState> shipList=world.getShips(ship);
- for (int i = 0; i < shipList.size(); i++) {
- try {
- if (priorityShip == -1 || getPriorityShip(shipList.get(priorityShip), shipList.get(i), ship)) {
- priorityShip = i;
- }
- } catch (Exception e) {
- // TODO: handle exception
- priorityShip = 0;
- }
- }
- if (x != shipList.get(priorityShip).getX() || y != shipList.get(priorityShip).getY()) {
- x = shipList.get(priorityShip).getX();
- y = shipList.get(priorityShip).getY();
- myX = x;
- myY = y;
- if (Math.abs(x - ship.getX()) > Math.abs(y - ship.getY())) {
- try {
- if (world.isPassable(x + 3, y) && world.isPassable(x - 3, y)) {
- if (x < ship.getX()) {
- myX += 3;
- System.out.println("1");
- } else {
- System.out.println("2");
- myX -= 3;
- }
- } else {
- myX = ship.getX();
- myY = ship.getY()
- ;
- if (world.isPassable(myX, myY + 1)) {
- myY += 1;
- } else {
- myY -= 1;
- }
- }
- } catch (OutOfThisWorldException e) {
- // TODO Auto-generated catch block
- myX = ship.getX();
- try {
- myY = ship.getY() + 1;
- } catch (IndexOutOfBoundsException e2) {
- myX = ship.getY() - 1;
- }
- ship.fire();
- }
- } else {
- try {
- if (world.isPassable(x, y + 3) && world.isPassable(x, y - 3)) {
- if (y < ship.getY()) {
- System.out.println("3");
- myY += 3;
- } else {
- System.out.println("4");
- myY -= 3;
- }
- } else {
- myX = ship.getX();
- myY = ship.getY()
- ;
- if (world.isPassable(myX + 1, myY)) {
- myX += 1;
- } else {
- myX -= 1;
- }
- }
- } catch (OutOfThisWorldException e) {
- // TODO Auto-generated catch block
- try {
- myX = ship.getX() + 1;
- } catch (IndexOutOfBoundsException e2) {
- myX = ship.getX() - 1;
- }
- myY = ship.getY();
- ship.fire();
- }
- }
- }
- try{
- ArrayList<Point> shortestPath=findPath(myX, myY);
- for(Point p:shortestPath)
- sailTo(p.x,p.y);
- }catch(Exception o){
- }
- if (x == myX) {
- ship.setDirection(3);
- ship.fire();
- } else {
- ship.setDirection(1);
- ship.fire();
- }
- }
- }
- public ArrayList<Point> findPath(int x, int y) throws OutOfThisWorldException
- {
- boolean found=false;
- ArrayList<Node> path=new ArrayList<Node>();
- ArrayList<Node> visited=new ArrayList<Node>();
- Node start=new Node(ship.getX(),ship.getY(), 0, null);
- Node end=null;
- path.add(start);
- while(path.size()>0)
- {
- Node curr = getBest(path, x, y);
- if(curr.getX()==x && curr.getY()==y)
- {
- found=true;
- end=curr;
- break;
- }
- //top
- if(isValid(curr.getX(), curr.getY()-1) && !contains(visited, curr.getX(), curr.getY()-1) && world.isPassable(curr.getX(), curr.getY()-1))
- {
- Node top=new Node(curr.getX(),curr.getY()-1,curr.getCurrCost()+ 1, curr);
- exploreNode(path, top);
- }
- //right
- if(isValid(curr.getX()+1, curr.getY()) && !contains(visited, curr.getX()+1, curr.getY()) &&world.isPassable(curr.getX()+1, curr.getY()))
- {
- Node right=new Node(curr.getX()+1,curr.getY(),curr.getCurrCost()+ 1,curr);
- exploreNode(path, right);
- }
- //bottom
- if(isValid(curr.getX(), curr.getY()+1) && !contains(visited, curr.getX(), curr.getY()+1) &&world.isPassable(curr.getX(), curr.getY()+1))
- {
- Node bottom=new Node(curr.getX(),curr.getY()+1,curr.getCurrCost()+ 1,curr);
- exploreNode(path, bottom);
- }
- //left
- if(isValid(curr.getX()-1, curr.getY()) && !contains(visited, curr.getX()-1, curr.getY()) &&world.isPassable(curr.getX()-1, curr.getY()))
- {
- Node left=new Node(curr.getX()-1,curr.getY(),curr.getCurrCost()+ 1,curr);
- exploreNode(path, left);
- }
- visited.add(curr);
- path.remove(curr);
- }
- if(found)
- {
- ArrayList<Point> shortestPath=new ArrayList<Point>();
- while(end.getParent()!=null)
- {
- shortestPath.add(0,new Point(end.getX(),end.getY()));
- end=end.getParent();
- }
- return shortestPath;
- }
- else
- return null;
- }
- public void exploreNode(ArrayList<Node> searchSpace, Node trialNode)
- {
- Iterator<Node> nodeList=searchSpace.iterator();
- while(nodeList.hasNext())
- {
- Node curr=nodeList.next();
- if(curr.getX()==trialNode.getX() && curr.getY()==trialNode.getY())
- nodeList.remove();
- }
- searchSpace.add(trialNode);
- }
- public Node getBest(ArrayList<Node> searchSpace, int targetX, int targetY)
- {
- Node best=searchSpace.get(0);
- double bestScore=searchSpace.get(0).getCurrCost()+searchSpace.get(0).getHeuristic(targetX, targetY);
- for(Node n:searchSpace)
- {
- if(n.getCurrCost()+n.getHeuristic(targetX, targetY)<bestScore)
- {
- bestScore=n.getCurrCost()+n.getHeuristic(targetX, targetY);
- best=n;
- }
- }
- return best;
- }
- public boolean contains(ArrayList<Node> container, int x, int y)
- {
- for(Node n:container)
- if(n.getX()==x && n.getY()==y)
- return true;
- return false;
- }
- public boolean isValid(int x, int y)
- {
- return (x>=0 && x<Resources.MAP_WIDTH && y>=0 && y<Resources.MAP_HEIGHT);
- }
- public void sailTo(int x, int y)
- {
- //Keep moving toward the x,y location
- if(ship.getX()>x)
- ship.moveLeft();
- else
- if(ship.getX()<x)
- ship.moveRight();
- else
- if(ship.getY()>y)
- ship.moveUp();
- else
- if(ship.getY()<y)
- ship.moveDown();
- }
- public class Node {
- private int x, y;
- private double currCost;
- private Node parent;
- public Node(int x, int y, double currCost, Node parent) {
- this.x=x;
- this.y=y;
- this.parent=parent;
- this.currCost=currCost;
- }
- public int getX() {
- return x;
- }
- public void setX(int x) {
- this.x = x;
- }
- public int getY() {
- return y;
- }
- public void setY(int y) {
- this.y = y;
- }
- public Node getParent() {
- return parent;
- }
- public void setParent(Node parent) {
- this.parent = parent;
- }
- public double getCurrCost() {
- return currCost;
- }
- public void setCurrCost(double currCost) {
- this.currCost = currCost;
- }
- public double getHeuristic(int targetX, int targetY)
- {
- return Math.abs(getX()-targetX)+Math.abs(getY()-targetY);
- }
- @Override
- public String toString() {
- return getX()+", "+getY() + ": "+getCurrCost();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement