Skip to content

Final projects for Harvard University's computer science and programming programme: module CS50P

Notifications You must be signed in to change notification settings

ipersids/game-of-life

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Conway's Game of Life

Description:



The Game of Life is a zero-player simulation game designed by the British mathematician John Horton Conway in 1970.

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


More about the game and its history: Wiki

What's inside
The content of the project folder:

Game_of_Life/
├── project.py
├── test_project.py
└── README.md

The project/project.py file contains all the necessary code to run the program.
The project/test_project.py file is used to test the main functions.

Used libraries:

import curses
import sys
import time
import collections

Let's take a closer look on the content of the project/project.py file.

def get_pattern(name=None):
    patterns = {
        "Glader": [[1, 0, 0], [0, 1, 1], [1, 1, 0]],
        "Blinker": [[1, 1, 1]],
        ...
    }
    ...

>>> Example Output
{
    "Glader": [(1, 1), (2, 2), (3, 2), (1, 3), (2, 3)]
    "Blinker": [(1, 1), (2, 1), (3, 1)],
    ...
}

This function contains a dictionary of available patterns for the game and generates a dictionary with the coordinates of alive cells. You can find new patterns on the internet (typically formatted similarly to this function, using list[list[int]] typing) and add new patterns to the game menu. If name is not None, the function returns the specific pattern chosen by name only.

def get_neighbours(pattern: dict[str, list[tuple[int, int]]]) -> dict:
    ...

>>> Example Output
{
    ...
    (2, 0): 1,
    (2, 1): 3,
    (2, 2): 4,
    (2, 3): 3,
    ...
}

The get_neighbours function counts the total number of alive cells for each neighbor and returns a dictionary with the coordinates of each neighbor and the number of alive cells around them.

def run_evolution(pattern: dict, neighbour_dict: dict, width: int, height: int) -> list[tuple[int, int]]:
    ...

>>> Example Output
[(2, 0), (2, 1), (2, 2)]

This function implements the rules of the game and returns a new pattern that fits within the current width and height of the game grid for dynamic resizing during the game.

class GameOfLife:
    def __init__(self, pattern_dict, timeout=0.15, GRID='‧', LIFE='♥'):
        self.pattern_dict = pattern_dict
        self.pattern_names = list(pattern_dict.keys())
        self.timeout = timeout
        self.GRID = GRID
        self.LIFE = LIFE

        self.names = self.pattern_names.append("CREATE PATTERN")

    ...

The GameOfLife class uses the curses library to draw the Game of Life in the terminal.

Game Features Description:

  • Pattern menu: Access this menu by pressing p, choose a pattern using the arrow keys (up or down), and run it by pressing Enter.
  • Create your own pattern from scratch.
  • Main game screen: Pause/start evolution by pressing Space.
  • Add/remove new alive cells by clicking on the game grid on the main game screen.
  • Exit from the pattern menu or the game by pressing q.
  • Dynamic screen resizing.

Installation

To run Conway's Game of Life, follow these steps:

  1. Clone the Repository to your local machine using git:
git clone git@github.com:ucylama/Game_of_Life.git
  1. Dependencies are not required.
  2. Run the Game
    This will launch the game interface in the terminale using the curses library.
python project.py
  1. Enjoy Playing!
    Explore the pattern menu, interact with the game grid, and enjoy the evolution!


_________

Written by Julia Persidskaia.
LinkedIn


About

Final projects for Harvard University's computer science and programming programme: module CS50P

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages