Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.05 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.util.*;
  3. import info.gridworld.grid.*;
  4. import info.gridworld.actor.*;
  5.  
  6. /**
  7.  *
  8.  * @author Andrew Song, Siddharth Gampa, Willie Chen
  9.  * Period 4
  10.  *
  11.  * This class simulates a critter that self-destruct after
  12.  * a number of steps, but survives and lands on a random spot.
  13.  */
  14. public class BombCritter extends Critter{
  15.    
  16.     private int maxSteps, steps;
  17.     private Random rand;
  18.    
  19.     /**
  20.      * Initializes the Critter with given number of steps
  21.      * @param num - number of steps before destruction
  22.      */
  23.     public BombCritter(int num){
  24.         maxSteps = num;
  25.         steps = 0;
  26.         setColor(Color.YELLOW);
  27.         rand = new Random();
  28.     }//end constructor
  29.  
  30.     /**
  31.      * Eats everything; however, if a rock,
  32.      * the Critter turns it into a flower.
  33.      */
  34.     public void processActors(ArrayList <Actor> actors){
  35.         for(Actor a: actors){
  36.             Location current = a.getLocation();
  37.             a.removeSelfFromGrid();
  38.             if(a instanceof Rock || a instanceof Bug){
  39.                 Flower flow = new Flower();
  40.                 flow.putSelfInGrid(getGrid(), current);
  41.             }//end if
  42.         }//end for-each
  43.     }//end method
  44.    
  45.     /**
  46.      * Acts like a normal critter if number of steps taken
  47.      * is less than given; otherwise blows up, leaving
  48.      * bomb fragments.
  49.      */
  50.     public void makeMove(Location loc){
  51.         if(steps < maxSteps){
  52.             super.makeMove(loc);
  53.             steps++;
  54.         }else{
  55.             Grid <Actor> current = getGrid();
  56.             ArrayList <Location> surroundings = getGrid().getValidAdjacentLocations(getLocation());
  57.             for(int a = 0; a < surroundings.size(); a++){
  58.                 Rock bombFrag = new Rock(Color.RED);
  59.                 bombFrag.putSelfInGrid(getGrid(), surroundings.get(a));
  60.             }//end for
  61.             if(current instanceof UnboundedGrid<?>){
  62.                 Location curLoc = getLocation();
  63.                 moveTo(new Location(curLoc.getRow() + rand.nextInt(21) - 10, curLoc.getCol() + rand.nextInt(21) - 10));
  64.                 //Moves to a random new location within 10 spaces of the original
  65.             }//end if
  66.             moveTo(new Location(rand.nextInt(current.getNumRows()),rand.nextInt(current.getNumCols())));
  67.             steps = 0;
  68.         }//end statement
  69.     }//end method
  70.    
  71. }//end class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement