出现RecursionError:比较中超过了最大递归深度

时间:2019-11-18 21:54:17

标签: python python-3.x error-handling pygame

enter image description here在pygame中创建扫雷游戏,运行代码时出现递归错误。我该如何缓解呢?这是我检查的代码,以查看所单击的网格正方形是否为空,如果是,则显示该网格正方形以及所有相邻的正方形。出现此错误的部分如下:

def reveal_empty(rn,c, grid, revealed,box):
    if grid[rn][c] != '0' and grid[rn][c] != '*':
        revealed[rn][c] = True
    if grid[rn][c] == '0':
        revealed[rn][c] = True
        # change row above
        if rn-1 > -1:
            r = grid[rn-1]

            if c-1 > -1:
                if not r[c-1] == '*':
                    revealed[rn-1][c-1] = True
                    reveal_empty(rn-1,c-1, grid, revealed,box)

            if not r[c] == '*':
                revealed[rn-1][c] = True
                reveal_empty(rn-1,c, grid, revealed,box)

            if c+1 < 10:
                if not r[c+1] == '*':
                    revealed[rn-1][c+1] = True
                    reveal_empty(rn-1,c+1, grid, revealed,box)

        #change same row                
        r = grid[rn]

        if c-1 > -1:
            if not r[c-1] == '*':
                revealed[rn][c-1] + True
                reveal_empty(rn,c-1, grid, revealed,box)
        if c+1 < 10:
            if not r[c+1] == '*':
                revealed[rn][c+1] = True
                reveal_empty(rn,c+1, grid, revealed,box)

        #change row below
        if rn+1 < 11:
            r = grid[rn + 1]

            if c-1 > -1:
                if not r[c-1] == '*':
                    revealed[rn+1][c-1] = True
                    reveal_empty(rn+1,c-1, grid, revealed,box)

            if not r[c] == '*':
                revealed[rn+1][c] = True
                reveal_empty(rn+1,c, grid, revealed,box)

            if c+1 < 11:
                if not r[c+1] == '*':
                    revealed[rn+1][c+1] = True
                    reveal_empty(rn+1,c+1, grid, revealed,box)

2 个答案:

答案 0 :(得分:1)

猜测,您遇到了这个问题,因为递归函数没有快速退出子句。我怀疑是因为您不检查是否已显示该单元格(revealed[row][col] == True),所以它永远不会退出-它会继续递归处理队列(堆栈)中已经完成的单元格。

也许在功能开始时快速检查一下就能解决:

def reveal_empty( row, col, grid, revealed, box ):
    if ( revealed[row][col] == False ):
        # do recursive check else here!
    else:
        print("Cell[%d][%d] is already revealed" % ( row, col ) )

答案 1 :(得分:1)

我知道了。我必须在递归的每个步骤中添加一个检查,以检查该值是否已显示。见下文:

# change row above
    if rn-1 > -1:
        r = grid[rn-1]

        if c-1 >= -1:
            if not r[c-1] == '*' and revealed[rn-1][c-1] == False:
                revealed[rn-1][c-1] = True
                if grid[rn-1][c-1] == '0':
                    reveal_empty(rn-1,c-1, grid, revealed,box)

        if not r[c] == '*' and revealed[rn-1][c] == False:
            revealed[rn-1][c] = True
            if grid[rn-1][c] == '0':
                reveal_empty(rn-1,c, grid, revealed,box)

        if c+1 < 10:
            if not r[c+1] == '*' and revealed[rn-1][c+1] == False:
                revealed[rn-1][c+1] = True
                if grid[rn-1][c+1] == '0':
                    reveal_empty(rn-1,c+1, grid, revealed,box)

    #change same row                
    r = grid[rn]

    if c-1 > -1:
        if not r[c-1] == '*' and revealed[rn][c-1] == False:
            revealed[rn][c-1] + True
            if grid[rn][c-1] == '0':
                reveal_empty(rn,c-1, grid, revealed,box)
    if c+1 < 10:
        if not r[c+1] == '*' and revealed[rn][c+1] == False:
            revealed[rn][c+1] = True
            if grid[rn][c+1] == '0':
                reveal_empty(rn,c+1, grid, revealed,box)

    #change row below
    if rn+1 < 11:
        r = grid[rn + 1]

        if c-1 > -1:
            if not r[c-1] == '*' and revealed[rn+1][c-1] == False:
                revealed[rn+1][c-1] = True
                if grid[rn+1][c-1] == '0':
                    reveal_empty(rn+1,c-1, grid, revealed,box)

        if not r[c] == '*' and revealed[rn+1][c] == False:
            revealed[rn+1][c] = True
            if grid[rn+1][c] == '0':
                reveal_empty(rn+1,c, grid, revealed,box)

        if c+1 < 11:
            if not r[c+1] == '*' and revealed[rn+1][c+1] == False:
                revealed[rn+1][c+1] = True
                if grid[rn+1][c+1] == '0':
                    reveal_empty(rn+1,c+1, grid, revealed,box)