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

2012/12/8

Little Crab - Counting steps

mborders7786 mborders7786

2012/12/8

#
I need to be able to count steps of the crab so that it uses up "energy" after so many steps. And once it uses up all that "energy" (worms) it is removed from the board.
public class Crab extends Animal
{
    private GreenfootImage image1;
    private GreenfootImage image2;
    private int wormsEaten;
    private int energyBurnRate;  //BORDERS: Rate at which the crab burns one worm. This code satisifies requirement R7.
    private int numberOfSteps; //BORDERS: Stores the number of steps the crab takes. This code satisfies requirement R7.
    
    /**
     * 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(16) + 5; //BORDERS: Sets the initial wormsEaten from 5 to 20. This code satisfies
                                                        //BORDERS: requirement R5.
        numberOfSteps = 0;
        energyBurnRate = 20;
    }
        
    /** 
     * 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()
    {
        turnAtEdge();
        randomTurn();
        move();
        lookForWorm();
        switchImage();
    }
    
    /**
     * Alternate the crab's image between image1 and image2.
     */
    public void switchImage()
    {
        if (getImage() == image1) 
        {
            setImage(image2);
        }
        else
        {
            setImage(image1);
        }
    }
    
    /**
     * BORDERS: 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);
        }
    }
    
    /**
     * BORDERS: 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 have stumbled upon a worm.
     * If we have, eat it. If not, do nothing.
     */
    public void lookForWorm()
    {
        if (canSee(Worm.class))
        {
            eat(Worm.class);
            
            wormsEaten = wormsEaten + 1;
            if (wormsEaten == 10) //BORDERS: This code satisfies requirement R6.
            {
                return;
            }
        }
        if (wormsEaten <= 0) //BORDERS: This code satisfies requirement R8.
        {
            getWorld().removeObject(this);
            return;
        }
    }
    
    /**
     * BORDERS: Count the number of steps by incrementing the numberOfSteps property by one every time the crab moves. This code
     * satisfies requirement R7. 
     */
    public void countSteps()
    {
        numberOfSteps = numberOfSteps + 1;
    }
    
    /**
     * BORDERS: Tell how many steps the crab has moved. This code satisfies requirement R7.
     */
    public int  getNumberOfSteps()
    {
       return numberOfSteps;        
    }    
    
    /**
     * BORDERS: This method simulates the energy that the crab burns when it moves by subtracting one worm every so many steps
     * given by the burningRate parameter. This code satisfies requirement R7.
     */
    public void burnEnergy(int burningRate)
    {
        if (numberOfSteps >= burningRate)
        {
            numberOfSteps = 0;
            wormsEaten = wormsEaten - 1;
        }
    }    
}
actinium actinium

2012/12/8

#
heres a link on the subject. The appropriate video is called Timers joc #29. Do the counting from the act method is usually the best way. So you can put countSteps in the act method.
vonmeth vonmeth

2012/12/8

#
Hah, I think we might have the same teacher (unless they gave the same projects) You already have the method that increments the number of steps ( countSteps() ), you just need to implement it by adding it to the act() method. You might want to move the following from your lookForWorm() method to elsewhere or move the lookForWorm() method to the bottom of the act method:
        if (wormsEaten <= 0) //BORDERS: This code satisfies requirement R8.  
        {  
            getWorld().removeObject(this);  
            return;  
        }  
Else, switchImage() will be performed after removing your actor (Crab) from the world. This will cause an exception that will halt the program. R6 is not satisfied as is (R6. Once the crab has eaten 10 worms, the crab is full and cannot eat more worms.) Right now, it eats a worm no matter how many it has eaten.
danpost danpost

2012/12/8

#
vonmeth wrote...
You might want to move the following from your lookForWorm() method to elsewhere or move the lookForWorm() method to the bottom of the act method:
        if (wormsEaten <= 0) //BORDERS: This code satisfies requirement R8.  
        {  
            getWorld().removeObject(this);  
            return;  
        }  
Else, switchImage() will be performed after removing your actor (Crab) from the world. This will cause an exception that will halt the program.
Actually, this is not a problem. The method 'switchImage' never deals with the world or the location that this actor may be at in the world.
mborders7786 mborders7786

2012/12/8

#
See, this is why I'm never taking another programming class again. Thanks guys!
danpost danpost

2012/12/9

#
@mborders7786, I hope you are not taking what I posted the wrong way. I was only correcting vonmeth and stating the 'because the method 'switchImage does not deal with the world or the actor's location, having it called after calling 'lookForWorm' in the act method is not a problem'. I realize that the way it was worded could have been taken in a way it was not intended, and I apologize if that was the case.
actinium actinium

2012/12/9

#
Programming is not easy. The link i posted really is what you need to watch, it describes what you need to understand. Dont give up.
danpost danpost

2012/12/9

#
I found that the Java trails (tutorials) helped out immensely in learning the basics. The second section on the left 'Trails Covering the Basics', and the second trail listed 'Learning the Java Language' is an excellent place to start.
vonmeth vonmeth

2012/12/9

#
danpost wrote...
@mborders7786, I hope you are not taking what I posted the wrong way. I was only correcting vonmeth and stating the 'because the method 'switchImage does not deal with the world or the actor's location, having it called after calling 'lookForWorm' in the act method is not a problem'. I realize that the way it was worded could have been taken in a way it was not intended, and I apologize if that was the case.
Thank you for the correction, I had not realized that. =)
mborders7786 mborders7786

2012/12/9

#
@danpost, Not at all. I appreciate all the help I've had on here. Just don't think I have the gift of programming.
You need to login to post a reply.