I am trying to make Minesweeper with Python arrays and have successfully generated the board and the bombs. However, I am having difficulty with Minesweeper's "zero recursion". If you select a 0 in Minesweeper, it will reveal all adjacent tiles and if any of the adjacent tiles are 0 it will reveal all adjacent tiles to that 0 and so on. Ultimately, there can be no 0s on the edges of the revealed tiles, as all adjacent tiles to revealed 0s will be revealed. My recursion algorithm is exceeding python's maximum recursion depth. Here's the code:
def zero():
for y in range(height):
for x in range(width-1):
if hidden[y][x] == 0 and (hidden[y+1][x] == '?' or hidden[y-1][x] == '?' or hidden[y+1][x+1] == '?' or hidden[y-1][x-1] == '?' or hidden[y+1][x-1] == '?' or hidden[y-1][x+1] == '?' or hidden[y][x+1] == '?' or hidden[y][x-1] == '?'):
if y+1 < height:
hidden[y+1][x] = board[y+1][x]
if y-1 >= 0:
hidden[y-1][x] = board[y-1][x]
if y+1 < height and x+1 < width:
hidden[y+1][x+1] = board[y+1][x+1]
if y-1 >= 0 and x+1 < width:
hidden[y-1][x+1] = board[y-1][x+1]
if y-1 >= 0 and x-1 >= 0:
hidden[y-1][x-1] = board[y-1][x-1]
if x+1 < width:
hidden[y][x+1] = board[y][x+1]
if x-1 >= 0:
hidden[y][x-1] = board[y][x-1]
if y+1 < height and x-1 >= 0:
hidden[y+1][x-1] = board[y+1][x-1]
zero()
The code checks to see if there are any revealed zeroes with any hidden adjacent tiles in the array. (Hidden tiles are represented with a '?'). If there are any zeroes that meet these parameters, all adjacent tiles to the zero will be revealed. It then restarts the function. Once there are no zeroes that meet the parameters in the entire array, the function loop will be broken and the code will continue flowing. This exceeds the recursion limit. Any suggestions?