rendered paste bodyimport 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();
}
}