Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.util.*;
- import info.gridworld.grid.*;
- import info.gridworld.actor.*;
- /**
- *
- * @author Andrew Song, Siddharth Gampa, Willie Chen
- * Period 4
- *
- * This class simulates a critter that self-destruct after
- * a number of steps, but survives and lands on a random spot.
- */
- public class BombCritter extends Critter{
- private int maxSteps, steps;
- private Random rand;
- /**
- * Initializes the Critter with given number of steps
- * @param num - number of steps before destruction
- */
- public BombCritter(int num){
- maxSteps = num;
- steps = 0;
- setColor(Color.YELLOW);
- rand = new Random();
- }//end constructor
- /**
- * Eats everything; however, if a rock,
- * the Critter turns it into a flower.
- */
- public void processActors(ArrayList <Actor> actors){
- for(Actor a: actors){
- Location current = a.getLocation();
- a.removeSelfFromGrid();
- if(a instanceof Rock || a instanceof Bug){
- Flower flow = new Flower();
- flow.putSelfInGrid(getGrid(), current);
- }//end if
- }//end for-each
- }//end method
- /**
- * Acts like a normal critter if number of steps taken
- * is less than given; otherwise blows up, leaving
- * bomb fragments.
- */
- public void makeMove(Location loc){
- if(steps < maxSteps){
- super.makeMove(loc);
- steps++;
- }else{
- Grid <Actor> current = getGrid();
- ArrayList <Location> surroundings = getGrid().getValidAdjacentLocations(getLocation());
- for(int a = 0; a < surroundings.size(); a++){
- Rock bombFrag = new Rock(Color.RED);
- bombFrag.putSelfInGrid(getGrid(), surroundings.get(a));
- }//end for
- if(current instanceof UnboundedGrid<?>){
- Location curLoc = getLocation();
- moveTo(new Location(curLoc.getRow() + rand.nextInt(21) - 10, curLoc.getCol() + rand.nextInt(21) - 10));
- //Moves to a random new location within 10 spaces of the original
- }//end if
- moveTo(new Location(rand.nextInt(current.getNumRows()),rand.nextInt(current.getNumCols())));
- steps = 0;
- }//end statement
- }//end method
- }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement