📄 livingbeing.java
字号:
/*!Begin Snippet:LivingBeing*/
/*
* Created on Jul 4, 2003
*
*/
/**
* Each living being lives in a location (row, column) at any given time.
* More than one living being can be in a location. E.g.: algae and fish.
* Depending on the species, living beings could move.
* <p>
* Each living being has some energy level. If the energy level falls below
* the minimum needed to live, it dies. When a living being dies, it is deleted
* from the simulation.
* </p>
*
* @author iCarnegie av
*/
public class LivingBeing {
/**
* The living being is born "alive".
* Then it dies, becoming a corpse.
*/
protected static final String ALIVE = "alive";
/**
* The living being is born "alive".
* Then it dies, becoming a "dead" corpse.
*/
protected static final String DEAD = "dead";
/**
* Row-wise location of the living being
*/
protected int row;
/**
* Column-wise location of the living being
*/
protected int column;
/**
* Is the living being dead or alive?
*/
private String deadOrAlive;
/**
* Amount of energy the living being has.
*/
private int energy;
/**
* Age expressed as blocks of time lived
*/
private int age = 0;
/**
* Name of this living being.
*/
private final String name;
/**
* The simulation to which this living being belongs.
* This is needed so the living being can send a message
* to simulation and ask
* for prey (or predator) in the neighboring locations.
* Prey is food. Food is good!
*/
protected Simulation simulation;
/**
* Minimum energy level needed to survive.
* The minimum could increase as the individual grows.
*/
protected int minEnergy;
/**
* Maximum energy level that the living being could carry.
* The maximum could change as the individual grows.
*/
protected int maxEnergy;
/**
* Create a living being at a given location with a
* given energy and store the simulation to which the living being belongs.
* Cap row and column within lake boundary.
*
* @param initialRow - the row location of living being
* @param initialColumn - the column locaiton of living being
* @param initialSimulation - the simulation to which this being belongs
* @param initialName - name of the living being
* @param initialMinEnergy - minimum energy to survive
* @param initialMaxEnergy - max energy the living being can carry
*/
protected LivingBeing(
int initialRow,
int initialColumn,
Simulation initialSimulation,
String initialName,
int initialMinEnergy,
int initialMaxEnergy) {
simulation = initialSimulation;
deadOrAlive = ALIVE;
// Set the Row within bounds
if (initialRow > simulation.getLastRow()) {
row = simulation.getLastRow();
} else if (initialRow < simulation.getFirstRow()) {
row = simulation.getFirstRow();
} else {
row = initialRow;
}
// Set the Column within bounds
if (initialColumn > simulation.getLastColumn()) {
column = simulation.getLastColumn();
} else if (initialColumn < simulation.getFirstColumn()) {
column = simulation.getFirstColumn();
} else {
column = initialColumn;
}
// Set the minEnergy and maxEnergy
minEnergy = initialMinEnergy;
maxEnergy = initialMaxEnergy;
energy =
simulation.getRand().nextInt(maxEnergy - minEnergy) + minEnergy;
age = 0;
name = initialName;
}
/**
* Create an organism that belongs to a specified species at
* a specified location and add it to the specified simulation.
*
* @param simulation - the simulation this organism belongs to
* @param species - the organism's species
* @param value - row and column values specified as String from HTML form.
* row value is the last two digits,
* column value is given in the remaining digits.
*/
public static void createLivingBeing(
Simulation sim,
String species,
String value) {
int rowAndCol = Integer.parseInt(value);
int row = rowAndCol / 100;
int column = rowAndCol - (100 * row);
if (species.equals("algae")) {
sim.addLivingBeing(new AlgaeColony(row, column, sim));
}
if (species.equals("catfish")) {
sim.addLivingBeing(new Catfish(row, column, sim));
}
if (species.equals("crocodile")) {
sim.addLivingBeing(new Crocodile(row, column, sim));
}
}
/**
* Get the row at which the living being is located
*
* @return - the row of the living being's location.
*/
public int getRow() {
return row;
}
/**
* Get the column at which the living being is located
*
* @return - the column of the living being's location.
*/
public int getColumn() {
return column;
}
/**
* Get the living being's age
*
* @return the age of the living being expressed in blocks of time
*/
public int getAge() {
return age;
}
/**
* Color of the living being expressed in hex notation.
* For example, the "green-est" color is "#00FF00",
* "blue-est" is "#0000FF", the "red-est" is "#FF0000".
*
* @return the rgb color in hex notation. preceded by a pound character '#'
*/
public String getColor() {
return "#FFFFFF"; // default is white.
}
/**
* Get the name of this living being
*
* @return the name of the living being.
*/
public String getName() {
return name;
}
/**
* Get the filename that contains the image of the living being
*
* @return the image of the living being.
*/
public String getImage() {
return "/blank.gif";
}
/**
* Get the preferred display mechanism for living being
*
* @return one of the constants IMAGE or COLOR,
* depending on the display mechanism for the living being.
*/
public String getDisplayMechanism() {
return Simulation.IMAGE;
}
/**
* Get the minimum energy needed to live.
*
* @return the minimum energy needed for the living being to live.
*/
protected int getMinEnergy() {
return minEnergy;
}
/**
* get the maximum energy that the living being can carry.
*
* @return the maximum energy the living being can carry.
*/
protected int getMaxEnergy() {
return maxEnergy;
}
/**
* Returns the species.
*
* @return the species
*/
public String getSpecies() {
return "Unknown";
}
/**
* Get the energy currently carried by the living being.
*
* @return current energy level of the organism
*/
public int getEnergy() {
return energy;
}
/**
* Sets energy level.
* If new energy level is less than minimum energy level, the organism dies.
* New energy level is capped at maximum energy level.
*/
protected void setEnergy(int newEnergy) {
if (newEnergy < getMinEnergy()) {
energy = newEnergy;
die();
} else if (newEnergy > getMaxEnergy()) {
energy = getMaxEnergy();
} else {
energy = newEnergy;
}
}
/**
* Die: Change the deadOrAlive to DEAD.
*/
protected void die() {
deadOrAlive = DEAD;
}
/**
* Is the living being dead?
*
* @return <code>true</code> if dead. <code>false</code>, otherwise.
*/
public boolean isDead() {
return (deadOrAlive == DEAD);
}
/**
* The living being's age increases by one time block.
*/
public void liveALittle() {
age = age + 1;
}
}
/*!End Snippet:LivingBeing*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -