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)