I need to make the crab stop eating worms after eating 10 worms. This is what was given to me...
public class Crab extends Animal
{
private GreenfootImage image1;
private GreenfootImage image2;
private int wormsEaten;
/**
* 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 (23)+5);//Hartley - this makes the crab's initial number of worms eaten from 5 to 27 - satifies R5.
}
/**
* 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()
{
randomTurn();//Hartley - this allows the Crab to move in a random direction each time it moves. Satisfies R4.
move();
lookForWorm();
switchImage();
turnAtEdge();
}
/**
* Alternate the crab's image between image1 and image2.
*/
public void switchImage()
{
if (getImage() == image1)
{
setImage(image2);
}
else
{
setImage(image1);
}
}
/**
* Check whether we have stumbled upon a worm.
* If we have, eat it. If not, do nothing. If we have
* eaten eight worms, we win.
*/
public void lookForWorm()
{
if ( canSee(Worm.class) )
{
eat(Worm.class);
Greenfoot.playSound("slurp.wav");
wormsEaten = wormsEaten + 1;
if (wormsEaten == 8)
{
Greenfoot.playSound("fanfare.wav");
Greenfoot.stop();
}
}
}
/**
* 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 are at the edge of the world. If we are, turn a bit.
* If not, do nothing.
*/
public void turnAtEdge()
{
if ( atWorldEdge() )
{
turn(17);
}
}
}
