Results 1 to 3 of 3
- 11-01-2012, 06:21 AM #1
Member
- Join Date
- Oct 2012
- Location
- Sufflok, VA
- Posts
- 1
- Rep Power
- 0
Beginner needs help with Wombats scenario in Greenfoot
Hi,
I have customized code for the Wombats class in Greenfoot. I am very new in Java and am not able to figure out exactly what I am supposed to do to change the code to read the following:
I have adjusted the code to have the Wombat eat the first 5 leafs it finds and then to eat a leaf after the 24 steps. I still need to do the following:
Change the wombats code so that each time they move, they use 1/24 of leaf energy.
If the leaves Eaten is less than 5 the wombat can eat more leaves.
I have tried to attach this code as an attachment and have not been able to so, here it goes!
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;
private int stepsTaken;//J.Mojica count number of steps taken
private int moves;//J. Mojica count number of moves made
public Wombat()
{
setDirection(EAST);
leavesEaten = 0;
}
/**
* Do whatever the wombat likes to to just now.
*/
public void act()
{
if(foundLeaf()&&leavesEaten<5)//J. Mojica if finds leaves don't eat more than 5 leafs
{
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;
}
{
moves++;
if(moves >= 24){//JMojica when and if wombat moves eat every 24 moves
moves = 0;
leavesEaten--;
}
}
}
/**
* 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. 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;
}
}
I think I am supposed to add the act and then change the method. But every thing I write is always returned with an error.
Any suggestions?
- 11-01-2012, 12:32 PM #2
Re: Beginner needs help with Wombats scenario in Greenfoot
Please go through Guide For New Members and BB Code List - Java Programming Forum and edit your post accordingly.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 11-01-2012, 02:47 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,460
- Rep Power
- 16
Re: Beginner needs help with Wombats scenario in Greenfoot
First use [code] tags [/code] when posting code.
Second, if you're getting errors then show us the code causing the errors and copy/paste the full error message here.
Without seeing what you've tried we can't tell where it is you are going wrong.Please do not ask for code as refusal often offends.
Similar Threads
-
Greenfoot help
By Big-D in forum New To JavaReplies: 2Last Post: 05-17-2012, 03:24 PM -
need help in ERD scenario
By Qurawe in forum JDBCReplies: 4Last Post: 06-30-2011, 10:56 PM -
Scenario
By ron87 in forum New To JavaReplies: 1Last Post: 04-01-2009, 05:14 AM -
Greenfoot 1.4.1
By JavaBean in forum Java SoftwareReplies: 0Last Post: 03-24-2008, 06:02 PM -
Greenfoot 1.2.1
By Jamie in forum Java SoftwareReplies: 0Last Post: 06-14-2007, 02:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks