康威生命游戏的问题

时间:2016-11-02 01:49:46

标签: python arrays 2d conways-game-of-life

我遇到了一个问题,我通过其他人的帖子找不到解决方案。我们遵循康威生命游戏的标准规则。

到目前为止,我已经能够创建二维数组,并使用随机字符填充数组' - '对于死亡和' O'为活着(字母O不是数字零)。我相信我正在经历并正确检查所有邻居。 *我使用try / catch的原因是因为这是我能弄清楚如何避免出现越界错误信息的唯一方法。

我的主要问题是,在检查/更改值时,我不确定是否正确浏览数组。此外,我不知道如何打印"更新"将live / die规则应用于整个数组后的2d数组。

截至目前,输出打印随机数组一次,然后再次打印完全相同的数组,然后询问我是否要继续(再次通过while循环)。如果我选择' y',我会收到错误消息

TypeError: list indices must be integers, not list

我已经迷失了,任何想法都会有所帮助,因为我大约需要3周时间才能进入python。

这是我的代码:

import random

numrows = 25
numcols = 25


def rnd():
    rn = random.randint(0,1)
    return rn


def initial():

    grid = []
    count = 0

    for x in range(numrows):
        grid.append([])
        for y in range(numcols):
            rand=random.randrange(1,3)
            if(rand == 1):
                grid[x].append('O')
            else:
                grid[x].append('-')

    for x in grid:    
        print(*x, sep=' ',end="\n") #prints initial grid with random values

    print("")# space for clarity sake
    print("")#      "       "

    answer = 'y'
    count = 0

    while(answer == 'y'):

        #This is where I am checking each neighbor. Adding 1 to count for each
        #live neighbor.

        for r in range(0,numrows):
            for c in range(0,numcols):


                try:
                    if(grid[r-1][c-1] == 'O'):
                        count += 1

                except:
                    pass

                try:
                    if(grid[r-1][c] == 'O'):
                        count += 1

                except:
                    pass

                try:
                    if(grid[r-1][c+1] == 'O'):
                        count += 1

                except:
                    pass

                try:
                    if(grid[r][c-1] == 'O'):
                        count += 1

                except:
                    pass

                try:

                    if(grid[r][c+1] == 'O'):
                        count += 1

                except:
                    pass

                try:
                    if(grid[r+1][c-1] == 'O'):
                        count += 1

                except:
                    pass

                try:
                    if(grid[r+1][c] == 'O'):
                        count += 1

                except:
                    pass

                try:
                    if(grid[r+1][c+1] == 'O'):
                        count += 1

                except:
                    pass


                #this is where I am applying the rules of living or dying
                #based on how many neighbors there are.

                if(grid[r][c] == '-'):
                        if(count == 3):
                            grid[r][c].append('O')

                if(grid[r][c] == 'O'):
                    if(count < 2):
                        grid[r][c].append('-')

                if(grid[r][c] == 'O'):
                    if(count == 2 or count == 3):
                        grid[r][c].append('O')

                if(grid[r][c] == 'O'):
                    if(count > 3):
                        grid[r][c].append('-')

                for r in grid:                 #This is a problem area as i dont
                    print(*r, sep=' ',end="\n")#know how to print the grid 

                answer = input("Continue? y or n( lower case only): ")

                if(answer != 'y'):
                    print(" Hope you had a great life! Goodbye!")

1 个答案:

答案 0 :(得分:1)

问题在于:崩溃发生在if行:

                if(grid[r][c] == '-'):  # <=== here on this line
                        if(count == 3):
                            grid[r][c].append('O')

在该行上,r不再是整数,而是一个列表。但为什么?答案发生在几行之后:

            for r in grid:                 #This is a problem area as i dont
                print(*r, sep=' ',end="\n")#know how to print the grid

在这个块中,r是一个int,但现在你把它变成了一个列表。我承认你在那里的评论,不知道如何打印网格,所以我可以建议快速:

print('\n'.join(' '.join(str(cell) for cell in row) for row in grid))

这样,您就不会覆盖变量r