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). Explanation of important methods (and data structures used)Ī different outlook on Conway's Game of Life.A different outlook on Conway's Game of Life.Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.Any live cell with more than three live neighbors dies, as if by overpopulation.Any live cell with tow or three live neighbors live on to the next generation.Any live cell with fewer than two live neighbors dies, as if by underpopulation.In hopes of receiving more feedback, here is the theory behind my implementation:įor reference, here are the rules for Conway's game of life (taken from wikipedia): What can I improve on? Are there faster ways to implement certain areas of my code? Is there excess code that I can trim away? Is there a smarter way of implementing Conway's Game of Life? I have just finished implementing a version of Conway's Game of Life using Java.īeing only a college student, I am sure that my code is no where near perfect, and was wondering if you could look at my code.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |