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

2012/10/22

leaves-and-wombats

mborders7786 mborders7786

2012/10/22

#
Trying to modify or add a method so that every time a wombat moves 24 blocks it uses up one leaf.
Gevater_Tod4711 Gevater_Tod4711

2012/10/22

#
you just need a variable in wombat which counts the moves, and one which counts the leafes. In your eatLeaf method you count up the leafs counter and in your act method you count the moves like this:
pulbic void act() {
    moves++;
    if (moves >= 24) {
        moves = 0;
        leafesEaten--;
    }
}
mborders7786 mborders7786

2012/10/24

#
I get errors trying when I put that in. This is what I have now. Can I modify one of these variables to do what I want?
public void act()
    {
        if(foundLeaf() && leavesEaten < 5) 
        {                                  
            eatLeaf();                      
        }
        else if(canMove()) 
        {
            move();
        }
        else 
        {
            turnLeft();
        }
    }
danpost danpost

2012/10/24

#
If you show the entire code for the class or explain what error message you are getting (in detail), it would help.
mborders7786 mborders7786

2012/10/24

#
It's the basic leaves-and-wombats scenario. The only modification I've made to it is that when it eats 5 leaves it doesn't eat any more. I'm trying to figure out a way so that when he moves 24 times he uses up 1 leaf.
public class Wombat extends Actor
{
    private static final int EAST = 0;
    private static final int WEST = 1;
    private static final int NORTH = 2;
    private static final int SOUTH = 3;

    private int direction;
    private int leavesEaten;

    public Wombat()
    {
        setDirection(EAST);
        leavesEaten = 0;
    }

    /**
     * Do whatever the wombat likes to to just now.
     */
    public void act()
    {
        if(foundLeaf() && leavesEaten < 5) 
        {                                  
            eatLeaf();                      
        }
        else if(canMove()) 
        {
            move();
        }
        else 
        {
            turnLeft();
        }
    }

    /**
     * Check whether there is a leaf in the same cell as we are.
     */
    public boolean foundLeaf()
    {
        Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class);
        if(leaf != null) 
        {
            return true;
        }
        else {
            return false;
        }
    }
    
    /**
     * Eat a leaf.
     */
    public void eatLeaf()
    {
        Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class);
        if(leaf != null) 
        {
            // eat the leaf...
            getWorld().removeObject(leaf);
            leavesEaten = leavesEaten + 1; 
        }
    }
    
    /**
     * Move one cell forward in the current direction.
     */
    public void move()
    {
        if (!canMove()) 
        {
            return;
        }
        switch(direction) 
        {
            case SOUTH :
                setLocation(getX(), getY() + 1);
                break;
            case EAST :
                setLocation(getX() + 1, getY());
                break;
            case NORTH :
                setLocation(getX(), getY() - 1);
                break;
            case WEST :
                setLocation(getX() - 1, getY());
                break;
        }
    }

    /**
     * Test if we can move forward. Return true if we can, false otherwise.
     */
    public boolean canMove()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();
        switch(direction) 
        {
            case SOUTH :
                y++;
                break;
            case EAST :
                x++;
                break;
            case NORTH :
                y--;
                break;
            case WEST :
                x--;
                break;
        }
        // test for outside border
        if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) 
        {
            return false;
        }
        else if (x < 0 || y < 0) 
        {
            return false;
        }
        return true;
    }

    /**
     * Turns towards the left.
     */
    public void turnLeft()
    {
        switch(direction) 
        {
            case SOUTH :
                setDirection(EAST);
                break;
            case EAST :
                setDirection(NORTH);
                break;
            case NORTH :
                setDirection(WEST);
                break;
            case WEST :
                setDirection(SOUTH);
                break;
        }
    }

    /**
     * Sets the direction we're facing. The 'direction' parameter must
     * be in the range [0..3].
     */
    public void setDirection(int direction)
    {
        if ((direction >= 0) && (direction <= 3)) 
        {
            this.direction = direction;
        }
        switch(direction) 
        {
            case SOUTH :
                setRotation(90);
                break;
            case EAST :
                setRotation(0);
                break;
            case NORTH :
                setRotation(270);
                break;
            case WEST :
                setRotation(180);
                break;
            default :
                break;
        }
    }

    /**
     * Tell how many leaves we have eaten.
     */
    public int getLeavesEaten()
    {
        return leavesEaten;
    }
}
danpost danpost

2012/10/24

#
Make the following changes:
// insert at line 10
private int moves;
// insert at line 89 (after the 'switch' block
moves++;
if (moves == 24)
{
    moves = 0;
    if (leavesEaten > 0)
    {
        leavesEaten--;
    }
    else
    {
        // die?
    }
}
mborders7786 mborders7786

2012/10/24

#
Thank you. Just so I understand everything, the private int moves; is what lets the program count the number of moves and the if-else statement is basically "if you've moved 24 times, eat one leave and subtract one from your total"
danpost danpost

2012/10/24

#
The 'private int moves;' declares an integer variable in the world class (just like 'leavesEaten'). The 'moves++;' command will increment the counter once for each time the wombat moves (being we placed it right after the code where the wombat is moved). The 'if-else statement' basically says "if you've moved 24 times, then, if you have previously eaten a leaf, it is consumed else you die". Think of it like 'energy', let's say your wombat started with 24 energy units, each movement consumes one unit and each leaf eaten adds 24 more units. If the number of energy units gets to zero, the wombat will die. If the number of units is greater than 96 (4 leaves worth) then the wombat won't eat because he is already full.
Jammn3 Jammn3

2012/10/31

#
I'm reading this and did this but the Wombat does not die after running out of leaves... How can I get the Wombat to: 1- get low on energy if it doesnt find leaves to eat 2- Show on screen that he is slowing down when does not find the energy source 3- Show that he dies when leaves run out? (Sorry, doing this same scenario)
danpost danpost

2012/11/2

#
You will have to decide how you wish to show that the wombat is low on energy and die. When you have decided how, then you need to program it into your code. If you have a problem with programming it in, then ask for help (explain how you wish to proceed and show what code you have tried; and explain how it is not working the way you intended).
You need to login to post a reply.