Karel the Robot Problem Book


This software has been tested under Linux and Windows.
The archive below has executables for both environments.

http://primepuzzle.com/tc/karel.tar

Karel the Robot Problem Book

Problem 1

Gather all the beepers and place them on top of the location which has 2
beepers. Return to the start and face in the original direction. 

S A

T 9             @
                                  *
R 8                               *
                  *               *
E 7               *               *
          *********************   *
E 6           * 2 *         >
              *   *
T 5     @           @

S 4

  3

  2

  1

    1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   G   H   I
    A   V   E   N   U   E   S

Use world file http://primepuzzle.com/tc/p1.kw.txt

If when asked bag (0-9999)? you enter 0 you error out
because you are unable to put a marker beeper.



If you enter 1 or more you will succeed and your beeper bag will have
the same number you started with.

And you'll be pointing in your original direction!





A solution :

http://primepuzzle.com/tc/mykarel.p1.tc

dir char anyfile(0) [
 pl ""
 char where(10000),lfs(1);int cnt,ix,lines
 lfs(0)=10
 if ((cnt=readfile(anyfile,where,where+10000,1))>0) [
  while ((ix=index(where,cnt,lfs,1))>0) [
   pft where,where+ix-1
   where=where+ix
   cnt=cnt-ix
   if (++lines>19) [sak;pl "";pl "";lines=0]
 ]]]

setup [
 hilo '1';color '3'
 posc 10,20;ps "mykarel.tc - tct - 12/19/16 - p1"
 color '7'
 posc 21,20;ps "seed (odd number from 1-999)? ";last=seed=gn
 posc 22,20;ps "bag (0-9999)? ";bag=gn
 off
 ]

/* ref: http://primepuzzle.com/tc/karel-problem-book.html */

turnaround [turnleft;turnleft]

fetch_beeper [
 while (not_next_to_a_beeper) move
 pickbeeper
 ]

goto_wall [
 while (front_is_clear) move
 ]

turnright [turnleft;turnleft;turnleft]

main [
 setup
 putbeeper /* leave marker */
 turnright
 move
 turnright
 for (i=1;i<=2;++i) fetch_beeper
 turnright
 for (i=1;i<=4;++i) move  
 turnright
 fetch_beeper
 turnright
 goto_wall
 turnright
 while (left_is_blocked) move
 turnleft;move;turnleft
 goto_wall;turnright
 move
 turnleft
 move
 turnleft
 goto_wall;putbeeper;putbeeper;putbeeper
 turnaround;move;turnleft;move;turnleft
 goto_wall;turnright
 fetch_beeper
 ]

Screencast 2016-12-19 07-21-48.mp4

Problem 2

Write a crude simulation of Tinyc Tim doing the Jack Bristol Lake
Waramaug Ultra. It starts with an out and back section (4 streets out, 4
streets back) and is followed by 3 counterclockwise loops. He uses a run
/ walk strategy. Walking is simulated by turning around twice and then
moving 1 street (or avenue) forward. At the end of the loop he keeps
going and does an out and back. The loop is a square which looks like
so.

S A
      *********************************
T 9   *                               *
      *                               *
R 8   *                               *
      *                               *
E 7   *                               *
      *                               *
E 6   *                               *
      *                               *
T 5   *                               *
      *                               *
S 4   *                               *
      *                               *
  3   *                               *
      *                               *
  2   * ^                             *
      *********************************
  1

    1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   G   H   I
    A   V   E   N   U   E   S

Use world file http://primepuzzle.com/tc/lw.kw.txt

A solution :

http://primepuzzle.com/tc/mykarel.lw.tc

http://youtu.be/HDCzZHCShDs

Problem 3

Write a program that sets up the pins in a bowling alley. The initial
world looks like this:

S A

T 9

R 8

E 7

E 6

T 5

S 4

  3

  2

  1                 ^

    1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   G   H   I
    A   V   E   N   U   E   S

The final world looks like this:

S A

T 9

R 8

E 7

E 6                 v

T 5     @       @       @       @

S 4         @       @       @

  3             @       @

  2                 @

  1

    1   2   3   4   5   6   7   8   9   A   B   C   D   E   F   G   H   I
    A   V   E   N   U   E   S

Karel starts with 10 beepers in his bag.

Use world file http://primepuzzle.com/tc/ps.kw.txt

A solution :

http://primepuzzle.com/tc/mykarel.ps.tc

Problem 4

Do a Storm Nemo snow shovel simulation.

During Storm Nemo, Karel the Robot was watching Lee solve the problem of
where to put the snow. The only way to get rid of it was to shovel a
swath and pile it up. Then shovel another swath and pile it in another
pile. These piles would then be moved onto Lee's lawn. The piles could
only be four feet high. Here's what the piles looked like after one day
of shoveling.



S A
      *********************************
T 9   *                               *
      *                               *
R 8   *         p                   l *
      *                             a *
E 7   *         q                   w *
      *                             n *
E 6   *                               *
      *                               *
T 5   *         v       r             *
      *                               *
S 4   *     @   @   @   @             *
      *                               *
  3   *     @   @   @   @             *
      *                               *
  2   *                               *
      *********************************
  1

    1   2   3   4   5   6   7   8   9   A
    A   V   E   N   U   E   S

Put the 4 beepers that are nearest Karel in a pile at location
(avenue,street) = (4,8).  Put the remaining beepers in a pile at
location (4,7). Move the pile you 1st built to some place on the lawn
and move the 2nd pile to another place on the lawn.  The p, q, r and
lawn are just there to help you locate key places.

k 4 5 S
w 1 1 9 1
w 1 9 9 9
w 1 1 1 9
w 9 1 9 9
b 3 4 1
b 4 4 1
b 5 4 1
b 6 4 1
b 3 3 1
b 4 3 1
b 5 3 1
b 6 3 1
g 0

http://primepuzzle.com/tc/nemo.tc

http://primepuzzle.com/tc/nemo.kw

Screencast 2016-12-11 16-49-23.mp4

Problem 5

Maze

Escape the maze and pick up the beeper on exit.

http://primepuzzle.com/tc/maze.tc

http://primepuzzle.com/tc/maze.kw



Problem 6

Treasure Hunt



http://primepuzzle.com/tc/trh.tc

http://primepuzzle.com/tc/trh.kw

Screencast 2016-12-12 09-16-07.mp4

Problem 7

Write a program that shows Karel the Robot doing a random walk. Use the function

turn [int r;r=random(0,3);while(r) [--r;turnleft]]

Use world file http://primepuzzle.com/tc/rand13.kw

A solution :

http://primepuzzle.com/tc/randwalk.tc



Problem 8

Make Karel "skywrite" the message HELLO



http://primepuzzle.com/tc/mykarel.hello.tc

http://primepuzzle.com/tc/hello.kw

Screencast 2016-12-13 06-58-19.mp4