// CircleFish.java
import java.awt.Color;
/**
* CircleFish objects inherit instance variables and much of their
* behavior from the Fish class.
*
* @author Chris Nevison
* @author Alyce Brady
* @version 25 March 2003
**/
public class CircleFish extends Fish
{
private boolean justTurned;
public CircleFish(Environment env, Location loc)
{
// Construct and initialize the aspects inherited from Fish.
super(env, loc);
justTurned = true;
}
/** Constructs a CircleFish of that is orange at the specified
* location and direction.
* @param env environment in which fish will live
* @param loc location of the new fish in env
* @param dir direction the new fish is facing
**/
public CircleFish(Environment env, Location loc, Direction dir)
{
// Construct and initialize the aspects inherited from Fish.
super(env, loc, dir, Color.orange);
justTurned = true;
}
/** Constructs a CircleFish of the specified color at the specified
* location and direction.
* @param env environment in which fish will live
* @param loc location of the new fish in env
* @param dir direction the new fish is facing
* @param col color of the new fish
**/
public CircleFish(Environment env, Location loc, Direction dir,
Color col)
{
// Construct and initialize the aspects inherited from Fish.
super(env, loc, dir, col);
justTurned = true;
}
// redefined methods
/** Returns a string representing information about this Circlefish.
* @return a string indicating the fish's ID and location
**/
public String toString()
{
return "CircleFish " + super.toString();
}
/** Creates a new CircleFish, with the same color as this CircleFish
* @param loc location of the new fish
* @return the new fish
**/
protected void generateChild(Location loc)
{
new CircleFish(environment(), loc,
environment().randomDirection(), color());
}
/** Moves this fish in its environment.
* A CircleFish alternates moves one location forward, and one
* location on the diagonal to its right, turning to the right
* in the latter case.
* If it does change location, the CircleFish turn right,
* thereby avoiding being trapped at a side or corner.
**/
protected void move()
{
// Find a location to move to.
Debug.print(toString() + " attempting to move. ");
Location oldLoc = location();
Location nextLoc = nextLocation();
if (nextLoc.equals(oldLoc))
{
changeDirection(direction().toRight());
justTurned = true;
Debug.println(" Now facing " + direction());
}
else
{
// Move.
changeLocation(nextLoc);
if (!justTurned)
{
changeDirection(direction().toRight());
justTurned = true;
Debug.println(" Now facing " + direction());
}
else
{
justTurned = false;
}
}
}
/** Finds this fish's next location.
* A CircleFish alternates moves one location forward, and one
* location on the diagonal to its right.
* If the CircleFish cannot move forward, then
* nextLocation
returns the fish's current location.
* If the fish moves forward, the boolean justTurned is set to false,
* otherwise, it is set to true.
* @return the next location for this fish
**/
protected Location nextLocation()
{
Environment env = environment();
Location forward = env.getNeighbor(location(), direction());
Location rightDiag =
env.getNeighbor(forward, direction().toRight());
if (justTurned && env.isEmpty(forward))
{
return forward;
}
else if (!justTurned && env.isEmpty(rightDiag))
{
return rightDiag;
}
else
{
return location();
}
}
}