Lab 2: Karel the Robot (II)


Lab 2: Programming Karel (part II)

Again, start by opening Snap with Karel. (Note that this is not the same link as in lab 1: This version comes with a few other blocks ready-made.) Open it in a new tab or window so that you can do the lab exercises and continue reading these instructions at the same time.

Reminder: Loading the page can take a long time - how long depends on which browser you’re using and the computer you’re on. We recommend using an up-to-date version of the chromium (chrome) browser, but we have tested Snap! also with firefox and Internet Explorer. If the browser asks you to “Stop script” or “Continue” while loading, choose “Continue”.

Reminder: Before you can start Karel, or run any program, you have to initialise the grid and robot, by clicking the "Setup" block.

For the first exercise, you should use a block called "Setup with maze" instead. You will find it near the end of the list in the Variables category.

Exercise 2.1

As we talked about in the lectures, a reporter block (also known as a function) is a block that returns a value, and that can be used in expressions (like +, join, etc). Reporter blocks are distinguished from command blocks by having rounded corners. Snap! also has a third shape of block, called a predicate. This is a reporter block that always returns one of the values true or false. Predicate blocks can be used in conditional blocks, like if and repeat until. Snap! allows you to define custom predicate blocks, just the same as custom reporters and command blocks.

Karels perception abilities are limited: he can only look for a wall right in front of him, or a beeper on the spot where he stands. In the Sensing category, you will find a predicate called “Beeper in front?” Right-click and select “edit” to open it in the block editor. This predicate is incomplete. You must fill it in so that it

  • reports true if the corner in front of Karel contains a beeper, and there is no wall blocking the way there;
  • reports false if there is a wall in front of Karel, or no beeper on the corner in front; and
  • after execution of the block, Karel is in the same place and facing the same direction as he was before it started.
  • “In front” means in front of Karel with one step away.

Testing your new block

As always, test your new block! Set up test cases by placing a beeper in front of Karel, turning him to face a wall, and so on.

After you are satisfied that it works correctly, try using it to make Karel follow a trail. You will find a block called "Lay a trail" which lays out a trail to get Karel out of the maze, and a block called “Follow a trail” (in the Motion category) that makes Karel follow this trail. “Follow a trail” uses your “Beeper in front?” predicate, so if that does not work correctly, it is likely that trail following will not either.

Exercise 2.2

In the lectures, we have shown you how to make Karel collect beepers. Now it is time to load up Karel with a stack of beepers and instruct him to lay them out in a nice pattern. Specifically, given any number of beepers, we want him to lay them out in a square field, or as close to a square as he can. (If the number is not a perfect square, the number of rows and columns should not differ by more than one, and the number of beepers in either rows or columns should be the same except for possibly the last one.) For example,

"layout 1" "layout 2" "layout 3" "layout 4"
"layout 5" "layout 6" "layout 7" "layout 8"
"layout 9" "layout 10"

You should create a top-level block called “Make a square with (number) beepers”, where the number is the input. To make sure that Karel has enough beepers to fill the square, use the block “tell” block, like this:

Definition of the block "Make a square with (number) beepers"

Before you start on this exercise, run the "Setup" block again. This will remove the maze, giving Karel space to lay out his beepers. You can use the clear beepers and reset blocks to remove all beepers from the grid and put Karel back in his starting corner; that will probably be useful as you test and debug the square layout block.

Assignment submission

To submit your assignment, you need to export blocks (as described on the lab 1 page and shown in this video). You should export the two blocks that you defined in this lab, “Beeper in front?” and “Make a square with (number) of beepers”, as well as any other custom blocks you created that are used inside those two (or used inside those used inside those two, and so on).

Submit the file with the saved blocks through wattle.

Testing your submission As always, test your submission file – to make sure everything that should be included is indeed included!

To do so:

  • Start a fresh chrome/chronium session
  • Load the Snap+Karel workspace for Lab 2
  • Use the “import … ” function from Snap to load your submission file containing the blocks.

Now run these blocks – is Karel doing what she’s asked to do in 2.1 and 2.2?

15 January, 2017
863 words


Written by
Tags
labs

Quick links

2017 S1 lectures schedule:
  • Monday 1200-1300 (PSYC G8)
  • Tuesday 1000-1100 (PSYC G8)
  • Wednesday 1200-1300 (PSYC G8)

Quick-access class pointers: TBC