Advertisement
Guest User

Untitled

a guest
Apr 15th, 2014
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.42 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. import info.gridworld.actor.Actor;
  4. import info.gridworld.actor.Critter;
  5. import info.gridworld.actor.Rock;
  6. import info.gridworld.grid.Location;
  7.  
  8.  
  9. public class GreninjaCritter extends Critter{
  10. /**
  11. * A critter acts by getting a list of other actors, processing that list, getting locations to move to,
  12. * selecting one of them, and moving to the selected location.
  13. */
  14. boolean moveOrNot;
  15. public GreninjaCritter(boolean b){
  16. moveOrNot = b;
  17. }
  18. public void act()
  19. {
  20. if (getGrid() == null)
  21. return;
  22. ArrayList<Actor> actors = getActors();
  23. processActors(actors);
  24. ArrayList<Location> moveLocs = getMoveLocations();
  25. Location loc = selectMoveLocation(moveLocs);
  26. makeMove(loc);
  27. }
  28.  
  29.  
  30.  
  31. /**
  32. * Gets the actors for processing. Implemented to return the actors that occupy neighboring grid locations.
  33. * Override this method in subclasses to look elsewhere for actors to process.
  34. * Postcondition: The state of all actors is unchanged.
  35. * @return a list of actors that this critter wishes to process
  36. */
  37. public ArrayList<Actor> getActors()
  38. {
  39. return getGrid().getNeighbors(getLocation());
  40. }
  41. /**
  42. * Processes the elements of actors. New actors may be added to empty locations.
  43. * Implemented to “eat” (i.e., remove) selected actors that are not rocks or critters.
  44. * Override this method in subclasses to process actors in a different way.
  45. * Postcondition: (1) The state of all actors in the grid other than this critter and the
  46. * elements of actors is unchanged. (2) The location of this critter is unchanged.
  47. * @param actors the actors to be processed
  48. */
  49. public void processActors(ArrayList<Actor> actors) {}
  50.  
  51.  
  52. /**
  53. * Gets a list of possible locations for the next move. These locations must be valid in the grid of this critter.
  54. * Implemented to return the empty neighboring locations. Override this method in subclasses to look
  55. * elsewhere for move locations.
  56. * Postcondition: The state of all actors is unchanged.
  57. * @return a list of possible locations for the next move
  58. */
  59. public ArrayList<Location> getMoveLocations() {
  60. //changed to get valid locations that can include a rock, actor, or critter
  61. ArrayList<Location> r = new ArrayList<Location>();
  62.  
  63. for(int row = 0; row<getGrid().getNumRows() ; row++){
  64. for(int col = 0; col<getGrid().getNumCols() ; col++){
  65. r.add(new Location(row, col));
  66. }
  67. }
  68. return r;
  69. }
  70.  
  71.  
  72. /**
  73. * Selects the location for the next move. Implemented to randomly pick one of the possible locations,
  74. * or to return the current location if locs has size 0. Override this method in subclasses that
  75. * have another mechanism for selecting the next move location.
  76. * Postcondition: (1) The returned location is an element of locs, this critter’s current location, or null.
  77. * (2) The state of all actors is unchanged.
  78. * @param locs the possible locations for the next move
  79. * @return the location that was selected for the next move
  80. */
  81. public Location selectMoveLocation(ArrayList<Location> locs)
  82. {
  83. int n = locs.size();
  84. if (n == 0)
  85. return getLocation();
  86. int r = (int) (Math.random() * n);
  87. return locs.get(r);
  88. }
  89. /**
  90. * Moves this critter to the given location loc, or removes this critter from its grid if loc is null.
  91. * An actor may be added to the old location. If there is a different actor at location loc, that actor is
  92. * removed from the grid. Override this method in subclasses that want to carry out other actions
  93. * (for example, turning this critter or adding an occupant in its previous location).
  94. * Postcondition: (1) getLocation() == loc.
  95. * (2) The state of all actors other than those at the old and new locations is unchanged.
  96. * @param loc the location to move to
  97. */
  98. public void makeMove(Location loc) {
  99. if(moveOrNot){
  100. if (loc == null) {
  101. removeSelfFromGrid();
  102. }//end loc == null if
  103. if (getGrid().get(loc) instanceof Rock){
  104. Rock s = new Rock(getGrid().get(loc).getColor());
  105. if(getGrid().isValid(loc.getAdjacentLocation(Location.WEST))){
  106. s.putSelfInGrid(getGrid(), loc.getAdjacentLocation(Location.WEST));
  107. }
  108. Rock b = new Rock(getGrid().get(loc).getColor());
  109. if(getGrid().isValid(loc.getAdjacentLocation(Location.EAST))){
  110. b.putSelfInGrid(getGrid(), loc.getAdjacentLocation(Location.EAST));
  111. }
  112. try{
  113. getGrid().get(loc).removeSelfFromGrid(); } catch(Exception e) {}
  114. moveTo(loc);
  115. } //end rock if
  116. if(getGrid().get(loc) instanceof Actor && !(getGrid().get(loc) instanceof GreninjaCritter)){
  117. Location l = getLocation();
  118. getGrid().get(loc).removeSelfFromGrid();
  119. for(int i = 0; i<3; i++){
  120. loc = loc.getAdjacentLocation(Location.NORTH);
  121. }//end for
  122. if(getGrid().isValid(loc)){
  123. moveTo(loc);
  124. GreninjaCritter f = new GreninjaCritter(false);
  125. f.setColor(getColor());
  126. if(l!=null) {
  127. f.putSelfInGrid(getGrid(), l); }
  128. }//end is valid if
  129.  
  130. else{
  131. //dissapear and create subsitute in a random open location
  132.  
  133. removeSelfFromGrid();
  134. try {
  135. putSelfInGrid(getGrid(), selectMoveLocation(getMoveLocations())); }
  136. catch(Exception e) {}
  137. }//end dissapear else
  138. }//end actor but not greninja if
  139.  
  140. else{
  141. setDirection(getLocation().getDirectionToward(loc)); //turns in the direction it will move in
  142. moveTo(loc);
  143. }
  144. }
  145. } //end method
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement