This site requires JavaScript, please enable it in your browser!
Greenfoot back
jennnnay24
jennnnay24 wrote ...

2013/4/23

This is probably something really easy...

jennnnay24 jennnnay24

2013/4/23

#
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);
        }
    }
}
danpost danpost

2013/4/23

#
Remove line 15 and change line 59 to 'if (wormsEaten == 10)'
jennnnay24 jennnnay24

2013/4/23

#
i cant remove line 15 because one of the requirements on this assignments is to set the crab's initial worms eaten to a random number from 5 to 27.
jennnnay24 jennnnay24

2013/4/23

#
Plus, I dont want Greenfoot to stop after eating 10 worms, I just need the crab to stop eating
danpost danpost

2013/4/23

#
Then you need another field (besides 'wormsEaten') to hold the target of 'wormsEaten'; set it to 'wormsEaten + 10' immediately after line 15. Check for 'wormsEaten' to equal the target on line 59.
danpost danpost

2013/4/23

#
To stop the crab from eating, put the condition (from line 59) on the call to eat (instead of playing the fanfare and stopping the scenario).
jennnnay24 jennnnay24

2013/4/23

#
I dont understand what you mean
danpost danpost

2013/4/23

#
Add another instance field 'private int targetWormsEaten;'; after line 15 set this field to 10 more than whatever 'wormsEaten' was set to. Then, change line 53 to 'if (wormsEaten != targetWormsEaten && canSee(Worm.class))'.
jennnnay24 jennnnay24

2013/4/27

#
so i added the private int targetWormsEaten; but im not sure what to do next.
danpost danpost

2013/4/28

#
What does your Crab class code look like now?
jennnnay24 jennnnay24

2013/4/28

#
public class Crab extends Animal
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private int wormsEaten;
    private int targetWormsEaten;
    
    /**
     * 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) 
            {
                return;
            }
            if (wormsEaten <= 0)  
            {  
            getWorld().removeObject(this);  
            return;  
            } 
        }
    }
    
    /**
     * 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);
        }
    }
      
}
You need to login to post a reply.