Hello. I am almost completed my project and just have one mistake left to correct. I am making a maze game which includes a robot that moves by itself and sticks to a path of always having a wall on its left side. For some reason, when it moves to a space where it does not have a wall on its left side, it does not properly turn left and move forward like it should. Instead, it moves back and forth infinitely. Any help would be appreciated.
Robot code:
MazeWalker code (MazeWalker is the Robot's superclass):
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Robot extends MazeWalker
{
public Robot()
{
super();
}
public void act()
{
if(wallOnLeft()==true || edgeOnLeft()==true){
if(canMove()==true){
moveForward();
}
if(canMove()==false){
turn(90);
}
}
if(wallOnLeft()==false && edgeOnLeft()==false){
turn(-90);
moveForward();
}
isTouchingTarget();
}
public boolean wallOnLeft()
{
int xOffset=0, yOffset=0;
switch(getRotation()){
case EAST: yOffset=-1; break;
case SOUTH: xOffset=1; break;
case WEST: yOffset=1; break;
case NORTH: xOffset=-1; break;
}
return getOneObjectAtOffset(xOffset, yOffset, Wall.class)!=null;
}
public boolean edgeOnLeft()
{
switch(getRotation()){
case EAST: return getY()==0;
case SOUTH: return getX()==getWorld().getWidth()-1;
case WEST: return getX()==getWorld().getHeight()-1;
case NORTH: return getX()==0;
}
return false;
}
public boolean robotCanMove()
{
return wallOnLeft() || edgeOnLeft();
}
public void moveForward()
{
counter++;
if(counter==5){
move(1);
counter=0;
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class MazeWalker extends Actor
{
protected final int NORTH = 270;
protected final int EAST = 0;
protected final int SOUTH = 90;
protected final int WEST = 180;
protected int counter = 0;
public MazeWalker()
{
getImage().scale(20,20);
}
public void act()
{
isFacingWall();
isFacingEdge();
canMove();
}
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().getWidth()-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 void isTouchingTarget()
{
Actor target = getOneIntersectingObject(Target.class);
if(target!=null){
getWorld().addObject(new GameOver(), 14, 14);
Greenfoot.stop();
}
}
}

