Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import info.gridworld.actor.Actor;
- import info.gridworld.actor.Critter;
- import info.gridworld.grid.Location;
- import info.gridworld.grid.Grid;
- import java.util.ArrayList;
- import java.awt.Color;
- /**
- * TODO bluster critter class that creates a new critter that changes color TODO
- * depending on how many other critters are in a 2-block radius from the
- * critter, and the critter's courage value, the critter will either turn darker
- * or lighter.
- *
- * @author TODO Your Name
- * @version TODO Date
- * @author Period: TODO Your Period
- * @author Assignment: BlusterCritter - GridWorld Part4 Exercise 4
- *
- * @author Sources: TODO I received help from ...
- */
- public class BlusterCritter extends Critter
- {
- private int courage;
- private Color color = getColor();
- /**
- * constructs a new critter
- *
- * @param c - courage level
- */
- public BlusterCritter( int c )
- {
- courage = c;
- }
- /**
- * TODO sets the location of the critter and scans through all the blocks in
- * a 2 block radius to search for how many other critters there are <br />
- * Postcondition: The state of all actors is unchanged.
- *
- * @return a list of actors that this critter wishes to process.
- */
- public ArrayList<Actor> getActors()
- {
- Grid<Actor> gr = getGrid();
- ArrayList<Actor> actors = new ArrayList<Actor>();
- Location location = getLocation();
- for ( int i = location.getRow() - 2; i <= location.getRow() + 2; i++ )
- { // for each space within 2 blocks in row
- for ( int j = location.getCol() - 2; j <= location.getCol()
- + 2; j++ )
- { // for each space within 2 blocks in column
- Location location2 = new Location( i, j );
- if ( getGrid().isValid( location2 ) )
- { // if location is in the grid
- Actor a = gr.get( location2 ); // finds object at location
- if ( a != null && a != this )
- {
- actors.add( a );
- }
- }
- }
- }
- return actors;
- }
- /**
- * TODO if number of actors is less than courage level, turn brighter.
- * otherwise, turn darker <br />
- * Postcondition: (1) The state of all actors in the grid other than this
- * critter and the elements of <code>actors</code> is unchanged. (2) The
- * location of this critter is unchanged.
- *
- * @param actors
- * the actors to be processed
- */
- public void processActors( ArrayList<Actor> actors )
- {
- if ( actors.size() < courage )
- {
- lighten();
- }
- else if ( actors.size() >= courage )
- {
- darken();
- }
- }
- /**
- * TODO darkens the color of the critter
- */
- private void darken()
- {
- setColor( color.darker() );
- }
- /**
- * TODO lightens the color of the critter
- */
- private void lighten()
- {
- setColor( color.brighter() );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement