📄 animal.java
字号:
/**
* Animals can move.
*
* @author 张维
* @version 1.0.0
*
*/
public class Animal extends LivingBeing {
/**
* String constant - used to indicate the direction animal is facing.
*/
protected static final String RIGHT = "right";
/**
* String constant - used to indicate the direction animal is facing.
*/
protected static final String LEFT = "left";
/**
* String constant - used to indicate the direction animal is facing.
*/
protected static final String UP = "up";
/**
* String constant - used to indicate the direction animal is facing.
*/
protected static final String DOWN = "down";
/**
* Which direction am I facing.
*/
private String direction;
/**
* Create an animal at a given location with a
* given energy and store the simulation to which the animal belongs.
* Cap row and column within lake boundary.
*
* @param initialRow - the row location of animal
* @param initialColumn - the column locaiton of animal
* @param initialSimulation - the simulation to which this animal belongs
* @param initialName - name of the animal
* @param initialMinEnergy - minimum energy to survive
* @param initialMaxEnergy - max energy the animal can carry
*/
public Animal(
int initialRow,
int initialColumn,
Simulation initialSimulation,
String initialName,
int minEnergy,
int maxEnergy) {
super(
initialRow,
initialColumn,
initialSimulation,
initialName,
minEnergy,
maxEnergy);
direction = RIGHT; // Start by facing east.
}
/**
* Get the direction faced by the animal.
*
* @return the facing direction.
*/
protected String getDirection() {
return direction;
}
/**
* Is the animal hungry?
*
* @return True, if hungry. False, otherwise.
*/
protected boolean isHungry() {
// Hungry, if current energy level is less than twice the
// amount needed for survival.
return (getEnergy() < (2 * getMinEnergy()));
}
/**
* Move the animal 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
*/
protected 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 animal 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.
*/
protected int moveToColumn(int newColumn) {
if (isDead()) {
return -1;
}
// 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;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -