My Python Implementation of Conway's Game of Life
Conway's Game of Life:
The universe of the Game of Life is an infinite, two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead, (or populated and unpopulated, respectively). Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
Rules:
- Any live cell with fewer than two live neighbours dies, as if by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
Logic for game is written in LifeBoard.py and LifeEngine.py
LifeBoard.py: houses logic for getting neighbors and position in board
LifeEngine.py: uses LifeBoard.py to check the four rules of Conway's game and run the simulation
A webpage is also provided for visualization which uses flask as the framework
- sends request to the server to obtain data from the python scripts over how the simulation should progress
Visualization can optionally be done through terminal alone by running python LifeEngine.py
- ensure the termcolor package is installed, if not run
pip install termcolor
- you can change variables in the file to test different configurations, generation numbers, etc...
NOTE: NO game logic is written in the javascript for the webpage; all data is requested from the python script
Flask activation instructions were extracted from:
https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
To start local flask server to run the webpage:
- Create a virtual environment by running
python -m venv ./venv
- Activate the virtual environment by running
source venv/bin/activate
- To download the required python packages run
pip install -r requirements.txt
- If your FLASK_APP environmental variable is not set you must set it by running
export FLASK_APP=lifepage.py
- Optionally, you can run
pip install python-dotenv
and create a .flaskenv file in the top-level directory with the file having one line: FLASK_APP=microblog.py - This removes the need of having to use export every new terminal session
- Optionally, you can run
- Then simply run
flask run
to start the server- Run
export FLASK_ENV=development
if you want to run the server and have it reload for code changes
- Run
- Enjoy!