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

2013/4/27

Taking any help I can get...

jennnnay24 jennnnay24

2013/4/27

#
I have done 1-5 as well as 9. I know that it will be the same for 5-8 as 9-12. I just need help knowing how to do it the first time. If there is ANYTHING you can do to help me out, I'd appreciate it. R01. At start time, 5 crab are placed randomly in the crab world (use loops). R02. At start time, 2 lobsters are placed randomly in the crab world (use loops). R03. At start time, 50 worms are placed randomly in the crab world (use loops). R04. The crab and the lobsters move in random directions each time they move. R05. Set the crab initial worms eaten to a random number from 5 to 27 (energy). R06. Once the crab has eaten 10 worms, the crab is full and cannot eat more worms. R07. At every move, the crab will lose 1/20 of a worm (energy). R08. When the crab runs out of energy it cannot move and dies (removed from the world). R09. Set the Lobster initial crabs eaten to a random number from 10 to 39 (energy). R10. Once the Lobster has eaten 20 crabs, the Lobster is full and cannot eat more crabs. R11. At every move, the Lobster will lose 1/40 of a crab (energy). R12. When the Lobster runs out of energy it cannot move and dies (removed from the world). R13. Stop the game once all the Worms, or Crabs, or Lobsters’ numbers (population) reaches zero.
Duta Duta

2013/4/28

#
I guess you've moved over to this thread now. To add to what I said in the previous one, consider creating a new variable "energy" to go alongside "wormsEaten". This should deal with at least 7, 8, 11 & 12. I'm about to go to bed so can't be more help, but you should post the code if you want to get help - I know you did it in the previous thread, but people won't know that. Copying it across would be useful. Anywho, off to sleep. Sorry I couldn't be of more help, and good luck!
jennnnay24 jennnnay24

2013/4/28

#
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) 
            {
                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.