import java.awt.Color;
import java.util.ArrayList;
import java.util.Random;
/**
* Marine Biology Simulation:
* The BDFish class represents a breeding and dying fish subclass in
* the Marine Biology Simulation.
* Each BDfish has a unique ID, which remains constant throughout its
* life. A BDfish also maintains information about its location and
* direction in the environment.
**/
public class BDFish extends Fish
{
private double probOfBreeding; // defines likelihood in each timestep
private double probOfDying; // defines likelihood in each timestep
// constructors and related helper methods
/** Constructs a BDFish at specified location in a given environment.
* The BDFish is assigned a random direction and random color.
* (Precondition: parameters are non-null.)
* @param env environment in which fish will live
* @param loc location of the new fish in env
**/
public BDFish(Environment env, Location loc)
{
// Construct and initialize the attributes inherited from Fish.
super(env, loc, env.randomDirection());
// Define the likelihood that a BDFish will breed or die
// myAge initialization provided, to be used if desired
probOfBreeding = 1.0/7.0;
probOfDying = 1.0/5.0;
}
/** Constructs a BDFish at the specified location and direction in a
* given environment. The BDFish is assigned a random color.
* (Precondition: parameters are non-null.)
* @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 BDFish(Environment env, Location loc, Direction dir)
{
// Construct and initialize the attributes inherited from Fish.
super(env, loc, dir);
// Define the likelihood that a BDFish will breed or die
// myAge initialization provided, to be used if desired
probOfBreeding = 1.0/7.0;
probOfDying = 1.0/5.0;
}
/** Constructs a BDFish of the specified color at the specified
* location and direction.
* (Precondition: parameters are non-null.)
* @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 BDFish(Environment env, Location loc, Direction dir, Color col)
{
// Construct and initialize the attributes inherited from Fish.
super(env, loc, dir, col);
// Define the likelihood that a BDFish will breed or die
// myAge initialization provided, to be used if desired
probOfBreeding = 1.0/7.0;
probOfDying = 1.0/5.0;
}
/** Returns a string representing key information about this BDfish.
* @return a string indicating the fish's ID and location
**/
public String toString()
{
return "BDFish " + super.toString();
}
// modifier method
// (was originally a check for aliveness and a simple call to move)
/** Acts for one step in the simulation.
**/
public void act()
{
// Make sure fish is alive and well in the environment -- fish
// that have been removed from the environment shouldn't act.
if ( ! isInEnv() )
return;
// Try to breed.
if ( ! breed() )
// Did not breed, so try to move.
move();
// Determine whether this fish will die in this timestep.
Random randNumGen = RandNumGenerator.getInstance();
if ( randNumGen.nextDouble() < probOfDying )
die();
// uncomment the following line of code to increase probability
// of dying as fish ages
//probOfDying += 0.1;
}
// helper methods
/** Attempts to breed into neighboring locations.
* @return true
if fish successfully breeds;
* false
otherwise
**/
protected boolean breed()
{
// Determine whether this fish will try to breed in this
// timestep. If not, return immediately.
Random randNumGen = RandNumGenerator.getInstance();
if ( randNumGen.nextDouble() >= probOfBreeding )
return false;
// Get list of neighboring empty locations.
ArrayList emptyNbrs = emptyNeighbors();
Debug.print(toString() + " attempting to breed. ");
Debug.println("Has neighboring locations: " + emptyNbrs.toString());
// If there is nowhere to breed, then we're done.
if ( emptyNbrs.size() == 0 )
{
Debug.println(" Did not breed.");
return false;
}
// Breed to all of the empty neighboring locations.
for ( int index = 0; index < emptyNbrs.size(); index++ )
{
Location loc = (Location) emptyNbrs.get(index);
generateChild(loc);
}
return true;
}
/** Creates a new fish with the color of its parent.
* @param loc location of the new fish
**/
protected void generateChild(Location loc)
{
// Create new fish, which adds itself to the environment.
BDFish child = new BDFish(environment(), loc,
environment().randomDirection(), color());
Debug.println(" Newly created: " + child.toString());
}
/** Removes this fish from the environment.
**/
protected void die()
{
Debug.println(toString() + " about to die ");
environment().remove(this);
}
}