MineSweeper 递归式洪水填充清理板
MineSweeper frecursive flood-fill clearing board
I am having trouble creating a recursive function for mine sweep that when a blank space is selected, all adjacent blank spaces are shown.我正在使用两个游戏板,它们采用 2D 列表的形式,一个是为用户隐藏的地雷位置生成的数字,另一个是显示的填充有 "covered" 个图块。
目前我有一个 flood-fill 功能,它只是清除面板,而不是像预期的那样显示所有空白 spaces 直到它遇到一个用整数或炸弹标记的字段。
我正在尝试传入未显示的板,选定的行,选定的列和显示的板。
尝试让显示的看板用来自未显示的看板的字段替换空字段。
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 # #
# # # # # # # # # #
````
假设 notDisplayed 包含您想要显示并放入显示板的字符,这应该有效
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)
问题是你的测试条件。您必须测试 notDisplayed
个字符才能知道是否应该递归,否则您将黑屏。
I am having trouble creating a recursive function for mine sweep that when a blank space is selected, all adjacent blank spaces are shown.我正在使用两个游戏板,它们采用 2D 列表的形式,一个是为用户隐藏的地雷位置生成的数字,另一个是显示的填充有 "covered" 个图块。
目前我有一个 flood-fill 功能,它只是清除面板,而不是像预期的那样显示所有空白 spaces 直到它遇到一个用整数或炸弹标记的字段。
我正在尝试传入未显示的板,选定的行,选定的列和显示的板。
尝试让显示的看板用来自未显示的看板的字段替换空字段。
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 # #
# # # # # # # # # #
````
假设 notDisplayed 包含您想要显示并放入显示板的字符,这应该有效
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)
问题是你的测试条件。您必须测试 notDisplayed
个字符才能知道是否应该递归,否则您将黑屏。