I am making Minesweeper in Python. I have all of the code done and working, but one feature I would like to add is that when you open a tile with zero mines nearby, it opens all of the squares around it. The problem stems from the fact that if any of the boxes I open are also zero, they also need to open all of the boxes around them. I believe I have a function to do this, but the max recursion depth is always reached. I tried raising the limit to probably higher than I should have, but I still got the error. I was wondering if their was a different way to do this without so much recursion. Thanks for any help, here is my current code:
def open_zero(x):
# For opening the boxes with no nearby mines
# First, opens the box the user clicked on
button_list[x].config(bg="#90949b")
# Then opens all nearby boxes through the box_open function
# I need to run these values through the function so that if they are
# zero, they will open all nearby tiles as well.
# This causes too much recursion.
box_open(x+1)
box_open(x-1)
box_open(x+width)
box_open(x+width+1)
box_open(x+width-1)
box_open(x-width)
box_open(x-width-1)
box_open(x-width+1)
def box_open(x):
if box_list[x] == "M":
# Checks if the block is a mine
button_list[x].config(image=photo_mine, relief = SUNKEN)
# Stops if it was a mine
button_frame.destroy()
all_code()
elif box_list[x] == 0:
# If there are no nearby mines, open all nearby tiles.
open_zero(x)
else:
# If not a mine, change button text to the amount of nearby mines.
button_list[x].config(text=box_list[x], bg="#90949b")
Hopefully you can understand my code from this snippet. It may not be possible the way I have coded it, but if anyone has any ideas, I would love to hear them. Thank you for any help!