Thursday 17 February 2011

TwisterRunner Excersise

This is my twister class.


import info.gridworld.actor.Bug;
import info.gridworld.actor.Actor;
import info.gridworld.grid.Grid;
import info.gridworld.actor.ActorWorld;
import java.awt.Color;
import java.util.ArrayList;
import info.gridworld.grid.Location;

/**
* Write a description of class Twister here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Twister extends Actor
{
 public Twister()
 {
           setColor (Color.black);
        }
  
        public void act()
        {
                destroy();
                 move ();
        }
  
        public void destroy()
       {

                 Grid<Actor> gr = getGrid();
                
                 ArrayList<Location> locs = gr.getOccupiedLocations();
                  
                 for (Location loc : locs) {
                    
                     Actor a = gr.get(loc);
                      
                     Location aLoc = a.getLocation();
                      
                     if(aLoc.getCol() == getLocation().getCol() &&
                        
                     aLoc.getRow() > getLocation().getRow()) {
                          
                         a.removeSelfFromGrid();
                        }
                    }
       }
  
       public void move()
       {

           Location next = getLocation().getAdjacentLocation(Location.EAST);
           if(Math.random() < 0.5) {
        
               next = getLocation().getAdjacentLocation(Location.WEST);
                  {
                    if(getGrid().isValid(next)) {
                      moveTo(next);
                             } else {
                                 removeSelfFromGrid();
                                }
                            }
                        }
       }
    }

1 comment: