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!

OK, do you mean the "right hand rule" is that the robot will always turn right if it can turn right? Or it should try to go through the maze by hugging the wall on the right all the time? If it is the first meaning, it will be very difficult to solve the maze because eventually your robot will come back to the same location again.

If it is the latter meaning, it is not that bad. You need to index the wall instead of the path. The wall you use is the right side of the robot. Therefore, you also need to be able to turn your robot and know which direction it is heading to. If there is no wall to the right of it, turn its head to that direction and move toward that direction until it see the first wall on its right.

|X|          |X|          |X|          |X|
   --+ +--      --+ +--      --+ +--      --+ +--
    X X X    =>  X ^ X   =>   X > X   =>   X X >
   --+ +--      --+ +--      --+ +--      --+ +--
     |^|          |X|          |X|          |X| ^
                                                 \_ wall

The problem with this rule is that if your robot is place in an empty space with no wall at all to all around, you need to first move your robot forward until it hits a wall. Then you can start the right hand rule.

Edited 5 Years Ago by Taywin: n/a

I plan on making all of my mazes start with a starting position in the middle of two walls. My only concern is that if the starting position is at a different point of the map (such as the top, left, or right) the robot will have to determine which direction it's going toward to then determine which way its right hand is facing.

So essentially I could just write turn methods for each way the robot is facing? Whether that's to the left, right, top, or bottom of the maze?

I could then change the orientation to a new meaning and name it Direction. If the robot is in the direction of Left, it will check for walls to the left and above it - when it reaches a wall directly in front of it, it will turn Up or Down depending on where the next wall is?

Not exactly. If you are talking about Right Hand rule and I understood it, it means that you need a set of direction rules that it will always select in clock-wise order. For example, the robot must pick UP before RIGHT before DOWN before LEFT. If it can go to UP first, it will go; otherwise, it will attempt the next direction.

That makes perfect sense. So I'm sort of on the right track then :) Thanks for the help!

This article has been dead for over six months. Start a new discussion instead.