Advertisement
Guest User

Untitled

a guest
Sep 20th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.39 KB | None | 0 0
  1. import aicapn.agents.ShipLogic;
  2. import aicapn.bot.Bot;
  3. import aicapn.exceptions.OutOfThisWorldException;
  4. import aicapn.resources.Resources;
  5. import aicapn.world.ShipState;
  6.  
  7. import java.awt.Point;
  8. import java.util.ArrayList;
  9. import java.util.Iterator;
  10.  
  11. public class ABot extends Bot{
  12. public static boolean getPriorityShip(ShipState priorityShip, ShipState shipState, ShipLogic ship) {
  13. double shipStateDistance = Math.sqrt(Math.pow(shipState.getX() - ship.getX(), 2) + Math.pow(shipState.getY() - ship.getY(), 2));
  14. double priorityShipDistance = Math.sqrt(Math.pow(priorityShip.getX() - ship.getX(), 2) + Math.pow(priorityShip.getY() - ship.getY(), 2));
  15.  
  16. if (shipStateDistance < priorityShipDistance || shipStateDistance == priorityShipDistance && shipState.getHealth() < priorityShip.getHealth()) {
  17. return true;
  18. }
  19. return false;
  20. }
  21.  
  22. @Override
  23. public void action() {
  24. int priorityShip = -1;
  25. int x = 0, y = 0, myX = 0, myY = 0;
  26.  
  27. while (ship.isAlive()) {
  28. ArrayList<ShipState> shipList=world.getShips(ship);
  29.  
  30. for (int i = 0; i < shipList.size(); i++) {
  31. try {
  32. if (priorityShip == -1 || getPriorityShip(shipList.get(priorityShip), shipList.get(i), ship)) {
  33. priorityShip = i;
  34. }
  35. } catch (Exception e) {
  36. // TODO: handle exception
  37. priorityShip = 0;
  38. }
  39. }
  40.  
  41. if (x != shipList.get(priorityShip).getX() || y != shipList.get(priorityShip).getY()) {
  42. x = shipList.get(priorityShip).getX();
  43. y = shipList.get(priorityShip).getY();
  44. myX = x;
  45. myY = y;
  46.  
  47. if (Math.abs(x - ship.getX()) > Math.abs(y - ship.getY())) {
  48. try {
  49.  
  50. if (world.isPassable(x + 3, y) && world.isPassable(x - 3, y)) {
  51. if (x < ship.getX()) {
  52. myX += 3;
  53. System.out.println("1");
  54. } else {
  55. System.out.println("2");
  56. myX -= 3;
  57. }
  58. } else {
  59. myX = ship.getX();
  60. myY = ship.getY()
  61. ;
  62. if (world.isPassable(myX, myY + 1)) {
  63. myY += 1;
  64. } else {
  65. myY -= 1;
  66. }
  67.  
  68. }
  69. } catch (OutOfThisWorldException e) {
  70. // TODO Auto-generated catch block
  71. myX = ship.getX();
  72.  
  73. try {
  74. myY = ship.getY() + 1;
  75. } catch (IndexOutOfBoundsException e2) {
  76. myX = ship.getY() - 1;
  77. }
  78.  
  79. ship.fire();
  80. }
  81. } else {
  82. try {
  83. if (world.isPassable(x, y + 3) && world.isPassable(x, y - 3)) {
  84. if (y < ship.getY()) {
  85. System.out.println("3");
  86. myY += 3;
  87. } else {
  88. System.out.println("4");
  89. myY -= 3;
  90. }
  91. } else {
  92. myX = ship.getX();
  93. myY = ship.getY()
  94. ;
  95. if (world.isPassable(myX + 1, myY)) {
  96. myX += 1;
  97. } else {
  98. myX -= 1;
  99. }
  100. }
  101. } catch (OutOfThisWorldException e) {
  102. // TODO Auto-generated catch block
  103. try {
  104. myX = ship.getX() + 1;
  105. } catch (IndexOutOfBoundsException e2) {
  106. myX = ship.getX() - 1;
  107. }
  108.  
  109. myY = ship.getY();
  110. ship.fire();
  111. }
  112. }
  113.  
  114. }
  115.  
  116. try{
  117. ArrayList<Point> shortestPath=findPath(myX, myY);
  118. for(Point p:shortestPath)
  119. sailTo(p.x,p.y);
  120. }catch(Exception o){
  121.  
  122. }
  123.  
  124. if (x == myX) {
  125. ship.setDirection(3);
  126. ship.fire();
  127. } else {
  128. ship.setDirection(1);
  129. ship.fire();
  130. }
  131. }
  132. }
  133. public ArrayList<Point> findPath(int x, int y) throws OutOfThisWorldException
  134. {
  135. boolean found=false;
  136. ArrayList<Node> path=new ArrayList<Node>();
  137. ArrayList<Node> visited=new ArrayList<Node>();
  138. Node start=new Node(ship.getX(),ship.getY(), 0, null);
  139. Node end=null;
  140. path.add(start);
  141. while(path.size()>0)
  142. {
  143. Node curr = getBest(path, x, y);
  144. if(curr.getX()==x && curr.getY()==y)
  145. {
  146. found=true;
  147. end=curr;
  148. break;
  149. }
  150. //top
  151. if(isValid(curr.getX(), curr.getY()-1) && !contains(visited, curr.getX(), curr.getY()-1) && world.isPassable(curr.getX(), curr.getY()-1))
  152. {
  153. Node top=new Node(curr.getX(),curr.getY()-1,curr.getCurrCost()+ 1, curr);
  154. exploreNode(path, top);
  155. }
  156. //right
  157. if(isValid(curr.getX()+1, curr.getY()) && !contains(visited, curr.getX()+1, curr.getY()) &&world.isPassable(curr.getX()+1, curr.getY()))
  158. {
  159. Node right=new Node(curr.getX()+1,curr.getY(),curr.getCurrCost()+ 1,curr);
  160. exploreNode(path, right);
  161. }
  162. //bottom
  163. if(isValid(curr.getX(), curr.getY()+1) && !contains(visited, curr.getX(), curr.getY()+1) &&world.isPassable(curr.getX(), curr.getY()+1))
  164. {
  165. Node bottom=new Node(curr.getX(),curr.getY()+1,curr.getCurrCost()+ 1,curr);
  166. exploreNode(path, bottom);
  167. }
  168. //left
  169. if(isValid(curr.getX()-1, curr.getY()) && !contains(visited, curr.getX()-1, curr.getY()) &&world.isPassable(curr.getX()-1, curr.getY()))
  170. {
  171. Node left=new Node(curr.getX()-1,curr.getY(),curr.getCurrCost()+ 1,curr);
  172. exploreNode(path, left);
  173. }
  174. visited.add(curr);
  175. path.remove(curr);
  176. }
  177.  
  178. if(found)
  179. {
  180. ArrayList<Point> shortestPath=new ArrayList<Point>();
  181. while(end.getParent()!=null)
  182. {
  183. shortestPath.add(0,new Point(end.getX(),end.getY()));
  184. end=end.getParent();
  185. }
  186. return shortestPath;
  187. }
  188. else
  189. return null;
  190. }
  191.  
  192. public void exploreNode(ArrayList<Node> searchSpace, Node trialNode)
  193. {
  194. Iterator<Node> nodeList=searchSpace.iterator();
  195. while(nodeList.hasNext())
  196. {
  197. Node curr=nodeList.next();
  198. if(curr.getX()==trialNode.getX() && curr.getY()==trialNode.getY())
  199. nodeList.remove();
  200. }
  201. searchSpace.add(trialNode);
  202. }
  203.  
  204. public Node getBest(ArrayList<Node> searchSpace, int targetX, int targetY)
  205. {
  206. Node best=searchSpace.get(0);
  207. double bestScore=searchSpace.get(0).getCurrCost()+searchSpace.get(0).getHeuristic(targetX, targetY);
  208. for(Node n:searchSpace)
  209. {
  210. if(n.getCurrCost()+n.getHeuristic(targetX, targetY)<bestScore)
  211. {
  212. bestScore=n.getCurrCost()+n.getHeuristic(targetX, targetY);
  213. best=n;
  214. }
  215. }
  216. return best;
  217. }
  218.  
  219. public boolean contains(ArrayList<Node> container, int x, int y)
  220. {
  221. for(Node n:container)
  222. if(n.getX()==x && n.getY()==y)
  223. return true;
  224. return false;
  225. }
  226.  
  227. public boolean isValid(int x, int y)
  228. {
  229. return (x>=0 && x<Resources.MAP_WIDTH && y>=0 && y<Resources.MAP_HEIGHT);
  230. }
  231. public void sailTo(int x, int y)
  232. {
  233. //Keep moving toward the x,y location
  234. if(ship.getX()>x)
  235. ship.moveLeft();
  236. else
  237. if(ship.getX()<x)
  238. ship.moveRight();
  239. else
  240. if(ship.getY()>y)
  241. ship.moveUp();
  242. else
  243. if(ship.getY()<y)
  244. ship.moveDown();
  245. }
  246.  
  247. public class Node {
  248.  
  249. private int x, y;
  250. private double currCost;
  251. private Node parent;
  252.  
  253. public Node(int x, int y, double currCost, Node parent) {
  254. this.x=x;
  255. this.y=y;
  256. this.parent=parent;
  257. this.currCost=currCost;
  258. }
  259.  
  260. public int getX() {
  261. return x;
  262. }
  263. public void setX(int x) {
  264. this.x = x;
  265. }
  266. public int getY() {
  267. return y;
  268. }
  269. public void setY(int y) {
  270. this.y = y;
  271. }
  272. public Node getParent() {
  273. return parent;
  274. }
  275. public void setParent(Node parent) {
  276. this.parent = parent;
  277. }
  278.  
  279. public double getCurrCost() {
  280. return currCost;
  281. }
  282.  
  283. public void setCurrCost(double currCost) {
  284. this.currCost = currCost;
  285. }
  286.  
  287. public double getHeuristic(int targetX, int targetY)
  288. {
  289. return Math.abs(getX()-targetX)+Math.abs(getY()-targetY);
  290. }
  291.  
  292. @Override
  293. public String toString() {
  294. return getX()+", "+getY() + ": "+getCurrCost();
  295. }
  296. }
  297.  
  298. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement