import java.util.Vector;
public class FloweringWaterLily extends WaterLily {
private static final String SPECIES
= "flowering water lily";
private static final int MIN_AGE_TO_REPRODUCE = 2;
private static final int ENERGY_TO_REPRODUCE = 20;
private static final int STRONG_MIN_ENERGY = 40;
private static final int WEAK_MIN_ENERGY = 20;
private static final String STRONG_IMAGE_NAME
= "/flowering-lily-strong.gif";
private static final String WEAK_IMAGE_NAME
= "/flowering-lily-weak.gif";
private static final String VERY_WEAK_IMAGE_NAME
= "/flowering-lily-very-weak.gif";
private static int nFloweringWaterLilyCreated = 0;
public FloweringWaterLily(
int initialRow,
int initialColumn,
Simulation initialSimulation) {
super(
initialRow,
initialColumn,
initialSimulation,
SPECIES + nFloweringWaterLilyCreated);
nFloweringWaterLilyCreated++;
}
public String getSpecies() {
return SPECIES;
}
public String getImage() {
if (getEnergy() >= STRONG_MIN_ENERGY ) {
return STRONG_IMAGE_NAME;
} else if (getEnergy() >= WEAK_MIN_ENERGY ) {
return WEAK_IMAGE_NAME;
} else {
return VERY_WEAK_IMAGE_NAME;
}
}
public void reproduceIfPossible() {
if (getAge() >= MIN_AGE_TO_REPRODUCE
&& getEnergy() >= ENERGY_TO_REPRODUCE) {
int atRow = getRow();
int atColumn = getColumn();
int lastRow = atRow + 1;
int lastColumn = atColumn + 1;
for (int rowToCheck = atRow - 1;
rowToCheck <= lastRow;
rowToCheck++) {
for (int colToCheck = atColumn - 1;
colToCheck <= lastColumn;
colToCheck++) {
if (rowToCheck >= simulation.getFirstRow()
&& rowToCheck <= simulation.getLastRow()
&& colToCheck >= simulation.getFirstColumn()
&& colToCheck <= simulation.getLastColumn()
&& simulation.getNeighbors(
rowToCheck, colToCheck, 0).size() == 0 ) {
simulation.addLivingBeing(new FloweringWaterLily(
rowToCheck,
colToCheck,
simulation));
setEnergy(getEnergy() - ENERGY_TO_REPRODUCE);
return;
}
}
}
}
}
}
import java.util.Vector;
public class GreenWaterLily extends WaterLily {
private static final String SPECIES = "green water lily";
private static final int MIN_AGE_TO_REPRODUCE = 2;
private static final int ENERGY_TO_REPRODUCE = 10;
private static final int STRONG_MIN_ENERGY = 60;
private static final int WEAK_MIN_ENERGY = 30;
private static final String STRONG_IMAGE_NAME
= "/green-lily-strong.gif";
private static final String WEAK_IMAGE_NAME
= "/green-lily-weak.gif";
private static final String VERY_WEAK_IMAGE_NAME
= "/green-lily-very-weak.gif";
private static int nGreenWaterLilyCreated = 0;
public GreenWaterLily(
int initialRow,
int initialColumn,
Simulation initialSimulation) {
super(
initialRow,
initialColumn,
initialSimulation,
SPECIES + nGreenWaterLilyCreated);
nGreenWaterLilyCreated++;
}
public String getSpecies() {
return SPECIES;
}
public String getImage() {
if (getEnergy() >= STRONG_MIN_ENERGY ) {
return STRONG_IMAGE_NAME;
} else if (getEnergy() >= WEAK_MIN_ENERGY ) {
return WEAK_IMAGE_NAME;
} else {
return VERY_WEAK_IMAGE_NAME;
}
}
public void reproduceIfPossible() {
if (getAge() >= MIN_AGE_TO_REPRODUCE) {
int atRow = getRow();
int atColumn = getColumn();
int lastRow = atRow + 1;
int lastColumn = atColumn + 1;
for (int rowToCheck = atRow - 1;
rowToCheck <= lastRow;
rowToCheck++) {
for (int colToCheck = atColumn - 1;
colToCheck <= lastColumn;
colToCheck++) {
if (getEnergy() >= ENERGY_TO_REPRODUCE) {
if (rowToCheck >= simulation.getFirstRow()
&& rowToCheck <= simulation.getLastRow()
&& colToCheck >= simulation.getFirstColumn()
&& colToCheck <= simulation.getLastColumn()
&& simulation.getNeighbors(
rowToCheck, colToCheck, 0).size() == 0) {
simulation.addLivingBeing(new GreenWaterLily(
rowToCheck,
colToCheck,
simulation));
setEnergy(getEnergy() - ENERGY_TO_REPRODUCE);
}
} else {
return;
}
}
}
}
}
}