一个普通技术宅的点点滴滴

0%

学习总结第二十七天——康威生命游戏

看了那么多天的算法,今天转变下心情,学习用python写了一个康威生命游戏(conway Game of Life)。


记得第一次接触到“康威生命游戏”是因为一部番《すべてがFになる THE PERFECT INSIDER》,作品用了很多计算机相关的事物,其中片尾曲的动态演化的康威生命游戏让我记忆犹新。其实所谓生命游戏,不过就是一些点在几条规则下进行演化罢了,但是能从简短的几条规则进行演化为如此复杂的系统着实令人感到有趣。也许这个简单的数学游戏,也隐藏着很深刻的哲学意义。其表明了复杂的结构可以由简单的规则推进。下图为我运行一个”高斯帕斯卡机枪”的截图
!img
代码如下:
#!/usr/bin/python3
#--coding:utf-8--

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


ON = 255
OFF = 0
vals = [ON, OFF]

def getGridStaus(i, j, glid, N):
    return int(glid[i%N][j%N])/ON

def addGlider(i, j, glid):
    glider = np.array([[OFF, OFF, ON],
                       [ON, OFF, ON],
                       [OFF, ON, ON]])
    glid[i:i+3, j:j+3] = glider

def addGosperGun(i, j, glid):
    gun = np.zeros(11*38).reshape(11, 38);
    gun[5][1] = gun[5][2] = 255
    gun[6][1] = gun[6][2] = 255

    gun[3][13] = gun[3][14] = 255
    gun[4][12] = gun[4][16] = 255
    gun[5][11] = gun[5][17] = 255
    gun[6][11] = gun[6][15] = gun[6][17] = gun[6][18] = 255
    gun[7][11] = gun[7][17] = 255
    gun[8][12] = gun[8][16] = 255
    gun[9][13] = gun[9][14] = 255

    gun[1][25] = 255
    gun[2][23] = gun[2][25] = 255
    gun[3][21] = gun[3][22] = 255
    gun[4][21] = gun[4][22] = 255
    gun[5][21] = gun[5][22] = 255
    gun[6][23] = gun[6][25] = 255
    gun[7][25] = 255

    gun[3][35] = gun[3][36] = 255
    gun[4][35] = gun[4][36] = 255

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

def readPattern(file, N):
    content=file.read()
    return np.array(content.split()).astype(int).reshape(N, N)

def randomGrid(N):
    return np.random.choice(vals, N*N, p=[0.2, 0.8]).reshape(N, N)

def update(frameNum, img, grid, N):
    newGrid = grid.copy()
    for i in range(N):
        for j in range(N):
            total = 0
            #计算周围有几个激活的细胞
            for iOffset in range(-1, 1+1):
                for jOffset in range(-1, 1+1):
                    if iOffset != 0 or jOffset != 0:
                        total = total+getGridStaus(i+iOffset, j+jOffset, grid, N)
                        #print ("x=%d,y=%d,io=%d,jo=%d,total=%d"%(i,j,iOffset,jOffset,total));
            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="Running for Conway's Game of Life simulation")
    #创建互斥参数组
    group = parser.add_mutually_exclusive_group()
    #解析参数
    parser.add_argument('--grid-size', dest='N', type=int, required=False)
    parser.add_argument('--mov-file', dest='movfile', required=False)
    group.add_argument('--pattern-file', dest='patternfile', required=False)
    parser.add_argument('--interval', dest='interval', required=False)
    group.add_argument('--glider', action='store_true', required=False)
    group.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:#当设置了glider标志位时使用滑翔机初始化
        grid = np.zeros(N*N).reshape(N, N)
        addGlider(1, 1, grid)
    elif args.gosper and N > 40:#使用高斯帕滑翔机枪初始化
        grid= np.zeros(N*N).reshape(N,N)
        addGosperGun(1,1,grid)
    elif args.patternfile:#使用文件初始化
        file = open(args.patternfile)
        N = int(file.readline())
        grid=readPattern(file,N)
        #print (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=100)
    if args.movfile:
        ani.save(args.movfile, fps=30, extra_args=['-vcodec', 'libx264'])
    plt.show()
if __name__ == '__main__':
    main()