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

2013/4/27

Would someone be so kind?

jennnnay24 jennnnay24

2013/4/27

#
Can someone please help me out with these?
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);
        }
    }
}
1. Once the crab has eaten 10 worms, the crab is full and cannot eat more worms. 2. At every move, the crab will lose 1/20 of a worm (energy). 3. When the crab runs out of energy it cannot move and dies (removed from the world).
Duta Duta

2013/4/27

#
1. In lookForWorm(), change the condition part of the first if-statement to include an extra wormsEaten < 10 check. You should also remove the if(wormsEaten == 8) { /* stop game */ } code. 2. You need to first change the type of wormsEaten from int to double, and then each act method remove 0.05 from its value (0.05 is 1/20). 3. Once you've done (2), then you just need to add an if-statement to the end of the act() method. Its condition would be wormsEaten (i.e. energy) is less than or equal to 0, and the code would do:
getWorld().removeObject(this);
jennnnay24 jennnnay24

2013/4/27

#
The problem with 1 is that another requirement is that the crab's initial worms eaten have to be between 5 and 27
You need to login to post a reply.