📄 catfish.java.bak
字号:
/**
* Move the catfish to a new row, if new row is within lake bounds.
*
* @param newRow - the row to move to.
* @return the row moved to. Lake boundary limits movement. -1, if dead.
*/
private int moveToRow(int newRow) {
if (isDead()) {
return -1;
}
// Keep the new value within lake boundary.
if (newRow > simulation.getLastRow()) {
newRow = simulation.getLastRow();
} else if (newRow < simulation.getFirstRow()) {
newRow = simulation.getFirstRow();
}
// I might face a new direction.
if (newRow < row) {
direction = UP;
} else if (newRow > row) {
direction = DOWN;
}
row = newRow;
return row;
}
/**
* Move the catfish to a new column, if new column is within lake bounds.
*
* @param newColumn - the column to move to.
* @return the column moved to. Lake boundary limits movement.
*/
private int moveToColumn(int newColumn) {
if (isDead()) {
return -1;
}
// System.out.println("column = " + column + ", newCOlumn = " + newColumn);
// System.out.flush();
// Keep the new value within lake boundary.
if (newColumn > simulation.getLastColumn()) {
newColumn = simulation.getLastColumn();
} else if (newColumn < simulation.getFirstColumn()) {
newColumn = simulation.getFirstColumn();
}
// I might face a new direction.
if (newColumn < column) {
direction = LEFT;
} else if (newColumn > column) {
direction = RIGHT;
}
column = newColumn;
return column;
}
/**
* This individual belongs to the Catfish species.
*
* @return The string indicating the species
*/
public String getSpecies() {
return SPECIES;
}
/**
* Catfish should be displayed as an image.
*
* @return a constant defined in {@link Simulation#IMAGE Simulation} class
*/
public String getDisplayMechanism() {
return Simulation.IMAGE;
}
/**
* Get the image of the catfish
*
* @return filename of Catfish image
*/
public String getImage() {
if (getDirection() == RIGHT) {
return "/Catfish-right.gif";
}
if (getDirection() == LEFT) {
return "/Catfish-left.gif";
}
if (getDirection() == UP) {
return "/Catfish-up.gif";
}
if (getDirection() == DOWN) {
return "/Catfish-down.gif";
}
return "Catfish-right.gif";
}
/*!Begin Snippet:lookForFoodInNeighborhood*/
/**
* Look for food in the neighborhood. Consume some energy in the process.
*
* @return a neighboring algae that is food.
*/
private AlgaeColony lookForFoodInNeighborhood() {
int neighborIndex;
// Looking for food consumes energy.
setEnergy(getEnergy() - ENERGY_TO_LOOK_FOR_FOOD);
if (isDead()) {
return null;
}
Vector neighbors =
simulation.getNeighbors(getRow(), getColumn(), 1);
for (neighborIndex = 0;
neighborIndex < neighbors.size();
++neighborIndex) {
if (neighbors.get(neighborIndex) instanceof AlgaeColony) {
return (AlgaeColony) neighbors.get(neighborIndex);
}
}
return null;
}
/*!End Snippet:lookForFoodInNeighborhood*/
/**
* Swim to a new location if possible.
* Consumes some energy.
*/
private void swimIfPossible() {
AlgaeColony food;
if (isDead()) {
return;
}
// If hungry, swim to a location where there is food.
if (isHungry()) {
// Naive swimming - We are not checking if a predator is in the destination.
food = lookForFoodInNeighborhood();
if (food != null) {
// Consume energy to swim.
setEnergy(getEnergy() - ENERGY_TO_SWIM);
if (isDead()) {
return;
}
if (DEBUG > 50) {
System.out.println(
"moving from (row, col) = ("
+ getRow()
+ ", "
+ getColumn()
+ ") to ("
+ food.getRow()
+ ", "
+ food.getColumn()
+ ")");
}
moveToRow(food.getRow());
moveToColumn(food.getColumn());
} else {
// Do not swim. Need to conserve energy.
}
return;
} else {
// Consume energy to swim.
setEnergy(getEnergy() - ENERGY_TO_SWIM);
if (isDead()) {
return;
}
// Swim at random in one of four directions.
// Naive swimming - We are not checking if a predator is in the destination.
int direction = simulation.getRand().nextInt(4);
// Swim up.
if (direction == 0) {
moveToRow(getRow() - 1);
}
// Swim down.
if (direction == 1) {
moveToRow(getRow() + 1);
}
// Swim left.
if (direction == 2) {
moveToColumn(getColumn() - 1);
}
// Swim right.
if (direction == 3) {
moveToColumn(getColumn() + 1);
}
}
}
/**
* Eat food if available.
*
*/
private void eatIfPossible() {
Vector foodMaybe;
int neighborIndex;
if (isDead()) {
return;
}
foodMaybe = simulation.getNeighbors(getRow(), getColumn(), 0);
for (neighborIndex = 0;
neighborIndex < foodMaybe.size();
++neighborIndex) {
if (foodMaybe.get(neighborIndex) instanceof AlgaeColony) {
AlgaeColony alg = (AlgaeColony) foodMaybe.get(neighborIndex);
int energyGained = alg.giveUpEnergy(ENERGY_IN_A_FULL_MEAL);
// Spend ENERGY_TO_EAT, irrespective of amount gained.
setEnergy(getEnergy() + energyGained - ENERGY_TO_EAT);
return;
}
}
}
/**
* Catfish lives its life. It may lose or gain energy.
*/
public void liveALittle() {
if (isDead()) {
return;
}
age = age + 1;
swimIfPossible();
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;
mateIfPossible();
}
/**
* Minimum age at which fish can mate.
*/
private static final int ADULTHOOD_AGE = 10;
private Catfish findMate() {
/* Student To Do: Add code to find a mate.
* If a mate is found, return the mate object, else return null
*/
/*Use the getNeighbors method of simulation to get the cohabitants of the catfish.
*/
Vector matesMaybe=simulation.getNeighbors(getRow(), getColumn(), 0);
for (int fishIndex = 0;
fishIndex < matesMaybe.size();
++fishIndex) {
if (matesMaybe.get(fishIndex) instanceof Catfish) {
Catfish fish = (Catfish) matesMaybe.get(fishIndex);//A catfish cannot mate with itself! So, check if the mate is a Catfish object and is different from the catfish object that is looking for a mate. Make use of the fact that each catfish has a unique name.
if(fish.getName()!=name && fish.getAge()>ADULTHOOD_AGE)//A mate must be at least 10 time blocks old.
{
return fish;
}
}
}
return null;
}
/**
* Energy needed to mate
*/
private static final int ENERGY_TO_MATE = 30;
/**
* Give birth if possible.
*
*/
private void mateIfPossible() {
Catfish mate;
Catfish baby;
int energyNeeded;
if (isDead()) {
return;
}
mate = findMate();
if (mate == null) {
return;
}
// Name for the baby is my name followed by child number.
energyNeeded = BABY_MIN_ENERGY + ENERGY_TO_MATE;
// I must have reached adulthood age, and have enough energy to mate
// and enough energy for the child.
if (age < ADULTHOOD_AGE || getEnergy() < energyNeeded) {
return;
}
// My mate does not lose energy. But I do.
setEnergy(getEnergy() - ENERGY_TO_MATE);
// Give birth to a new baby.
baby = new Catfish(
getRow(),
getColumn(),
simulation);
// Add my baby to simulation.
simulation.addLivingBeing(baby);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -