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

2013/9/20

Help with Mazewalking Algorithm

JasonZhu JasonZhu

2013/9/20

#
I am having trouble figuring out how to write the code to check if a wall is beside the player. Also, I am unsure of how to write the facingWall Method. Help greatly appreciated!
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


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()
    {
        GreenfootImage endpoint = new GreenfootImage(25,25);
        endpoint.setColor(Color.BLUE);
        endpoint.fill();
        setImage(endpoint);
    }
    
    public boolean wallOnLeft()
    {
        return false;
    }
    
    public boolean canWalk()
    {
        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; break;
        }
        Actor wall = getOneObjectAtOffset(xOffset,yOffset,Wall.class);
        return wall != null;
        //return !facingWall() && !facingEdge();
    }
    
    public boolean facingWall()
    {
        switch ( getRotation()) {
            case EAST: return getX()==getWorld().getWidth()-1;
            case SOUTH: return getY()==getWorld().getHeight()-1;
            case WEST: return getX()==0;
            case NORTH: return getY()==0;
        }
        return false;
    }
    
    public void walk()
    {
        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 turnLeft()
    {
        setRotation(getRotation()-90);
    }
    
    public void turnRight()
    {
        setRotation(getRotation()+90);
    }
    
    public boolean facingEdge()
    {
        switch ( getRotation()) {
            case EAST: return getX()==getWorld().getWidth()-1;
            case SOUTH: return getY()==getWorld().getHeight()-1;
            case WEST: return getX()==0;
            case NORTH: return getY()==0;
        }
        return false;
    }
    
    public void act() 
    {
        if(wallOnLeft()) {
            if(canWalk()) {
                walk();
            }else{
                turnRight();
            }
        }else{
            turnLeft();
            walk();
        }
    }    
    
    
}
danpost danpost

2013/9/20

#
The code you have in your 'facingWall' method is exactly the same as the code you have in your 'facingEdge' method. Move the code in your 'canWalk' method to replace the code in your 'facingWall' method and uncomment the one line left in the 'canWalk' method.
JasonZhu JasonZhu

2013/9/20

#
Yea, I thought the facingWall would be similar to that of facingEdge, but with the symbol getObjectAtOffset? I've tried that, but it doesn't work. Also, I still have trouble figuring out how to write the wallOnLeft Method. Thanks for all your help
JasonZhu JasonZhu

2013/9/20

#
//return !facingWall() && !facingEdge(); shows up with an error of unreachable statement...
danpost danpost

2013/9/20

#
That should be the only statement in your 'canWalk' method!
danpost danpost

2013/9/20

#
You do not need a 'wallOnLeft' method; you just need the proper code in your act method:
public void act()
{
    turn(-90);
    while (!canWalk()) turn(90);
    walk();
}
JasonZhu JasonZhu

2013/9/20

#
Can you provide me with how to write the wallOnLeft Method anyway? I want to know how it's done. Thanks
JasonZhu JasonZhu

2013/9/20

#
You are BRILLIANT, danpost! Your code is so simper yet it accomplishes the task! It just tells the player to scale the left wall! How long have you been in this business, because you are amazing.
danpost danpost

2013/9/20

#
The easiest way to code it with what you have now is as follows:
public boolean wallOnLeft()
{
    turn(-90);
    boolean leftWallPresent = facingWall();
    turn(90);
    return leftWallPresent;
}
JasonZhu JasonZhu

2013/9/20

#
Thanks once again, it works perfect now!
JasonZhu JasonZhu

2013/9/20

#
I have one more quick question, can you explain to me what the facingEdge method does? I don't understand why there are those numbers there
danpost danpost

2013/9/20

#
JasonZhu wrote...
I have one more quick question, can you explain to me what the facingEdge method does? I don't understand why there are those numbers there
When a world is created with a 'super(w, h, 1);' statement, the world becomes w pixels wide and h pixels high and getWidth() will return w and getHeight() will return h. But in java, we start counting at zero (the left-most column of pixels is at x = 0 and the top-most row is at y = 0); this means the getX() or getY() will return a value of zero at one of those pixels. If we started counting at one, as normal, the last column or row would be x = w or y = h to end up with an array of 'w x h' pixels; but, since we are starting at zero, the last column and row end up at x = w-1 and y = h-1. That is what is being checked for in the EAST and SOUTH directions in the facingEdge method.
JasonZhu JasonZhu

2013/9/20

#
Ohhhhh I get it now, thanks for all your help Dan!
You need to login to post a reply.