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);
}
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.
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