Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.util.ArrayList;
  3. import info.gridworld.grid.Grid;
  4. import info.gridworld.grid.Location;
  5. import info.gridworld.actor.*;
  6.  
  7. public class RockHopper extends Bug
  8. {
  9. private Location moveLoc;
  10.  
  11. public RockHopper()
  12. {
  13. this.setColor(Color.RED);
  14. this.setDirection(Location.EAST);
  15.  
  16. }
  17.  
  18. public void act()
  19. {
  20. if (canMove())
  21. move();
  22. else
  23. spawnRock();
  24. }
  25.  
  26. public void move()
  27. {
  28. Grid<Actor> gr = getGrid();
  29. if(gr == null)
  30. return;
  31.  
  32. Location loc = getLocation();
  33.  
  34. if(gr.isValid(moveLoc))
  35. moveTo(moveLoc);
  36. else
  37. removeSelfFromGrid();
  38.  
  39. Flower flower = new Flower(Color.RED);
  40. flower.putSelfInGrid(gr, loc);
  41. }
  42.  
  43. public boolean canMove()
  44. {
  45. Grid<Actor> gr = getGrid();
  46.  
  47. if (gr == null)
  48. return false;
  49.  
  50. Location loc = getLocation();
  51.  
  52. int myCol = loc.getCol();
  53. int rows = gr.getNumRows();
  54. int cols = gr.getNumCols();
  55. int r=0;
  56. Location jumpLoc;
  57.  
  58. if(myCol == gr.getNumCols() -1)
  59. {
  60. setDirection(Location.WEST);
  61. r = (int) (Math.random() * rows);
  62. jumpLoc = new Location(r, loc.getCol());
  63. moveTo(jumpLoc);
  64. }
  65.  
  66. if (myCol == 0 && getDirection() == Location.WEST)
  67. {
  68. setDirection(Location.EAST);
  69. }
  70.  
  71.  
  72. Location NE, E, SE;
  73.  
  74. NE = loc.getAdjacentLocation(getDirection() + Location.HALF_LEFT);
  75. E = loc.getAdjacentLocation(getDirection());
  76. SE = loc.getAdjacentLocation(getDirection() + Location.HALF_RIGHT);
  77.  
  78. Actor neighborNE,neighborE, neighborSE;
  79.  
  80. neighborNE = gr.get(NE);
  81. neighborE = gr.get(E);
  82. neighborSE = gr.get(SE);
  83.  
  84. if(neighborE instanceof Rock)
  85. {
  86. if(!gr.isValid(E))
  87. return false;
  88.  
  89. moveLoc = E;
  90. return true;
  91. }
  92. else if(neighborNE instanceof Rock)
  93. {
  94. if(!gr.isValid(NE))
  95. return false;
  96.  
  97. moveLoc = NE;
  98. return true;
  99. }
  100. else if(neighborSE instanceof Rock)
  101. {
  102. if(!gr.isValid(SE))
  103. return false;
  104.  
  105. moveLoc = SE;
  106. return true;
  107. }
  108.  
  109.  
  110. return false;
  111. }
  112.  
  113. private void spawnRock()
  114. {
  115. Grid<Actor> gr = getGrid();
  116. if(gr == null)
  117. return;
  118.  
  119. Location loc = getLocation();
  120. ArrayList <Location> L = new ArrayList<Location>();
  121.  
  122. Location next;
  123. Actor N;
  124. int i,j;
  125. for (i = 0; i < gr.getNumRows(); i++)
  126. for (j = 0; j < gr.getNumCols(); j++)
  127. {
  128. next = new Location(i,j);
  129. N = gr.get(next);
  130. if(!(N instanceof Rock) && !next.equals(loc))
  131. {
  132. L.add(next);
  133. }
  134. }
  135. i = 0;
  136. int p;
  137. Rock rock;
  138. while (i < 3 && i < L.size())
  139. {
  140. p = (int) (Math.random() * L.size());
  141. rock = new Rock();
  142. rock.putSelfInGrid(gr, L.get(p));
  143. L.remove(p);
  144. i++;
  145. }
  146. }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement