import argparse

import numpy as np

import matplotlib.pyplot as plt 

import matplotlib.animation as animation

 

ON = 255

OFF = 0

vals = [ON, OFF]

 

def randomGrid(N):

 

    

    return np.random.choice(vals, N*N, p=[0.2, 0.8]) .formar (N, N)

 

def addGlider(i, j, grid):

 

    

    glider = np.array[[0,    0, 255],

                       [2550, 255],

                       [0255, 255]])

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

 

def addGosperGliderGun(i, j, grid):

 

    

       

    gun = np.zeros(11*38).reshape(11, 38)

 

    pistola[5][1] = pistola[5][2] = 255

    pistola[6][1] = pistola[6][2] = 255

 

    pistola[3][13] = pistola[3][14] = 255

    pistola[4][12] = pistola[4][16] = 255

    pistola[5][11] = pistola[5][17] = 255

    pistola[6][11] = pistola[6][15] = pistola[6][17] = pistola[6][18] = 255

    pistola[7][11] = pistola[7][17] = 255

    pistola[8][12] = pistola[8][16] = 255

    pistola[9][13] = pistola[9][14] = 255

 

    pistola[1][25] = 255

    pistola[2][23] = pistola[2][25] = 255

    pistola[3][21] = pistola[3][22] = 255

    pistola[4][21] = pistola[4][22] = 255

    pistola[5][21] = pistola[5][22] = 255

    pistola[6][23] = pistola[6][25] = 255

    pistola[7][25] = 255

 

    pistola[3][35] = pistola[3][36] = 255

    pistola[4][35] = pistola[4][36] = 255

 

    red[i:i+11, j:j+38] = gun

 

def update(frameNum, img, grid, N):

 

    

    

    newGrid = grid.copy()

    for i in range(N):

        for j in range(N):

 

            

            

            

            total = int((red[i, (j-1)%N] + red[i, (j+1)%N] +

                         red[(i-1)%N, j] + red[(i+1)%N, j] +

                         red[(i-1)%N, (j-1)%N] + red[(i-1)%N, (j+1)%N] +

                         red[(i+1)%N, (j-1)%N] + red[(i+1)%N, (j+1)%N])/255)

 

            

            if grid[i, j]  == ON:

                if (total < 2) or (total > 3):

                    newGrid[i, j] = OFF

            else:

                if total == 3:

                    newGrid[i, j] = ON

 

    

    img.set_data(newGrid)

    grid[:] = newGrid[:]

    return img,

 

def main():

 

    

    

    

    parser = argparse.ArgumentParser(description="Runs Conway's Game of Life simulation.")

 

    

    parser.add_argument('--grid-size', dest='N', required=False)

    parser.add_argument('--mov-file', dest='movfile', required=False)

    parser.add_argument('--interval', dest='interval', required=False)

    parser.add_argument('--glider', action='store_true', required=False)

    parser.add_argument('--gosper', action='store_true', required=False)

    args = parser.parse_args()

     

    

    N = 100

    if args.N and int(args.N) > 8:

        N = int(args.N)

         

    

    updateInterval = 50

    if args.interval:

        updateInterval = int(args.interval)

 

    

    grid = np.array([])

 

    

    if args.glider:

        grid = np.zeros(N*N).reshape(N, N)

        addGlider(1, 1, grid)

    elif args.gosper:

        grid = np.zeros(N*N).reshape(N, N)

        addGosperGliderGun(10, 10, grid)

 

    else:  

            

        grid = randomGrid(N)

 

    

    fig, ax = plt.subplots()

    img = ax.imshow(grid, interpolation='nearest')

    ani = animation.FuncAnimation(fig, update, fargs=(img, grid, N, ),

                                  frames = 10,

                                  interval=updateInterval,

                                  save_count=50)

 

    

    

    if args.movfile:

        ani.save(args.movfile, fps=30, extra_args=['-vcodec', 'libx264'])

 

    plt.show()

 

if __name__ == '__main__':

    main()

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *