I have to write code for an assignment in Beginner's Programming class and I am completely lost. I must do the following:
In this assignment you will use the “if” conditional statement to do the following changes to the leaves-and-wombats scenario.
Currently, the wombats eat as many leaves as they want. Make the wombats stop eating leaves once the attribute (variable) leavesEaten reaches the value of “5” leaves or more.
I have done the following following advise given to others:
When I run this my wombat eats 5 leaves in the beginning, but then he does not skip the 24 steps before he eats again. I must change the wombats’ code so that each time they move, they use 1/24 of leaf energy and if the leavesEaten is less than 5 the wombat can eat more leaves. Can someone tell me what I am doing wrong?
private int direction; private int leavesEaten; private int moves;//JM public Wombat() { setDirection(EAST); leavesEaten = 0; } /** * Do whatever the wombat likes to to just now. */ public void act() { if(foundLeaf() && leavesEaten <5)//JM don't eat more than 5 leaves { 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; } moves++;//JM if (moves == 24)//JM { moves = 0;//JM if (leavesEaten > 0)//JM { leavesEaten--;//JM } else { //die?//Jessica Mojica } }//Jessica Mojica 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; } }