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



