Monty Karel Simulators

 

The simulators to go with the book are under active development. There are currently four different versions, all packaged together. They should run on all systems that support the needed software libraries. It was built on a Macintosh and Fedora and briefly tested on Windows. The code written by the students runs in all versions if the system supports them. There is no difference in, for example, imports in the student files. Choose one of the versions depending on what software you have installed on your system and your preferences. Early versions of the simulators are on the CD from cafepress. Latest versions are available here.

The first is not graphical and prints to the console. It can use ascii art to picture the world, but not dynamically. It is most useful if nothing else works OR if you are driving the system using PyUnit unit tests, in which case graphics just get in the way.


The next works only with Jython, a Python version written in Java and supporting the Java libraries. The graphics system for this version is the same as that of Karel J Robot and, in fact, uses the Karel jar file. Here is a screen shot from this version showing a remote controller as well as the world. The program the student writes is pure Python, only the imports differ, but even that is buffered through an adapter file that presents the same "world" view in all three simulators.


The third is Python/Tkinter so that it should run on nearly every system. Tkinter is packaged with Python on most computers. This uses abstract graphics, rather than gif files for the robots (to avoid using PIL, which is problematic to install). Here are two screen shots from this version. The first shows a blue robot facing South (as part of a right turn) in the middle of the StairSweeper example. If you don't like the robot graphic, it can be tailored as it is driven by a list of tuples giving its outline. This is one of the first examples in the book that is used to show procedural decomposition.

The Java/Jython version works best with Eclipse/PyDev, though it will work stand-alone. It will not work with the IDLE system, however.

The second shot is of four robots executing the Dining Philosophers multi threaded program. The beepers are the forks and the yellow robot is eating and holds two forks. The other three each hold one and are waiting for the other. Note that the threading is unsynchronized. The example is used to introduce basic ideas of multi-threading and some problems that can occur, rather than to give a comprehensive treatment. As a result, the solutions to the typical problems of deadlock and race conditions are not presented in the text, though these problems may easily be demonstrated.

The Tkinter version is in the distribution with the others, but may change as we get more experience with it. It works with IDLE, the simple Python interpreter, and Eclipse. It is likely to work with other Python IDE's as well, though none have been tested. This version now has figures that look more like the traditional Karel J Robot figures.

This version now includes a world builder written in python/Tkinter as well as a remote controller.

To see whether your system supports this version, run Python and type at the prompt:

>>> import Tkinter

If you don't get an error, then this system should work.

 

This version lets you set the look of the robots to suit your preferences. Three different versions are included. Look in the file tkwindow.py for this line:

package = self.karelPackage

Change the assignment to self.alienPackage or self.crabPackage to see the alternatives. You can define your own "packages" as well. The alienPackage is shown in the figures here. The default is the karelPackage which shows more "traditional" robots. (Here the word package is not related to the python notion of a code package.)


The fourth version was built in Python/PyGTK so that it may run on the OLPC. This is a major goal of this project. This version requires PyGTK version 2, and uses graphic files for the robots, so looks very similar to the Java version.

This version also includes both a world builder and a remote controller

To see whether your system supports this version, run Python and type at the prompt:

>>> import pygtk

If you don't get an error, then this system should work.


Worlds

Each of the graphic versions has a world builder and you can also create worlds programatically. Worlds have exactly the same format as the Karel J Robot worlds. You can download world files from the KJR pages.

All versions read Karel J Robot worlds if you have those files: To read a world, use

world.readWorld("filename.kwld") # the extension is NOT significant, actually, and txt works just as well

To build a world programmatically

world.placeWallNorthOf(street, avenue) # places an east-west wall segment
world.placeWallEastOf(street, avenue) # places a north-south wall segment

world.placeBeepers(street, avenue, howMany) #default for howmany=1. Use the variable infinity to place infinitely many beepers.

You can then

world.saveWorld("filename.kwld")

You can also

world.showWorldWithRobots()

to see the world on the console. The latter call has options. See robotworldbase.py to see them.

Make sure you have the latest simulator also. It has a graphical world builder now. Just run tkworldmaker.py or gtkWorldMaker.py as your main, depending on your graphics preferences. If you are using Jython graphics. then just doubleclick the jar file included in the distrubtion. In fact, you can do this anyway if you have Java installed properly on your computer. .

Last Updated: March 7, 2008