康威生命游戏中的问题

问题描述 投票:1回答:1

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

到目前为止,我已经能够创建2d数组,并使用随机字符' - '填充数组,对于dead和'O'表示存活(字母O不是数字0)。我相信我正在经历并正确检查所有邻居。 *我使用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!")
python arrays 2d conways-game-of-life
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.