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: The immediate top left neighbor would be: matrix.The immediate bottom neighbor would be: matrix.The immediate top neighbor would be: matrix.The immediate right neighbor would be: matrix. The immediate left neighbor would be: matrix.So, noting that every neighbor is one position away, we can add or subtract one from the row and column numbers to find the neighbors. Recall that we can reference any cell in our matrix structure using the notation matrix. So if we create a function that looks at each cell one position away from the target cell and counts how many of those cells are alive, we can use this to find the number of neighbors a cell has. Notice how each neighbor cell is one row or column away from the cell we are looking at. A handy function to have would be one that finds all the neighbors of a particular cell, and counts how many of them are alive. In particular, the rules refer to the number of "live" neighbors a cell has. For example, for the blue cell below, all the red cells are neighbors. Neighbors are any cells that touch a particular cell. In the Game of Life rules, there are multiple references to "neighbor" cells. We set all the values to false, or dead, to start. This method takes a single value, and sets all the values in the array to that value. To set the value of each cell in the row, we use the fill method on the row arrays. The first call to the constructor creates the "outer" array, and then we use a for loop to repeatedly create the "inner", or row, arrays. Note that we use the array constructor by calling new Array(MATRIX_SIZE) to create each array. Now we can add reference to Kaboom, and initialize it: In the "main" code file, first delete all the example code. It already has some code in it, but we'll replace that. Name it something like "Game of life", and click "Create Repl".Īfter the repl has booted up, you should see a main.js file under the "Code" section. Head over to Replit and create a new repl, using "Kaboom" as the template. In this tutorial, we'll build the Game of Life using JavaScript and Kaboom. It's pretty cool to set some patterns, and then watch how they evolve. The interesting thing about Game of Life is that, despite its simple rules, it can create amazingly complex and interesting patterns, and even "lifeforms" and machines. He first played it using a Go board, updating the game manually. He didn't build it for a computer initially. John Conway spent about 18 months of his coffee breaks tweaking the rules for the game, to come up with the rule set that made the most interesting patterns and properties. If a cell is dead and has exactly three live neighbors, it becomes a live cell.If a cell is alive and has two or three live neighbors, it lives on to the next generation.If a cell is alive and has more than three live neighbors, it dies.If a cell is alive and has less than two live neighbors, it dies.A set of rules is applied on each generation to determine the next state of the cells. Each cell has a state of being either alive or dead. The Game of Life is played on a grid of cells. We could call it a sim game as well, as we create the initial state of the game, and then let it evolve according to pre-defined rules. He called it a Zero-Player Game, as it was played by the computer. Building Conway's Game of Life with Kaboom.jsĬonway's Game of Life was invented back in 1970 by John Conway.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |