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

2015/1/16

Boolean for sensing objects on one side?????

DiplomatikCow DiplomatikCow

2015/1/16

#
Ok, so, here is my latest problem. I am now trying to make a game in which a standard algorithm is used for the actors movement. So i decided to make a maze game, in which the main actor always wants to keep a wall on its left, and won't go through wall, but just continue walking through the maze. I have only one possible solution to my maze, so it should end up working out. Any help for the code for wallOnLeft()???
public class Player extends Actor
{
    private static final int EAST = 0;
    private static final int SOUTH = 90;
    private static final int WEST = 180;
    private static final int NORTH = 270;
    
    public Player()
    {
        getImage().scale(20,20);
    }
    public void act() 
    {
        if(wallOnLeft())
        {
            if(canMove())
            {
                moveForward();
            }else{
                turnRight();
            }
        }else{
            turnLeft();
            moveForward();
        }
    }    
    public boolean wallOnLeft()
    {
        int xOffset=0, yOffset=0;
        switch(getRotation())
        {
            case EAST: xOffset=1;break;
            case SOUTH: yOffset=1;break;
            case WEST: xOffset=-1;break;
            case NORTH: yOffset=-1;
        }
        return getOneObjectAtOffset(xOffset, yOffset, Wall.class) == null;
    }
    public boolean canMove()
    {
        int xOffset=0, yOffset=0;
        switch(getRotation())
        {
            case EAST: xOffset=1;break;
            case SOUTH: yOffset=1;break;
            case WEST: xOffset=-1;break;
            case NORTH: yOffset=-1;
        }
        return getOneObjectAtOffset(xOffset, yOffset, Wall.class) == null;
    }
    public void moveForward()
    {
        int dx=0, dy=0;
        switch(getRotation())
        {
            case EAST: dx =1; break;
            case SOUTH: dy=1; break;
            case WEST: dx=-1; break;
            case NORTH: dy=-1; break;
        }
        setLocation(getX()+dx, getY()+dy);
    }
    public void turnRight()
    {
        switch(getRotation())
        {
            case EAST: setRotation(SOUTH);break;
            case SOUTH: setRotation(WEST);break;
            case WEST: setRotation(NORTH);break;
            case NORTH: setRotation(EAST);
        }
    }
    public void turnLeft()
    {
        switch(getRotation())
        {
            case EAST: setRotation(NORTH);break;
            case SOUTH: setRotation(EAST);break;
            case WEST: setRotation(SOUTH);break;
            case NORTH: setRotation(WEST);
        }
    }
    
}
DiplomatikCow DiplomatikCow

2015/1/16

#
Please Help....
danpost danpost

2015/1/16

#
What is the cellsize of your world?
DiplomatikCow DiplomatikCow

2015/1/16

#
super(30, 20, 20);
danpost danpost

2015/1/16

#
Ok. a simple movement code would be the following:
public void act()
{
    if (/** maze completed */) return;
    turn(-90);
    move(1);
    while(isTouching(Wall.class))
    {
        move(-1);
        turn(90);
        move(1);
    }
}
DiplomatikCow DiplomatikCow

2015/1/16

#
I need to have it in my boolean wallOnLeft() method, I know what I put was wrong, so I'm wondering what I would put in there to make it work
danpost danpost

2015/1/16

#
Change line 30 to:
switch((getRotation()+270)%360)
and change '==' in line 37 to '!='.
DiplomatikCow DiplomatikCow

2015/1/16

#
I need it to return as true if there is a wall on the left side.
DiplomatikCow DiplomatikCow

2015/1/16

#
How can i make it so that the edge of the world counts as a wall as well, because when I press run, the ant moves, and then gets stuck at the edge of the world where there is no wall
danpost danpost

2015/1/16

#
You should be able to use code similar to the code of the act method except that you will reverse the direction of the turns and look for walls instead of looking for where walls are not. This should be done once, when the actor is added into the world (use the 'addedToWorld' method).
You need to login to post a reply.