python - Conway 的 Python 人生游戏

标签 python conways-game-of-life

看上了康威的《生命游戏》,开始尝试用python写。目前我还没有为程序的边界编写任何代码,所以我只是在寻求帮助我现在拥有的东西。我似乎在初始化“信号灯”编队时遇到了麻烦。它没有像它应该的那样振荡,而是似乎把自己变成了一个立方体。

#File: gameoflife.py
#Description: This is a basic "life" simulation that follows three distinct rules:
#1.Any live cell with fewer than two live neighbours dies
#2.Any live cell with two or three live neighbours lives
#3.Any live cell with more than three live neighbours dies
#4.Any dead cell with exactly three live neighbours becomes a live cell
#A neighbor is deemed as any cell directly horizantal/vertical/diagonal
#meaning there are 9 neighbors to a cell at any time



from graphics import *
import random
from time import sleep


def initial(D,M,win):    
    #Creates the white board background      
    for i in range (11):
        m = [] # rectangle list
        for j in range (11):
            rec = Rectangle(Point(6 + 4 * i, 6 + 4 * j), Point(10 + 4 * i, 10 + 4 * j))

            D[i][j] = 0
            rec.setFill("white")
            rec.draw(win)    
            m.append(rec)
        M.append(m)


def check(x,y,D):
    #Checks all 9 adjacent neihbors to see if "alive" and checks this number
    #means the cell should stay alive(1), die(0), or if already dead come
    #back alive(2)
    counter = 0

    if D[x+1][y] == 1:
        counter += 1
    if D[x-1][y] == 1:
        counter += 1
    if D[x][y+1] == 1:
        counter += 1
    if D[x][y-1] == 1:
        counter +=1
    if D[x+1][y+1] == 1:
        counter+=1
    if D[x+1][y-1] == 1:
        counter+= 1
    if D[x-1][y-1] == 1:
        counter += 1
    if D[x-1][y+1] == 1:
        counter +=1
    if counter<2 or counter>3:
        return 0
    if counter == 2:
        return 1
    if counter == 3:
        return 2



def main():
    win = GraphWin("Game of Life", 700, 600)
    win. setCoords(0, 0, 70, 60)

    #Initialize two dimesion arrays.
    #D records color of grids, M records rectangles
    D = []
    M = []
    C = []

    #initialize the grids to create all "white"
    for i in range(11):
        d = []
        c = []
        for j in range(11):
            d.append(0)
            c.append(0)
        D.append(d)
        C.append(c)

    initial(D,M,win)
    #Initialzes three "alive" units
    D[5][5],D[4][5] ,D[6][5]= 1,1,1
    C[5][5],C[4][5] ,C[6][5]= 1,1,1
    M[5][5].setFill("Black")
    M[4][5].setFill("Black")
    M[6][5].setFill("Black")

    #Contiually checking
    while True:
        #Purposfully not checking the "Borders" of the array
        for i in range (len(D)-1):
            for j in range(len(D[i])-1):
                #If the cell is alive
                if D[i][j] == 1:
                    #If the cell should die
                    if check(i,j,D) == 0:
                        sleep(1)
                        #Sets a temporary list to white
                        C[i][j] = 0
                        #Fills the cell white
                        M[i][j].setFill("White")
                #if the cell is dead
                if D[i][j] == 0:
                    #If the cell should be revived
                    if check(i,j,D) == 2:
                        sleep(1)
                        #Sets a temporary list to black
                        C[i][j] = 1
                        #Fills the cell black
                        M[i][j].setFill("Black")
        #Sets the main list = to the temporary list                
        D = C


main()

最佳答案

您需要交换 D 和 C,而不仅仅是将 C 分配给 D。就目前而言,D 和 C 将在第一次迭代后引用同一个列表。

关于python - Conway 的 Python 人生游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8484802/

相关文章:

python - InvalidArgumentError : Input to reshape is a tensor with 178802 values, 但请求的形状有 89401

python - 在输出文件中添加空间而不必先阅读整个文件

python - 当 XPath 验证器返回正确结果时,为什么 XmlNode.SelectNodes 返回空列表?

python - 如何使用 python pandas 计算总天数、小时数和分钟数?

python - python中的热图表示给定矩形区域中的(x,y)坐标

java - Java 生命游戏程序

javascript - 我的人生游戏逻辑有问题吗?

C:图像(数组)的索引公式,在嵌套for循环中更新索引

javascript - 如何遍历由二维数组表示的板上单元格的所有邻居?

javascript - 你怎么称呼这个 JavaScript 语法,所以我可以研究它?