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

2012/1/3

I have been given a Scenario that i need to solve PLEASE HELP!

77JJJ77 77JJJ77

2012/1/3

#
Ok so i am extremely new to java. I can learn quick but here is my problem I need to get the actor over to the other side of the world while avoiding an object (called a Wall). My actor (robot) starts in random places each time you start the world and i need help with the programming that will get me to avoid the the walls that are randomly placed. For example if my robot hits a "wall" i need it to go around it but i have no idea how to go about this. Below is the code that i was given for the robot.
public class robot  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;
    
  
    public robot()
    {
        setDirection(EAST);
    }
    
    
    public void act() 
    {
        if(endItNow()==true)
        {
            Greenfoot.stop();
        }
        else
        {
             int x = getX();      
             int y = getY();        
             
             setLocation(x + 1, y);     


     
        }
    }    
    
    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;
        }
    }
        
    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;
        }
    }
    
    public void turnRight()
    {
        switch(direction) {
            case SOUTH :
                setDirection(WEST);
                break;
            case EAST :
                setDirection(SOUTH);
                break;
            case NORTH :
                setDirection(EAST);
                break;
            case WEST :
                setDirection(NORTH);
                break;
        }
    }
    
     public boolean canMove()
    {
        World myWorld = getWorld();
        int x = getX();
        int y = getY();

        // test for outside border
        if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) {
            return false;
        }
        else if (x < 0 || y < 0) {
            return false;
        }
        return true;
    }
    
    
    public void forward(int howMany)
    {
        for(int loop=0;loop<howMany;loop++)
        {
            fwd();
        }
    }
    public void backward(int howMany)
    {
        for(int loop=0;loop<howMany;loop++)
        {
            bwd();
        }
    }  
    
     public void bwd()
    {
        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;
        }
    } 
    
     public void fwd()
    {
        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;
        }
    } 
    public boolean checkBumpers()
    {
        Actor wall = getOneObjectAtOffset(0, 0, wall.class);

       if(wall!=null)
       {
             return true;
        }
        else {
            return false;
        }
        
    }
        public boolean endItNow()
    {
        Actor end_ = getOneObjectAtOffset(0, 0, end_.class);
        if(end_ != null) {
            return true;
        }
        else {
            return false;
        }
        
    }
    
}
b321234 b321234

2012/1/3

#
Well, there are lotta codes, it certainly takes time to solve this problem but I think I can give you some suggestions about the writing of the codes first.. 1. A class name's first letter should be in upper case (e.g. Robot), both World and Actor. 2. Line 19 change it to if(endItNow()) and it'll still works And I have a question, you already have the setDirection method why do you still need the turnLeft and turnRight methods?
michiele michiele

2012/1/3

#
Your code has the robot moving East from the beginning. You want to add some code that will check to see if you are sitting on top of a wall (the method checkBumpers does this for you. It returns true if you are sitting on a wall, false if you are not). If you ARE sitting on a wall, you would want to move backwards and then turn (pick left or right and AWAYS turn that way). I would use the methods given (fwd,checkBumpers,turnLeft) and replace the code at lines 25-28 with something like:
fwd();
if(checkBumpers()) {
    bwd();
    turnLeft();
}
Probably needs some refinement after that, but it should get you started.
77JJJ77 77JJJ77

2012/1/3

#
Cheers guys, much appreciated for your help. Is there any way i can take that code further. for example if my Robot was to hit a "wall" and could not also turn Left. Would there be a way of him being able to turn the opposite way like right, and still reach the end point? any other suggestions are welcome... i can use all this information for my report thanks....
danpost danpost

2012/1/3

#
You could code it that way, but the problem with that is you could end up going back and forth without really making any progress. To clarify what michiele was saying about always turning the same way, I believe the intent was to always keep the wall on the same side of the robot. The way I would approach the problem, is from the start, once the robot finds an obstacle (whether a wall or a non-target edge of the world), put it to one side of the robot and move like the robot has a hand rubbing on the obstacles the whole way to its destination. If the robot does not ever make it there, then it is totally blocked from getting there. (it is like finding your way out of most mazes, by keeping a hand on the wall, and keep moving without ever removing your hand).
danpost danpost

2012/1/3

#
BTW your canMove() method will always return true: you are getting the actors current location (which must always be within the ranges of the world) and trying to return a false if they are outside the world (which will never be).
You need to login to post a reply.