Hi all, I'm working on a maze project with different types of robots. One of the robots is supposed to be a right-hand-rule robot which follows his right hand.
The way I'm coding it so far, is I have an Enum Orientation. If the right hand is facing UP, DOWN, LEFT, or RIGHT depends on which way the robot moves.
Currently this is what I have to test the orientation:
Where: newRobotMaze is a copy of the maze its given
getX(), getY() are its current position
and orientation is of Enum type Orientation
//If the robot's arm is facing the top side of the screen his orientation will be UP and will move left
if(newRobotMaze[getX()][getY()-1]=="|" && newRobotMaze[getX()-1][getY()] == null){
orientation = Orientation.UP; // Right hand faces top side of screen and will move Left
}
//If the robot's arm is facing the right side of the screen his orientation will be RIGHT and will move up
else if((newRobotMaze[getX()+1][getY()]=="|" && newRobotMaze[getX()][getY()-1] == null) && (newRobotMaze[getX()+1][getY()-1] == "|" || newRobotMaze[getX()+1][getY()-1] == null)){
orientation = Orientation.RIGHT; //Right hand faces right side of screen and will move up
}
//If the robot's arm is facing the left side of the screen his orientation will be LEFT and will move down
else if((newRobotMaze[getX()-1][getY()]=="|" && newRobotMaze[getX()][getY()+1] == null) && (newRobotMaze[getX()-1][getY()+1] == "|" || newRobotMaze[getX()-1][getY()+1] == null)){
orientation = Orientation.LEFT; //Right hand faces left side of screen and will move down
}
//If the robot's arm is facing the bottom side of the screen his orientation will be DOWN and will move right
else if((newRobotMaze[getX()][getY()+1]=="|" && newRobotMaze[getX()+1][getY()] == null) && (newRobotMaze[getX()+1][getY()+1] == "|" || newRobotMaze[getX()+1][getY()+1] == null)){
orientation = Orientation.DOWN; //Right hand faces bottom side of screen and will move right
}
From there, the orientation would determine if the robot just came from either of the spaces to the left, right, top, or bottom of it to decide which way it is heading.
I feel there's an easier way to do this because I feel I've hit a wall. Does anyone else have any easier ways to figure out this right-hand-rule algorithm?
The main problem with this so far is in a situation like this:
| X |
XXX - Where X's are null, * is the robot, and | is a wall.
| * |
When the robot moves into the middle of that intersection, it's going to choose the wrong orientation depending on the sequential order of my algorithm.
Anyone have any easier way to accomplish this? Thanks in advance!