Hi, welcome to this 4th video in our final, 5th week of this course. We are working through the project of the wave function collapse algorithm. We're going to be looking at how do we collapse a cell. The idea of collapsing a cell is going from a large possibility space of maybe four or eight or 16 different possible options for a cell to a singular one. How we're going to do it? We're going to be doing it by selecting out of all the possible options that we have, the actual options that have to do with the compatibility with other adjacent tiles, we're going to pick one at random. We're not really going to be determining any criteria for picking one of these cells. You could start giving some criteria here, but we're going to be using one at random as long as it matches the adjacencies of the tiles around it that have been collapsed. Meaning that we only evaluate the possibilities that actually work. Based on compatibility, we can pick one at random. That's the beauty of this algorithm. As long as that tile can exist, we can just collapse it by a random selection. As you can see here, this particular tile that we have represented in this graphic has eight possible options. We pick one at random, we make that the actual, we get rid of all the other options and at that point that cell is collapsed, we change its graphic and we will move later into the propagation. We're getting close to start really implementing the algorithm as is intended. Let's jump into the code and write the collapsing of the cell. Here we are, the collapsing of the cell will invite us in a way to start really writing the wave function collapse algorithm as such. We are here at the beginning, we're going to be working on the environment area just after the run because this is the part of the algorithm that will run continuously. We're going to be defining the function wave_function_collapse. We're going to do that here with the self like that. This function we want to run every, let's just run it every frame. Nice. The first thing that I would like to do with this algorithm, we're going to separate the wave function collapse. It's going to be the general algorithm that is going to contain a series of functions. The function that we're going to be writing today, it's the collapse function. But here first I would like to identify the cell that we're going to be first working on. Think of the following. We want to identify what is the cell that we would like to start with. Let's define a variable called the next_cell. We can do that here. We could say self.next_ cell_x. We're going to say the first one is going to be 15. This is an arbitrary number. Let's write that correctly, next_cell. I'm going to be placing it roughly at the center of the screen, so 15 and seven. I believe that our Canvas we're working with 30 and 15. That's roughly in the middle. Here now we're going to start writing the algorithm. Let's just start defining that. The way we're going to start will be self.next_cell_x. This is the cell_x and y we're going to be working on. We do want to do a first check here and we want to check if cell_x is none or cell_y is none, return none. We want to do this just because the algorithm will run sequentially. At some point we will run out of next cells to perform. We want to make sure that we have some provisions for that. At this point we could actually pass these cells. The first function to execute within this algorithm will be in the collapse cell. Let's call self.collapse_cell, which is a function we haven't created yet, but we're going to write it just now. Let's collapse those two cells. Let's define that function now, collapse_cell and for this function we are going to start with self, x, y. That's the information that is required, the arguments, which is the cells that we're going to be collapsing. Let's just write possibilities. If we think about the possibilities, we know that the cells variable contains all the cells. It has x and y. We're basically identifying the cell that we want to collapse. But we're calculating its possibilities, meaning at this point it will be four. The list of possible tiles exists within the nested list. We have one of those and we basically want to select the chosen tile and it's going to be using the random module choice. Choice is very good because when you have a list and you want to select random from that list, we can use this function random.choice that we pass the list and we'll return one element of that list. Now we can actually say that the self.cells [x] [y] is the chosen tile. We are basically going to say get rid of everything else. If there's four options, make one of them collapse and select one of them at random. That's it. At this point, we're not propagating, we're not kind of iterating over the process. We're just saying, let's start with a cell at random, which is the middle one, 15 and seven. Pick that cell, see how many possibilities it has. Store those, pick one at random and now assign it's actual value of the cell. At that point, the tiles, it's going to be not four, but one. It's just going to be a singular. We're collapsing that cell effectively. Let's see if this in fact is running. We're running into an error here. Let's see what we wrote incorrectly. I think it might be that we're missing an s here. Let's see if that's the trick. There we go. What we can see here is that now we're actually invoking both of the different display functions that we have for the cell. Like if the cells have a large possibility space, we select the one that represents the number of its entropy. If it's been collapsed, we show that cell has been collapsed and it has a representation of its compatibility. We're good. We're going to leave this video here. We have in fact collapsed one cell. We need to start learning how do we propagate this algorithm to the adjacent cells. We're going to be seeing that in the video to come. I'll see you then.