/* CSE 142, Autumn 2007, Marty Stepp We wrote this critter in this week's section. Elephants move toward a common point in the center of the map, gathering into a herd. */ import java.awt.*; import java.util.*; public class Elephant implements Critter { private int myAttack; private int randomNumber; // 0 for always hungry; 1 for never hungry public Elephant(int attack) { myAttack = attack; Random randy = new Random(); randomNumber = randy.nextInt(2); // pick random number for hunger later } public boolean eat() { return (randomNumber == 1); // 50/50 chance of being true } public int fight(String opponent) { return myAttack; } public Color getColor() { return Color.GRAY; } public int getMove(CritterInfo info) { int direction = CENTER; if (info.getX() != info.getWidth() / 2) { direction = WEST; } else if (info.getY() != info.getHeight() / 2) { direction = NORTH; } if (info.getNeighbor(direction).equalsIgnoreCase(toString())) { direction = CENTER; // don't move if an Elephant is there } return direction; } public String toString() { if (eat()) { return "J"; } else { return "j"; } } }