Coder Social home page Coder Social logo

ucylama / game_of_life Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 11 KB

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

Home Page: https://cs50.harvard.edu/python/2022/

Python 100.00%
conways-game-of-life game-of-life game-of-life-python cs50p

game_of_life's Introduction

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 [email protected]: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


game_of_life's People

Contributors

ucylama avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.