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

2013/11/20

Unreachable statements

jsheehan jsheehan

2013/11/20

#
The aim of this was so the bug would check for obstacles and avoid them.I keep on getting this error message - import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; import java.util.ArrayList; /** * Write a description of class Bug here. * * @author (your name) * @version (a version number or a date) */ public class Bug 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 foodEaten; public Bug() { setDirection(NORTH); } /** * Act - do whatever the Bug wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { if (atEnd()){ removeBug(); Greenfoot.stop(); } else if(foundFood()) { eatFood(); } else if (canMove()) { processKeys(); move(); } else { processKeys(); } } /** * Process keys to handle keyboard input */ private void processKeys() { if (Greenfoot.isKeyDown("left")) { setDirection(WEST); } else if (Greenfoot.isKeyDown("right")) { setDirection(EAST); } else if (Greenfoot.isKeyDown("up")) { setDirection(NORTH); } else if (Greenfoot.isKeyDown("down")) { setDirection(SOUTH); } } /** * 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; } } /** * 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{ return true; } List obstacles = myWorld.getObjectsAt(x,y,Obstacle.class); List bugs = myWorld.getObjectsAt(x,y,Bug.class); if(obstacles.isEmpty() && bugs.isEmpty()){ return true; } else{ return false; } } /** * 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 are in top left cell (home). */ private boolean atEnd() { int x = getX(); //get current x position of bug int y = getY(); //get current y position of bug if (x == 0 && y == 0 || x == 59 && y == 39 || foodEaten>5) { //if x and y are equal to 0 return true; // return true } else { //otherwuse return false return false; } } /** * foundfood method. */ private boolean foundFood() { Actor food = getOneObjectAtOffset(0, 0, Food.class); if(food != null) { return true; } else { return false; } } /** *eatfood method. */ private void eatFood() { Actor food = getOneObjectAtOffset(0, 0, Food.class); if(food != null) { // eat the leaf... getWorld().removeObject(food); foodEaten = foodEaten + 1; } else if(canMove()) { move(); } } /** * Remove the bug from the world. */ private void removeBug() { World myWorld = getWorld(); // get the world to remove the bug from myWorld.removeObject(this); // remove the bug (this) } }
davmac davmac

2013/11/20

#
Ok, first, please use tags for code, so that it looks like this:
    else if(canMove()) {
        move();
    }
and not this: else if(canMove()) { move(); } Secondly, please indent your code correctly. You can use ctrl+shift+I in the editor to do this for you, before you post it here. Having said all that, it looks like the problem is in your canMove method:
if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) {
    return false;   // <-- returns here
}
else if (x < 0 || y < 0) {
    return false;    // <-- and here
}
else {
    return true;    // <-- and here!
} 
List obstacles = myWorld.getObjectsAt(x,y,Obstacle.class);
The 'if' statement always returns, so the next line can't possibly be reached. The return statement stops any further code in that method from running. As there is a return statement in every branch of the if-statement, it must always return and so the next line - "List obstacles = ..." - can't possibly be reached. It looks to me as if you should remove the 'else { return true; }' part completely.
jsheehan jsheehan

2013/11/20

#
Hi when I do that it then stops me from moving the bug with the keyboard arrows
jsheehan jsheehan

2013/11/20

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

import java.util.List;
import java.util.ArrayList;

/**
 * Write a description of class Bug here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bug 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 foodEaten;

    public Bug()
    {
        setDirection(NORTH);
    }

    /**
     * Act - do whatever the Bug wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (atEnd()){
            removeBug();   
            Greenfoot.stop();

        }
        else if(foundFood()) {
            eatFood();
        }
        else if  (canMove()) {
            processKeys();
            move(); }
        else {
            processKeys();    
        } 
    }

    /**
     * Process keys to handle keyboard input
     */
    private void processKeys()
    {
        if (Greenfoot.isKeyDown("left"))
        {
            setDirection(WEST);
        }
        else if (Greenfoot.isKeyDown("right")) 
        {
            setDirection(EAST);
        }
        else if (Greenfoot.isKeyDown("up"))
        {
            setDirection(NORTH);
        }
        else if (Greenfoot.isKeyDown("down"))
        {
            setDirection(SOUTH);
        }
    }

    /**
     * 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;
        }
    }

    /**
     * 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{
            return true;
        }
        List obstacles = myWorld.getObjectsAt(x,y,Obstacle.class);
        List bugs = myWorld.getObjectsAt(x,y,Bug.class);
        if(obstacles.isEmpty() && bugs.isEmpty()){
            return true;
        }
        else{
            return false;
        }
    }

    /**
     * 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 are in top left cell (home).
     */
    private boolean atEnd()
    {
        int x = getX(); //get current x position of bug
        int y = getY(); //get current y position of bug
        if (x == 0 && y == 0 || x == 59 && y == 39 || foodEaten>5) { //if x and y are equal to 0
            return true;        // return true 
        }
        else {             //otherwuse return false 
            return false;
        }

    }

    /**
     *  foundfood method.
     */
    private boolean foundFood()
    {
        Actor food = getOneObjectAtOffset(0, 0, Food.class);
        if(food != null) {
            return true;
        }
        else {
            return false;
        }
    }

    /**
     *eatfood method.
     */
    private void eatFood()
    {
        Actor food = getOneObjectAtOffset(0, 0, Food.class);
        if(food != null) {
            // eat the leaf...
            getWorld().removeObject(food);
            foodEaten = foodEaten + 1; 
        }
        else if(canMove()) {
            move();
        }
    }

    /**
     * Remove the bug from the world.
     */
    private void removeBug()
    {
        World myWorld = getWorld(); // get the world to remove the bug from 
        myWorld.removeObject(this); // remove the bug (this)
    }
}
davmac davmac

2013/11/20

#
Hi when I do that it then stops me from moving the bug with the keyboard arrows
I guess the problem is line 129:
        List bugs = myWorld.getObjectsAt(x,y,Bug.class);  
        if(obstacles.isEmpty() && bugs.isEmpty()){  
Your bug is looking for any bugs at its own position (line 129) - of course, it's going to find itself. So the condition 'bugs.isEmpty()' will never be true. You should probably use the actor method getObjectsAtOffset(...) - with an offset of 0,0 - rather than the world's getObjectsAt(...) method; the getObjectsAtOffset method won't return 'this' object in the list.
jsheehan jsheehan

2013/11/20

#
Thanks I realized that I didn't need to add in the code about the bug.
 List obstacles = myWorld.getObjectsAt(x,y,Obstacle.class);
        if(obstacles.isEmpty()){
            return true;
        }
        else{
            return false;
        }
Its the list obstacles bit which is reaching a unreachable statement
davmac davmac

2013/11/21

#
Yes, I already gave you the solution to that in my first post:
The 'if' statement always returns, so the next line can't possibly be reached. The return statement stops any further code in that method from running. As there is a return statement in every branch of the if-statement, it must always return and so the next line - "List obstacles = ..." - can't possibly be reached. It looks to me as if you should remove the 'else { return true; }' part completely.
You need to login to post a reply.