22C:005 Problem Solving and Computing - Spring 2000

Homework 3 - Karel Programming Using Conditionals and Iteration

Due: Feb. 11  for Friday discussions, Feb. 14  for Monday discussions, and Feb. 16  for Wednesday discussions

Notes on Homework Assignments and What You Hand In

Your homework assignments are always due (unless otherwise noted) prior to your discussion section on the date it is due.   Please respect this.

To turn in your Karel assignments, you are required to do the following two things:

If you fail to satisfy both requirements, your graded assignment will not receive full credit, and may even be given no credit.


In this assignment, your end goal will be to write a MazeWalker robot based off the Robot class (not the ur_Robot) that will be capable of escaping a maze.  You can always escape a maze if there are no islands by following the right or left wall.  In this assignment, you will eventually create a class of robot that will follow the wall to the right of the robot until it escapes the maze.

Part 1 - Questions

  1. Q1: In which cases is a WHILE command useful and in which cases is a LOOP command useful?
  2. Q2: Explain what the following code snippet does with the rectangular, beeper filled world given below (the robot is already placed in the world and it's name is Harold).  Note, Harold is not currently standing on a beeper.  Also, how many beepers will Harold pick up?

  3. loop(15) {
       if (Harold.frontIsClear()) {
         if (Harold.nextToABeeper()) {
            Harold.pickBeeper();
         }
         else {
            Harold.move();
         }
       }
       else {
          Harold.turnLeft();
       }
    }


     

  4. Q3: Give an example of when it is beneficial to use the "!" operator in a conditional statement.
  5. Q4: Note: Answer this question only when you have completed the programming portion of this assignment.

  6. Explain the result of running your MazeWalker task on the following Karel World?


Part 2 - Code Snippets

  1. Q1:  Write a snippet of code that will get the robot R2D2 from the initial configuration to the ending configuration.

  2. Starting Configuration:     Ending Configuration:

    I'll do this one for you.  To get the robot from the initial configuration to the ending
    configuration, we would simply need the following commands:
     

      R2D2.move();
       
    Now, you can do the remaining snippets.
     
  3. Q2: Write a snippet of code that will move the robot R2D2 from the initial configuration to the ending configuration.

  4. Starting Configuration:      Ending Configuration:
     
  5. Q3: Write a snippet of code that will move the robot R2D2 from the initial configuration to the ending configuration.

  6. Starting Configuration:        Ending Configuration:
     
  7. Q4: Write a snippet of code that will move the robot R2D2 from the initial configuration to the ending configuration.

  8.  Starting Configuration:      Ending Configuration:
     
  9. Q5: The snippets above represent the four (4) cases that the robot can be in if it needs to keep a wall to its right while moving around a room.  Notice that in each starting configuration the robot has a wall to its right and in each ending configuration, the robot has a wall to its right.   In computer science terms, this is called an "invariant".  In other words, the fact that the robot has a wall to its right should never vary.

  10. Write a function called followWallRight, assuming that whenever a robot executes this function there is a wall directly to the right.  This function should maintain the invariant of having a wall directly to the right of the robot.  Use and combine the four configurations illustrated in Q1-Q4 to maintain the invariant.

    For additional references, see Problem 17, pg. 141 and Problem 9, pg. 97 in the Karel++ book.


Part 3 - Programming the MazeWalking Robot

For both example mazes, you should start your robot at Street 3, Avenue 4 facing North.  Your robot should then go about escaping from the maze.  Your robot will know it has escaped from the maze when it locates the beeper that has been placed just outside the maze.

Initially, your robot will be placed in the middle of the maze, not next to a wall.  Your first task is to write a function that will get your robot situated so there is a wall to its right.

Next, use the function you wrote in Q5 above (followWallRight) to escape the maze.  You will know you have escaped the maze when you find the beeper which is placed just outside the maze.

Example Worlds

The following images of worlds should be used as example mazes for your robot to escape from.  You will need to recreate these worlds using the Karel software.  Be sure to note the beeper in each world.  This beeper is placed in the Karel world so your robot will know it has escaped from the maze.

You will need to "download" the maze files onto your disk, or computer so you can use them later if you'd like. By downloading, we want you to save both WLD files to you disk so you can open them with the Karel program.   The files we provide are ONLY for IBM-Compatible PCs.  If you are using a Macintosh to program Karel, you will need to recreate the worlds by yourself.



22C:005 Problem Solving and Computing - Homework 3 - February 4, 2000