Results 1 to 3 of 3
  1. #1
    Jammn3 is offline Member
    Join Date
    Oct 2012
    Location
    Sufflok, VA
    Posts
    1
    Rep Power
    0

    Default Beginner needs help with Wombats scenario in Greenfoot

    Hi,
    I have customized code for the Wombats class in Greenfoot. I am very new in Java and am not able to figure out exactly what I am supposed to do to change the code to read the following:

    I have adjusted the code to have the Wombat eat the first 5 leafs it finds and then to eat a leaf after the 24 steps. I still need to do the following:

    Change the wombats’ code so that each time they move, they use 1/24 of leaf energy.

    If the leaves Eaten is less than 5 the wombat can eat more leaves.

    I have tried to attach this code as an attachment and have not been able to so, here it goes!

    import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)

    import java.util.List;
    import java.util.ArrayList;

    /**
    * Wombat. A Wombat moves forward until it can't do so anymore, at
    * which point it turns left. If a wombat finds a leaf, it eats it.
    *
    * @author Michael Kolling
    * @version 1.0.1
    */
    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;
    private int stepsTaken;//J.Mojica count number of steps taken
    private int moves;//J. Mojica count number of moves made

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

    /**
    * Do whatever the wombat likes to to just now.
    */
    public void act()
    {
    if(foundLeaf()&&leavesEaten<5)//J. Mojica if finds leaves don't eat more than 5 leafs
    {
    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;
    }


    {
    moves++;
    if(moves >= 24){//JMojica when and if wombat moves eat every 24 moves
    moves = 0;
    leavesEaten--;
    }
    }
    }

    /**
    * 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;
    }
    }


    I think I am supposed to add the act and then change the method. But every thing I write is always returned with an error.

    Any suggestions?

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,452
    Rep Power
    20

    Default Re: Beginner needs help with Wombats scenario in Greenfoot

    Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,183
    Rep Power
    20

    Default Re: Beginner needs help with Wombats scenario in Greenfoot

    First use [code] tags [/code] when posting code.
    Second, if you're getting errors then show us the code causing the errors and copy/paste the full error message here.
    Without seeing what you've tried we can't tell where it is you are going wrong.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Greenfoot help
    By Big-D in forum New To Java
    Replies: 2
    Last Post: 05-17-2012, 04:24 PM
  2. need help in ERD scenario
    By Qurawe in forum JDBC
    Replies: 4
    Last Post: 06-30-2011, 11:56 PM
  3. Scenario
    By ron87 in forum New To Java
    Replies: 1
    Last Post: 04-01-2009, 06:14 AM
  4. Greenfoot 1.4.1
    By JavaBean in forum Java Software
    Replies: 0
    Last Post: 03-24-2008, 07:02 PM
  5. Greenfoot 1.2.1
    By Jamie in forum Java Software
    Replies: 0
    Last Post: 06-14-2007, 03:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •