Table of Contents

Write an instruction followWallRight for the MazeWalker class, assuming that whenever a robot executes this instruction there is a wall directly to the right. It should be able to handle these four possible situations:

Before

After

These four different position changes is the cornerstone for the algorithm that directs a robot to escape from a maze simply by following the right wall. It isn't the most efficient algorithm, and it won't work on mazes that have islands (Can you imagine why?). Do you think following the left walls would be better?

import kareltherobot.*;
public class MazeWalker extends Robot
{
 
	public MazeWalker(int street, int avenue, 
                       Direction direction, int beepers) 
       {
		super(street, avenue, direction, beepers);
 
	}
    /**
     * This is an algorithm to run a maze. It isn't the fastest method,
     * and won't work if the maze has any islands (Can you imagine why?)
     * Would it be better to follow the leftWalls?
     */
	public void escapeMaze() {
		while (! nextToABeeper() )
			followRightWall();		
	}
    /**
     * This will move the Robot according to the diagram
     * above.  Use if () else statements to handle the 4 cases
     */
	public void followRightWall() 
	{
 
	}
 
	public void turnRight() {
		for (int i=0; i<3; i++)
			turnLeft();
	}
 
}

First use the MazeWalkerTester to see if your code deals with the four situations correctly.

MazeWalkerTester.java

Once the tester shows that the four cases are handled correctly, here is a maze runner class with its own maze:

MazeWalkerRunner.java

Running your MazeWaker with Student Mazes

Download the text file you find at https://www.mathorama.com/karel/maze/ and put it in the same folder as the project that has your MazeWalker.java. Change the name of the file in the Runner file:

import java.awt.Color;
import kareltherobot.*;
public class MazeWalkerRunner implements Directions
{
    public static void task()
    {
        // change the location to where the first beeper is:
        MazeWalker mayzie = new MazeWalker(1,1,North,0);  
        mayzie.pickBeeper(); // pick up the first beeper
        mayzie.escapeMaze();
        mayzie.turnOff();
    }
 
    public static void main(String[] args)
    {
        World.reset();
        World.setDelay(1);
        World.setBeeperColor(Color.RED);
        // change the name to match the maze world file
        World.readWorld("BobS.txt"); 
        World.setVisible();
        task();
    }
}

MazeWalker Troubleshooting

Hints:

Links