Game of Life

Level: Advanced

Concepts: State Algorithms


Implement Conway's Game of Life, a cellular automaton that simulates the evolution of a population of cells based on simple rules. The game is played on a grid of cells, where each cell can be either alive or dead.

Rules

The rules of the game are:

  1. Any live cell with fewer than two live neighbors dies (underpopulation)
  2. Any live cell with two or three live neighbors lives on to the next generation
  3. Any live cell with more than three live neighbors dies (overpopulation)
  4. Any dead cell with exactly three live neighbors becomes a live cell (reproduction)

Requirements

  1. Create a grid of cells that can be initialized with a specific pattern
  2. Implement the rules to calculate the next generation
  3. Handle edge cases (cells at the boundaries of the grid)
  4. Support different grid sizes
  5. Provide a way to visualize the current state of the grid

Hint

Start by implementing the core rules for a single cell, then expand to handle the entire grid. Consider using a two-dimensional array or a more sophisticated data structure to represent the grid. Think about how to handle the edges of the grid - you could either wrap around (toroidal) or consider cells outside the grid as dead.

Bonus

  • Implement different initial patterns (glider, blinker, etc.)
  • Add the ability to save and load patterns
  • Implement a graphical interface to visualize the evolution
  • Add the ability to pause and resume the simulation
  • Implement different rule variations (e.g., High Life, Day & Night)