📄 crocodile.java.bak
字号:
import java.util.Random;
import java.util.Vector;
/**
* Crocodile - simulates a catfish - can swim, eat, and consume
* energy in the process.
*
* @author Yuzhen Chen.
*/
public class Crocodile extends Animal
{
/**
* Energy expended to wade during a block of time.
*/
private static final int ENERGY_TO_WADE = 10;
/**
*amount of energy a catfish must expend to eat.
*/
private static final int ENERGY_TO_EAT = 10;
/**
* Lowest possible energy needed for a baby to survive.
*/
private static final int BABY_MIN_ENERGY = 1000;
/**
* Maximum energy that a baby can store.
*/
private static final int BABY_MAX_ENERGY = 2000;
/**
* For each block of time, the min energy grows by a certain amount
*/
private static final int MIN_ENERGY_GROWTH_INCREMENT = 5;
/**
* For each block of time, the max energy grows by a certain amount
*/
private static final int MAX_ENERGY_GROWTH_INCREMENT = 10;
/**
* Name of species
*/
private static final String SPECIES = "Crocodile";
/**
*
* Number of Catfish created
*/
private static int nCrocodilesCreated = 0;
/**
* Constructor. Initialize an algae to start life at a specified
* location with a specified energy. If location is out of bounds,
* locate the catfish at the nearest edge.
*
* @param initialRow - the row at which the catfish is located
* @param initialColumn - the column at which the catfish is located
* @param initialSimulation - the simulation that the catfish belongs to
*/
public Crocodile(
int initialRow,
int initialColumn,
Simulation initialSimulation)
{
super(initialRow,
initialColumn,
initialSimulation,
SPECIES + nCrocodilesCreated,
BABY_MIN_ENERGY,
BABY_MAX_ENERGY);
++nCrocodilesCreated;
}
/**
* This individual belongs to the Croodile; species.
*
* @return The string indicating the species
*/
public String getSpecies()
{
return SPECIES;
}
/**
* The display mechanism to use to display a crocodile.
*
* @return a constant defined in {@link Simulation#IMAGE Simulation} class
*/
public String getDisplayMechanism()
{
return Simulation.IMAGE;
}
/**
* Get the image of the Croodile
*
* @return filename of Croodile image
*/
public String getImage()
{
if(getDirection() == "right")
{
return "/Crocodile-right.gif";
}
if(getDirection() == "left")
{
return "/Crocodile-left.gif";
}
if(getDirection() == "up")
{
return "/Crocodile-up.gif";
}
if(getDirection() == "down")
{
return "/Crocodile-down.gif";
} else
{
return "Crocodile-right.gif";
}
}
/**
* Wade to a new location if possible.
* Consumes some energy.
*/
private void wadeIfPossible()
{
setEnergy(getEnergy() - ENERGY_TO_WADE);
/**
*IF the Catfish is dead,return the Value of the Catfish.
*/
if(isDead())
{
return;
} else
{
// get the lake boundary.
int firstRow = simulation.getFirstRow();// lake boundary
int firstColumn = simulation.getFirstColumn();// lake boundary
int lastRow = simulation.getLastRow();// lake boundary
int lastColumn = simulation.getLastColumn();// lake boundary
// Wade to a random location in the lake.
int newRow = simulation.getRand().nextInt((lastRow - firstRow) + 1);// new location for croc
int newColumn = simulation.getRand().nextInt((lastColumn - firstColumn) + 1);// new location for croc
newRow += firstRow;
newColumn += firstColumn;
// Move to the new location.
moveToRow(newRow);
moveToColumn(newColumn);
return;
}
}
/**
* Eat if food is available in the current location.
*/
private void eatIfPossible()
{
if(isDead())
{
return;
}
Vector foodMaybe = simulation.getNeighbors(getRow(), getColumn(), 0);
for(int neighborIndex = 0; neighborIndex < foodMaybe.size(); neighborIndex++)
{
if(foodMaybe.get(neighborIndex) instanceof Catfish)
{
Catfish fish = (Catfish)foodMaybe.get(neighborIndex);
int energyGained = fish.getEnergy();
fish.die();
// Gain energy.
// And spend ENERGY_TO_EAT, irrespective of amount gained.
setEnergy((getEnergy() + energyGained) - 10);
}
}
}
/**
* Croodile lives its life. It may lose or gain energy.
*/
public void liveALittle()
{
if(isDead())
{
return;//return the value of the crocodile if it's dead.
} else
{
super.liveALittle();
wadeIfPossible();
eatIfPossible();
// As I am growing bigger, I need to increase my minEnergy
// and maxEnergy.
minEnergy = getMinEnergy() + MIN_ENERGY_GROWTH_INCREMENT;
maxEnergy = getMaxEnergy() + MAX_ENERGY_GROWTH_INCREMENT;
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -