// Class: FastFish class // // Author: Alyce Brady // // 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; import java.util.ArrayList; import java.util.Random; /** * Marine Biology Simulation: * The FastFish class represents a fish in the Marine Biology * Simulation that moves very quickly. A fast fish looks for empty * neighbors that are one or two cells away from it. It can only "see" * an empty location two cells away if the cell in between is empty * also. In other words, if an immediately adjacent location is empty, * the fast fish looks at the cell beyond it in the same direction to * see if it is empty also. The left diagram below shows all the * possible neighboring locations (~) of a fast fish (F). The diagram * on the right shows how neighboring fish (N) can keep the fast fish * from seeing some of its empty neighboring locations (the ones * marked X). *
* ~ X * ~ N * ~ ~ F ~ ~ X N F ~ ~ * ~ ~ * ~ ~ ** *
* FastFish objects inherit instance variables and much of their
* behavior from the Fish class.
*
* @author Alyce Brady
* @author APCS Development Committee
* @version 1 June 2002
**/
public class FastFish extends Fish
{
// constructors
/** Constructs a fast fish at the specified location in a
* given environment. This fast fish is colored cyan.
* (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 FastFish(Environment env, Location loc)
{
// Construct and initialize the attributes inherited from Fish.
super(env, loc, env.randomDirection(), Color.cyan);
}
/** Constructs a fast fish at the specified location and direction in
* a given environment. This fast fish is colored cyan.
* (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 FastFish(Environment env, Location loc, Direction dir)
{
// Construct and initialize the attributes inherited from Fish.
super(env, loc, dir, Color.cyan);
}
/** Constructs a fast fish 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 FastFish(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 key information about this fish.
* @return a string indicating the fish's ID, location, and
* direction
**/
public String toString()
{
return "FF " + super.toString();
}
/** Creates a new fast fish.
* @param loc location of the new fish
**/
protected void generateChild(Location loc)
{
// Create new fish, which adds itself to the environment.
FastFish child = new FastFish(environment(), loc,
environment().randomDirection(),
color());
Debug.println(" New FastFish created: " + child.toString());
}
/** Finds this fish's next location.
* Fast fish may move to an empty adjacent location one or two
* cells away in any direction. To move to a location two cells
* away, though, the intervening location must also be empty.
* If the fast fish cannot move, nextLocation
* returns the fish's current location.
* @return the next location for this fish
**/
protected Location nextLocation()
{
// Get list of possible move locations.
ArrayList moveLocations = findMoveLocs();
Debug.print("Possible new postions are: " +
moveLocations.toString());
// If there are no possible moves, then we're done.
if ( moveLocations.size() == 0 )
return location();
// Randomly choose one neighboring empty location and return it.
Random randNumGen = RandNumGenerator.getInstance();
int randNum = randNumGen.nextInt(moveLocations.size());
return (Location) moveLocations.get(randNum);
}
/** Finds locations to which this fish might move.
* Fast fish can move further than to just their immediate neighbors.
* They can move up to two squares in each of the 4 directions, if
* not blocked.
* @return a list of locations to which this fish might move
**/
protected ArrayList findMoveLocs()
{
Environment env = environment();
// Generate a list of all the immediately adjacent empty neighbors.
ArrayList emptyNbrs = emptyNeighbors();
Debug.println("Has adjacent empty positions: " +
emptyNbrs.toString());
// Build list of possible move locations.
ArrayList moveLocations = new ArrayList();
for ( int index = 0; index < emptyNbrs.size(); index++ )
{
// Add this location to list of possible moves.
Location neighborLoc = (Location) emptyNbrs.get(index);
moveLocations.add(neighborLoc);
// Find the next location over in the same direction.
Direction dirToNeighbor =
env.getDirection(location(), neighborLoc);
Location nextOver = env.getNeighbor(neighborLoc, dirToNeighbor);
if ( env.isEmpty(nextOver) )
{
// Next location over is empty, so add it too.
moveLocations.add(nextOver);
}
}
return moveLocations;
}
}