PDA

View Full Version : need help finding correction ?


maximus123
05-05-2009, 11:16 PM
im doing a greenfoot program in class and my problem was that some how i can seem to see what are my mistake in this program... can anyone check this out (below)
this are my code :( u might need to download the program call greenfoot to be able to figure out what wrong with my program)

(its a bit long sor sry)

import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage

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. This wombat can not move over rocks.
* 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 GreenfootImage wombatRight;
private GreenfootImage wombatLeft;

public Wombat()
{
wombatRight = getImage();
wombatLeft = new GreenfootImage(getImage());
wombatLeft.mirrorHorizontally();

setDirection(EAST);
leavesEaten = 0;
}

/**
* Do whatever the wombat likes to to just now.
*/
public void act()
{
if(getLeavesEaten >=4) {
breed();
}
turnRandom();
replaceleaves();

}

/**
* Check whether there is a leaf in the same cell as we are.
*/
public boolean foundLeaf()
{
int a=0;
int b=0;
switch(direction) {
case SOUTH :
b++;
break;
case EAST :
a++;
break;
case NORTH :
b--;
break;
case WEST :
a--;
break;
}
Actor leaf = getOneObjectAtOffset(a, b, 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;
//addObject(leaf,x, y);

}
}

/**
* 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;
}
else
{
List obj = myWorld.getObjectsAt(x, y, null);
if(obj.isEmpty())
{
return true;
}
else
{
return false;
}
}
}

/**
* Turn in a random direction.
*/
public boolean turnRandom()
{
if (lookForLeaf()) return true;
else
{
do
{
int rndTurn = (int)(Math.random() * 4+.5);
for (int t=0; t<rndTurn; t++) turnLeft();
} while (!canMove());
move();
return true;

}
}


public boolean lookForLeaf()
{
int intTurn=0;
if (foundLeaf())
{
move();
eatLeaf();
return true;
}
else
{
do
{
turnLeft();
intTurn++;
if (foundLeaf())
{
move();
eatLeaf();
return true;
}
}while (!foundLeaf() && intTurn < 4);
//System.out.println("" + intTurn);
return false;
}
}
/**
* 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.
*/
public void setDirection(int direction)
{
this.direction = direction;
switch(direction) {
case SOUTH :
setImage(wombatRight);
setRotation(90);
break;
case EAST :
setImage(wombatRight);
setRotation(0);
break;
case NORTH :
setImage(wombatLeft);
setRotation(90);
break;
case WEST :
setImage(wombatLeft);
setRotation(0);
break;
default :
break;
}
}

/**
* Tell how many leaves we have eaten.
*/
public boolean breed()
{
world.myWorld = getWorld();
int a = 0;
int b = 0;
int c = getX()+a;
int d = getY()+b;
switch(direction) {
case SOUTH :
b++;
break;
case EAST :
a++;
break;
case NORTH :
b--;
break;
case WEST :
a--;
break;
}
Actor Wombat = getOneObjectAtOffset(a,b,Wombat.class);

if (Wombat!=null)
{
Wombat wombat2 = new Wombat();
List obj = myWorl.getObjectAt(a,b,null);
if(obj.isEmpty() || foundLeaf()) {
myWorld.addObject(wobat2,c,d);
}
else
{
C--;
C++;
}
}
return false;




public int (getLeavesEaten())
{
return leavesEaten;
}

public void replaceleaves()
{
World myWorld = getWorld();
int x;
int y;
for(x=0; x < getX(); x++)
{
for(y=0; y < getY(); y++)
{
List obj = myWorld.getObjectsAt(x, y, null);
if(obj.isEmpty())
{
Leaf l = new Leaf();
myWorld.addObject(l, x, y);
}
}
}

}

}

Fou-Lu
05-05-2009, 11:54 PM
I'm not really interested in downloading the package for this.
What do you mean by not working? Compilation or runtime error? Logic error?
If its a logic error, set a breakpoint on the line(s) in question, and use a debugger to step though the code to compare results to expectations.

maximus123
05-06-2009, 04:32 PM
let be break it up for you , some how with all that code above when i click it to compile the program point it to these code :

public int (getLeavesEaten())
{
return leavesEaten;
}

public void replaceleaves()
{
World myWorld = getWorld();
int x;
int y;
for(x=0; x < getX(); x++)
{
for(y=0; y < getY(); y++)
{
List obj = myWorld.getObjectsAt(x, y, null);
if(obj.isEmpty())
{
Leaf l = new Leaf();
myWorld.addObject(l, x, y);
}
}
}

}

} (end here )


(it said illegal statement)

(to me i i think there got to be a mistake somewhere on code above or these code here((below))

public void act()
{
if(getLeavesEaten >=4) {
breed();
}
turnRandom();
replaceleaves();

}

Fou-Lu
05-06-2009, 11:47 PM
Well, what is this: public int (getLeavesEaten()). It looks like its an un-named method signature that takes a method? That is likely to be a problem.
You should post you're actual error message, the compiler will tell you more information on where to look. Otherwise, these are possible problems:

No scope to World
No scope to List
ReplaceLeaves has one to many closing brackets (though its actually you're class close, it looks like it in you're last post).
Act should be working on the method getLeavesEaten, not a member under that name.


So, the act method is probably you're first problem, then the declaration of a method without a name taking a method (which is um... not ok.).