Project 10.1. Your task is to program robots with varyingbehaviors. The robots t
ID: 3549092 • Letter: P
Question
Project 10.1. Your task is to program robots with varyingbehaviors. The robots try to escape a maze, such as the
following:
A robot has a position and a method void move (Maze m) thatmodifies the position. Provide a common
superclass Robot whose move method does nothing. Provide subclassesRandomRobot,
RightHandRuleRobot, and MemoryRobot. Each of these robots has adifferent strategy for escaping. The
RandomRobot simply makes random moves. The RightHandRuleRobot movesaround the maze so that
it's right hand always touches a wall. The MemoryRobot remembersall positions that it has previously
occupied and never goes back to a position that it knows to be adead end.
Explanation / Answer
public class RightHandRuleRobot extends Robot {
// Constructor
public RightHandRuleRobot (Maze maze) {
// Calls Robot's constructor
super(maze);
}
public void move () {
// If the robot is facing north
if (super.getFacingDirection() == Direction.NORTH) {
// Try to move east first because you want the robot to have a wall to the right of it at all times.
// Facing north would mean a wall has to be to the east
// If there's no wall to the east, move east and face east
if (super.getMaze().isWall(super.getCurrentLocation() .getAdjacentLocationTowards(Direction.EAST)) == false
&& super.getMaze().isValid(super.getCurrentLocation() .getAdjacentLocationTowards(Direction.EAST)) == true) {
super.setCurrentLocation (super.getCurrentLocation().getAdjacentLocationTowards(Direction.EAST));
super.setFacingDirection(Direction.EAST);
}
// At this point, there's a wall to the east of the robot, so you want to try to move north
// (keeping the wall to the right)
// If there's no wall to the north, move north and face north
else if (super.getMaze().isWall(super.getCurrentLocation() .getAdjacentLocationTowards(Direction.NORTH)) == false
&& super.getMaze().isValid(super.getCurrentLocation() .getAdjacentLocationTowards(Direction.NORTH)) == true) {
super.setCurrentLocation (super.getCurrentLocation().getAdjacentLocationTowards(Direction.NORTH));
super.setFacingDirection(Direction.NORTH);
}
// At this point, there's a wall to the east AND north of the robot.
// Since there's a wall in front of the robot, you can turn west and keep a wall to the right of it
// (To the right of a robot facing west would be to the north)
// If there's no wall to the west, move west and face west
else if (super.getMaze().isWall(super.getCurrentLocation() .getAdjacentLocationTowards(Direction.NORTH)) == true
&& super.getMaze().isWall(super.getCurrentLocation(). getAdjacentLocationTowards(Direction.WEST)) == false) {
super.setCurrentLocation (super.getCurrentLocation().getAdjacentLocationTowards(Direction.WEST));
super.setFacingDirection(Direction.WEST);
}
// At this point there's a wall on all 3 sides except the south, so move south and face south.
// When you turn the robot around, it will still have a wall on its right because it's surrounded by walls.
else {
super.setCurrentLocation (super.getCurrentLocation().getAdjacentLocationTowards(Direction.SOUTH));
super.setFacingDirection(Direction.SOUTH);
}
}
// Do the same kind of thing for the other directions, keeping in mind that
// to the right of East is South,
// to the right of South is West,
// and to the right of West is North.
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.