0

The Following is a code snippet of a game called Ink Spill from the book 'Make Games with Python & Pygame' by Al Sweigart. (Full Code here: http://invpy.com/inkspill.py)

This recursive function is responsible for changing the old color ( in this case '2', which is mainBoard[0][0]) to the new color (in this case '3', which is mainBoard[1][0]) that the player clicks on .

My question is: In this example why even when the base condition is met and the return is executed, the execution still jumps to the next block inside the function.

I have tested this with print statements all over the function many times and with different parameters and also on Visualize Code website ...I still don't understand it!

Here, I have deleted many of my print statements and have left just two at the beginning. If you run this code you will see on your console that on the third line the condition is met (3 !=2), but the execution continues!

I really appreciate any help. Thank you very much.

And by the way unfortunately, the other question that was asked by someone else: Why does this recursive function continue even after its base case has been satisfied didn't answer my question although very similar!

boardWidth = 3
boardHeight = 3
mainBoard = [[2, 0, 0], [3, 0, 0], [4, 0, 0]]
# (blue, red, red),(yellow, red, red),(orange, red, red)


def floodFill(board, oldColor, newColor, x, y):
    print(board[x][y], oldColor)
    print(mainBoard)
    if board[x][y] != oldColor:
        return

    board[x][y] = newColor  # change the color of the current box

    # Make the recursive call for any neighboring boxes:
    if x > 0:
        floodFill(board, oldColor, newColor, x - 1, y)

    if x < boardWidth - 1:
        floodFill(board, oldColor, newColor, x + 1, y)

    if y > 0:
        floodFill(board, oldColor, newColor, x, y - 1)

    if y < boardHeight - 1:
        floodFill(board, oldColor, newColor, x, y + 1)


floodFill(mainBoard, 2, 3, 0, 0)
  • Because the outer function call, from which the recursive call was made, still needs to continue. That's exactly what the duplicate tells you. – jonrsharpe Aug 10 '19 at 17:26
  • this code raises `NameError: name 'boardWidth' is not defined` – Tomerikoo Aug 10 '19 at 17:28
  • Yes, there is an outer function which itself is in the main pygame loop and this function is called from there. but I am still not getting it! Would you pleases elaborate on this? What duplicate are you talking about? – Trevor Novak Aug 10 '19 at 17:31
  • Oh...please add boardWidth = 3 I don't know how it got deleted! I am sorry ... I fixed it! – Trevor Novak Aug 10 '19 at 17:32
  • The other question you linked... If you just want to change the first `2` encountered, add `return` to all recursive calls: `return floodfill(...)` in all conditions – Tomerikoo Aug 10 '19 at 17:33
  • Tomeriko, it is not my code to change. I just want to understand it :) – Trevor Novak Aug 10 '19 at 17:37
  • Please don't use link shorteners for links on Stack Exchange. There's no need to save characters in an answer or question, which is the only legitimate use for a link shortener. Using a shortened link makes it appear that the link is nefarious (e.g. goes to a spam site, infected download, or a site that contains viruses/trojans, etc.). – Makyen Aug 10 '19 at 18:09
  • Makyen, if the length of the link was reasonable I wouldn't use the shortener. That link would make my question aesthetically very ugly. The Permanent link is 1027 character long! I wanted to include it here but the site wouldn't allow me. – Trevor Novak Aug 10 '19 at 19:17
  • #johrsharpe, thank you. Now, I understand what you said about the outer function. Thanks a lot :) – Trevor Novak Aug 11 '19 at 08:41

0 Answers0