【问题标题】:Getting RecursionError: maximum recursion depth exceeded in comparison获取 RecursionError:比较超过最大递归深度
【发布时间】:2020-03-14 08:22:15
【问题描述】:

在 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)

【问题讨论】:

  • 您收到:RuntimeError: maximum recursion depth exceeded 吗?
  • 欢迎来到 StackOverflow。见minimal, reproducible example。在您发布 MRE 代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您指定的问题。此发布没有输入,没有跟踪输出,也没有驱动程序显示错误。您是否希望我们在没有参考的情况下检查您的代码?
  • @Prune 抱歉,我不想让我的整个代码超载,我将在下面发布。
  • @Prune 我无法添加整个代码,因为它太长了
  • 再次阅读我的评论:我们特别要求您不要发布您的整个代码。

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


【解决方案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)

【讨论】:

    【解决方案2】:

    你有这个问题,因为你的递归函数没有快速退出子句。我怀疑是因为您不检查单元格是否已经显示(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 ) )
    

    【讨论】:

    • 我收到 RecursionError: maximum recursion depth exceeded in comparison 在函数开头添加检查可以解决递归错误,但不会像我想要的那样显示单元格。
    猜你喜欢
    • 2019-03-23
    • 2022-01-22
    • 1970-01-01
    • 2020-04-01
    • 2019-04-18
    • 2021-02-13
    • 2021-03-23
    • 2017-11-05
    • 2013-11-30
    相关资源
    最近更新 更多