I need to be able to count steps of the crab so that it uses up "energy" after so many steps. And once it uses up all that "energy" (worms) it is removed from the board.
public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; private int energyBurnRate; //BORDERS: Rate at which the crab burns one worm. This code satisifies requirement R7. private int numberOfSteps; //BORDERS: Stores the number of steps the crab takes. This code satisfies requirement R7. /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = Greenfoot.getRandomNumber(16) + 5; //BORDERS: Sets the initial wormsEaten from 5 to 20. This code satisfies //BORDERS: requirement R5. numberOfSteps = 0; energyBurnRate = 20; } /** * Act - do whatever the crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { turnAtEdge(); randomTurn(); move(); lookForWorm(); switchImage(); } /** * Alternate the crab's image between image1 and image2. */ public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } /** * BORDERS: Check whether we are at the edge of the world. If we are, turn a bit. * If not, do nothing. */ public void turnAtEdge() { if (atWorldEdge()) { turn(17); } } /** * BORDERS: Randomly decide to turn from the current direction, or not. If we * turn, turn a bit left or right by a random degree. */ public void randomTurn() { if (Greenfoot.getRandomNumber(100) > 90) { turn(Greenfoot.getRandomNumber(90)-45); } } /** * Check whether we have stumbled upon a worm. * If we have, eat it. If not, do nothing. */ public void lookForWorm() { if (canSee(Worm.class)) { eat(Worm.class); wormsEaten = wormsEaten + 1; if (wormsEaten == 10) //BORDERS: This code satisfies requirement R6. { return; } } if (wormsEaten <= 0) //BORDERS: This code satisfies requirement R8. { getWorld().removeObject(this); return; } } /** * BORDERS: Count the number of steps by incrementing the numberOfSteps property by one every time the crab moves. This code * satisfies requirement R7. */ public void countSteps() { numberOfSteps = numberOfSteps + 1; } /** * BORDERS: Tell how many steps the crab has moved. This code satisfies requirement R7. */ public int getNumberOfSteps() { return numberOfSteps; } /** * BORDERS: This method simulates the energy that the crab burns when it moves by subtracting one worm every so many steps * given by the burningRate parameter. This code satisfies requirement R7. */ public void burnEnergy(int burningRate) { if (numberOfSteps >= burningRate) { numberOfSteps = 0; wormsEaten = wormsEaten - 1; } } }