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

2015/6/6

Help with stopping one greep at a tomato

Lednarg Lednarg

2015/6/6

#
I'm trying to stop one Greep at a tomato while the others take the tomato back to them but I can't get a Greep to stop at the tomato. Please help!
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;
/**
 * An Alien is an alien creature that likes to collect tomatoes.
 * 
 * @author (your name here)
 * @version 0.1
 */
public class Alien extends Creature
{
    // Remember: you cannot extend the Alien's memory. So:
    // no additional fields (other than final fields) allowed in this class!
    /**
     * Default constructor for testing purposes.
     */
    public Alien()
    {
        this(null);
    }

    
    /**
     * Create an Alien with its home space ship.
     */
    public Alien(Ship ship)
    {
        super(ship);
    }

    /**
     * Do what a Alien's gotta do.
     */
    public void act()
    {
        super.act();   // do not delete! leave as first statement in act().
        returnToShip();
        reachedWater();
        bounceOffWalls();
        stopAtTomato();
        if(carryingTomato())
         {
           if(atShip())
            {
                dropTomato();
                reachedWater();
                stopAtTomato();
           }
           else
            {
                move();
                reachedWater();
                turnHome();
                bounceOffWalls();
                stopAtTomato();
           }
        }
        else
         {
            move();
            checkFood();
            reachedWater();
            bounceOffWalls();
            stopAtTomato();
        }
    }

    /**
     * Is there any food here where we are? If so, try to load some!
     */
    public void checkFood()
    {
        // check whether there's a tomato pile here
        TomatoPile tomatoPile = (TomatoPile) getOneIntersectingObject(TomatoPile.class);
        if(tomatoPile != null)
        {
            loadTomato();
            setImage("alien-with-food.png");
            turnHome();
            setFlag(1, true);
            stopAtTomato();
        }
    }


    /**
     * This method specifies the name of the author (for display on the result board).
     */
    public static String getAuthorName()
    {
        return "Keegan Grandel";  // write your name here!
    }


    /**
     * This method specifies the image we want displayed at any time. (No need 
     * to change this for the competition.)
     */
    public String getCurrentImage()
    {
        if(carryingTomato())
        {
            return "alien-with-food.png";
        }
        else
        {
            return "alien.png";
        }
    }
    
    public void bounceOffWalls()
    {
        int yCoord = getY();
        if(yCoord <= 0 || yCoord >= 599)
        {
            turn(45);
        }
        int xCoord = getX();
        if(xCoord <= 0 || xCoord >= 799)
        {
            turn(20);
        }
    }
    
    public void returnToShip()
    {
        Actor ship = getOneIntersectingObject(Ship.class);
        if(ship != null)
        {
            setImage("alien.png");
        }
    }
    
    public void reachedWater()
    {
        if(atWater())
        {
            if(carryingTomato() == true)
            {
                turnHome();
            }
            if(carryingTomato() == false)
            {
                turn(45);
                move();
            }
        }
    }
    
    public void stopAtTomato()
    {
        Alien alien = (Alien) getOneIntersectingObject(Alien.class);
        if(!getFlag(1))
         {
            move();
        }
        else
         {
            if(getFlag(1))
             {
                stop();
            }
        }
    }
    
    public void stop()
    {
        move(0);
    }
}
davmac davmac

2015/6/6

#
Line 167 calls 'move(int)' (i.e. the move method with an int parameter), which you are not supposed to do in Greeps. The main problem is that you call stopAtTomato() on line 39, which may or may not move() depending on whether the flag is set. But then, on lines 40-64, you usually call move() anyway. So if 'stopAtTomato()' doesn't move the Greep, the code following it will cause the Greep to move regardless. Eg on line 59 there is a call to 'move()'. You should check the flag before moving, always. Finally, please don't post Greeps code here. The idea with the Greeps competition is for students to come up with their own ideas about how to improve the Greep efficiency (and write their own code to implement it). We don't want it to be possible to find 'pre-made' Greep code.
You need to login to post a reply.