Greenfoot programing
843789May 4 2009 — edited May 4 2009Hi
i am just starting out with some of this sort of stuff and i have hit a hurdle.When ever i type in all the code into the text editor it comes up high lighted and i don't know what i am doing wrong below is my version and the version in the tutorial if can see anything that i am doing wrong can you please let me know and how i can fix it thanks.
All that i have done is enter in the text and hit compile
The tutorial:
/**
* Turn in a random direction.
*/
public void turnRandom()
{
// get a random number between 0 and 3...
int turns = Greenfoot.getRandomNumber(4);
// ...an turn left that many times.
for(int i=0; i<turns; i++) {
turnLeft();
}
}
Then we modify ?act()? to make use of turnRandom. The act() method currently reads:
public void act()
{
if(foundLeaf()) {
eatLeaf();
}
else if(canMove()) {
move();
}
else {
turnLeft();
}
}
Replace the call to ?turnLeft()? at the end with a call to ?turnRandom()?.
*******And now my version*******_
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
import java.util.ArrayList;
/**
* Turn in a random direction
*/
{
//get a random number between 0 and 3...
int turns=greenfoot.getRandomNumber(4);
//...an turn left that many times.
for(inti=0;i<turns;i++){
turnRandom();
}
}
{
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;
}
/**
* 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 :
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;
}
}
Edited by: RICHO12 on May 4, 2009 6:00 AM