If you are not comfortable with conditions and loops yet, you may want to rework the previous modules. We will now put all that stuff together, add the Extended World concept, and create a labyrith for the robots to escape from. STEP 1 - Creating an maze of Blocks and Walls Clear the Code box, paste in this code segment and run the program: define { WorldEntity Maze { wall from 1,1 north for 18; wall from 2,18 east for 10; wall from 12,1 north for 10; wall from 12,12 north for 7; wall from 9,2 north for 15; wall from 3,16 east for 6; wall from 6,2 north for 12; wall from 2,1 east for 10; wall from 3,3 north for 13; wall from 5,14 east for 3; wall from 14,6 north for 10; block at 5,6; block at 11,15; block at 8,3; block at 11,3; block at 4,11; block at 7,9; beeper at 13,11; } } execute { place Maze; } As you can see, we now have a labyrinth displayed on the World! Our new WorldEntity, maze, is made up of a collection of walls and blocks. A WALL is in general defined this way: wall from x,y direction for # of squares; x,y are the start coordinates direction is North, South, East or West # of squares defines the length of the wall A BLOCK is a one-by-one wall, and is simply defined by its coordinates: block at x,y; Blocks and walls are obstacles that terminate robots on impact. Try adding some walls, blocks and a robot, and move the robot around to see what happens. The following are observations you may have made: 1) placing walls on top of other walls (crossing) creates A WARNING message in the Messages Windows, but does not stop execution 2) placing robots on squares already occupied by walls or blocks causes AN ERROR message and immediately stops the execution. 3) letting robots walk into a block or a wall causes AN ERROR and terminates that robot, but does not stop execution. By the way - The beeper at the opening of the maze serves as the prize for making it through the labyrinth, and (more importantly) gives us an excellent way of creating an outer loop for our program; while (! BeepersInBag) { } we simply let the robot go through the maze untill the beeper is found. STEP 2 - where we guide a robot through the maze By smart use of conditions it is possible to guide the robot out of this mess. We assume our robot is born on a valid square, and that there is a way out of the maze. Let's make a list of possible things that can happen and convert this to RoBOTL statements. We can check for obstacles in front, to the left and to the right. We will look to the left first at all times, and have that as our main rule (if possible, go left). Look at this as an executive decision. If we cannot move to the left, we will try the other options in a systematic manner. Let's look at the basic scenarios: 1) Robot has an obstacle to the left and must do something a) look in front, if open move forward 2) Robot also has an obstacle in front and must do something a) look to the right, if open move forward 3) Robot also has an obstacle to the right and must a) turn around 180 degrees and move forward 4) Robot can go left, an will a) turn and move forward that way. We bind these scenarios together with IF and ELSE statements, and can express the basic rules in RoBOTL this way: if (! leftisclear) { if (frontisclear) { move 1; } else { if (rightisclear) { iterate 3 times turnleft; move 1; } else { iterate 2 times turnleft; move 1; } } } else { turnleft; move 1; } As usual, there are other ways of doing this. The important thing is to have a system to follow, and keep track of the different tests we are running. Spacing out the statements, as we have done above is a help in keeping track of which else belongs to which if, and what belong within the different statements. EXERCISE Modify the maze program by adding a robot, LabRat, place it somewhere in the labyrinth, and have the LabRat search for an opening. You may want to include the outer loop and the tests we already worked out. You will also need to tell LabRat to pick up any beepers found in its way. Don't be discouraged if it takes you a while to crack this one; it is easy to get entangled in bundles of nested ifs. If you lose the overview, try printing your program to paper, and draw lines to connect the different if clauses. You need to have a printer connected to your computer or your network in order to do this. Print your program by pasting it into your favorite editor, notepad or the clipboard. A commented solution to the problem is accessible from the View Code/Load Code option. KEYWORDS Block, Wall
Send questions and comments to: Karen Lemone