The aim of this was so the bug would check for obstacles and avoid them.I keep on getting this error message -
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
import java.util.ArrayList;
/**
* Write a description of class Bug here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bug 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 foodEaten;
public Bug()
{
setDirection(NORTH);
}
/**
* Act - do whatever the Bug wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (atEnd()){
removeBug();
Greenfoot.stop();
}
else if(foundFood()) {
eatFood();
}
else if (canMove()) {
processKeys();
move(); }
else {
processKeys();
}
}
/**
* Process keys to handle keyboard input
*/
private void processKeys()
{
if (Greenfoot.isKeyDown("left"))
{
setDirection(WEST);
}
else if (Greenfoot.isKeyDown("right"))
{
setDirection(EAST);
}
else if (Greenfoot.isKeyDown("up"))
{
setDirection(NORTH);
}
else if (Greenfoot.isKeyDown("down"))
{
setDirection(SOUTH);
}
}
/**
* Sets the direction we're facing.
*/
public void setDirection(int direction)
{
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;
}
}
/**
* 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;
}
else{
return true;
}
List obstacles = myWorld.getObjectsAt(x,y,Obstacle.class);
List bugs = myWorld.getObjectsAt(x,y,Bug.class);
if(obstacles.isEmpty() && bugs.isEmpty()){
return true;
}
else{
return false;
}
}
/**
* 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 are in top left cell (home).
*/
private boolean atEnd()
{
int x = getX(); //get current x position of bug
int y = getY(); //get current y position of bug
if (x == 0 && y == 0 || x == 59 && y == 39 || foodEaten>5) { //if x and y are equal to 0
return true; // return true
}
else { //otherwuse return false
return false;
}
}
/**
* foundfood method.
*/
private boolean foundFood()
{
Actor food = getOneObjectAtOffset(0, 0, Food.class);
if(food != null) {
return true;
}
else {
return false;
}
}
/**
*eatfood method.
*/
private void eatFood()
{
Actor food = getOneObjectAtOffset(0, 0, Food.class);
if(food != null) {
// eat the leaf...
getWorld().removeObject(food);
foodEaten = foodEaten + 1;
}
else if(canMove()) {
move();
}
}
/**
* Remove the bug from the world.
*/
private void removeBug()
{
World myWorld = getWorld(); // get the world to remove the bug from
myWorld.removeObject(this); // remove the bug (this)
}
}