Skip to main content

A packed made for simulating and visualizing Conway`s game of life instances

Project description

LifePy

Installation

Install via pip:

python -m pip install lifepy

Tutorial on how to use the lifepy

Generate a simulation of size m*n. In this example, a 10x10 simulation will be generated, using a random array for the initial position of live and dead cells.

import lifepy
simulator = lifepy.Simulator(m_size=10, n_size=10, mode='ASCII')
simulator.generate_array()

The mode argument determines how to show the simulation. Default mode uses DEFAULT, where live cells are shown as white blocks and dead cells are black blocks, for this mode, ANSI escape sequences are used. The other available mode is ASCII, where live cells are show as # and dead cells as -.

You can see the current simulation by using the show_simulation method:

simulator.show_simulation()

Output:

--#-##-#--
##-#----#-
##-#######
-#-######-
-#--#-#--#
###---##-#
#-####-###
#----#--#-
---###-#-#
-##-##-#--

The simulation can procced in two ways.

Step by step

The step method only moves the simulation foward once. In other words, the simulation does one step.

Using the same simulation as before:

simulation.step(printout=True)

Output:

-####-----
#--------#
---------#
-#--------
----#----#
#--------#
#-####---#
-##-------
-###---#--
--#--#--#-

The printout method prints the simulation after the step, when set to True. By default, it's value is False.

Continuous simulation

The continuous_simulation method does the step method until a KeyboardInterrupt exception occurs, or until all life in the simulation has ended.

import lifepy
simulator2 = lifepy.Simulator(m_size=10, n_size=10, mode='DEFAULT')
simulator2.generate_array()
print("Starting simulation...")
simulator2.continous_simulation(step_deyal=1,printout=True)

Output:

TO-DO: ADD GIF OF SIMULATION RUNNING

The step_delay determines the time (in seconds) between each step.

When taking steps

It is important to take into account that the m*n size specified at the start is the size of the array, but also the size of the simulation. Therefore, any live cells that 'should' generate but are outside the m*n size are never generated.

Loading an array

Instead of generating a random array for the simulation, a predetermined one can be loaded. However the size of the array must equal the size of the simulation.

import lifepy
import numpy

simulator3 = lifepy.Simulator(m_size=10, n_size=10, mode='ASCII')
pre_array = numpy.array([[1,1,1,1,1,1,1,1,1,1],
                        [0,0,0,0,0,0,0,0,0,0],
                        [1,1,1,1,1,1,1,1,1,1],
                        [0,0,0,0,0,0,0,0,0,0],
                        [1,1,1,1,1,1,1,1,1,1],
                        [0,0,0,0,0,0,0,0,0,0],
                        [1,1,1,1,1,1,1,1,1,1],
                        [0,0,0,0,0,0,0,0,0,0],
                        [1,1,1,1,1,1,1,1,1,1],
                        [0,0,0,0,0,0,0,0,0,0]])

simulator3.load_array(pre_array)
simulator3.show_simulation()

Output:

##########
----------
##########
----------
##########
----------
##########
----------
##########
----------

Get values from the array

The get_array method returns a copy of the array being used for the simulation in its current step. For the dimensions of the array, the methods get_m_size (for rows) and get_n_size (for columns) will return said dimensions.

Notes

  • When an array are being used for the simulation, it will have a 'border' of zeros. This was done for preventing out of index errors when checking for live cells. Example:

    • Original array: [[1,1],[1,1]] -> Array simulated: [[0,0,0],[0,1,1,0],[0,1,1,0],[0,0,0]]

    However, the get_array method will NOT return the simulated array, it will return a properly 'cropped' array.

  • When using the method continuous_simulation with printout set to True, the module sys is used to move the cursor up and delete the line in the terminal m times (m being the amount of columns) by using sys.stdout.write("\x1b[1A\x1b[2K"). This is done because:

    1. It allows to clear the screen without using os.system('clear'), which makes the terminal 'blink', making the simulation appear less smooth, as well as having possible security issues.
    2. Works better than using other escape sequences that create empty lines, shifting everything up and therefore making a 'mess'.
    3. Most importantly, it does not deleted nor shifts too far up any previous printed content.

    A more efficent way to clear the screen is being looked into, possibly by using the curses module.

  • Related to the previous point, it is recommended to use m and n values that fit within the screen when printing the simulation. Otherwise, it most likely will appear in an odd way.

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

lifepy-1.0.0.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

lifepy-1.0.0-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file lifepy-1.0.0.tar.gz.

File metadata

  • Download URL: lifepy-1.0.0.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for lifepy-1.0.0.tar.gz
Algorithm Hash digest
SHA256 933fba1a5ecf9ae5e0fe6d69f343e88cc750dab72c96ffb9762f1323545b197c
MD5 1f2db63b48c0cfd4cc4250d19c5e2020
BLAKE2b-256 ebf64c592ede203ab342f4844f950aaefccd0a609dff4b69d4065bc5bb0439f9

See more details on using hashes here.

File details

Details for the file lifepy-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: lifepy-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10

File hashes

Hashes for lifepy-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5aea104955700796b9b976988a25f90c95ed43bad73ebc570cbbad5bbffb0e1a
MD5 25b91d9c6c4ddc0d7d13b42f1a386465
BLAKE2b-256 8996d40890c9e3b52efc9e7d3a2060cc495d9ac8304b2496db376cba36496066

See more details on using hashes here.

Supported by

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