Skip to main content

TDD-style implementation of Conway's Game of Life in Python

Project description

Build Status codecov Pyton Version Support

py-conway

TDD-style implementation of Conway's Game of Life in Python. Built with zero dependencies in order to be portable to Web, CLI and embedded applications (like MicroPython and CircuitPython).

View Project on PyPi

Installation

pip install py-conway

Usage

To create a game, you'll need to provide dimensions and a starting two-dimensonal list to function as the game board. For example:

from py_conway import Game

seed = [[0, 1, 0],
        [1, 1, 1],
        [0, 1, 0]]

new_game = Game(3, 3, seed)

If no seed is provided, the game can either generate a seed of zeroes, or a random starter seed. use the random=True positional argument to generate a random seed.

new_game = Game(12, 12, random=True)

Once the game board is populated, call the start() method. The game values and state will be initialized so you can interact with the board one step at a time. You can also use this method to re-initialize the game:

new_game.start()

You can also instruct the game to self-run with the ThreadedGame class and start_thread() method. The game will start on a background thread and update the full game board as well as a number of informational instance variables:

from py_conway import ThreadedGame

threaded_game = ThreadedGame(12, 12, random=True)

threaded_game.start_thread()

threaded_game.current_board # hold the complete game state after each step
threaded_game.live_cells # the count of live cells on the board
threaded_game.generations # the number of steps elapsed in the current game.

threaded_game.stop_thread()

It's also possible to call the run_generation() method and control the game state yourself from one iteration to the next. Make sure you call the start() method first:

new_game.run_generation()

According to the rules of Conway's Game of Life, the board is meant to be infinite. If you wish to emulate this behavior in your own games, you can wrap the board around on itself with the initialization flag enforce_boundary, which is true by default.

seed = [[1, 0, 0, 0],
        [1, 0, 0, 0],
        [1, 0, 0, 0],
        [0, 0, 0, 0]]

new_game = Game(4, 4, seed=seed, enforce_boundary=False)

If the above code is run for a single generation, the board will look like this

[[0, 0, 0, 0],
 [1, 1, 0, 1],
 [0, 0, 0, 0],
 [0, 0, 0, 0]]

Here's an example that runs the game and plots the game board after intialization and the first generation. You can run this from the example folder, either in a Jupyter notebook or standalone script:

import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from py-conway import Game

my_game = Game(12, 12, random=True, enforce_boundary=False)
my_game.start()

board, ax = plt.subplots()
plt.title("Conway's Game of Life")

ylim, xlim = my_game.board_size

ax.set_xlim(0, xlim)
ax.set_ylim(0, ylim)

image = plt.imshow(my_game.current_board, animated=True, cmap='binary')


def update(*args, **kwargs):
    my_game.run_generation()
    plt.xlabel(f"Generation: {my_game.generations} |\
                Population: {my_game.live_cells}")
    image.set_array(my_game.current_board)
    return image,


conway_animation = FuncAnimation(board, update, interval=50, blit=True)
plt.show()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

py-conway-0.6.0.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

py_conway-0.6.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file py-conway-0.6.0.tar.gz.

File metadata

  • Download URL: py-conway-0.6.0.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9

File hashes

Hashes for py-conway-0.6.0.tar.gz
Algorithm Hash digest
SHA256 30ce50bf2a9aafc537fbf7fa344581c7169ab6c298354a8397768dd6a559496a
MD5 e2f8082ac3c2a4a243c2c5188a8a655f
BLAKE2b-256 ad4f269bc40851d4c8f50c569c6d8ccd188c8e00ceb7eb14ee570bc899c0f1fb

See more details on using hashes here.

File details

Details for the file py_conway-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: py_conway-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.6.9

File hashes

Hashes for py_conway-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8088ab753eafa2378b1d6cac0deae2854dcad0ce12f5298bca47ff9ec55db9fc
MD5 b1f4b56ed3e68751929e254a25b48c05
BLAKE2b-256 1346bc6e85a6cb66e4dd78b60010ed0c4cfea9f330872b4527dda5ccdb76abe6

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page