User:Aadenboy/O(n) CGoL
Jump to navigation
Jump to search
O(n) implementation of Conway's Game of Life. Worded off of my Lua implementation of such.
- All currently alive cells are stored in a hashmap B, where the key is the position of the cell.
- Create two new empty hashmaps, one hashmap H for the counting of dead cells and another N for the new state of the board.
- Loop through all currently alive cells in hashmap B.
- For each cell C, loop through all eight of its neighbors. Keep a count A of how many alive neighbors it has.
- If the neighbor is alive, increment A by one.
- If the neighbor is dead (or has no associated value in the hashmap B), increment its associated key in hashmap H by one, or set it to one if it does not exist.
- If the value in hashmap H is set to three, set the associated key in hashmap N to be alive.
- If the value in hashmap H is set to four (after being three), remove the associated key in hashmap N.
- After looping through all neighbors, set cell C's associated key in hashmap N to be alive if count A is two or three.
- Set hashmap B to hashmap N. Discard hashmap H.