Joel:Hi there. This is Joel Castellanos of the University of New Mexico with a CS4 video on conditional logic, Netlogo’s if Statement and Bumper Turtles, and ultimately Bumper Turtles in 3D. Conditional Control Flow, also called conditional logic is used in all computer programming languages. As a vehicle for getting to know the conditional logic thing, we will be building wild, crisscrossing, crazy bumper turtle tracks.

The program running in the lower right is an example of not so wild and crazy bumper turtle track. This maze game has five basic rules. When a turtle is about to hit a black patch it reverses its direction. A turtle also reverses its direction if it’s about to hit another turtle.

When a turtle is about to hit a blue patch, it turns left. When a turtle is about to hit a red patch, it turns right. Otherwise, the turtle continues straight, unperturbed across the glorious green grass.

The goal of this game is to see how complex and how cool we can make the paths without any of the turtles, either getting stuck in one patch or stuck in simple loops that bypass most of the blocks and turns.

Conditional control flow enables the programmer to control the flow of program execution according to the answer of a true/false question. In the bumper turtle example, if the patch ahead is black, the program needs to execute a different sequence of commands than when the patch ahead is not black.

I’ve loaded a Netlogo model of these bumper turtles. Weare going to take a detailed look at these first four setup procedures and at the partly working go procedure. The first setup button places two black patches on a green field and one turtle between them, facing directly to the left.

When I click go, the go procedure is called. Since the patch ahead of the turtle is green the turtle should move one patch forward, then the go procedure ends. The go button has it’s forever option checked , thus, after the go procedure returns from having [00:02:00] moved the turtle one space ahead, the go procedure will be called again and the turtle will again move forward one patch. This will continue until the turtle detects that the patch ahead is black. Then the turtle will make a 180 degree turn, reversing its direction.

This is the code of the partly working go procedure. Let’s starts with S turtles to iterate through each turtle. Then, inside of S turtles, we have ask patch ahead one. Patch ahead records the single patch that is the given distance, in this case one, ahead of the current turtle. Inside that patch ahead context we’re going to set a local variable, which I called color of patch ahead to P color. P color, inside this context, is referring to the patch color of the patch that’s one ahead. Then, we come out of this patch ahead context and [inaudible 00:02:39] statement is back inside the turtle context. The if statement is one way to do conditional control flow in that logo.

The syntax is the keyword,if, is followed by a condition that must evaluate to true or false. In this case, if the variable color of patch ahead is equal to black, then it’s true. When it’s true then this block of code is executed. You have to have an open square bracket, list of code; in this case I just have one statement, left 180.

This left 180 is inside the turtle context so it is going to be having the turtle turn left 180 degrees and reverse its direction. This block of code only gets executed when this condition is true. If this condition is false then we skip over this block.

Either way, whether the condition was true and we executed the block or we skipped over the block, next we come to this if statement. In this if statement, we are going to ask if the patch ahead is equal to green. If so, then this entire block here; here is the open square bracket and here is the corresponding closed square bracket.

Now, inside of that, we have another if statement and other [00:04:00] blocks. If this condition is true, then we have an open bracket and the matching closed bracket is all the way down here. All of this is what happens if it’s true. If it’s false we skip over the whole block. Then we are going to close the S turtles, advance the tick counter and that’s the end of our procedure.

Now we will take a closer look at that complex block of code if the color of the patch ahead is green. In that case, there are two possibilities. It could be that there is a turtle right ahead of us. In that case we need to make a 180 degree turn, reverse our direction. Or the path could be completely clear. If it’s green and there is not another turtle then we are going to move forward.

The ifelse statement; and this is written all as one word, ifelse; if it has a condition and if this condition is true then we do this first block of code. Then it’s followed by a second block of code. This block gets executed, if, and only if, the condition was false. So, let’s take a look at the condition.

We have patch ahead one. So we already know patch ahead one is going to report the patch that is one ahead. This is the same reporter that we used up here. This is going to report the patch that is one ahead.

Next in line in this condition is the turtles on reporter. Turtles on reporter has to be given a patch; just like patch ahead has to be given a distance. Patch ahead was given a distance one and it returned to patch. And that patch is then passed into turtles on.

Turtles on takes a patch and returns the set of all turtles that are on that patch, which will be the empty set if there are no turtles on that patch.

Finally, we come to the any reporter. The question mark here is part of the name of this reporter. In Netlogo, the convention is that whenever you have a reporter that reports either true or false, its name ends in a question mark. This any reporter takes a set and if there is at least one element in the set it returns true. If the set is empty, it returns false.

So this [00:06:00] whole expression is going to be either true or false based on the result of any. The order of execution is that patch ahead one is called within the turtle’s context. So we look ahead one patch and it returns the patch that’s one ahead. That’s [inaudible00:06:14] turtle on, which returns the set of all turtles that are on that patch ahead.

That set is then passed to any, and if the set contains an item, this whole expression is true. So we are going to turn left by 180 degrees or make a U-turn, like an about-face. If this expression is false then there are no turtles on that patch ahead and we can move forward, go to this block.

Now that we’ve seen how each of the parts work I want to go back to looking at this compound if statement, this [nestative 00:06:42] statement. Here we have, if the color of the patch ahead is green, we’re going to do this whole block of code here. If this is false, if the color of the patch ahead is not green, then we don’t even ask this question. This whole if statement gets skipped.

But if the color of the patch ahead is green, we come inside the if statement and here we have an ifelse statement, which has to have two blocks. An ifelse statement has to have a block that happens when the expression is true and a separate block that happens if the expression is false.

The first block starts with an open square bracket and that block must be closed before we can start the second block. And that’s our conditional control flow.

Now, I’ve named this procedure partly work and go because it’s missing a few things. If I click check syntax now, Netlogo tells us there is an error; that the highlighted color of patch ahead has not been defined. But, it is defined, right? Right here I said, “Let color of patch ahead equals P color”.

The problem is that this is a local variable. When I use let I am creating a local variable and this local variable has a scope; an area of over which is defined. The scope of a local variable ends with the closed bracket of the block in which it’s contained. So it’s defined only in the block right here, [ask patch ahead 00:07:54]. Yet I am trying to use it [00:08:00] outside of that block. In order to use the local variable, color of patch ahead, outside of the patch ahead block, I’ve got to define that variable outside the block.

So I could take this and define it outside the block. Now if I check the syntax I have no syntax errors. But now I have a semantic error. My program doesn’t do what I want it to because, when I say “Let color of patch ahead equal P color”, when I say “P color”, in this context, in the context of S turtles, it’s going to give me the patch color that the turtle is standing on. And that’s not what I want. I want the patch color that’s one ahead.

What I need to do is define the variable color of patch ahead in this larger block of code inside the S turtles and outside color of patch ahead. I have to define it here and initialize it. And I can initialize it to anything because I’m going to override that value when I come inside if patch ahead. So I’ll call it green.

And then, inside, I’m not going let anymore because I’ve already defined the variables. So I’m going to set, I’m just going to change it. I’ve initialized it to green and then I’m going to come inside, looking at the patch ahead, and actually set it to the P color.

So now I don’t have a syntax error because I’ve defined it at this larger scope; the same scope where I am trying to use it. Yet, I’ve still set it inside where I have the proper context, where I am looking at the P color of the patch ahead.

Okay then, this partly work and go procedure works correctly for the black and the green patches. It doesn’t handle the blue and red patches and that’s left for you to do. Before taking a look at the setup code, I want to go back to the interface and remind you of what the setup looks like.

We’re going to look now at the code behind the setup two blacks, click that and it makes a field of green with two blacks, and the turtle is right in the same line with those blacks. Now I can hit our partly work and go and we see it’s working just fine with the two blacks.

Here we are in the code for setup two blacks. [00:10:00] [inaudible 00:10:01] clear all reset tics and I’m going to go through every patch that’s set to green. Then, I’m going to go specifically to the patches I want to change and do them one at a time.

The way I set individual patches is; I can ask the patch at the x coordinate minus ten, the y coordinate zero. At that patch and in this block of code I set its P color to black. Then I’m going to ask the second patch; this one I want the same height. But, here where I am minus ten I’m going to plus ten, and set that one also to black.

Now, I’ll create my turtle. I have just one turtle and I’m going to set its x/y position to six zero. I want it to be between these two blacks. So it’s got to be at the same height. These are both at zero so I have to put this at the height of zero and six is, it could be anywhere between minus ten and plus ten.

Also, how I set the heading is important, because it needs to be moving either toward the one block or towards the other block; which means it either has to be going directly west, to the left, which is two seventy, or it has to be going directly to the east at ninety. Set the color to white, pin down, and that’s our setup.

The next pattern I want to take a look at is these four reds. I’ve set them up so that the turtle will move in a box pattern. It comes in from the side, passes right past this first red block, but it’s about to hit this one. So when it’s going to hit a red block it makes a right turn, moves up, hits another red block so it makes a right turn and gets caught in this loop.

Next I want to show this setup L pattern, which uses one of each of the colors. I’ll say go, so it’s moving forward until it hits the red. Then it’s going to turn to its right, moves up until it hits the black. It’s going to make an about-face. Now when it comes down to hit the blue it’s going to turn to its left, which will be right on the screen because it’s pointed downward. Then it will move over until it hits the black and bounce [00:12:00] back again and continue in an L shape.

Finally I want to show an example that uses two turtles. We still have the L shape. I start the turtles out pointed next to each other but in opposite directions, hit go; each moving in opposite directions and they’re moving in their L shape, except that when they’re about to collide they also reverse direction.

This is the Netlogo code for the setup that makes the two turtles moving in the L shape. It starts out the same as I did on the previous setup; clear all, reset the tics, [ask00:12:27] the patches, set everything to green and then I go through each patch individually that I want to set a specific color with its coordinates.

In this create turtles block I’m going to be creating two turtles. I want to do something differently for each of the two turtles. What’s going to allow me to do that is Netlogo’s who and conditional control flow. When a turtle is created in Netlogo, it’s automatically assigned an index number. The first turtle that’s created is index zero. The second one is one, two, three, etc. So if there are twelve turtles, they’ll be indexes zero through eleven.

So I have two turtles. One will have and index of zero and the other will have an index of one. And Netlogo’s who command will tell me what the turtles ID is. So, if the turtle’s ID is zero, I’m going to set it’s x/y position and it’s heading. This block only executes when who is equal to zero.

Similarly, this block only executes when who is equal to one. Finally, the set color white and pin down, those are outside of both of these statements, so they’ll happen whether who is equal to zero or who is equal to one. [inaudible00:13:29] set the color to white, always put the pin down and that ends the create turtles block and that ends the whole procedure.

Now you’ve got the moves, just put them together to make your own creative patterns. Remember to plan it out on graft paper first. Draw all of the boxes and the lines, each place where it’s making a turn and which direction it’s turning and have fun.

But to really have fun, you’ve got to do this is 3D. If you’ve ever wondered about that Netlogo 3D program inside your Netlogo folder, now’s the chance you can find out what it does.

In Netlogo 3D [00:14:00] patches are cubes, rather than squares. I’ve already clicked the set up square button, which sets up the same square pattern we looked at in the two dimensional version. With Orbit selected, I can click and drag, to rotate our view of the space. From this point of view, we can see the cubic shape of the four red colored patches and the spaceship-like shape of the turtle.

What was the green grass in the 2D model is colored black in this model. There’s a reason for this. In Netlogo 3D the black patches are not really black but transparent. If, in this model, I colored all the patches green, then we would not be able to see the interior of the world. The turtles would all be moving and bouncing off the red blocks. But our view of the action would be blocked by the green cubes on the outer sides of the cubical world.

While this model is a three dimensional world, this set up square procedure places the four cubes and the turtle all in the same plane. Thus, the motion in this model is the same as the motion in the 2D model.

Now let’s look at the coding differences required to make our two dimensional square pattern run in a three dimensional world. In Netlogo 3D, to specify a particular patch, instead of needing just two numbers, we need three numbers; the x, the y and the z; the up and down.

I’ve put all four of the red patches at a z value of minus five so they’re all in the same plane. The x and y values I left them exactly the same as they were in the 2D version. Similarly, the turtle also requires three vales; the x value, the y value and the z value; how far up and down it is. I put that at minus 5 too. So all of these patches and the turtle are all in the same plane.

Staying in the plane is boring so I extended the bumper turtle game to include two new block colors. A violet block makes the turtle tilt upward by ninety degrees and a cyan block makes the turtle tip downward by ninety degrees.

Notice that there are two blocks in the corner that the turtle is now approaching. The turtle is tilted downward. When it hits the violet block, it will tilt back upward. But it’s heading, from when it was in the upper part of the cube, is still toward the right side of the display.