Hi, welcome to this new lesson. We're going to continue and conclude, in a way, this second week of discourse by concluding the Langton's Ant algorithm. We have already started so if you haven't really started, this algorithm is something that we've been working for three sessions now. This is going to be the final fourth session, so please go back and check some of those videos before in case you are joining us at this point. Let's dive into it. This is where we left off from the last video. We have a walker. We have very simple behavior at the moment, which is driven. Let's find the place down here which is the walker's movement. The walker movement is based on some functions that we wrote, rotate clockwise, move forward, invoking that function twice. We are going to be using these functions. We actually need to create one more function that I'm going to do again manually, is the counterclockwise rotation. Perhaps you can explore ways of writing these functions in a much more succinct way. I'm trying to make them very explicit so that we're understanding what's going on. But if you think about the counterclockwise function, it's very much the same as the clockwise rotation. We're going to copy this function here. Let's call it counterclockwise. Then we're go, counterclockwise in 90 degrees. The counterclockwise rotation will also affect the direction but the other way around. If it's zero, it's going to move towards the three. If it's one, it should move towards zero. If it's two, it should be moved to one. If it's three, it should go to two. We're just basically imagining the last rotation is three. If we're looking towards the ride, facing up would be going to the rotation 3. If we're facing down, we look to the right. If we're facing left, we go down. If we're actually facing up, we end up facing left, so again, very straightforward. Maybe a long code here. I'm sure there are much better ways that you can figure out on how to write this, but we want to be very explicit about some of these algorithms as we are learning how to write them. Now that we have the rotate counterclockwise, let's just copy paste this function. We can actually create a sequence of behaviors. This is the area in which we're actually drawing the walker movement. Let's do something like rotate clockwise, 90 degrees, move forward, rotate counterclockwise, and then move forward again. This should create something like a diagonal. But I've realized that I think just checking some of the code that we have, I feel like we made a mistake somewhere here. Our boundary condition has an error. We actually get to the bottom of the screen. We will be requiring the entity, the walker, to stay in a number that doesn't exist within our data structure and that's going to create a null reference. We are going to say here resolution minus one in both cases. This is something that, if you look at the notebooks that we prepared for the class, this is the way it's supposed to be written. I think that we forgot to add this. Basically, because we start at zero, the actual final number for the resolution is not the 50 or the 100 that we actually have. It's going to be the 49 or the 99. We want to add this minus one as a way of accounting for starting at zero and not starting at one. Let's see if this actually works. We actually want the walker to rotate first in 90 degrees, move forward, rotate counterclockwise, and then move forward again. We actually do get this behavior because the behavior gets executed until here and then it starts picking up on the constrained condition of the boundary, so we only get the movement to the right. That's an interesting also interaction between the two different pieces of behaviors that we have. On the one hand, we have a sequence of movements. On the other hand, we have a constraint for the boundary. This is a very quick example. This is not in fact the Langton's Ant, but the Langton's Ant uses these functions as movements. I'm going to comment this out for us, it's just for reference. Feel free to just delete them if you want. Let's write first some comments of what do we want the Langton Ant to do. Sorry. Here we go. The Langton's Ant is an algorithm that considers the cell information. If the cell information is white, we are going to move clockwise. We're going to rotate clockwise. We're going to change the cell. We're going to flip the cell which we are already doing. I think we can actually, this is the area of our code that currently flips the cell. We're going to get rid of that as well or, you know, change it in some capacity. If it's white, we rotate clockwise, we flip the cell and then we move forward. If the cell is black, we're going to rotate counterclockwise, we flip the cell as well, and we move forward. The only thing that really changes is the clockwise or counter clockwise based on the cell condition. I'd really like to make this algorithm quite explicit. Let's just write it almost from scratch here. We're going to get rid of the flipping of the cells, even though we're going to rewrite this. Let's just make sure that our index calculations. Let's just leave that there. Let's write our agent movement down here. Let's write a comment here. If the cell is white, so how do we check for that? That would be an if statement that considers that the cell, considering the index is equals to 1. This is cell is white condition. We want to do the rotate clockwise in 90 degrees. We also want to flip the cell. We're going to say cells using the index value, which we learned how to use a couple of sessions ago. That's going to become zero. This will convert the cell into a black cell. Finally, we're going to say move forward considering the variable direction that we have created. Those are the three conditions that happens if I'm on top of a white cell. The second condition, again, we're going to write it as an LF because we don't want to be checking these two conditions at the same time. Let's just maybe copy the whole thing. It's quite similar, but it's slightly different. If the cell in this case is black, then we actually change the cell. We are going to rotate counter clockwise. We will switch the cell from black to white and then we will move forward. Let's add a comment here. If the cell is black. This is basically the Langton's ant algorithm. It's taking into consideration a cell that has its direction. Let's see if it runs and then we can explain it a bit further. I think we are. Here we go. As you can see the ant in this case is actually operating within this grid. What is happening is that we're starting to see a pattern emerge. There's a behavior if you leave this algorithm running for quite some time and you can probably find in YouTube different instances where people have made this algorithm run for many, many hours. You'll see that there's an emergent pattern that starts occurring. The sequence is quite simple sequence, but it actually operates over the outcome it's producing. There's a circularity between what the algorithm is doing ultimately and how is that used for information, for the behavior of the worker for the next iteration. It's not absolutely random, it's a specific pattern that keeps coming back to its own production. These are very powerful techniques that we can actually start using for design. You might like the pattern graphically, but more than that, the idea of the emergence of simple rules leading towards larger behaviors is something that really is one of the great attributes that you can explore using code as a generative design approach. Yes, you can certainly use the patterns graphically, but at the same time when you're starting to learn how to code, you're starting to understand how to basically design with code perhaps in a different way. We're going to be continue exploring these ideas throughout this course. We're going to be expanding on these notions in the next weeks, but with this algorithm, we are going to conclude this week. I'll see you in the next session.