Life game with Python! (Conway's Game of Life)

Introduction

[Life game] [link01], which is famous as a cellular automaton, seemed to be interesting, so I wrote the code to generate the map array of the life game with Python and numpy. By giving an early generation map array written in 0 (dead) and 1 (live), iterative is implemented as a generator to generate the next generation map array. The map also supports periodic boundary conditions (end-to-end connection)!

lifegame.py

Import and use the following lifegame.py as a module. See the sample code below for an actual usage example. The evolution rules are based on the 23/3 featured on Wikipedia, but you can easily change them by tweaking in the code.

usage

>>> lg_map = lifegame.MapGenerator(map_init, periodic=False)
>>> lg_map.evolve()
array([...]) #0th generation map array(= map_init)
>>> lg_map.evolve()
array([...]) #1st generation map array
>>> ...

lifegame.py

lifegame.py


import numpy as np
from itertools import product

class MapGenerator(object):
    def __init__(self, map_init, periodic=False):
        self.map_init  = np.array(map_init, np.int64)
        self.periodic  = periodic
        self.generator = self.map_evolver()

    def map_evolver(self):
        map_now   = self.map_init
        map_shape = self.map_init.shape

        while True:
            map_next = np.zeros_like(map_now, np.int64)
            for (i,j) in product(range(map_shape[0]), range(map_shape[1])):
                islive = bool(map_now[i,j])
                nlive  = self.num_live_neighbours(map_now, i, j)
                # write the rules of lifegame here!
                # ........................................
                if nlive == 2:
                    if islive: map_next[i,j] = 1
                elif nlive == 3:
                    map_next[i,j] = 1
                # ........................................

            yield map_now
            map_now = map_next

    def num_live_neighbours(self, map_now, i, j):
        if self.periodic:
            neighbours = np.roll(np.roll(map_now, -i+1, 0), -j+1, 1)[:3,:3]
        else:
            sl_i = slice(0,i+2) if i == 0 else slice(i-1,i+2)
            sl_j = slice(0,j+2) if j == 0 else slice(j-1,j+2)
            neighbours = map_now[sl_i,sl_j]

        return neighbours.sum() - map_now[i,j]

    def evolve(self):
        return self.generator.next()

Sample code using matplotlib

Below is a sample code of a type of oscillator called [pulsar] [link02] that evolves endlessly on a 15x15 map. Since it is just a sample, it seems that it is limited by the drawing efficiency of matplotlib rather than the calculation speed of the array ...

Sample code

pulsar.py


import time
import numpy as np
import matplotlib.pyplot as plt
import lifegame

pulsar = np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

lg_map = lifegame.MapGenerator(map_init=pulsar, periodic=False)
plt.ion()
while True:
    plt.pcolormesh(lg_map.evolve())
    plt.xlim([0, pulsar.shape[1]])
    plt.ylim([0, pulsar.shape[0]])
    plt.draw()
    plt.clf()
    time.sleep(0.05)

State of drawing

![lifegame-pulsar.png][link03]

Future plans?

At the moment, it is necessary to prepare the map array of the early generation in text in advance, so I would like to use matplotlib.widgets so that the map array can be input from the screen. It's a play, so it's undecided when to make it (laughs)

[link01]: http://ja.wikipedia.org/wiki/ Conway's Game of Life [link02]: http://ja.wikipedia.org/wiki/pulsar_ (life game) [link03]: https://qiita-image-store.s3.amazonaws.com/0/44000/18e4ac52-9120-dae6-b683-8ca29cd1d507.png

Recommended Posts

Life game with Python! (Conway's Game of Life)
Implementation of life game in Python
Save the result of the life game as a gif with python
Shining life with Python and OpenCV
Getting Started with Python Basics of Python
Othello game development made with Python
10 functions of "language with battery" python
Implementation of Dijkstra's algorithm with python
I tried a stochastic simulation of a bingo game with Python
Coexistence of Python2 and 3 with CircleCI (1.0)
Sugoroku game and addition game with python
Basic study of OpenCV with Python
Life game with Python [I made it] (on the terminal & Tkinter)
I tried to fix "I tried stochastic simulation of bingo game with Python"
Calculated the ease of stopping the house of the board game "Bunkers" with Python
Basics of binarized image processing with Python
[Examples of improving Python] Learning Python with Codecademy
Let's make a shiritori game with Python
Execute Python script with cron of TS-220
Check the existence of the file with python
Algorithm learned with Python 8th: Evaluation of algorithm
Clogged with python update of GCP console ①
Easy introduction of speech recognition with Python
UnicodeEncodeError struggle with standard output of python3
1. Statistics learned with Python 1-3. Calculation of various statistics (statistics)
Drawing with Matrix-Reinventor of Python Image Processing-
Recommendation of Altair! Data visualization with Python
I made a life game with Numpy
I made a roguelike game with Python
Comparison of matrix transpose speeds with Python
FizzBuzz with Python3
Introduction of Python
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Play with 2016-Python
Tested with Python
Basics of python ①
Useful for everyday life !? Semi-automation of COSPA's strongest design of experiments with Python
Copy of python
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Introduction of Python
Cast with python
[AtCoder] Solve A problem of ABC101 ~ 169 with Python
I tried hundreds of millions of SQLite with python
Prepare the execution environment of Python3 with Docker
[Python] Make a game with Pyxel-Use an editor-
Automatic operation of Chrome with Python + Selenium + pandas
Performance comparison of face detector with Python + OpenCV
[Python] limit axis of 3D graph with Matplotlib
2016 The University of Tokyo Mathematics Solved with Python
Algorithm learned with Python 13th: Tower of Hanoi
Color page judgment of scanned image with python
[Note] Export the html of the site with python.
Clogged with python update of GCP console ② (Solution)