regba wrote...
No it is not before, it is either or.
Here are the choices:
Once a Crab has reached zero Worms eaten, it dies and disappears from the world.
Stop the game once all the Worms have been eaten or all the Crabs have died.![Twitter](/assets/twitter-4e19209ef84344ee0c433f4c7bad8d49.png)
![Twitter.hover](/assets/twitter.hover-1fb19a5bafc50deace8f88eaec867845.png)
public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; // R3. /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = 0; wormsEaten= Greenfoot.getRandomNumber(7) +5; //R3. Random number between 5-11. } /** * 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() { //checkKeypress(); lookForWorm(); move(); movement(); switchImage(); randomTurn();// R7 } public void randomTurn() { if (Greenfoot.getRandomNumber(100) < 10 ) // R7 { turn (Greenfoot.getRandomNumber(90)-45);// R7 } } /** * Alternate the crab's image between image1 and image2. */ public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } /** * Check whether a control key on the keyboard has been pressed. * If it has, react accordingly. */ //public void checkKeypress() { if (Greenfoot.isKeyDown("left")) { turn(-4); } if (Greenfoot.isKeyDown("right")) { turn(4); } } /** * 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) && wormsEaten < 7 ) // R4 { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten > 6) // R4 { move(); //Greenfoot.playSound ("fanfare.wav"); } // if (wormsEaten == 8) { //Greenfoot.playSound("fanfare.wav"); //Greenfoot.stop(); } } } private int moveCount = 0; private void movement() // R5 { move(1); moveCount++; if (moveCount == 20) { moveCount = 0; wormsEaten = wormsEaten - 1; if (wormsEaten == 0) //R6 { getWorld().removeObject(this); } } } }
public void act() { if ( countCrabsInTheWorld() <= 0 || countWormsInTheWorld() <= 0 ) { Greenfoot.stop(); } } public int countCrabsInTheWorld() { int crabCount; crabCount = getObjects(Crab.class).size(); return crabCount; } public int countWormsInTheWorld() { int wormsCount; wormsCount = getObjects(Worm.class).size(); return wormsCount; }
public void act() { if (getObjects(Crab.class).isEmpty() || getObjects(Worm.class).isEmpty()) greenfoot.stop(); }