What if ... Karel gets smart
Karel's Sensuary Equipment | |
IF ... THEN constructions | |
ITEATRE constructions | |
WHILE constructions |
As you know, Karel has three video cameras and a small microphone to help him stay out of trouble. He can request the following information from his sensuary equipment:
front-is-clear | front-is-blocked |
left-is-clear | left-is-blocked |
right-is-clear | right-is-blocked |
facing-north | not-facing-north |
facing-south | not-facing-south |
facing-east | not-facing-east |
facing-west | not-facing-west |
next-to-a-beeper | not-next-to-a-beeper |
any-beepers-in-beeper-bag | no-beepers-in-beeper-bag |
Each request will result in a true or false answer. There are no ambiguitites.
Karel can check his video camera and microphone using the if ... then ... else construction. That construction could be inside a new definition or inside the execution block of the program. There are two variations of the IF construction, both of which can be nested inside eachother.
Instruction Syntax | Explaination |
IF <test> THEN <instruction-block> |
|
IF <test> THEN <instruction-block> ELSE <instruction-block> |
|
Here are some examples for IF ... THEN constructions. Note that especially nested IF ... THEN's can be quite confusing, and it is important to make sure that those types of instructions really work as intended.
Sample Program | Explaination |
IF next-to-a-beeper THEN pickbeeper; move |
Karel checks if a beeper is present on the current corner. If there is one, he will pick it up and move on step forward. If there is no beeper present, he will just move one step forward. |
IF front-is-clear THEN move ELSE turnleft; |
Karel would move ahead only if his front is not blocked by a wall. If his front is blocked, he would turn left instead. |
DEFINE-NEW-INSTRUCTION face-north-if-south AS BEGIN IF facing-south THEN BEGIN turnleft; turnleft END END; |
If Karel is facing south, this new command will turn him north. If Karel is not facing south, this new command will do nothing at all. |
DEFINE-NEW-INSTRUCTION escape AS BEGIN IF font-is-clear THEN BEGIN move; turnleft END ELSE BEGIN turnleft; move END END; |
Karel would move and turn left if his front is clear. Otherwise, he would first turn left and then move. Note that if his front and left side was blocked, this new command would lead to an error shutoff. |
DEFINE-NEW-INSTRUCTION give-or-take AS BEGIN IF next-to-a-beeper THEN IF any-beepers-in-beeper-bag THEN putbeeper ELSE pickbeeper END; |
Karel would first check if he is next to a beeper. If so, he would add another beeper to the current corner, but only if there was one in his beeper bag. If he was next to a beeper but without beepers in his beeper bag he would pick up the beeper from the current corner. If he was not near a beeper, he would do nothing. |
Karel has a simple way to repeat instructions any number of times without us having to retype the commands. That new constructions if defined using the reserved words ITERATE and TIMES.
ITERATE <positive-number> TIMES <instruction-block>
Sample Program | Explaination |
DEFINE-NEW-INSTRUCTION turnright AS BEGIN ITERATE 3 TIMES turnleft; END; |
The basic instruction turnleft is repeated three times, which is equivalent to a right turn. |
DEFINE-NEW-INSTRUCTION harvest-one-row AS BEGIN pickbeeper; ITERATE 4 TIMES BEGIN move; pickbeeper END END; |
Karel would pick a single beeper, and then repeat the sequence move; pickbeeper four times. The new instruction therefore would execute five pickbeeper instructions and four move's. |
There are many situation where Karel needs to repeat an instruction but it is not yet know how often. For example, if Karel encounters a pile of beepers, he needs to repeatedly execute the pickbeeper command, but since he does not know the number of beepers in the pile he does also not know how often to execute that command.
The WHILE construction is made-to-order for this situation: you can use it to tell Karel to repeat something until a certain test is met; for example to pick up beepers until a pile is empty.
WHILE <test> DO <instruction-block>
Sample Program | Explaination |
DEFINE-NEW-INSTRUCTION get-all-beepers AS BEGIN WHILE next-to-a-beeper DO pickbeeper END; |
Karel would pick up all beepers on the current corner, regardless how many there are (if it is a finite number, at least). |
DEFINE-NEW-INSTRUCTION harvest-one-row AS BEGIN WHILE next-to-a-beeper DO BEGIN pickbeeper; move END END |
Karel would pick up all beepers in a row, regardless of how many beepers there are. The beepers must form an uninterrupted line, otherwise Karel would stop. After picking up the last beeper in the row, Karel would move one more step. |