All pastes #405081 Raw Edit

Anonymous

public text v1 · immutable
·published 2007-03-21 14:33 UTC
rendered paste body
// Jim Herrero
import java.util.Random;
import java.awt.Color;
import java.util.ArrayList;
public class PredatorFish extends Fish
{
  public PredatorFish(Environment env, Location loc)
  {
    super(env,loc);
  }
  public PredatorFish(Environment env, Location loc, Direction dir)
  {
    super(env,loc,dir);
  }
  public PredatorFish(Environment env, Location loc, Direction dir, Color col)
  {
    super(env,loc,dir,col);
  }
  public String toString()
  {
    return "ForwardFish " + super.toString();
  }
  protected Location nextLocation()
  {
    ArrayList nbrs = environment().neighborsOf(location());
    Direction oppositeDir = direction().reverse();
    Location locationBehind = environment().getNeighbor(location(),
                                                        oppositeDir);
    nbrs.remove(locationBehind);
    Debug.print("Possible new locations are: " + nbrs.toString());
    for(Object l:nbrs)
    {
      Location loc = (Location)l;
      if(!environment().isEmpty(loc))
      {
        Debug.print("Found food at: " + loc);
        return loc;
      }
    }
    Random randNumGen = RandNumGenerator.getInstance();
    int randNum = randNumGen.nextInt(nbrs.size());
	return (Location) nbrs.get(randNum);
  }
  protected void move()
  {
    Debug.print("PredatorFish " + toString() + " attempting to move.  ");
    Location nextLoc = nextLocation();
    Location oldLoc = location();
    if(!environment().isEmpty(nextLoc))
    {
      ((Fish)environment().objectAt(nextLoc)).die();
      Debug.println("  PredatorFish made a kill at " + location());
    }
    changeLocation(nextLoc);
    Direction newDir = environment().getDirection(oldLoc, nextLoc);
    changeDirection(newDir);
    Debug.println("  Moves to " + location() + direction());
  }
}