TDD-style implementation of Conway's Game of Life in Python
Project description
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.
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 conway.game 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 will start on a background thread and update the full game board as well as a number of informational instance variables:
new_game.start()
new_game.current_board # hold the complete game state after each step
new_game.live_cells # the count of live cells on the board
new_game.generations # the number of steps elapsed in the current game.
It's also possible to call the run_generation()
method and control the game state yourself from one iteration to the next:
new_game.run_generation()
Here's an example that runs the game and plots the game board after intialization and the first generation:
import matplotlib.pyplot as plt
from py-conway import Game
def create_zeros(x, y):
dim_one = [0 for item in range(x)]
return [dim_one[:] for item in range(y)]
seed = create_zeros(12, 12)
seed[0][1] = 1
seed[1][2] = 1
seed[2][3] = 1
game = Game(12, 12, seed)
plt.imshow(game.current_board, cmap='binary')
plt.show()
game.run_generation()
plt.imshow(game.current_board, cmap='binary')
plt.show()
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for py_conway-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae4a6080efe4284eda592e3083b2e660a3aa17ae65e707826dc5a50944519175 |
|
MD5 | ac4277cabd0db01158183206170a9eb4 |
|
BLAKE2b-256 | 65c4f62f914ab90ddc173b68ba9b69eb1feb3a4f1443aa2511246c0a1cbe1ffc |