Hi, welcome to this new video. In this lesson, we are going to finally start addressing the final script of the second week, which is the Langton's ant algorithm. I'm a big fan of this algorithm. It's the way I learn how to deal with the data structure of grids and really start understanding principles of generative design, how you could actually create emerging systems between a simple interaction between a grid and a walker agent, something that moves around. Let's talk a little bit about what the algorithm does. We have an entity that is situated in a grid. The grid has a bit of data, so we can actually have cells being black or white, represented by 0 and 1 in the data that they contain. This walker, as we've been talking, has a direction. It has an orientation. If we say move forward, it would move along the axis of what we consider a forward direction. You can see here we can also rotate. What we will actually write is a function is rotate clockwise and move forward, which are going to be two different functions. One that allows us to rotate the direction of the agent, and another one that would allow us to move forward. The other function which we have already been writing is the flip cell, the capacity for the agent to change the information of the grid in which it's sitting. These are kind of basically the moves. We're going to start this first video creating some functions of the possible moves that this agent will be able to do. We're going to understand what those moves could be, and then we're going to start putting them together into a pattern. A sequence of moves that actually creates a very specific pattern and what we would actually call the Langton's ant. Let's just jump into it. I'm going to continue from this script that we left in last session. Let's just make sure that we are all in the same page. This is where we left off. We have a random walker that it's kind of flipping the information of a grid in the background. We have a grid made out of black and white cells. We can initiate that information at random, but we're currently initiating that information. If you remember at the very end of the lesson, we actually turned off this line here that would initiate all the information at random, and we replaced it with a zero so that we could actually start with all the cells black. Therefore the agent starts printing in a way, white cells as it moves. We have an area here which has to do with redrawing of the cell, separate frame. Finally, the area in which we have our walker. The main bit here, the walking behavior has to do with the area in which we are adding a random movement. This is what it's going to be replaced in our case. Currently, this is the movement of our walker. Let's just comment that out because we want to make sure that our walker doesn't move anymore and we're going to introduce new forms of walking. If you wanted to run it here, nothing should happen. You shouldn't move. This is a boundary condition. We also don't need these lines. We can delete that. There we go. This is the information of the flipping of the cell. We are going to start breaking these blocks into functions. So we're going to be a little bit more clear of how we are moving the agent. Finally, this is the area in which we draw the walker. Let's go all the way to the top of the script and define a new variable. This variable is going to represent the direction of the agent. I'm going to use an integer, I'm going to call it direction, and it's going to be zero. If you remember a little bit of what we've been discussing in prior lessons, this direction, it's going to be either 0, or 1, or 2, or 3. That's going to represent the four cardinal directions in which this agent will be able to move. This algorithm works with these four orientations. Now we have a direction. Let's visualize that direction. Let's just go all the way to the bottom here when we're actually drawing the agent and let's just do a bit of logic or some representation that would allow us to draw the direction. Where do we want to draw the direction as? We want to draw the direction as a line? We could say something like a line. That goes from the position. A line it's a primitive that requires 4 pieces of data, two points basically, if we're doing a 2D line. We need the position of the agent in x and in y and we need to provide a distance. We will use the same position in x, but we're going to give it a little bit of maybe plus 20, some amount of distance and a position in y just like so. We're going to draw a line in the x-axis. For the second point in x, we add 20. That means that the line is just going to be flat horizontally, and that should represent our direction. We're going to actually use a stroke to be red. Let's just do a little bit of the stroke weight to be slightly stronger. We could parameterize. This Number 20 is something that we could make a variable here. Let's call it the arrow size because we're going to be using this variable a few times. Let's see what we're getting at this point. You see the agent is not moving, but we could actually see the dot and a line 20 pixels to the right in red color. That's certainly showing us a little bit of the information that the agent will need for demonstrating its direction. But if we actually change the direction, the direction currently it's zero. If we change the direction, this arrow wouldn't change. The line wouldn't change under this formula. Let's just create an if statement or a condition by which we could say something like if the direction is zero, let's draw this line. This line is only going to be drawn in this way if the direction is zero. Let's again, double check that. That's the case. Now we have one condition if the direction is zero. We know that the direction could actually be 1, 2, 3, or 4. Let's just copy this line a handful of times, four times specifically and let's just change the number here to Direction 1, Direction 2, Direction 3. Basically, every time that we draw this line, there's going to be a slightly different place where we have to locate the arrow size. In the case of the Direction 1, we are going to move the arrow size here. Because this is going to be facing downwards. The position in y, the size is going to be facing downwards. Let's just do it for the position when we're looking to the left. That's in the same location that we had it initially. But instead of being a positive, it's going to be a negative. We're going to say minus arrow size. Finally we are going to get rid of this line here. We're going to do the same thing for D. Final direction, which is up, we're going to have the arrow size facing up by using the negative size of the arrow side. If we're looking to the right, we have 20 pixels to the right. If we are down, we have 20 pixels down because the processing counts positive numbers as we go down. Then if we are looking to the left, we have the arrow on the left and so on, and we have finally the direction Three. We have all the possible directions as something that we can represent. But there's nothing at this point changing the way in which our agent rotates. We want to start doing functions for this, because as you can start understanding, this is where our scripts really start becoming long, and it's not really a good practice for us to keep writing code within these structures, so we're going to start learning how to implement some of the knowledge that we've already acquired through Pros sessions, put it to exercise, creating some functions. What would be a rotate function? Let's just write first our first rotate function. Let's define that as a rotate_clockwise, because we could actually do an anti-clock or counterclockwise rotation. I'm going to also say of 90, because the rotation, again, we could use a parameter, but because this agent specifically can only rotate between four cardinal directions, we're just going to be very explicit with the naming of this function. Let's use the global int_direction, which is the variable that we're going to be transforming or affecting here, and we are going to say that whenever this function is invoked. If the current indirection or let's just say the statement, if the indirection equals 0, the direction becomes a 1. It's a very straightforward function, but it's basically quite effective in this case. We are making sure that if this direction is 0 and we invoke this function, it's going to be now 1. Let's do an L if statement. Just make sure that this actually checks all the four possible conditions that we have. This cannot be an if by itself. If you do an if, you would cascade from one statement to the next one, and that's not what you want. You need to do it as an L if statement. When if is a 1, it should become 2. Let's just do one more of those, actually two more of those. Those are all the different versions just to make sure that we have all the different information here. If it's a 2, this will become a 3, and if it's a 3, it will become a 4. This is a function. The function takes the current direction, and it will change it with a 90 degree rotation. Which is just basically, perhaps a quite a long way but very explicit way of saying, hey, we're going to rotate in 90 degrees. What other function do we want? The other function that we want to draw here is, before we were doing a function that would move randomly. Let's think of a function, what would it be to move forward. Move forward means that I move along the axis of my direction. Let's define a function that we're going to move forward and we are going to provide a direction. We're going to provide an argument which is going to be the direction which we're going to evaluate. Let's just affect the values that we want to affect or change. Is the global, basically these number is here, the index in x and the index in y. These are the values that represent where the walker is located in the grid. If I'm facing to the right, I want to move to the cell on the right. Let's just evaluate. The direction passes the argument direction here. Check if that is 0, that means that we are in moving to the right state. What we want to say is that, in that condition, the index of the cell plus equals 1. Again, it's a very straightforward thing, but if we're adding these conditionals, if statements to check, if I'm looking towards the right, I'm going to change my value in this direction. Let's add an elif statement if the direction is equals to 1. Remember that, what I'm using here, the value there for direction, represents the current direction that is going to be passed on in this function. We're using a placeholder name for that function. Here, if the direction is one, what we want to do is that the current index in Y, so now X is going to be plus equals 1. This would allow us to move move down, and we need two more conditions. we have those two. Copy pasting both at the same time so we could start seeing, I really like seeing the code as a texture. Sometimes I start understanding, we're doing this four times. Then the second, for the direction number two, which is when we're actually looking to the left, we will use the X axis with a minus one. If we are using the direction three, we are going to do the Y axis with a negative facing up. That's it. We have rotate clockwise and move forward function. Let's try to use them. I think we've been writing a lot of code. We haven't press play. It's always a good practice to make sure that things are still running. It seems that script is still there. Let's see if we can go down here. If you remember, our walker was moving randomly. Let's test one of them. Move forward. For the move forward function requires the direction. What is the variable for direction? Let me find it so I can copy pasted in direction. There we go. I'm going to get rid of these slides. These are getting confusing here. Let's just call this the walker movement. If I move forward, let's see if that function is working, that seems to be working, fine. It actually is moving towards the right. What if we actually ask to rotate? I'm going to quickly go and copy paste the number and the name of the function. I don't want to get it wrong. Sorry for the quick scrolling. In the walker movement, we can use our rotate clockwise for 90 degrees. If we start by rotating in 90 degrees, then we move. The next frame, we rotate and then we move. We will actually get the agent, technically should be spinning in place constantly. I wonder if that was an error. Let's just double check that. Let's add a second line for move forward. We rotate, we move two lines, so we're not getting this condition here. Let's double check if there's anything that we wrote wrong in our functions. We have the current index in X+1, the current index in Y+1, minus onein X and minus one in Y. Here, if the direction is zero, if it's one is two. If it's two is three. If it's three, is zero. We don't have a direction value four, the value that should have been a zero. We're going back to the beginning. That was the problem that we were facing. We could see the behavior moving right and going down and going left and going up, but it wouldn't come back to the original first orientation. Let's go back to the work. Let's see. You see that our agent right now moves in between these four states. It keeps flipping. We actually can control its behavior through this movement. We're going to leave this first part of this video here. We actually have functions for movement. We're going to start seeing how to leverage these function, these behaviors based on the information of the grid. I'll see you in the next video.