This site requires JavaScript, please enable it in your browser!
Greenfoot back
infant17
infant17 wrote ...

2013/4/26

losing energy

1
2
3
danpost danpost

2013/4/29

#
infant17 wrote...
im still getting error that says illegal start type on line if(wormEnergy=<0).......
You were probably getting this error because '=<' should be '<=' (order is important).
infant17 infant17

2013/4/29

#
I figured that was the reason and fixed the problem then after the crabs wasn't moving so I figured out how to fix that problem but the program isn't doing what it suppose to do........the crabs aren't losing energy and the crabs are eating more than 10 worms and their not dying........this is so frustrating, I been trying to figure how to do this programming from after 3 and still up now trying to figure it out. I really need some help in trying to figure out. public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; private int targetWormsEaten; private int wormEnergy; private int wormCount; /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = Greenfoot.getRandomNumber(23) + 5;//JC.....Sets the intial worms eaten from 5 to 27(energy).......this lines satisfies R5 requirement. } /** * Act - do whatever the crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { turnAtEdge(); randomTurn();//JC.....line allows the the crab to move in random directions......line satisfies R4 requirement. move(); lookForWorm(); switchImage(); } /** * Alternate the crab's image between image1 and image2. */ public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } /** * JC.....Check for wheather we are the edge of the world. If we are turn * * */ public void turnAtEdge() { if(atWorldEdge()) { turn(17); } } /** * JC.....Turn randomly from the current direction or dont. If we do turn, turn right or left at a random degree. * Function satisfies line R4 requirement. */ public void randomTurn() { if (Greenfoot.getRandomNumber(100)>90) { turn(Greenfoot.getRandomNumber(90)-45); } move(4); } /** * Check whether we have stumbled upon a worm. * If we have, eat it. If not, do nothing. If we have * eaten 10 worms, the crab is full and cannot eat more worms. *JC.....Function satisfies R6 requirements */ public void lookForWorm() { if ( wormsEaten !=targetWormsEaten&& canSee(Worm.class)) { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten == 10) { return; } } if(wormsEaten <=0) { getWorld().removeObject(this); return; } } public void move() { if(wormsEaten==10) { wormEnergy = (wormEnergy + 20); wormEnergy = (wormEnergy - 1); wormCount = (wormCount - 1); } } }//END OF CRAB CLASS
danpost danpost

2013/4/29

#
Please explain the requirements concerning the limit of 10 worms eaten and the initial value of 'wormsEaten' and any other related requirements
infant17 infant17

2013/4/29

#
well the requirements are: (1) To set the crab initial worms eaten to a random number from 5 to 27(energy) then (2) Once the crab has eaten 10 worms, the crab is full and cannot eat no more worms. (3) At every move, the crab is suppose to lose 1/20 of worm(energy) and (4) when the crab runs out of energy it cannot move and dies(removed from the world.
danpost danpost

2013/4/29

#
The first thing I notice, is that your 'move' method (1) does not anywhere 'move' or 'setLocation' the actor; (2) is restricted to when 'wormsEaten' is 10; and (3) changes 'wormEnergy' twice. I do not think you understand how programming works. First, state how you want it to work; then, re-phrase what you said using variables and method calls. Normally, an action will require a counter to increment, then that counter will need to be check for limits and actions performed when those limits are achieved. For example, when the actor moves, a move counter could be incremented and the value may be check for some limit, like 20. In code, it would look something like this:
// with the fields
private int moveCount = 0;
// a move method could be
private void move()
{
    move(1);  // or a 'setLocation' statement
    moveCount++;
    if (moveCount == 20)
    {
        moveCount = 0;
        // do whatever needs done
    }
}
In your case, what needs to get done when the move counter reaches 20? maybe, the 'wormsEaten' value get decremented and then its value will need to be checked to see if it is zero. So, the 'do whatever' could be:
wormsEaten--;
if (wormsEaten == 0)
{
    getWorld().removeObject(this);
}
Notice the pattern, (1) change a variable (2) check its limits (3) do appropriate actions on limits.
danpost danpost

2013/4/29

#
So, even if the crab uses up the energy from a given worm eaten, it cannot replenish that energy by eating another worm if it has already eaten 10?
infant17 infant17

2013/4/29

#
Im just learning programming......im new to it.....I try to understand with no help but it gets unbearable.......question on the move class why use private move and not public like I did?
danpost danpost

2013/4/29

#
You only need 'public' access when you are calling the method from another class.
danpost danpost

2013/4/29

#
danpost wrote...
So, even if the crab uses up the energy from a given worm eaten, it cannot replenish that energy by eating another worm if it has already eaten 10?
If 'wormsEaten' is already 10 or greater, does that mean the crab cannot eat any worms at all?
infant17 infant17

2013/4/29

#
another questions with the initial worms eaten set from 5 to 27 would the program automatically set the crab integer between those numbers that when you inspect a crab it shows that and the crab would have to eat 10 more crabs to reach the requirement to start losing energy
infant17 infant17

2013/4/29

#
yes from the requirement I was give it states once the crab has eaten 10 worms the crab is full and cannot eat more worms
infant17 infant17

2013/4/29

#
thank you danpost I think I understand a little......one more question is there a tutorial that I can go through to understand programming a little more or programming for greenfoot.
danpost danpost

2013/4/29

#
In your instance fields area, remove the 'targetWormsEaten' and 'wormCount' fields. In your constructor, add the following line at the end:
wormEnergy = wormsEaten*20;
Decrement 'wormEnergy' and check its value for zero on each 'move'. If allowed to and eats a worm, increment 'wormsEaten' and add 20 to 'wormEnergy'. In your 'lookForWorm' method, change the 'if' condition to
if (wormsEaten < 10 && canSee(Worm.class))
Also remove any other 'if' blocks from that method that use 'wormsEaten' in its condition. Make the appropriate changes to your 'move' method as I described above.
danpost danpost

2013/4/29

#
Alright, I think the above is ok, now. (it was edited)
infant17 infant17

2013/4/29

#
THANK YOU danpost for your help it is greatly appreciated
There are more replies on the next page.
1
2
3