You have seen that we can extend our World, that robots can be used to pick up and put down beepers, and that an instruction can be repeated by the use of iterate. Often, we want to do something IF something else happens. STEP 1 - Eve is circling around conditionally Load the example file "Eve circling around" and run the program. It looks like this: execute { new basic_bot Eve; tell Eve: { iterate 3 times { TurnLeft; if (FrontIsClear) Move 1; } } } The new thing now is that Eve will only conditionally move forward - IF there is nothing in her way. if (FrontIsClear) Move 1; FrontIsClear is a BOOLEAN FUNCTION; it can have one of two possible results - TRUE or FALSE. If there is an obstacle in front of the robot (the square is taken by another robot, a wall or a block), the function returns FALSE, and whatever comes after will NOT be executed. In all other cases, the function returns TRUE and the statement within the IF will be executed. Swap Move 1; with Move 19; in the Code box, and run the program again. What happened? Change Move 19; to move 20; and take a minute to imagine what will happen, before you hit the Run button. Did your predictions come true? Eve's determined meeting with the Wall made it clear that a conditional test has a pretty short span of action. We check if a conditions is met, then do something. If the conditions change in the mean time, we have no means of altering our commands to the robot unless we change the condition from a one-time test to a continous test (loop). We will look at that in module 5. For now - there was nothing in front of Eve right after she turned, so she went on about her journey - and got burned. If all this is clear, skip to Step 2, else take a minute to review the text and example above or check out the reference guide: STEP 2 - where Eve leaves Beepers behind, and Adam picks them up. You already wrote a program where one robot picks up a bunch of beepers, and leaves the rest for its companion. You may want to reload this program (copy it in from your favorite editor), or load the program "Spring Cleaning Eden style" from the Examples menu. This program is listed in a commented version below (lines starting with a double slash "//" denotes that this is a comment line and is not to be executed) : // definition of the new World entity define { WorldEntity Home { beeper at 0,19; beeper at 19,0 = 2; beeper at 19,19 = 2; beeper at 0,0; } } // start execution, place worldentity and create robots execute { place Home; new basic_bot Adam at 3,10; new basic_bot Eve; // make Eve pick up beepers tell Eve: { turnleft; iterate 4 times { move 19; iterate 3 times { turnleft; if (beepersOnFloor > 0) {pickbeeper;} } } // make Eve drop beepers in the corner iterate 4 times { putbeeper; if (FrontIsClear) { move 1; } } } // make Adam wind his way to the beepers tell Adam: { iterate 2 times turnleft; move 3; turnleft; move 5; turnleft; move 1; iterate 3 times turnleft; move 5; iterate 3 times turnleft; // make Adam pick up left over beepers iterate 6 times { if (BeepersOnFloor > 0) pickbeeper; if (FrontIsClear) move 1; else iterate 3 times turnleft; } } } Puh! This works, but is pretty nasty. There got to be a way of doing this MUCH SIMPLER, right? Right, and we will do this again in module 5, with the help of loops. But before you jump there, you should test yourself on the exercise here: EXERCISE Find out how many beepers each robot is holding after performing the original "Spring Cleaning". Remember you can "bond" with the robots at any time during execution of a program, or simply ask them to reveal the contents of their beeper bags. Scroll down to the bottom of this page for the correct answer. KEYWORDS //, BeepersOnFloor, FrontIsClear, If - Else, In module 5 we will look at how you can use conditional loops, and write more efficient programs. We will also look at the Extended World concept, and create a labyrinth for the robots to escape from. SOLUTION TO EXERCISE Adam holds 4 beepers, Eve 2 (she picks up six and leaves four for Adam). Remember you can shift-click on a robot at any time to get the information about its whereabouts, including the contents of the beeper bag!
Send questions and comments to: Karen Lemone