Table of Contents | Previous Page
Copyright Don Sleeth, 1998 ©

Page 13 - The Wandering Turtle

Programming Technique

Once again I would like to say that learning a computer language is not all that difficult. There are many books written and courses you can take to learn a language, it just takes time. What there is a shortage of however, is books and courses that teach you how to program. These lessons are not about learning MicroWorlds Logo, they are about learning how to program, and as a side effect you will also pick up some Logo.

Our next project is to build a procedure that makes the turtle wander around aimlessly. We are going to follow a defined set of steps in programming this procedure. They are:

  1. Decide on the goal.

  2. 'Sandbox' to acquire necessary knowledge

  3. Write pseudocode

  4. Code

  5. Test

  6. Do it all again, that is 'enhance'

1. Decide on the goal

In this step you need to think about exactly what you want the procedure to accomplish. There are always a few little decisions to be made and now is the time to make them. What I am thinking of is that this procedure will just handle one 'step' in the turtles aimless wander, and so will likely be called repeatedly, much like our first Walk procedure, which is shown here. We learned previously that it is better to have forward called twice inside the procedure so that the animation works well.

to Walk StepSize
    repeat 2
        [forward thing "StepSize
        wait 1]
end
	

The goal of our new procedure, which we will call Wander, is to slightly change the direction the turtle is going and then take a step in that direction. Another way to accomplish something close to this is to remember the original heading so that the turtle 'Zigzags' its way, but always heading in its original direction. Perhaps this would be a good exercise for you to do after we have finished Wander.

So in the "Decide on goal" step, we are making design decisions. It often happens that you don't think of all the decisions at this point, but at least try to. Also it often happens that you make the wrong decisions, that's why the last step is "Do it all again.". You keep going through the steps enhancing your procedure as you learn more about what you really want and how to accomplish it. This is called an "iterative process" and is the way most professional software is developed.

2. Sandbox to acquire necessary knowledge

This step is a learning step. Here you look at your goal and see if there are some aspects that you need to learn more about before you can write the code. If so, then you write some 'sandbox' or 'throw-away' code to teach yourself what you need to know.

In our case, we want the turtle to change its direction some random amount. Random means 'unpredictable'. We need to learn more about this so let's 'sandbox'.

In MicroWorlds lets start a new project and add our usual start button and code, like this:

==========================
to Main

end
==========================

to Start
    Main
end
	

Go to the Help screen and look up random. Whenever you look-up a new procedure, you should draw the jigsaw puzzle piece for that procedure. This will go a long way towards helping you really understand how to use the procedure. random is a procedure that has one input, a number and one output, also a number.

The number output by random is a number between zero and the input to random. Let's investigate. Write the following code:

==========================
to Main
    Wander
end
==========================

to Start
    Main
end

to Wander
    show random 100
end
	
Click on the Start button several times. Works, doesn't it? In our Wander procedure, we will want to alter the heading of the turtle by a negative amount or a positive amount. random however only outputs positive numbers. How can we make random give us numbers that vary equally on each side of 0? Well, we can subtract half the maximum value from the result of random.

We need to do a little more sandboxing because we need some experience with the procedure to subtract one number from another. A look in the Help index turns up difference

difference is a lot like sum. It requires two inputs and has one output. Its output is the result of subtracting the second input from the first input. Here is what the puzzle piece looks like.

Lets sandbox with difference. Change your code to the following:

==========================
to Main
    Wander
end
==========================

to Start
    Main
end

to Wander
    ; show random 100
    show difference 25 10
end
	

Try different numbers until you are sure you understand difference

Now, do you know how to make our procedure come up with random numbers that vary equally on each side of 0? Try it yourself.

Here is my solution, give it a try.


to Wander
    show difference random 100 50
end
	
Do you have difficulty figuring out that line? When you 'walk through' it, starting at the left, it is a bit easier. Just be sure to think of the outputs and inputs.

  1. show - takes one input
  2. difference - has one output; good, show needs that; takes two inputs
  3. random - has one output; good, that is one of the two needed by difference; needs one input
  4. 100 - has one output; good, that is the one needed by random so now random is happy
  5. 50 - has one output; good, that is the other of the two needed by difference so now difference is happy and it makes show happy

Click on the Start button several times. Good, it works. Did using the numbers 100 and 50 make you think of adding something to the goal? Perhaps it would be better if we made the input of random also an input to our procedure Wander. Then the 'degree' of wandering ould be different, depending on the parameter used when calling Wander. For instance we could say Wander 80 or Wander 20.

Important Tip: When you are sandboxing, pseudocoding or coding you will often think of enhancements. That is good. However, don't incorporate them yet! Write them down somewhere so you don't forget, but continue on until the procedure is finished and then, in step 6 Do it all again, that is when you can enhance the procedure. This is important! Get the procedure done in its simplest form and then go back and make it more flexible or better.

An aside about consistency: Many of you know that Logo provides + and - operators that can be used instead of sum and difference. Why don't I use them? Well, from Page 4: "The program must be consistent with the language and with itself. This means that your choice of names for your procedures should make sense and follow the same consistent pattern throughout. Consistency is a very important quality for a good programmer."

The - and + operators are not consistent with the rest of the language. In the instruction show 4 + 5 the input to show looks like it is 4 but it is not. It is the result of 4 + 5. That is very inconsistent with the rest of the Logo language where the inputs always follow the name of the procedure, not one before and one after as is the case with 4 + 5.

Try drawing the puzzle pieces for show sum 4 5.
Try drawing the puzzle pieces for show 4 + 5.
You can't do it for the second version.

That is why I don't use '+' and '-'.

There is a bit of a long term lesson here. Just because a computer language allows you to do something does not mean it is a good idea to do it. You can choose not to do something if you feel it hurts the readability or consistency or has any other negative impact on your program.

OK, I think that is enough sandboxing, let's move on to step 3, Write pseudocode.

3. Write pseudocode

Pseudocode is just like program code except that it is written in english. It works as a quick and easy plan to achieve the goal without worrying about the right number of inputs or worrying about using the right procedures. Here is what I mean for the Wander procedure.

to Wander
    change the turtle's heading by a random amount
    move forward a certain amount twice
end

See what I mean? It does not have to go into great detail but it should be clear. Try not to use too many real names of procedures or numbers. Pseudocode also makes good comments to keep right in your real code so you, or anyone else can see at a glance what the procedure does.

Typically, you would write the pseudocode sort of quick and rough as we just did and then revise it so it gets closer and closer to real code. We can easily revise 'a certain amount'.

to Wander StepSize
    change the turtle's heading by a random amount
    move forward by StepSize, twice
end

There, that should be close enough that we can easily code it. Now let's move on to the fun part.

4. Code

As always, you should try this on your own first. All we have to do now is add a couple of semicolons and we have great comments for our procedure:
to Wander StepSize
    ; change the turtle's heading by a random amount
    ; move forward by StepSize, twice
end
	
Now, just do what our pseudocode tells us to do.
to Wander StepSize
    ; change the turtle's heading by a random amount
    ; move forward by StepSize, twice


end
	
Wait a minute, that first line isn't all that easy. Hmmmm, maybe we should expand it a little. To 'change' the existing heading implies that we could add something to the existing heading and then set the new heading to sum. But do we know how to get the existing heading? Well, a quick look at the Help index turns up heading:

Let's rewrite our pseudocode making it a little more specific.

to Wander StepSize
    ; set the turtle's heading to the sum of its existing heading and a random amount
    ; move forward by StepSize, twice
    
end
	

Should this have been done in Step 3. rather than here in Step 4.? Well, yes it should but it is not uncommon to have to jump back and forth a bit. The lines between the steps are not solid barriers, just a gentle blending from one step to the next.

OK, let's try again.

to Wander StepSize
    ; set the turtle's heading to the sum of its existing heading and a random amount
    ; move forward by StepSize, twice
    seth sum heading difference random 100 50
end
	

There, that should handle the first line of our pseudocode, click the Start button to try it. I like it!

Now, lets finish it. We need to add an input (parameter) to Wander

to Wander StepSize
    ; set the turtle's heading to the sum of its existing heading and a random amount
    ; move forward by StepSize, twice
    seth sum heading difference random 100 50
    repeat 2
        [forward thing "StepSize
        ]
end
	

5. Test

Step 4 and 5 are usually all mixed together. You tend to code and test, fix and test several times in the development of a procedure. Now go to your program page and click on Start. Ha Ha tricked ya! Forgot to change your call to Main didn't you! Here is a revised Main

==========================
to Main
    repeat 60
        [Wander 2
        ]
end
==========================
	

Oops, we forgot the Wait. Here is a revised Wander:

to Wander StepSize
    ; set the turtle's heading to the sum of its existing heading and a random amount
    ; move forward by StepSize, twice
    seth sum heading difference random 100 50
    repeat 2
        [forward thing "StepSize
        wait 1
        ]
end
	

Try it. Neat! Try adjusting the StepSize, I like Wander 5. Try adjusting the input to random. Try putting the pendown in Main. If you make the input to random smaller, you can make smoother lines.

If you have the plug-in, you can try mine here: Page 13a Project

6. Do it again

As mentioned above, it is now time to enhance our procedure. We should be able to adjust the 'smoothness' of the wandering by a parameter when we call Wander. As always, first try it on your own.

1b. Decide on the goal - again

Our goal before was "to slightly change the direction the turtle is going and then take a step in that direction". We will now enhance this goal.

Goal of Wander - To change the direction the turtle is going by a random amount, based on a parameter to the procedure, and then take a step in that direction, the step size also based on a parameter to the procedure.

This will make Wander more flexible because the "spread" of the wandering turtle will be controllable by the parameter we use when calling Wander.

Writing a computer program really consists of writing many tools. Each procedure is a tool to be used in writing a bigger program. For instance, I have a commercial program called HVAC-Calc. I sell it to Heating and Air Conditioning contractors. The other day I decided that it should print out a more detailed report then the ones that I had originally programmed. Because I had already written many procedures such as DoHeading, LPrint (for flush left printing), RPrint, Separator and many others, I could concentrate on programming the report not worrying about all the little details. I had developed the tools before and I just had to use them again. It only took about a day and a half and my program had a new report that its users could use.

2b. Sandbox to acquire necessary knowledge - again

Here is our existing Wander.

to Wander StepSize
    ; set the turtle's heading to the sum of its existing heading and a random amount
    ; move forward by StepSize, twice
    seth sum heading difference random 100 50
    repeat 2
        [forward thing "StepSize
        wait 1
        ]
end
	

The number that we are going to make into a parameter instead of a 'hard-coded' number is the '100' which is an input to Random. It is this number that determines how much the heading of the turtle is changed. However, the number '50' is tied to '100'. Remember, we had to subtract 50 from the result of Random 100 so that the turtle's heading could be changed by a positive or negative amount. How did we determine the number 50? It was half of 100, that is half of the input to Random.

So we need to know how to divide one number by another number. A quick look in the online Help shows us that divide is not there, but scrolling down further turns up quotient, as shown below.

We have used this same type of 'puzzle piece' before with sum and difference. To sandbox we should type in a few lines directly in the Command Center to make sure we understand how it works. Try show quotient 80 2. That should do it for the Sandbox.

3b. Pseudocode - again

This would be a good place to think up a good descriptive name for the second parameter. It is going to determine how far from a straight line the turtle moves. How about dizziness? Let's add this pseudocode as a comment.

to Wander StepSize Dizziness
    ; set the turtle's heading to the sum of its existing heading and a random amount
    ; The heading changes by a random amount up to Dizziness
    ; move forward by StepSize, twice
   
	

4b. Code - again

Now we can write the code. Again, do it with out looking at mine.

Here is one way to do it:

to Wander StepSize Dizziness
    ; set the turtle's heading to the sum of its existing heading and a random amount
    ; The heading changes by a random amount up to Dizziness
    ; move forward by StepSize, twice
    local "HalfDizziness
    make "HalfDizziness quotient thing "Dizziness 2
    seth sum heading difference random thing "Dizziness thing "HalfDizziness
    repeat 2
        [forward thing "StepSize
        wait 1
        ]
end   
	

Here is another, I like this one better, but they are both fine.

to Wander StepSize Dizziness
    ; set the turtle's heading to the sum of its existing heading and a random amount
    ; The heading changes by a random amount up to Dizziness
    ; move forward by StepSize, twice
    seth sum heading difference random thing "Dizziness quotient thing "Dizziness 2
    repeat 2
        [forward thing "StepSize
        wait 1
        ]
end   
	

3b. Test - again

Now you can play with, I mean test your procedure. Here is my complete program.

==========================
to Main
    cc
    pd
    repeat 60
        [Wander 5 10
        ]
end
==========================

to Start
    Main
end


to Wander StepSize Dizziness
    ; set the turtle's heading to the sum of its existing heading and a random amount
    ; The heading changes by a random amount up to Dizziness
    ; move forward by StepSize, twice
    seth sum heading difference random thing "Dizziness quotient thing "Dizziness 2
    repeat 2
        [forward thing "StepSize
        wait 1
        ]
end   
	
Be sure to try many values for the arguments to Wander in Main. Can you find any values for Dizziness that cause an error? If you do, what do you think we should do about it?

To Be Continued ....

Table of Contents | Previous Page