Assignment 1. Karel the Robot


Due Friday, July 4 at 11:59 pm Pacific

  • Submissions received by due date receive a small on-time bonus.
  • All students are granted a pre-approved extension or "grace period" of 24 hours after the due date. Late submissions are accepted during the grace period with no penalty.
  • The grace period expires Sat, Jul 5 at 11:59 pm Pacific, after which we cannot accept further late submissions.
  • In this course, we express all date/times in Pacific time GMT -7. Our Paperless submission system also displays/records due dates and submission times in Pacific time.

Based on problems by Nick Parlante and Eric Roberts, lovingly modified by your current CS106A staff.

📦 Starter Code

This assignment consists of four Karel programs. There is a starter project linked here that contains the files you will write your Karel code in. Before you start on this assignment, make sure to read through the handout Using Karel with PyCharm in its entirety. Note: this assignment is to be done by yourself, while adhering to the Stanford honor code.

Here's an overview of what you'll need to do for this assignment:

  1. Download the starter code above.
  2. Edit the Karel program files so that the assignment code does what each task described that it is supposed to do. This will involve a cycle of coding, testing, and debugging until everything works.
  3. Once you have gotten each part of the program to run correctly in the default world associated with the problem, you should make sure that your code runs properly in all of the worlds that we have provided for a given problem.
  4. Submit your assignment on Paperless as described at the bottom of this handout. Remember that you can re-submit your assignment as much as you want, but only the most recent submission will be graded. If you discover an error after you've made a submission, just fix your program and submit a new copy of all files (not just the one you fixed).

The four Karel problems to solve are described later in this handout.

⚠️ Please remember that your Karel programs must limit themselves to the language features described in Karel the Robot Learns Python. You may not use other features of Python (including variables, parameters, break, and return), even though the PyCharm-based version of Karel accepts them.

Problem 1: Collect Newspaper Karel (CollectNewsaperKarel.py)

Your first task is to solve a simple story-problem in Karel's world. Suppose that Karel has settled into its house, which is the square area in the center of the image below.

karel.png

Karel starts off in the northwest corner of its house as shown in the diagram. The problem you need to solve is to get Karel to collect the newspaper. The newspaper, like all objects in Karel's world, is represented by a beeper. You must get Karel to pick up the newspaper located outside the doorway and then to return to its initial position.

This exercise is meant to help you get you started programming with Karel. You can assume that every part of the world looks just as it does in the diagram: the house is exactly this size, the door is always in the position shown, and the beeper is just outside the door. Thus, all you have to do is write the sequence of commands necessary to have Karel:

  1. Move to the newspaper
  2. Pick it up
  3. Return to its starting point.

Although the program does not have many lines of code, it is still worth getting some practice with decomposition. In your solution, you must write at least two helper functions: one for step 1 and another for step 3 in the outline above. You're welcome to write a helper function for step 2 as well, but since picking up a beeper is only one line of code, it's okay to do this step without a helper function.

Your program should run successfully in the following world: CollectNewspaperKarel.w (default world). Note that all Karel worlds are located in the worlds folder in the assign1 project folder.

Problem 2: Stone Mason Karel (StoneMasonKarel.py)

Your next task is to repair the damage done to the Main Quad in the 1989 Loma Prieta earthquake. In particular, Karel should repair a set of arches where some of the stones (represented by beepers, of course) are missing from the columns supporting the arches, as illustrated below.

stone_mason_start.png

Your program should work on the world shown above, but it should be general enough to handle any world that meets the basic conditions outlined at the end of this problem. There are several example worlds in the starter folder, and your program should work correctly in all of them.

When Karel is done, the missing stones in the columns should be replaced by beepers, so that the final picture resulting from the initial world shown above would look like the illustration below.

stone_mason_end.png

Karel's final location and the final direction Karel is facing at the end of the run do not matter.

Karel may count on the following facts about the world:

  • Karel starts at the corner where 1st Avenue and 1st Street meet, facing east, with an infinite number of beepers in Karel's beeper bag. The first column should be built on 1st Avenue.
  • The columns are always exactly four Avenues apart, so they would be built on 1st Avenue, 5th Avenue, 9th Avenue, and so on.
  • The final column will always have a wall immediately after it. Although this wall appears after 13th Avenue in the example figure, your program should work for any number of beeper columns.
  • The top of a beeper column will always be marked by a wall. However, Karel cannot assume that columns are always five units high, or even that all columns within a given world are the same height.
  • In an initial world, some columns may already contain beepers representing stones that are still in place. Your program should not put a second beeper on corners that already have beepers. Avenues that will not have columns will never contain existing beepers.

You should make sure your program runs successfully in all of the following worlds (which are just a few different examples to test out the generality of your solution): StoneMasonKarel.w (default world), SampleQuad1.w, SampleQuad2.w. Note that all Karel worlds are located in the worlds folder in the assign1 project folder.

Problem 3: Checkerboard Karel (CheckerboardKarel.py)

Your third task is to get Karel to create a checkerboard pattern of beepers inside an empty rectangular world, as illustrated below. (Karel's final location and the final direction it is facing at the end of the run do not matter.)

Example Start Example End

This problem has a nice decomposition structure along with some interesting algorithmic issues. As you think about how you will solve the problem, you should make sure that your solution works with checkerboards that are different in size from the standard 8x8 checkerboard shown in the example above. Some examples of such cases are discussed below. Odd-sized checkerboards are tricky, and you should make sure that your program generates the following pattern in a 5x3 world:

Other special cases you should consider are worlds with only a single column or a single row. The starter code folder contains several sample worlds with these special cases, and you should make sure that your program works for each of them.

This problem is hard: Try simplifying your solution with decomposition. Can you checker a single row/column? Make the row/column work for different widths/heights? Once you've finished a single row/column, can you make Karel fill two? Three? All of them? Incrementally developing your program in stages helps break it down into simpler parts and is a wise strategy for attacking hard programming problems.

You should make sure your program runs successfully in all of the following worlds (which are just a few different examples to test out the generality of your solution): CheckerboardKarel.w (default world), 8x1.w, 1x8.w, 7x7.w, 6x5.w, 3x5.w, 40x40.w, 1x1.w. Note that all Karel worlds are located in the worlds folder in the assign1 project folder.

Problem 4: Midpoint Karel (MidpointKarel.py)

As an exercise in solving algorithmic problems, you will program to find the midpoint of 1st Street. Say Karel starts in the 5x5 world at 1st Street, 1st Avenue. Karel should end in the center of 1st Street.

Example Start Example End

Note that the final configuration of the world should have only a single beeper at the midpoint of 1st Street. Along the way, Karel is allowed to place additional beepers wherever it wants to, but must pick them all up again before it finishes. Similarly, if Karel paints/colors any of the corners in the world, they must all be uncolored before Karel finishes.

In solving this problem, you may count on the following facts about the world:

  • Karel starts at 1st Avenue and 1st Street, facing east, with an infinite number of beepers in its bag.
  • The initial state of the world includes no interior walls or beepers.
  • The world need not be square, but you may assume that it is at least as tall as it is wide.

Your program, moreover, can assume the following simplifications:

  • If the width of the world is odd, Karel must put the beeper in the center square. If the width is even, Karel may drop the beeper on either of the two center squares.
  • It does not matter which direction Karel is facing at the end of the program.

There are many different algorithms you can use to solve this problem so feel free to be creative!

You should make sure your program runs successfully in all of the following worlds (which are just a few different examples to test out the generality of your solution): MidpointKarel.w (default world), Midpoint1.w, Midpoint2.w, Midpoint8.w. Note that all Karel worlds are located in the worlds folder in the Assignment1 project folder.

Optional extension: Create your own Karel project

If you finish early, you may optionally write a Karel project of your own choice. Modify ExtensionKarel.py to use Karel to complete any task of your choosing. Extensions are a great chance for practice and, if your extension is substantial enough, it might help you earn a + score. Make sure to write comments to explain what your program is doing and update ExtensionKarel.w to be an appropriate world for your program.

Submitting your code

All assignments are submitted electronically through the Paperless website. Since this timestamp is based on the time that the server that receives the assignment, we recommend submitting with at least a few minutes to spare, particularly if you discover that your computer's clock is running slow.

Click on the link to Paperless above. Select the assignment you wish to submit (in this case, Assignment 1: Karel the Robot). You should upload all of the appropriate files for the chosen assignment. The end of each assignment handout you will find a list of all of the files that we expect you to submit - please do not rename .py the files when you download them. Here are the files to submit for this assignment:

  • CollectNewspaperKarel.py
  • StoneMasonKarel.py
  • CheckerboardKarel.py
  • MidpointKarel.py

If you did an optional extension on the assignment, you should also submit along with these 4 files:

  • ExtensionKarel.py

If you are adding functionality to your project for extra credit in a file other than a designated ExtensionKarel.py file, please submit a regular version of your code along with the extended version in a separate file. This ensures that you get full credit for the main parts of the assignment in the case that your extended program introduces any bugs. If you have image files, audio clips, or extra Python files that are part of your extra credit submission, please also make sure to submit those.

Once they have been uploaded and you have verified that you've included all of the files outlined in the assignment handout, hit the "Submit Assignment" button.

You should always check the contents of your submission to make sure the code files are up-to-date and correct. To inspect the submission, click on the specific assignment and use the dropdown to view each file's contents.

Congratulations, you've now successfully submitted your assignment! You can submit each assignment as many times as you would like by following the instructions outlined in this document. However, your section leader will grade only the most recent submission.