1

enter image description herecreating a minesweeper game in pygame and i am getting a recursion error when running my code. how do i mitigate this? This is the code I have that checks to see if the clicked grid square is empty and if it is then it reveals that grid square as well as all the adjacent squares. the section that is getting this error is below:

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)
5
  • Are you getting: RuntimeError: maximum recursion depth exceeded ? Commented Nov 18, 2019 at 22:36
  • Welcome to StackOverflow. See minimal, reproducible example. We cannot effectively help you until you post your MRE code and accurately specify the problem. We should be able to paste your posted code into a text file and reproduce the problem you specified. This posting has no input, no tracing output, and no driver to show the error. Are you expecting us to eye-check your code without references? Commented Nov 18, 2019 at 23:07
  • @Prune sorry I didn't want to overload with my entire code I will post it below. Commented Nov 19, 2019 at 12:59
  • @Prune I cannot add my whole code as it is too long Commented Nov 19, 2019 at 13:28
  • Read my comment again: we specifically ask you not to post your whole code. Commented Nov 19, 2019 at 16:55

2 Answers 2

1

I guess you have this problem because there is no quick exit-clause for your recursive function. I suspect that because you don't check to see if the cell is already revealed ( revealed[row][col] == True ), then it never exits - it keeps recursing for ones already half-done in the processing queue (stack).

Maybe a quick check at the beginning of the function will fix it:

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 ) )
Sign up to request clarification or add additional context in comments.

1 Comment

I am getting RecursionError: maximum recursion depth exceeded in comparison Also adding a check at the beginning of the function takes care of the recursion error but doesn't reveal the cells like I want.
1

I figured it out. I had to add a check to each step of the recursion to check if the value has been revealed yet. see below:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.