I need to make a Wombat move towards the closest leaf and eat it until they are all gone.
Are there any good examples of this sort of greedy behavior I can look at?
if (leavesRemain())
{
// rest of phrasing coded here
}
private boolean leavesRemain()
{
return !getWorld().getObjects(Leaf.class).isEmpty(); // is world not empty of leaves
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
import java.awt.Font;
/**
* A Wombat.
*/
public class GreedyWombat 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 moves;
public GreedyWombat()
{
setDirection(EAST);
}
/**
* The Wombat executes its assigned behavior, increases the number of
* moves taken, and then checks if the last Leaf has been eaten.
*/
public void act()
{
wombatBehavior();
moves++;
checkComplete();
}
/**
* The Wombat behavior has to be implemented in this method.
* The Wombat should always take one step in the direction of the closest Leaf.
*/
public void wombatBehavior() {
if(foundLeaf()) {
eatLeaf();
}
else if(canMove()) {
move();
}
}
private boolean leavesRemain()
{
return !getWorld().getObjects(Leaf.class).isEmpty(); //is world not empty of leaves
}
/**
* The method to check if the last Leaf has been eaten by the Wombat.
* If the last Leaf has been eaten, the game stops and the number of moves
* taken by the Wombat is shown on the screen.
*/
public void checkComplete() {
List<Leaf> leaves = getWorld().getObjects(Leaf.class);
if(leaves.isEmpty()) {
getWorld().getBackground().setFont(new Font("Arial", 0, 30));
getWorld().getBackground().drawString("Number of moves: " + moves, 100, 175);
Greenfoot.stop();
}
}
/**
* 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) {
getWorld().removeObject(leaf);
}
}
/**
* Move one cell forward in the current direction.
*/
public void move()
{
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;
}
/**
* Changes the direction the Wombat is facing.
* This is relevant for the image shown and for the change in location
* executed by the move() method.
*/
public void setDirection(int direction)
{
this.direction = direction;
switch(direction) {
case SOUTH :
setImage("wombat.gif");
setRotation(90);
break;
case EAST :
setImage("wombat.gif");
setRotation(0);
break;
case NORTH :
setImage("wombat-left.gif");
setRotation(90);
break;
case WEST :
setImage("wombat-left.gif");
setRotation(0);
break;
default :
break;
}
getImage().scale(29,24);
}
} /**
* The Wombat behavior has to be implemented in this method.
* The Wombat should always take one step in the direction of the closest Leaf.
*/
public void wombatBehavior() {
if(foundLeaf()) {
eatLeaf();
}
else if(canMove()) {
move();
//turn and move toward closest leaf?
}
} /**
* The Wombat behavior has to be implemented in this method.
* The Wombat should always take one step in the direction of the closest Leaf.
*/
public void wombatBehavior() {
if(foundLeaf()) {
eatLeaf();
}
else if(getObjectsInRange(2, "Leaf.class")) {
//turn towards nearest leaf and move in that direction
}
} public void wombatBehavior() {
if(foundLeaf()) {
eatLeaf();
}
while(getNeighbours(2, 2, Leaf.class)) {
//turn towards nearest leaf and move in that direction
}
} public void wombatBehavior() {
if(foundLeaf()) {
eatLeaf();
}
while(getNeighbours(1, false, "Leaf.class")) {
//turn towards nearest leaf and move in that direction
}
}if (<List>)
if (<Boolean>) // or if (<list is not empty>)
public void wombatBehavior() {
if(foundLeaf()) {
eatLeaf();
}
while(getNeighbours((1,false),(1,true) "Leaf.class")) {
//turn towards nearest leaf and move in that direction
}
}while(!getNeighbours(2, 2, Leaf.class).isEmpty())