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

2015/2/6

maze-walkers "Missing Methods"

1
2
plant plant

2015/2/6

#
Hi, I am having a problem. I have created a maze and my player goes through walls and the robot that's suppose to cover every square of the maze is not doing what its suppose to be doing.. Here is my code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Robot here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Robot extends Maze_Walker
{
    public Robot()
    {
        super();
    }

    public void act() 
    {
        if (canMove()==true)
        {
            move(1);
        } else
        {
            turn(90);
            move(1);
        }    
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Maze_Walker
{
    private int SPEED = 5;
    private int COUNTER = 1;
    public Player()
    {
        super();
    }

    public void act() 
    {
        moveAround();
    }    

    public void moveAround()
    {
        move(1);
        COUNTER++;
        if (COUNTER==SPEED)
        {
            if (isFacingEdge()==false||isFacingWall()==false)
            {
                if (Greenfoot.isKeyDown("down")&&checkSOUTH()==true)
                {
                    //setLocation(getX(),getY()+SPEED);
                    setRotation(SOUTH);
                } else if (Greenfoot.isKeyDown("up")&&checkNORTH()==true)
                {
                    //setLocation(getX(),getY()-SPEED);
                    setRotation(NORTH);
                } else if (Greenfoot.isKeyDown("left")&&checkWEST()==true)
                {
                    //setLocation(getX()-SPEED,getY());
                    setRotation(WEST);
                } else if (Greenfoot.isKeyDown("right")&&checkEAST()==true)
                {
                    //setLocation(getX()+SPEED,getY());
                    setRotation(EAST);
                }
                COUNTER=0;
            }
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

public class Maze_Walker extends Actor
{
    protected final int NORTH = 270;
    protected final int SOUTH = 90;
    protected final int EAST = 0;
    protected final int WEST = 180;

    public Maze_Walker()
    {
        getImage().scale(20,20);
        setRotation(270);
    }

    public boolean isFacingWall()
    {
        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;
        }
        return getOneObjectAtOffset(xOffset,yOffset,Wall.class)!=null;
    }

    public boolean isFacingEdge()
    {
        switch (getRotation())
        {
            case EAST: return getX()==getWorld().getHeight()-1;
            case SOUTH: return getY()==getWorld().getHeight()-1;
            case WEST: return getX()== 0;
            case NORTH: return getY()==0;
        }
        return false;
    }

    public boolean canMove()
    {
        return !(isFacingWall() || isFacingEdge());
    }
    
    public boolean checkSOUTH()
    {
        Wall wall = (Wall)getOneObjectAtOffset(0,1,Wall.class);
        if (wall != null)
        {
            return false;
        }else{
            return true;
        }
    }
    
    public boolean checkEAST()
    {
        Wall wall = (Wall)getOneObjectAtOffset(1,0,Wall.class);
        if (wall != null)
        {
            return false;
        }else{
            return true;
        }
    }

    public boolean checkWEST()
    {
        Wall wall = (Wall)getOneObjectAtOffset(-1,0,Wall.class);
        if (wall != null)
        {
            return false;
        }else{
            return true;
        }
    }

    public boolean checkNORTH()
    {
        Wall wall = (Wall)getOneObjectAtOffset(0,-1,Wall.class);
        if (wall != null)
        {
            return false;
        }else{
            return true;
        }
    }

    public void act() 
    {
        /*if (isTouching(Target.class))
        {
        Greenfoot.stop();
        }*/
    }    
}
As I said before.. All I want is the player not to go through walls and the robot to cover the entire maze on its own. I do not have the methods isWallOnLeft() etc.
danpost danpost

2015/2/7

#
Line 29 of the Player class does not appear to be what you want. If the player IS facing a wall, it is not facing an edge and if the player IS facing an edge, it is not facing a wall. Either way, one of the two conditions will be true and therefore allow the player to move. The Player class is a subclass of the MazeWalker class and the MazeWalker class has a 'canMove' method. So, change line 29 of the Player class to:
if (canMove())
That should be a start (at least).
plant plant

2015/2/9

#
When I change that, my player does not move at all, but before it moved, but through walls.
danpost danpost

2015/2/9

#
What behavior do you want for your player? Do you want it to move, if possible, regardless of any keypress or must a key be pressed to trigger any movement? (it is hard to tell what you want because of the commented 'setLocation' lines within the keypress code).
Super_Hippo Super_Hippo

2015/2/9

#
If you only changed that line, it still should move even if it wouldn't be correct, because the 'move(1)' is before that. So if the player "does not move at all", you must have changed something else.
DoctorMitch DoctorMitch

2015/2/9

#
simplify the movement controls of the player to the following:
   
public void controller
{
        if (Greenfoot.isKeyDown("left") && !wallBehind()) 
        {
            move(-9);
        }
        else if (Greenfoot.isKeyDown("right") && !wallFront()) 
        {
            move(9);
        }
        else if (Greenfoot.isKeyDown("w") && !wallUp()) 
        {
            turn(-90);
            move(9);
            turn(90);
        }
        else if (Greenfoot.isKeyDown("s") && !wallDown()) {
            turn(90);
            move(9);
            turn(-90);
        }
}
public boolean wallDown()
    {
        Actor Wall = getOneObjectAtOffset(0,1, Wall.class);
        if (Wall==null)
        {
            return false;
        }
        else 
        {
            return true;
        }
    }
    public boolean wallUp()
    {
        Actor Wall1 = getOneObjectAtOffset(0,-1, Wall.class);
        if (Wall1==null)
        {
            return false;
        }
        else 
        {
            return true;
        }
    }
    public boolean wallFront()
    {
        Actor Wall2 = getOneObjectAtOffset(1,0, Wall.class);
        if (Wall2==null)
        {
            return false;
        }
        else 
        {
            return true;
        }
    }
    public boolean wallBack()
    {
        Actor Wall3 = getOneObjectAtOffset(-1,0, Wall.class);
        if (Wall3==null)
        {
            return false;
        }
        else 
        {
            return true;
        }
    }

DoctorMitch DoctorMitch

2015/2/9

#
9 is just a random move value, make it whatever one of your grid spaces are. I hope I could help, but it seems that I'm a little late to the party... sorry
Super_Hippo Super_Hippo

2015/2/9

#
The Problem about your code is that you check for walls 1 pixel to the side, although you walk 9. And I wonder which game is controlled with w, left s right. ^^
plant plant

2015/2/10

#
Sorry I forgot to mention the resolution of the world is 20. I already got it to work, but I do not understand the code I used to make it work at all... So you can put 'if' statements with-in switch statements?
danpost danpost

2015/2/10

#
Show what parts you do not understand. Maybe we can explain it to you.
davmac davmac

2015/2/10

#
plant wrote...
So you can put 'if' statements with-in switch statements?
Yes. In general, you can put statements inside a 'switch' statement, and an 'if' statement is a type of statement. In fact, you can put 'if', 'for', 'while' and 'switch' statements within each other.
DoctorMitch DoctorMitch

2015/2/11

#
whoops, I messed up on my code earlier... ignore all of the values I put for move and button presses... this is the exact code I put in the test world I ran, and it works
public void act() 
    {
        if (Greenfoot.isKeyDown("left") && !wallBack()) 
        {
            move(-1);
        }
        else if (Greenfoot.isKeyDown("right") && !wallFront()) 
        {
            move(1);
        }
        else if (Greenfoot.isKeyDown("up") && !wallUp()) 
        {
            turn(-90);
            move(1);
            turn(90);
        }
        else if (Greenfoot.isKeyDown("down") && !wallDown()) {
            turn(90);
            move(1);
            turn(-90);
        }
    }

public boolean wallDown()
    {
        Actor Block = getOneObjectAtOffset(0,1, Block.class);
        if (Block==null)
        {
            return false;
        }
        else 
        {
            return true;
        }
    }
    public boolean wallUp()
    {
        Actor Block1 = getOneObjectAtOffset(0,-1, Block.class);
        if (Block1==null)
        {
            return false;
        }
        else 
        {
            return true;
        }
    }
    public boolean wallFront()
    {
        Actor Block2 = getOneObjectAtOffset(1,0, Block.class);
        if (Block2==null)
        {
            return false;
        }
        else 
        {
            return true;
        }
    }
    public boolean wallBack()
    {
        Actor Block3 = getOneObjectAtOffset(-1,0, Block.class);
        if (Block3==null)
        {
            return false;
        }
        else 
        {
            return true;
        }
    }
block is the name of the wall, and unit is the name of the player. Sorry for the slip up :)
plant plant

2015/2/12

#
So does your player automatically move or do you have to move it
plant plant

2015/2/12

#
What is the significance of putting a return true or false statement in a Boolean method? Let's say canMove()==true, would I return true just to confirm the previous true value
Super_Hippo Super_Hippo

2015/2/12

#
A method with return type 'boolean' has to return true or false, so you can use it in a condition for "if" for example.
There are more replies on the next page.
1
2