/** Chapter 4, Exercise Set 2, Question 8 * Create a CircleFish class of fish that, within each step, * continually attempts to move forward and right so that it makes * a diagonal move and goes in a circular direction. * If the fish cannot make the diagonal move, it stays in its current * location but still turns 90 degrees to the right. **/ import java.awt.Color; public class CircleFish extends Fish { // constructors /** Constructs a CircleFish at the specified location in a * given environment. * (Precondition: parameters are non-null; loc is valid * for env.) * @param env environment in which fish will live * @param loc location of the new fish in env **/ public CircleFish(Environment env, Location loc) { // Construct and initialize the attributes inherited from Fish. super(env, loc, env.randomDirection(), Color.orange); } /** Constructs a CircleFish at the specified location and direction * in a given environment. * (Precondition: parameters are non-null; loc and * dir are valid for env.) * @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 attributes inherited from Fish. super(env, loc, dir, Color.orange); } /** Constructs a CircleFish of the specified color at the specified * location and direction. * (Precondition: parameters are non-null; loc and * dir are valid for env.) * @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 attributes inherited from Fish. super(env, loc, dir, col); } // 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. * included in case the CircleFish breeds * @param loc location of the new fish **/ protected void generateChild(Location loc) { // Create a new fish, which adds itself to the environment. CircleFish child = new CircleFish(environment(), loc, environment().randomDirection(), color()); Debug.println(" Newly created: " + child.toString()); } /** Moves this fish in its environment. * A CircleFish moves in a circular pattern (as specified * in nextLocation) if possible, * or remains in its current location and turns right. **/ protected void move() { Location oldLoc = location(); super.move(); if (oldLoc.equals(location())) { // Otherwise, turn right. ChangeDirection(direction().toRight()); } } /** Finds this fish's next location. * A CircleFish moves to the right diagonally if it can, otherwise * it remains in its current location and turns right. * A CircleFish can only move to empty locations diagonally forward * and to the right. * If the CircleFish cannot move diagonally forward, * nextLocationreturns the fish's current location. * @return the next location for this fish **/ protected Location nextLocation() { Environment env = environment(); Location oneInFront = env.getNeighbor(location(), direction()); Location inFrontAndRight = env.getNeighbor(oneInFront, direction().toRight()); // Test for empty location diagonally up and right if ( env.isEmpty(inFrontAndRight) ) { Debug.println(" Location diagonally up and right is empty? " + env.isEmpty(inFrontAndRight)); return inFrontAndRight; } // Only get here if there isn't a valid location to move to. Debug.println(" Circle is blocked."); return location(); } }