Go to the first, previous, next, last section, table of contents.


Karel User's Guide

Invoking the Karel Simulator

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.

Invoking in a terminal

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

Invoking under X Windows

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.

Defining Karel's World

Overview

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.

General Rules

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.

Reference

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

Example

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:

  - - - - -
 |+ + + + +|

 |+ + + +|+|

 |+ + *|+|+|
    - -
 |+ +|+|+|+|

 |+|+|+|+|+|
 ----------

Writing Karel Programs

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.

A Simple Program

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.

Sample Karel Program 2

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

Turning Right

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

Solving a Maze

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

Karel Language Reference

Primitives

Built-in: move
Move Karel one intersection forward.

Built-in: turnleft
Pivots Karel 90 degrees left.

Built-in: pickbeeper
Take a beeper from the current intersection and put it in the beeper bag.

Built-in: putbeeper
Take a beeper from the beeper bag and put it at the current intersection.

Built-in: turnoff
Turn Karel off.

Tests

Test: front-is-clear
True if there is no wall directly in front of Karel. False if there is.

Test: front-is-blocked
True if there is a wall directly in front of Karel. False otherwise.

Test: left-is-clear
True if there is no wall immediately to Karel's left. False if there is.

Test: left-is-blocked
True if there is a wall immediately to Karel's left. False otherwise.

Test: right-is-clear
True if there is no wall immediately to Karel's right. False if there is.

Test: right-is-blocked
True if there is a wall immediately to Karel's right. False otherwise.

Test: next-to-a-beeper
True if Karel is standing at an intersection that has a beeper. False otherwise.

Test: not-next-to-a-beeper
True if there is not beeper at the current intersection. False if there is a beeper at the current intersection.

Test: facing-north
True if Karel is facing north. False otherwise.

Test: not-facing-north
True if Karel is not facing north. False if he is facing north.

Test: facing-south
True if Karel is facing south. False otherwise.

Test: not-facing-south
True if Karel is not facing south. False if he is facing south.

Test: facing-east
True if Karel is facing east. False otherwise.

Test: not-facing-east
True if Karel is not facing east. False if he is facing east.

Test: facing-west
True if Karel is facing west. False otherwise.

Test: not-facing-west
True if Karel is not facing west. False if he is facing west.

Test: any-beepers-in-beeper-bag
True if there is at least one beeper in Karel's beeper bag. False if the beeper bag is empty.

Test: no-beepers-in-beeper-bag
True if Karel's beeper bag is empty. False if there is at least one beeper in the beeper bag.

Control Structures

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>

Defining Procedures

  DEFINE-NEW-INSTRUCTION <name> AS
    <stmt>


Go to the first, previous, next, last section, table of contents.