8. Peter Walks on Marks <<< Contents >>> 10. Keyboard Control
Peter has wandered into a large maze. He cannot find his way out, and we have to help him.
We will start by creating a new program. In the program window, click the New button. Type Way as the program name.
In the Global Variables and Functions window, we will prepare two items for the maze a wall and a door. We could draw them, but we may use the items that are already prepared in the item library. The library is in the bottom right window called Library of Variables and Functions. There are data elements like number, item, or picture in it. Expand the item element, and in the [examples] groups, Cottage subgroup, find the elements labeled Door and Wall . Drag them into the Global Variables and Functions window. This is a quick and easy way to create new items, which have already been named and drawn.
Now we will create the maze sheet. Double-click the sheet element. We will make the sheet smaller, so that the maze does not have to be too big. Click the Dimensions button. A window for specifying sheet dimensions appears. Type 12 as the width and 10 as the height.
Use the Door and Wall elements to create the following maze:
The program will be based on a conditional repeating of commands . We want Peter to repeat his steps around the maze until he finds the door. In the cycle condition, we will use the item on Peter's position element. It is similar to the item in front of Peter element, but it applies to the square, on which Peter is standing. It can be found in the Peter - extension group.
The cycle condition could be expressed like this: "If it is not true that the item on Peters position is the door, repeat " The word not tells us that we need the opposite of the item identity test. For this, we have an element called is not valid that . The element is called logical negation, and its function is to invert the result of the comparison from true to false, and from false to true. It is located in the calculations group, logic operations subgroup.
First, we will place the is not valid that element into the while is valid cycle condition. We will attach the item identity element, and insert the item on Peter's position and Door elements into it.
In the cycle body, we will ensure that when walking around the maze, Peter will stick to the border on his right. In the beginning of the cycle, we will tell Peter to turn right by using the right command. The following command will be a conditional repeating of commands cycle. This will tell Peter to turn to the left, until he will find a square onto which he can go. For this, we will use the left command.
In the second (inner) cycle, we will test if there is a blocked square in front of Peter, i.e. if he should turn further to the left. The first blocked square is the Wall . Peter also cannot go through the border of the sheet. This can be tested by a special element called sheet border (located in the sheet group). It is special by the fact that it is not a data variable, but a constant, and so you cannot edit it. Its value can be kept in variables and tested, but it cannot be viewed by putting it on the sheet.
We have two tests of items in front of Peter, and we need to express this: "As long as there is a wall or a sheet border in front of Peter, repeat left." The word or is a logical element again or , and it is called logical sum. It is located in the calculations group, logic operations subgroup. We will insert the or element into the cycle condition and add two comparisons of items in front of Peter, with the wall and the border.
Peter is facing an empty square now, so we have to add one last element into the main cycle step . This tells Peter to go to the empty square.
Finally, we will tell Peter that when he finds the door, he should turn towards us and wait untill we press a key. Behind the main cycle, add the left and key input (waits to be pressed) elements. The resulting program is shown here:
Run the program. Peter walks around the maze, until he finds the exit. It is just behind the corner on the left side, but he does not know it and tries to find the exit from the right, so it takes some time.
Let us replace the left and right elements. This changes Peters search method, so that he will stick to the wall on his left. In our maze, he finds the exit much sooner.
8. Peter Walks on Marks <<< Contents >>> 10. Keyboard Control