A Karel simulator is a program that ties the Karel execution engine to a user interface. The execution engine is responsible for running the karel program and moving Karel around his world. The user interface is responsible for displaying the state of the world and the robot to the user. The karel distribution comes with two such simulators: a terminal based interface and an X Window System based interface.
karelc
is a simulator written with the curses user interface. To
run the samples/maze.k Karel program in the karelc
simulator:
curses/karelc -p samples/maze.k -w samples/maze1.wld
karelg
is a simulator written with an X Window System GTK user
interface. To run the samples/maze.k Karel program in the karelg
simulator:
gtk/karelg -p samples/maze.k -w samples/maze1.wld
The X Window System front end is based on the Gimp ToolKit. The choice of X toolkits is largely personal preference. GTK was chosen because of its LGPL license. The core of the Karel Simulator, the execution engine, is independent of any user interface. It should be easy to create a new user interface for Karel with a different X toolkit. This was one of the goals for the Karel Architecture.
Karel's world has intersections, walls, and beepers. The programs the Karel executes are independent of the world he moves around in. You can execute the same program in multiple worlds, or multiple programs in the same world.
Karel's world is defined in a file which is loaded by a Karel simulator. The world file is a simple text file containing commands that define the size of the world, the position of walls and beepers, and Karel's starting position.
Each line in the file describes a part of Karel's world. We'll look at each in turn, but first there are some general rules for the world file.
World num_streets num_avenues World 5 5
The World command defines a world that is 5 avenues wide by 5 streets high.
Beepers street avenue number Beepers 3 3 1
The beeper command places a number of beepers on an intersection. In this case, one beeper would be placed on the intersection {3, 3}
Robot street avenue direction num_beepers Robot 4 3 1 0
The robot starting position is defined with the Robot command. In this example, the robot starts at {4, 3}, facing North (1), with zero beepers in his beeper bag.
Wall street avenue direction Wall 2 2 1
The Wall command places a wall section in Karel's world. Each wall section is one block long. Walls can either be north or west of an intersection (1 or 4 respectively). In this example, a horizontal wall is placed directly north of the intersection {2, 2}.
Here is a sample world file:
World 5 5 Beepers 3 3 1 Robot 4 3 1 0 Wall 2 2 1 Wall 3 2 1 Wall 1 1 4 Wall 2 1 4 Wall 2 2 4 Wall 3 1 4 Wall 3 2 4 Wall 3 3 4 Wall 4 1 4 Wall 4 2 4 Wall 4 3 4 Wall 4 4 4
The resulting world, in the curses interface, looks like this:
- - - - - |+ + + + +| |+ + + +|+| |+ + *|+|+| - - |+ +|+|+|+| |+|+|+|+|+| ----------
The definitive source of Karel information is Richard Pattis' book Karel the Robot: A Gentle Introduction to The Art of Programming. This section is intended to be a brief overview of the language in order to get started using Karel.
This is the simplest correct Karel program. All valid Karel programs must minimally have these 5 lines.
BEGINNING-OF-PROGRAM BEGINNING-OF-EXECUTION turnoff END-OF-EXECUTION END-OF-PROGRAM
This program does nothing but turn Karel off. It is a good sample because it concisely shows the structure of a valid Karel program.
By adding calls to the move
and turnleft
primitives, this
program causes Karel to walk a square, returning to his starting point.
Note that in this version of Karel, the
'{' and '}' are used for comments, just like Pascal.
{ A simple karel program to walk in a square to the left } BEGINNING-OF-PROGRAM BEGINNING-OF-EXECUTION move; turnleft; move; turnleft; move; turnleft; move; turnleft; turnoff END-OF-EXECUTION END-OF-PROGRAM
The Karel language has a turnleft
primitive, but lacks a
turnright
primitive. One of the first instructions a new Karel
programmer does is define the turnright
instruction for Karel.
As the old adage goes, "two wrongs don't make a right, but three lefts
do".
The following example demonstrates how to define a new instruction for Karel out of the existing primitives. This example will cause Karel to walk in a square to the right, instead of the left as in the previous example.
{ A simple karel program to walk in a square to the right } BEGINNING-OF-PROGRAM DEFINE-NEW-INSTRUCTION turnright AS ITERATE 3 TIMES turnleft; BEGINNING-OF-EXECUTION move; turnright; move; turnright; move; turnright; move; turnright; turnoff END-OF-EXECUTION END-OF-PROGRAM
Programs can be constructed for Karel that will allow him to find a beeper by navigating through a maze. This sample program has Karel follow walls looking for openings until he locates a beeper. You can find this program in the distribution in file `samples/maze.k'.
{ karel follows the right wall until a beeper is found} BEGINNING-OF-PROGRAM DEFINE-NEW-INSTRUCTION turnright AS ITERATE 3 TIMES turnleft; BEGINNING-OF-EXECUTION WHILE not-next-to-a-beeper DO BEGIN IF right-is-clear THEN turnright ELSE WHILE front-is-blocked DO turnleft; move END; turnoff END-OF-EXECUTION END-OF-PROGRAM
BEGINNING-OF-PROGRAM <definitions> BEGINNING-OF-EXECUTION <stmt>; <stmt>; <stmt>; ... END-OF-EXECUTION END-OF-PROGRAM
BEGIN <stmt>; <stmt>; <stmt>; ... END; IF <test> THEN <stmt> [ ELSE <stmt> ] ITERATE <positive-integer> TIMES <stmt> WHILE <test> DO <stmt>
DEFINE-NEW-INSTRUCTION <name> AS <stmt>
Go to the first, previous, next, last section, table of contents.