/**
* Write a description of class CIrclefish here.
*
* @author (your name)
* @version (a version number or a date)
*/
// CircleFish.java
import java.awt.Color;
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 circle fish 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 <code>env</code>
* @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 circle fish 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 <code>env</code>
* @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
/** Creates a new circle fish, with the same color as this
* @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 circle fish 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 circle fish turn right,
* thereby avoiding being trapped at a side or corner.
**/
protected void move()
{
// Find a location to move to.
Debug.print("CircleFish " + 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)
{
justTurned = false;
}
else{
changeDirection(direction().toRight());
justTurned = true;
Debug.println(" Now facing " + direction());
}
}
}
/** Finds this fish's next location.
* A circle fish alternates moves one location forward, and one location
* on the diagonal to its right.
* If the circle fish cannot move forward, then
* <code>nextLocation</code> 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()
{
Location forward = environment().getNeighbor(location(), direction());
Location rightDiag = environment().getNeighbor(forward, direction().toRight());
if(justTurned && environment().isEmpty(forward)){
return forward;
}
else if(!justTurned && environment().isEmpty(rightDiag)){
return rightDiag;
}
else{
return location();
}
}
}