Producing Two Dimensional Maps

It is easy to create a two dimensional map of a Karel World. When Cay Horstmann first gave me the core code of this system, he included this functionality. I have extended it a bit. Here is an example of how to use it. .

The advanced carpet layer problem is introduced as Problem 2 in Section 7.10 of the book. The AdvancedCarpetLayerWorld class needs a run method as usual. It might normally look like this:

public void run()
{
    karel.carpetRoom();
    karel.turnOff();
}
This assumes, of course that you have an appropriate AdvancedCarpetLayer class with a carpetRoom method. But if you add two statements to this (identical statements), you can create a map of the world before and after the execution.
public void run()
{
    this.show2DScene(1, 1, 12, 12);
    karel.carpetRoom();
    karel.turnOff();
    this.show2DScene(1, 1, 12, 12);
}

 

Now, when the run begins you will get an additional window in which the following graphic is the contents. The window has a menu and you can choose to save the graphic in one of several formats. I chose png format, simply by giving .png as the extension of the file. Several formats are supported. In particular, if you use .ps as the extension, a postscript file will be created that can be sent to a printer or transformed into a pdf file.

Original room

This shows the world before execution. The glyph in the upper left corner is the robot, facing East. Note that the world has been rotated 90 degrees so that North is to the right, and East is down. When you close the window, possibly after saving the contents as described above, the simulation will run as usual. After the robot turns off, you will get another window, like the first, but with the following contents, that you may also save.

Room with carpet

As you can see, the robot has returned to its original location (3, 2) but beepers have been laid at each accessible corner of the room.

This is useful for preparing handouts for students, but also for letting students verify that they have been successful at a task.

Note that the world builder also has a button to create the 2D map from the world you are working on.

Last Updated: March 21, 2010