Help! Robot in a maze (beginner)
Hi!
I'm doing a project where a robot is in a "corridor" with 4 sides and 4 corners. The robot starts in the top right corner and could be facing either South or West (it could move in a clockwise or counterclockwise direction) and should go one lap around the corridor and end up in the same spot.
I have created a method where the robot checks in every corner if it should turn left or right (see below) but it is very clumpsy. Is there
any easier way to code this?
My instructions says: "When the robot is at the end of one corridor and is facing the wall, it is easy to find out if it is moving in a
clockwise or counterclockwise direction. If there is a wall on the robots left side the movement is clockwise and vice versa.
But I don't understand how it can check if there is a wall on the sides? I only know the "frontIsClear"-method.
public void checkIfClear() {
if (!robot.frontIsClear()) {
robot.turnLeft();
counter++;
}
if (!robot.frontIsClear()){
turnAround();
counter++;
}
}
Re: Help! Robot in a maze (beginner)
What methods are provided by the robot class?
Re: Help! Robot in a maze (beginner)
Quote:
Originally Posted by
2kaud
What methods are provided by the robot class?
void move()
void turnLeft()
boolean frontIsClear()
boolean atEndOfWorld()
Location getLocation() r
int getDirection()
void setDelay(int t)
Re: Help! Robot in a maze (beginner)
Given just those methods it's not obvious if there is another way to check if left or right is blocked as the only useful test in this case seems to be frontIsClear().
Is there anything returned by getLocation() or getDirection() which would be of use? The instructions refer to movement, so perhaps getDirection() is helpful?
Re: Help! Robot in a maze (beginner)
I've thought about that. Since the start direction is either south or west i thought that maybe one could use getDirection in the beginning, and then have one method for south and one for west.
But then the instruction I have says "When the robot is at the end of one corridor and is facing the wall, it is *easy* to find out if it is moving in a
clockwise or counterclockwise direction. If there is a wall on the robots left side the movement is clockwise and vice versa.
I don't get this because there is no method for checking what is to the left/right.
Re: Help! Robot in a maze (beginner)
Quote:
Since the start direction is either south or west i thought that maybe one could use getDirection in the beginning, and then have one method for south and one for west.
Yes. If the start direction is south, then proceed forward until front is not clear. At which point you are at the bottom right, so you need to turn right. Then when way forward is not clear, turn right again. Basically when the way forward is not clear, turn right go forward and repeat until at starting point. As there is no direct turn right command, this can be done with three turn lefts.
Similar for start west. But in this case when way is not clear turn left.
Quote:
When the robot is at the end of one corridor and is facing the wall, it is *easy* to find out if it is moving in a
clockwise or counterclockwise direction
The initial start direction determines if moving clockwise or counterclockwise. Start south then it is moving clockwise, start west and it is moving anti-clockwise.