CROCKWAR

Duke 2011 M05 25
523
0
0
0

import java.util.*; public class WarCrocodile extends Crocodile{ private static final String SPECIES = "WarCrocodile"; private static final int ENERGY_TO_LOOK_FOR_ENEMIES = 1 ; private static final...

import java.util.*;

public class WarCrocodile extends Crocodile{
private static final String SPECIES = "WarCrocodile";
private static final int ENERGY_TO_LOOK_FOR_ENEMIES = 1 ;
private static final int ENERGY_TO_ATTACK = 5;
private String tribe;
public static final String RED_TRIBE_ID = "red";
public static final String GREEN_TRIBE_ID = "green";

public WarCrocodile(int initialRow, int initialColumn, Simulation initialSimulation, String initialTribe){
super(initialRow, initialColumn, initialSimulation);
tribe = initialTribe;
}

public String getSpecies(){
return SPECIES;
}

public String getTribe(){
return tribe;
}

public String getImage(){
if(getTribe().equals(GREEN_TRIBE_ID)){
if(getDirection() == RIGHT){
return "/Crocodile-right.gif";
}
if(getDirection() == LEFT){
return "/Crocodile-left.gif";
}
if(getDirection() == UP){
return "/Crocodile-up.gif";
}
if(getDirection() == DOWN){
return "/Crocodile-down.gif";
}

return "/Crocodile-right.gif";
} else {
if(getDirection() == RIGHT){
return "/REdCrocodile-right.gif";
}
if(getDirection() == LEFT){
return "/RedCrocodile-left.gif";
}
if(getDirection() == UP){
return "/RedCrocodile-up.gif";
}
if(getDirection() == DOWN){
return "/RedCrocodile-down.gif";
}

return "/RedCrocodile-right.gif";
}
}

private boolean inMyTribe(WarCrocodile croc){
if(getTribe().equals(croc.getTribe())){
return true;
} else {
return false;
}
}

private WarCrocodile lookForEnemy(){
setEnergy(getEnergy()-ENERGY_TO_LOOK_FOR_ENEMIES);

Vector neighbor;
neighbor=simulation.getNeighbors(getRow(), getColumn(), 0);

for(int i=0; i<neighbor.size(); i++){
if(neighbor.get(i) instanceof WarCrocodile){
WarCrocodile enemy = (WarCrocodile) neighbor.get(i);
if(!inMyTribe(enemy)){
return enemy;
}
}
}
return null;
}

private void killEnemy(WarCrocodile enemy){
setEnergy(getEnergy()-ENERGY_TO_ATTACK);
enemy.die();
}

private void attackEnemiesIfPossible(){
if(getEnergy()>=ENERGY_TO_LOOK_FOR_ENEMIES){
WarCrocodile enemy = lookForEnemy();
if(enemy!=null && getEnergy()>=ENERGY_TO_ATTACK){
killEnemy(enemy);
}
}
}

public void liveALittle(){
if(isDead()){
return;
}
attackEnemiesIfPossible();
super.liveALittle();
}
}

Оцените пост

0