Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.56 KB | None | 0 0
  1. import java.util.*;
  2. import java.awt.*;
  3. import info.gridworld.grid.*;
  4. import info.gridworld.actor.*;
  5. public class LooseCannon extends Critter
  6. {
  7. private Actor selectTarget(ArrayList<Actor> array)
  8. {
  9. Actor target = new Actor();
  10. Location location = getLocation();
  11. double distance = Integer.MAX_VALUE;
  12. int row = location.getRow();
  13. int col = location.getCol();
  14. for(Actor actor: array)
  15. {
  16. Location actLoc = actor.getLocation();
  17. int actRow = actLoc.getRow();
  18. int actCol = actLoc.getCol();
  19. double actDistance = Math.sqrt((actRow - row)^2 + (actCol - col)^2);
  20. if(actDistance < distance && (actor instanceof Critter && !(actor instanceof LooseCannon)))
  21. {
  22. target = actor;
  23. distance = actDistance;
  24. }
  25. }
  26. return target;
  27. }
  28.  
  29. public ArrayList<Actor> getActors()
  30. {
  31. ArrayList<Actor> actors = new ArrayList<Actor>();
  32. Grid<Actor> grid = getGrid();
  33. int row = grid.getNumRows();
  34. int col = grid.getNumCols();
  35. for(int rowCount = 0; rowCount < row; rowCount++)
  36. {
  37. for(int colCount = 0; colCount < col; colCount++)
  38. {
  39. Location location = new Location(rowCount, colCount);
  40. Actor actor = grid.get(location);
  41. if(actor != null && (actor instanceof Critter && !(actor instanceof LooseCannon)))
  42. {
  43. actors.add(grid.get(location));
  44. }
  45. }
  46. }
  47. return actors;
  48. }
  49.  
  50. public void processActors(ArrayList<Actor> array)
  51. {
  52. Actor target = selectTarget(array);
  53. if(target != null && target.getGrid() != null)
  54. {
  55. target.removeSelfFromGrid();
  56. }
  57. }
  58.  
  59. public ArrayList<Location> getMoveLocations()
  60. {
  61. Grid<Actor> grid = getGrid();
  62. ArrayList<Actor> array = getActors();
  63. Actor target = selectTarget(array);
  64. System.out.println("getMoveLocations:" + target.toString());
  65. Location targetLoc = target.getLocation();
  66. ArrayList<Location> arrayLoc = new ArrayList<Location>();
  67. if(targetLoc != null)
  68. {
  69. arrayLoc = grid.getEmptyAdjacentLocations(targetLoc);
  70. int size = arrayLoc.size();
  71. while(size == 0)
  72. {
  73. int direction = targetLoc.getDirectionToward(getLocation());
  74. targetLoc = targetLoc.getAdjacentLocation(direction);
  75. arrayLoc = grid.getEmptyAdjacentLocations(targetLoc);
  76. size = arrayLoc.size();
  77. if(targetLoc.equals(getLocation()) && size == 0)
  78. {
  79. arrayLoc.add(getLocation());
  80. size = arrayLoc.size();
  81. }
  82. }
  83. }
  84. else
  85. {
  86. arrayLoc = super.getMoveLocations();
  87. }
  88. return arrayLoc;
  89. }
  90.  
  91. public Location selectMoveLocation(ArrayList<Location> array)
  92. {
  93. int size = array.size();
  94. if(size != 0)
  95. {
  96. System.out.println("selectMoveLocation:" + array.get(size/2));
  97. return array.get(size/2);
  98. }
  99. else
  100. {
  101. System.out.println("selectMoveLocation: null");
  102. return null;
  103. }
  104. }
  105.  
  106. public void makeMove(Location location)
  107. {
  108. super.makeMove(location);
  109. if(location != null && getLocation() != null)
  110. {
  111. System.out.println("makeMove:" + location.getRow() + " " + location.getCol());
  112. Location currentLoc = getLocation();
  113. int direction = currentLoc.getDirectionToward(location);
  114. setDirection(direction);
  115. Grid grid = getGrid();
  116. ArrayList<Location> array = grid.getEmptyAdjacentLocations(currentLoc);
  117. Bug bugOne = new Bug();
  118. Bug bugTwo = new Bug();
  119. if(array.size() >= 1)
  120. {
  121. grid.put(array.get(0), bugOne);
  122. System.out.println("makeMove array(0):" + array.get(0).getRow() + " " + array.get(0).getCol());
  123. bugOne.setDirection(currentLoc.getDirectionToward(array.get(0)));
  124. }
  125. if(array.size() >= 2)
  126. {
  127. grid.put(array.get(array.size() - 1), bugTwo);
  128. System.out.println("makeMove array(size-1):" + array.get(array.size() - 1).getRow() + " " + array.get(array.size() - 1).getCol());
  129. bugTwo.setDirection(currentLoc.getDirectionToward(array.get(array.size() - 1)));
  130. }
  131. }
  132. }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement