healthcrock

Duke 2011 M05 25
621
1
0
0

import java.util.Vector; public class HealthCrocodile extends Crocodile { private static final int ENERGY_LOST_TO_DISEASE = 35; private static final int LIFE_SPAN_OF_DISEASE = 5; private static...

import java.util.Vector;

public class HealthCrocodile extends Crocodile {

private static final int ENERGY_LOST_TO_DISEASE = 35;

private static final int LIFE_SPAN_OF_DISEASE = 5;

private static final String SPECIES = "HealthCrocodile";

private boolean isHealthy = true;

private boolean isImmune = false;

private int sickTimeBlocks = 0;

private static boolean shouldNextBeDiseased = true;

public HealthCrocodile(
int initialRow,
int initialColumn,
Simulation initialSimulation) {

super(
initialRow,
initialColumn,
initialSimulation);

//first crocodile should be diseased
//the remaining should be healthy
if (shouldNextBeDiseased) {
infect();
shouldNextBeDiseased = false;
}

}

public String getSpecies() {

return SPECIES;
}

private boolean isHealthy() {

return isHealthy;
}

private void cure() {

isHealthy = true;
}

private void infect() {

isHealthy = isImmune;
}

private void immunize() {

isImmune = true;
}

public String getImage() {

String imageBase = "/Crocodile-";
if(!isHealthy()) {
imageBase="/SickCrocodile-";
}

if (getDirection().equals(RIGHT)) {
return imageBase + "right.gif";
}
if (getDirection().equals(LEFT)) {
return imageBase + "left.gif";
}
if (getDirection().equals(UP)) {
return imageBase + "up.gif";
}
if (getDirection().equals(DOWN)) {
return imageBase + "down.gif";
}

return imageBase + "right.gif";
}

public void liveALittle() {

super.liveALittle();
if(!isHealthy()) {
progressDisease();
}
}

private void infectIfPossible() {

Vector cohabitant;
int neighborIndex;

cohabitant = simulation.getNeighbors(getRow(), getColumn(), 0);
for (neighborIndex = 0;
neighborIndex < cohabitant.size();
++neighborIndex) {

if (cohabitant.get(neighborIndex) instanceof HealthCrocodile) {
HealthCrocodile hc = (HealthCrocodile) cohabitant.get(neighborIndex);
hc.infect();
}
}
}

private void progressDisease() {

sickTimeBlocks++;
infectIfPossible();
setEnergy(getEnergy() - ENERGY_LOST_TO_DISEASE);
recoverIfPossible();
}

private boolean recoverIfPossible() {
if(sickTimeBlocks>LIFE_SPAN_OF_DISEASE) {
cure();
immunize();
return true;
}
return false;
}

}

Оцените пост

0

Комментарии

0
Приглашаю виртуально посетить ОАЭ и многие другие красивые места
Показать комментарии
Дальше