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
block.
For the first exercise, you should use a block called
instead. You will find it near the end of the list in the
Variables category.
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
or
.
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
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
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.
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,
|
|
|
|
|
|
|
|
|
|
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:
Before you start on this exercise, run the
block again.
This will remove the maze, giving Karel space to lay out his beepers.
You can use the
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.
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:
Now run these blocks – is Karel doing what she’s asked to do in 2.1 and 2.2?