Игра жизни Конвея в Python 3 с помощью matplotlib - проблема с отображением формации - PullRequest
0 голосов
/ 23 ноября 2018

В настоящее время я пытаюсь запрограммировать реализацию игры жизни на python 3.Моей главной целью было показать сетку размером n, которая заполняется случайным образом в соответствии с правилами Конвея.Эта часть работает нормально.

Теперь я хотел реализовать второй режим, который позволяет начинать с одного из заданных образований - в моей первой попытке использовать планер.

Вот main ():

import matplotlib.pyplot as plt
import matplotlib.animation as animation
from gol_functions import *

def main():

    # get arguments from input function
    arguments = input_arguments()
    # set the arguments 
    gridsize = int(arguments.gridsize)
    interval = int(arguments.interval)
    formation = arguments.formationflag

    # if you want to start with a formation:
    if formation:
        grid = np.zeros(gridsize*gridsize).reshape(gridsize, gridsize)
        add_glider(1, 1, grid)

    # else display a randopm grid
    else:
        grid = randomgrid(gridsize)

    fig, ax = plt.subplots()

    # colormap: black -> alive, white -> dead
    img = ax.imshow(grid, cmap='binary', interpolation='nearest')

    # this will be used to save the animation in a later version
    ani = animation.FuncAnimation(fig, update, fargs=(img, grid, gridsize,),
                                  frames=10,
                                  interval=interval,
                                  save_count=50)

    # remove x and y - axis labels, numbers and ticks
    ax.axes.xaxis.set_ticklabels([])
    ax.axes.yaxis.set_ticklabels([])
    plt.xticks([])
    plt.yticks([])

    # plot the animated output
    plt.show()


    if __name__ == '__main__':
        main()

Вот функция добавления планера:

def add_glider(i, j, grid):
    """adds a glider with top-left cell at (i, j)"""
    glider = np.array([[0,    0, 255],
                       [255,  0, 255],
                       [0,  255, 255]])

    grid[i:i+3, j:j+3] = glider

Добавляет планер в сетку @ position 1, 1

А вот мойфункция обновления:

def update(frameNum, img, grid, gridsize):
    """Updates the grid every time it is refreshed"""
    newgrid = grid.copy()
    for i in range(gridsize):
        for j in range(gridsize):
            # this formula considers the edge/boundary conditions that appear
            # every cell has to have 8 neighbouring cells
            # to implement this in a grid of size n we simply fold the 4 edges to each parallel edge
            # we'll end up with a cylinder first, then with a geometric shape called torus (google it.)
            total = int((grid[i, (j - 1) % gridsize] + grid[i, (j + 1) % gridsize] +
                         grid[(i - 1) % gridsize, j] + grid[(i + 1) % gridsize, j] +
                         grid[(i - 1) % gridsize, (j - 1) % gridsize] +             
                         grid[(i - 1) % gridsize, (j + 1) % gridsize] +
                         grid[(i + 1) % gridsize, (j - 1) % gridsize] + grid[
                         (i + 1) % gridsize, (j + 1) % gridsize]) / 255)

        # apply conway's basic rules of the game of life for each cell
            if grid[i, j] == ON:
                if (total < 2) or (total > 3):
                    newgrid[i, j] = OFF
            else:
                if total == 3:
                    newgrid[i, j] = ON
                    # update data
                    img.set_data(newgrid)
                    grid[:] = newgrid[:]
                    return img,

Однако, когда я запускаю его с возможностью отображения планера, все, что я вижу, это следующее формирование ...

failglider_1.png

, который быстро превращается в это и становится статичным:

failedglider_2.png

... вместо фактического планера, как видно наматрица add_glider.Так что кажется, что программа каким-то образом добавляет ненужную живую ячейку прямо вверху.Я пытался найти, откуда это ... но я не могу его найти.

У кого-нибудь есть какие-нибудь подсказки?Я очень благодарен за любую помощь.Заранее спасибо!

1 Ответ

0 голосов
/ 23 ноября 2018

С некоторыми незначительными исправлениями кода из вашего исходного поста я могу произвести именно то, что вы хотели.Модифицированный код указан внизу.Первый кадр показан слева на изображении ниже.После нескольких кадров это выглядит как изображение справа.Таким образом, планер, кажется, работает очень хорошо:)

enter image description here

#!/usr/bin/python
# call with: python3 cgl.py 10 500 1 1

import os
import argparse
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation

ON = 255
OFF = 0


def update(frameNum, img, grid, gridsize):
    """Updates the grid every time it is refreshed"""
    newgrid = grid.copy()
    for i in range(gridsize):
        for j in range(gridsize):
            # this formula considers the edge/boundary conditions that appear
            # every cell has to have 8 neighbouring cells
            # to implement this in a grid of size n we simply fold the 4 edges to each parallel edge
            # we'll end up with a cylinder first, then with a geometric shape called torus (google it.)
            total = int((grid[i, (j - 1) % gridsize] + grid[i, (j + 1) % gridsize] +
                         grid[(i - 1) % gridsize, j] + grid[(i + 1) % gridsize, j] +
                         grid[(i - 1) % gridsize, (j - 1) % gridsize] +
                         grid[(i - 1) % gridsize, (j + 1) % gridsize] +
                         grid[(i + 1) % gridsize, (j - 1) % gridsize] + grid[
                         (i + 1) % gridsize, (j + 1) % gridsize]) / 255)

        # apply conway's basic rules of the game of life for each cell
            if grid[i, j] == ON:
                if (total < 2) or (total > 3):
                    newgrid[i, j] = OFF
            else:
                if total == 3:
                    newgrid[i, j] = ON
    # update data
    grid[:] = newgrid[:]
    img.set_data(newgrid)
    return img,


def add_glider(i, j, grid):
    """adds a glider with top-left cell at (i, j)"""
    glider = np.array([[0,    0, 255],
                       [255,  0, 255],
                       [0,  255, 255]])

    grid[i:i+3, j:j+3] = glider


def main():
    parser = argparse.ArgumentParser(description="Conway's game of life in Python 3")
    parser.add_argument('gridsize', type=int, help='Dimension of grid.')
    parser.add_argument('interval', type=int, help='Interval.')
    parser.add_argument('formationflag', type=bool, help='Predefined formation.')
    parser.add_argument('frame', type=int, help='How many frames to animate.')

    # get arguments from input function
    arguments = parser.parse_args()
    # set the arguments
    frame = int(arguments.frame)
    gridsize = int(arguments.gridsize)
    interval = int(arguments.interval)
    formation = arguments.formationflag

    # if you want to start with a formation:
    if formation:
        grid = np.zeros(gridsize*gridsize).reshape(gridsize, gridsize)
        add_glider(1, 1, grid)

    # else display a randopm grid
    else:
        grid = randomgrid(gridsize)

    fig, ax = plt.subplots()

    # colormap: black -> alive, white -> dead
    img = ax.imshow(grid, cmap='binary', interpolation='nearest')

    # # this will be used to save the animation in a later version
    ani = animation.FuncAnimation(fig, update, fargs=(img, grid, gridsize,),
                                  frames=frame,
                                  interval=interval,
                                  save_count=50)

    # remove x and y - axis labels, numbers and ticks
    ax.axes.xaxis.set_ticklabels([])
    ax.axes.yaxis.set_ticklabels([])
    plt.xticks([])
    plt.yticks([])

    # plot the animated output
    plt.show()

if __name__ == '__main__':
    main()
    print("DONE")
...