We now need a way to regularly update the model with the rules to create one generation of the simulation after the other. Let's start with some pseudo-code to find the outline of this strategy: Then we can iterate over each cell in the current generation's matrix, apply the rules to each cell, and set the value of the cell in the next generation's matrix according to the result of the rules. To avoid having a partially updated matrix, with cells in the next generation that are not yet updated, we can create a new, blank matrix. The rules are applied across all cells in the matrix with each generation. Recall that the rules of the game of life are: Now that we have a representation, and a way to query the model for the number of neighbors a cell has, we can implement the rules of the Game of Life. If it is, we increment the count of "living" neighbors. Finally, if we have a valid neighbor cell, we check if it is alive, by testing if its value is true. We also check if the current cell is outside the grid ( =sMATRIX_SIZE), in which case we don't count it as a neighbor. If it is the target cell, we don't count it as a neighbor. We know it's the target cell if both of the sweep values are 0. We use a conditional to check if the current cell is the target cell. Notice that we use for loops to sweep from -1 to 1, which represents left to right and up to down. So, if we have the target cell coordinates, x and y, we can use the following code to find the number of neighbors:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |