I am having trouble creating a recursive function for mine sweep that when a blank space is selected, all adjacent blank spaces are shown. I am using two game boards, that are in the form of 2D list, --one that has numbers generated for mine locations that is hidden from the user and another that is filled with "covered" tiles that is displayed.
Currently I have a flood-fill function that just clears the board, opposed to as intended where it reveals all blank spaces until it comes across a field that is marked with an integer or bomb.
I am trying to pass in the board that is not displayed, the selected row, selected column, and the displayed board.
Trying to have the displayed board replace empty fields with the fields with the fields that are from the board that is not displayed.
def flood(displayedBoard, row, col, notDisplayed):
mines = mineLocations(notDisplayed)
if displayedBoard[row][col] != " ":
displayedBoard[row][col] = " "
if row != 1:
flood(displayedBoard,row-1,col,notDisplayed)
if row != maxRow-1:
flood(displayedBoard,row+1,col,notDisplayed)
if col != 1:
flood(displayedBoard,row,col-1,notDisplayed)
if col != maxCol:
flood(displayedBoard,row,col+1,notDisplayed)
``````
the expected output if the space 4,2 is selected
```
1 2 3 4 5 6 7 8
# # # # # # # # # #
1 # . . . . . . . . #
2 # 1 1 1 1 . . . . #
3 # 2 . . . . #
4 # 1 . . . . . #
5 # 1 . . . . . #
6 # 1 . . . . . #
7 # 1 . . . . . . #
8 # 1 1 . . . . . . #
###################
````
what is being output
```
1 2 3 4 5 6 7 8
# # # # # # # # # #
1 # #
2 # #
3 # #
4 # #
5 # #
6 # #
7 # #
8 # #
# # # # # # # # # #
````
Assuming notDisplayed contains character you want to reveal and put in displayed Board, this should work
if row < 1 or row >= maxRow:
return
if col < 1 or col >= maxCol:
return
if displayedBoard[row][col] == " ":
return
displayedBoard[row][col] = notDisplayed[row][col]
if notDisplayed[row][col] == " ":
flood(displayedBoard,row-1,col,notDisplayed)
flood(displayedBoard,row+1,col,notDisplayed)
flood(displayedBoard,row,col-1,notDisplayed)
flood(displayedBoard,row,col+1,notDisplayed)
The problem was your test condition. You must test notDisplayed
chars to know if you should recurse or not, otherwise you will blank all screen.