康威的人生游戏,以及如何[关闭]

我是学习Python的初学者,我正在尝试为我的ADS类实现Conway的“人生游戏”项目。但是,我发现我的程序不能很好地工作,因为如果行或列的数量超过50,它将始终处于混乱状态,因此我想问一下如何改进它并获得正确的结果。这是我的代码:

'''

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

    fig, ax = plt.subplots()#Get figure and axes
    point = np.zeros((100,100))#initialize the array
    plt.title("Game of Life")
    im = ax.imshow(point)

    def init():
        _ = 0
        for i in point:
            while _ < len(i):
                i[_] = np.random.randint(0,2)
                _+=1
            _=0
        plt.imshow(point)
        return [im]

    def update(num):
        point_1 = point[:]
        i = 0
        while i < len(point_1):# count numbers
            if i ==0:
                d = 0
                while d <len(point_1[i]):
                    if d ==0:
                        inter = 0
                        if point_1[i][d+1] == 1 :
                            inter =+1
                        for  _y in range(d,d+2):
                            if point_1[i+1][_y] == 1:
                                inter +=1
                        if inter <2 or inter > 3:
                            point[i][d]=0
                        elif inter ==3:
                            point[i][d]=1
                    elif d <len(point_1[i])-1:
                        inter = 0
                        for _x in range(d-1,d+2):
                            if point_1[i][_x]==1:
                                if _x !=d:
                                    inter +=1
                        for _x in range(d-1,d+2):
                            if point_1[i+1][_x]==1:
                                inter +=1
                        if inter <2 or inter > 3:
                            point[i][d]=0
                        elif inter ==3:
                            point[i][d]=1     
                    else : 
                        inter = 0
                        if point_1[i][d-1] == 1 :
                            inter =+1
                        for  _y in range(d-1,d+1):
                            if point_1[i+1][_y] == 1:
                                inter +=1
                        if inter <2 or inter > 3:
                            point[i][d]=0
                        elif inter ==3:
                            point[i][d]=1            

                    d+=1
            if i <len(point_1)-1 and i >0:
                d= 0
                while d <len(point_1[i]):
                    if d ==0:
                        inter = 0
                        for _x in range(d,d+2):
                            for _y in range(i-1,i+1):
                                inter+= point_1[_y][_x]
                        inter = inter - point_1[i][d]
                        if inter <2 or inter > 3:
                            point[i][d]=0
                        if inter ==3:
                            point[i][d]=1                     


                    elif d <len(point_1[i])-1:
                        inter = 0
                        for _x in range(d-1,d+2):
                            for _y in range(i-1,i+2):
                                inter+= point_1[_y][_x]

                        inter = inter - point_1[i][d]
                        if inter <2 or inter > 3:
                            point[i][d]=0
                        elif inter ==3:
                            point[i][d]=1    

                    else : 
                        inter = 0
                        for _x in range(d-1,d+1):
                            for _y in range(i-1,i+2):
                                inter+= point_1[_y][_x]

                        inter = inter - point_1[i][d]
                        if inter <2 or inter > 3:
                            point[i][d]=0
                        elif inter == 3:
                            point[i][d]=1    
                    d +=1
            if i == len(point_1)-1:
                d = 0
                while d <len(point_1[i]):
                    if d ==0:   
                        inter = 0
                        if point_1[i][d+1] == 1 :
                            inter =+1
                        for  _y in range(d,d+2):
                            if point_1[i-1][_y] == 1:
                                inter +=1
                        if inter <2 or inter > 3:
                            point[i][d]=0
                        elif inter ==3:
                            point[i][d]=1     
                    elif d <len(point_1[i])-1:
                        inter = 0
                        for _x in [d-1,d+1]:
                            if point_1[i][_x]==1:
                                inter +=1
                        for _x in range(d-1,d+2):
                            if point_1[i-1][_x]==1:
                                inter +=1
                        if inter <2 or inter > 3:
                            point[i][d]=0
                        elif inter ==3:
                            point[i][d]=1    
                    else : 
                        inter = 0
                        if point_1[i][d-1] == 1 :
                            inter =+1
                        for  _y in range(d-1,d+1):
                            if point_1[i-1][_y] == 1:
                                inter +=1
                        if inter <2 or inter > 3:
                            point[i][d]=0
                        elif inter ==3:
                            point[i][d]=1         
                    d +=1
            i+=1 
        im = plt.imshow(point)
        return [im]
    ani = animation.FuncAnimation(fig,update,frames = 100000000000, interval=1,init_func=init, blit = True)
    plt.show()

''' 那是我的代码,还有另一个问题,如果我想减少复杂性,怎么办?

评论