// AP(r) Computer Science Marine Biology Simulation: // The DarterFish class is copyright(c) 2002 College Entrance // Examination Board (www.collegeboard.com). // // This class is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation. // // This class is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. import java.awt.Color; /** * AP® Computer Science Marine Biology Simulation:
* The DarterFish class represents a fish in the Marine * Biology Simulation that darts forward two spaces if it can, moves * forward one space if it can't move two, and reverses direction * (without moving) if it cannot move forward. It can only "see" an * empty location two cells away if the cell in between is empty also. * In other words, if both the cell in front of the darter and the cell * in front of that cell are empty, the darter fish will move forward * two spaces. If only the cell in front of the darter is empty, it * will move there. If neither forward cell is empty, the fish will turn * around, changing its direction but not its location. * *

* DarterFish objects inherit instance variables and much * of their behavior from the Fish class. * *

* The DarterFish class is * copyright© 2002 College Entrance Examination Board * (www.collegeboard.com). * * @author APCS Development Committee * @author Alyce Brady * @version 1 July 2002 **/ public class DarterFish extends Fish { // constructors /** Constructs a darter fish at the specified location in a * given environment. This darter is colored yellow. * (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 DarterFish(Environment env, Location loc) { // Construct and initialize the attributes inherited from Fish. super(env, loc); } /** Constructs a darter fish at the specified location and direction in a * given environment. This darter is colored yellow. * (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 * @param dir direction the new fish is facing **/ public DarterFish(Environment env, Location loc, Direction dir) { // Construct and initialize the attributes inherited from Fish. super(env, loc, dir); } /** Constructs a darter fish of the specified color at the specified * location and direction. * (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 * @param dir direction the new fish is facing * @param col color of the new fish **/ public DarterFish(Environment env, Location loc, Direction dir, Color col) { // Construct and initialize the attributes inherited from Fish. super(env, loc, dir, col); } // redefined methods /** Moves this fish in its environment. * A darter fish darts forward (as specified in nextLocation) * if possible, or reverses direction (without moving) if it cannot move * forward. **/ protected void move() { // Find a location to move to. Debug.print(this + " attempting to move. "); Location nextLoc = nextLocation(); // If the next location is different, move there. if ( ! nextLoc.equals(location())) { Fish enemy = (Fish) environment().objectAt(nextLoc); if (enemy != null){ if (age() > enemy.age()){ environment().remove(enemy); changeLocation(nextLoc); Debug.println(" Moves to " + location() + " and eats " + enemy); } else { // Otherwise, reverse direction. changeDirection(direction().reverse()); Debug.println(" Now facing " + direction()+ " (" + enemy + " in the way)."); } } else { changeLocation(nextLoc); Debug.println(" Moves to " + location()); } } else { // Otherwise, reverse direction. changeDirection(direction().reverse()); Debug.println(" Now facing " + direction()); } } /** Finds this fish's next location. * A darter fish darts forward two spaces if it can, otherwise it * tries to move forward one space. A darter fish can only move * to empty locations, and it can only move two spaces forward if * the intervening space is empty. If the darter fish cannot move * forward, nextLocation returns 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 twoInFront = env.getNeighbor(oneInFront, direction()); Debug.println(" Location in front is empty? " + env.isEmpty(oneInFront)); Debug.println(" Location in front of that is empty? " + env.isEmpty(twoInFront)); if ( env.isEmpty(oneInFront) ) { if ( env.isValid(twoInFront) ) return twoInFront; else return oneInFront; } else if (env.isValid(oneInFront)) return oneInFront; // Only get here if there isn't a valid location to move to. Debug.println(" Darter is blocked."); return location(); } }