Simple rules can produce structured, complex systems. PrintBoard() (should be named boardToString): uses a stringbuilder to format the grid into a string.From our sponsor: Design and publish websites for clients in one go-with Readymag All of the neighbors and alive cells are then put into a HashMap so that birth() can run properly Inserting a live cell follows the structure of minesweeper (neighbors of a live cell will be incremented by 1 and the alive cell will be incremented by 10 to denote that it is alive). InsertAlive(): pops the stack and inserts the alive cell into the grid. After each iteration, the value of the grid is reset to 0, and the key-value pair is removed from the HashMap. If the key-value pair follows the game of life’s rules above, the key (an integer value that represents the location of a cell) is then pushed onto a stack that contains the next generation of alive cells. It would be a lot faster if I stored all the currently alive cells and checked each alive cell along with their neighbors to determine the next generation (which is exactly what I did).Įxplanation of important methods (and data structures used)īirth(): iterates over a HashMap containing a key-value pair of all alive cells along with its neighbors. As a result, checking the entire grid (time complexity: O(n^2)) to determine the future generation of cells would be a complete waste. Only cells that are alive can die, cells that continue to live have to already be living, and cells can only be born if they have neighbors that are alive. ![]() One observation that occurred to me is that the game of life is only concerned about alive cells. What about the cells that are currently alive you might ask? Well, we can easily represent these as an integer greater than 10, where the one’s place indicates how many alive neighbors the currently alive cell has, and the ten’s places indicates that the cell is alive. In this way, we are able to easily use the rules above to determine the future generation (particularly which cells will die, and which cells will be born). We can think of an alive cell as a mine, and its neighbors storing the number of mines that are closest to it. In a different perspective, however, Conway’s game of life is very similar to the game minesweeper. In addition, the current state of all cells determines which cells will die, continue to live, or be born in the next generation in accordance to the rules found above. Each grid block can be thought of as a cell, which not only stores a Boolean value (dead or alive) to describe the cell’s status, but also details its location via its coordinates. This 2-Dimensional grid represents a group of n*n number of cells. Let us first imagine the Game of Life as a n x n grid (we will also assume that this grid has coordinates such that the bottom left hand corner is denoted as (0,0) and the top right hand corner is denoted as (n,n) where n is a positive integer).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |