📄 croodile.java.bak
字号:
import java.util.Random;
import java.util.Vector;
/**
* Crocodile - simulates a catfish - can swim, eat, and consume
* energy in the process.
*/
public class Crocodile extends Animal
{
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;
private static final int MIN_ENERGY_GROWTH_INCREMENT = 5;
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;
}
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";
}
}
private void wadeIfPossible()
{
setEnergy(getEnergy() - 10);
if(isDead())
{
return;
} else
{
int firstRow = simulation.getFirstRow();
int firstColumn = simulation.getFirstColumn();
int lastRow = simulation.getLastRow();
int lastColumn = simulation.getLastColumn();
int newRow = simulation.getRand().nextInt((lastRow - firstRow) + 1);
int newColumn = simulation.getRand().nextInt((lastColumn - firstColumn) + 1);
newRow += firstRow;
newColumn += firstColumn;
moveToRow(newRow);
moveToColumn(newColumn);
return;
}
}
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();
setEnergy((getEnergy() + energyGained) - 10);
}
}
}
/**
* Croodile lives its life. It may lose or gain energy.
*/
public void liveALittle()
{
if(isDead())
{
return;
} else
{
super.liveALittle();
wadeIfPossible();
eatIfPossible();
minEnergy = getMinEnergy() + 5;
maxEnergy = getMaxEnergy() + 10;
return;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -