I am pretty new at this and I have no idea how to make the wombat move through every cell to grab the leaves. Ideally, the wombat would go to the border where it would move to the next row and move through the cells in that row. I figured out how to make the wombat turn right and I made a boolean to check if the wombat can turn right based on its location but I have no idea where to go from here. Any help is appreciated.
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; public Wombat() { setDirection(EAST); leavesEaten = 0; } /** * Do whatever the wombat likes to to just now. */ public void act() { if(foundLeaf()) { 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; } /** * Test if the wombat can turn right. */ public boolean canTurnRight() { 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 > 7 || 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; } } /** * Turns towards the right. */ public void turnRight() { switch(direction) { case SOUTH : setDirection(WEST); break; case EAST : setDirection(SOUTH); break; case NORTH : setDirection(EAST); break; case WEST : setDirection(NORTH); 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; } }