Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. import info.gridworld.actor.Actor;
  2. import info.gridworld.actor.Critter;
  3. import info.gridworld.grid.Location;
  4. import info.gridworld.grid.Grid;
  5.  
  6. import java.util.ArrayList;
  7.  
  8. import java.awt.Color;
  9.  
  10.  
  11. /**
  12. * TODO bluster critter class that creates a new critter that changes color TODO
  13. * depending on how many other critters are in a 2-block radius from the
  14. * critter, and the critter's courage value, the critter will either turn darker
  15. * or lighter.
  16. *
  17. * @author TODO Your Name
  18. * @version TODO Date
  19. * @author Period: TODO Your Period
  20. * @author Assignment: BlusterCritter - GridWorld Part4 Exercise 4
  21. *
  22. * @author Sources: TODO I received help from ...
  23. */
  24. public class BlusterCritter extends Critter
  25. {
  26. private int courage;
  27.  
  28. private Color color = getColor();
  29.  
  30.  
  31. /**
  32. * constructs a new critter
  33. *
  34. * @param c - courage level
  35. */
  36. public BlusterCritter( int c )
  37. {
  38. courage = c;
  39. }
  40.  
  41.  
  42. /**
  43. * TODO sets the location of the critter and scans through all the blocks in
  44. * a 2 block radius to search for how many other critters there are <br />
  45. * Postcondition: The state of all actors is unchanged.
  46. *
  47. * @return a list of actors that this critter wishes to process.
  48. */
  49. public ArrayList<Actor> getActors()
  50. {
  51. Grid<Actor> gr = getGrid();
  52. ArrayList<Actor> actors = new ArrayList<Actor>();
  53.  
  54. Location location = getLocation();
  55. for ( int i = location.getRow() - 2; i <= location.getRow() + 2; i++ )
  56. { // for each space within 2 blocks in row
  57. for ( int j = location.getCol() - 2; j <= location.getCol()
  58. + 2; j++ )
  59. { // for each space within 2 blocks in column
  60. Location location2 = new Location( i, j );
  61. if ( getGrid().isValid( location2 ) )
  62. { // if location is in the grid
  63. Actor a = gr.get( location2 ); // finds object at location
  64. if ( a != null && a != this )
  65. {
  66. actors.add( a );
  67. }
  68. }
  69. }
  70. }
  71. return actors;
  72. }
  73.  
  74.  
  75. /**
  76. * TODO if number of actors is less than courage level, turn brighter.
  77. * otherwise, turn darker <br />
  78. * Postcondition: (1) The state of all actors in the grid other than this
  79. * critter and the elements of <code>actors</code> is unchanged. (2) The
  80. * location of this critter is unchanged.
  81. *
  82. * @param actors
  83. * the actors to be processed
  84. */
  85. public void processActors( ArrayList<Actor> actors )
  86. {
  87. if ( actors.size() < courage )
  88. {
  89. lighten();
  90. }
  91. else if ( actors.size() >= courage )
  92. {
  93. darken();
  94. }
  95. }
  96.  
  97.  
  98. /**
  99. * TODO darkens the color of the critter
  100. */
  101. private void darken()
  102. {
  103. setColor( color.darker() );
  104. }
  105.  
  106.  
  107. /**
  108. * TODO lightens the color of the critter
  109. */
  110. private void lighten()
  111. {
  112. setColor( color.brighter() );
  113. }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement