All pastes #404327 Raw Edit

Fixed

public text v1 · immutable
·published 2007-03-20 23:07 UTC
rendered paste body
import java.awt.Color;
import java.util.Random;
public class ForwardFish extends Fish
{
  private boolean justTurned, north, left, right;
  public ForwardFish(Environment e, Location l)
  {
    super(e,l);
  }
  public ForwardFish(Environment e, Location l, Direction d)
  {
    super(e,l,d,Color.blue);
  }
  public ForwardFish(Environment e, Location l, Direction d, Color c)
  {
    super(e,l,d,c);
  }
  protected void move()
  {
    Debug.print("ForwardFish " + toString() + " attempting to move.");
    Location nextLoc = nextLocation();
        if ( ! nextLoc.equals(location()) )
        {
            changeLocation(nextLoc);
            Debug.println("  Moves to " + location());
        }
        else
        {
            changeDirection(direction().reverse());
            Debug.println("  Now facing " + direction());
        }
  }
  protected Location nextLocation()
  {
    Location forward = environment().getNeighbor(location(), direction());
    Location rightDiag = environment().getNeighbor(forward, direction().toRight());
    Location leftDiag = environment().getNeighbor(forward, direction().toLeft());
    Random randNumGen = RandNumGenerator.getInstance();
    int randNum = randNumGen.nextInt(3);
    Location newloc;
   if(randNum == 2)
     newloc = forward;
   else if(randNum == 1)
   {
     changeDirection(direction().toRight());
     newloc = rightDiag;
   }
   else
   {
     changeDirection(direction().toLeft());
     newloc = leftDiag;
   }
   if ( environment().isEmpty(newloc) )
     return newloc;
   else
     return location();
  }
}