[英]Problem in implementing minesweeper reveal in python without functions
我正在為一個項目制作游戲掃雷,但在游戲的顯示部分遇到了問題(如果輸入任何周圍有 0 個地雷的單元格的坐標,那么游戲應該繼續顯示相鄰的瓷磚除非達到非零單元格值)。 我也為此在互聯網上搜索過,但找不到任何合適的解決方案(我不允許使用函數,因為它不是今年課程的一部分)。 有人可以提供有關我如何在 python 中實現此功能的任何見解。
PS作為參考,您可以查看此代碼
def neighbours(r, col):
global mine_values
global numbers
global vis
# If the cell already not visited
if [r,col] not in vis:
# Mark the cell visited
vis.append([r,col])
# If the cell is zero-valued
if numbers[r][col] == 0:
# Display it to the user
mine_values[r][col] = numbers[r][col]
# Recursive calls for the neighbouring cells
if r > 0:
neighbours(r-1, col)
if r < n-1:
neighbours(r+1, col)
if col > 0:
neighbours(r, col-1)
if col < n-1:
neighbours(r, col+1)
if r > 0 and col > 0:
neighbours(r-1, col-1)
if r > 0 and col < n-1:
neighbours(r-1, col+1)
if r < n-1 and col > 0:
neighbours(r+1, col-1)
if r < n-1 and col < n-1:
neighbours(r+1, col+1)
# If the cell is not zero-valued
if numbers[r][col] != 0:
mine_values[r][col] = numbers[r][col]
鏈接 - https://www.askpython.com/python/examples/create-minesweeper-using-python
這應該有效:
#before main loop
shifts = [
(-1,-1),
(-1, 1),
(-1, 0),
( 1,-1),
( 1, 1),
( 1, 0),
( 0,-1),
( 0, 1),
]
#main loop
to_reveal = []
if (x,y) not in vis:
to_reveal.append((x,y))
while to_reveal != []:
cell = to_reveal.pop()
vis.append(cell)
if board[cell[1]][cell[0]] == 0:
for shift in shifts:
if (cell[0] + shift[0],cell[1] + shift[1]) not in vis and 0 <= (cell[0] + shift[0]) <= 9 and 0 <= (cell[1] + shift[1]) <= 9:
to_reveal.append((cell[0] + shift[0],cell[1] + shift[1]))
這段代碼的作用是在玩家選擇要發現的瓷磚后,將該瓷磚添加到列表中。 然后如果是 0,則將所有鄰居添加到列表中,等等。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.