## Karel++ Exercises Submitted by Educators

These exercises were developed after the book went to press. They were developed by the people named. Note that unless the solution calls for multiple robots, these problems can also be solved using Karel The Robot.

If you would like to submit a new exercise and have it published here, send it to me with a solution. Be sure to send me identifying information about yourself and a statement releasing it for publication. I will not publish the solutions, but will make them available to educators. berginf@pace.edu

### Chapter 5

1N. (Joseph Bergin, Pace University)

A robot named Karel is at the Origin facing East with an infinite number of beepers in its beeper bag. Somewhere in front of Karel is a wall. Program Karel to compute the distance to the wall by placing a number of beepers at the origin equal to the distance between the Origin and the wall.

2N. (Joseph Bergin, Pace University)

A robot named Dersu is in a completely enclosed rectangular room with no beepers in the room. Dersu has an infinite number of beepers in its beeper bag. Program Dersu to compute the area of the room by placing a number of beepers in the south-west corner equal to the area of the room.

### Chapter 6

1N. (Joseph Bergin, Pace University)

A robot named Ilya is in a square room with sides 8 blocks long. Ilya has 8 beepers in its beeperBag. Program Ilya to place the 8 beepers so that there is only one on each street, one on each avenue, and one on each diagonal (both left and right diagonals must be considered). This is a classic problem called "Eight Queens." Think of the beepers as queens on a chess board. Queens can move horizontally, vertically, and diagonally. Place 8 queens on a chessboard so that none of them can attack any of the others. HINT: Think recursively.

2N. (Joseph Bergin, Pace University)

Write a recursive robot program to solve problem 1N of Chapter 5.

3N. (Joseph Bergin, Pace University)

Write a recursive robot program to solve problem 2N of Chapter 5.

4N. (Joseph Bergin, Pace University)

Program 8 robots to place themselves on a chessboard (8 by 8 square room) so that when moving according to the chess queen move rules, no robot attacks any other (see problem 1N of this Chapter). Each robot should start on the row on which it will eventually end up. It can leave that row to check if it attacks other robots, but should return to its home row.

Hint: use many classes and recursion. You do not need aliases for this exercise. The robots on the first seven streets will need to know the name of the robot on the street immediately to the north. Use local robots for this. Interestingly, all the robots can be named Garry.

Note: You may have noticed the phrase "Think recursively" here and in the text. There is a book by Eric Roberts named "Thinking Recursively." It is published by Wiley & Sons and I recommend it highly.