import java.util.*;
public class AllergicCrocodile extends Crocodile {
static private final String SPECIES = "Allergic Croc";
private static final int SUFFOCATION_THRESHOLD = 2;
private static final String CROCODILE_WITHOUT_ALLERGY_IMAGE = "/Crocodile-without-allergy.gif";
private static final String CROCODILE_WITH_ALLERGY_IMAGE = "/Crocodile-with-allergy.gif";
private static int nAllergicCrocsCreated =0 ;
int allergenLevel=0;
public AllergicCrocodile(int initialRow, int initialColumn, Simulation initialSimulation) {
super (initialRow, initialColumn,initialSimulation, SPECIES + nAllergicCrocsCreated );
++ nAllergicCrocsCreated;
}
public String getSpecies() {
return SPECIES;
}
public String getImage() {
if (allergenLevel== 0) {
return CROCODILE_WITHOUT_ALLERGY_IMAGE;
} else {
return CROCODILE_WITH_ALLERGY_IMAGE;
}
}
protected void catchAllergy() {
Vector localVector = simulation.getNeighbors(getRow(), getColumn(), 0);
for (int i = 0; i < localVector.size(); ++i)
if (localVector.get(i) instanceof PoisonAlgae) {
PoisonAlgae localPoisonAlgae = (PoisonAlgae)localVector.get(i);
if (localPoisonAlgae.isPoisonous())
allergenLevel += 1;
if (allergenLevel > SUFFOCATION_THRESHOLD) {
isDead();
}
}
}
/** Live for a block of time.*/
public void liveALittle() {
if (isDead()) {
return;
}
catchAllergy();
super.liveALittle();
}
}
import java.util.*;
public class PoisonAlgae extends AlgaeColony {
private static final String SPECIES ="PoisonAlgae";
private static final int ENERGY_TO_GROW =2;
private static final int ADULT_AGE = 2;
private static int nPoisonAlgaeCreated = 0;
private int age = 0;
public PoisonAlgae(int initialRow, int initialColumn, Simulation initialSimulation) {
super ( initialRow, initialColumn, initialSimulation , SPECIES + nPoisonAlgaeCreated);
nPoisonAlgaeCreated++;
}
public String getSpecies() {
return SPECIES;
}
public boolean isPoisonous() {
if ( getAge() >= ADULT_AGE ) {
return true;
}else{
return false;
}
}
private boolean isAlgaeAt(int row, int col) {
Vector neighbors = new Vector();
neighbors= simulation.getNeighbors(row,col,0);
for (int index = 0;index < neighbors.size(); index++ ) {
if ((neighbors.get(index) instanceof AlgaeColony) || (neighbors.get(index) instanceof PoisonAlgae)) {
return true;
}else{
return false;
}
}
return false;
}
private boolean growIfPossible() {
if (getAge() >= ADULT_AGE && getEnergy() >= ENERGY_TO_GROW) {
for (int i = getRow() - 1; i <= getRow() + 1; i++) {
for (int j = getColumn() - 1; j <= getColumn() + 1; j++) {
if (i >= simulation.getFirstRow() &&
i <= simulation.getLastRow() &&
j >= simulation.getFirstColumn() &&
j <= simulation.getLastColumn() &&
isAlgaeAt(i,j) == false) {
PoisonAlgae Palgae = new PoisonAlgae(i, j, simulation);
simulation.addLivingBeing(Palgae);
setEnergy(getEnergy() - ENERGY_TO_GROW);
return true;
}
}
} return false;
} else {
return false;
}
}
public void liveALittle() {
if (isDead()) {
return;
}
growIfPossible();
super.liveALittle();
}
}