Python扫雷游戏代码
时间: 2023-02-05 17:09:31 浏览: 73
下面是一个简单的 Python 扫雷游戏代码示例:
```
board = [
[0, 0, 1, 0],
[0, 1, 1, 0],
[0, 0, 0, 0],
[1, 1, 0, 0],
]
def count_adjacent_mines(board, row, col):
count = 0
for i in range(row-1, row+2):
for j in range(col-1, col+2):
if i >= 0 and i < len(board) and j >= 0 and j < len(board[0]) and board[i][j] == 1:
count += 1
return count
def print_board(board):
for row in board:
print(' '.join(str(cell) for cell in row))
print_board(board)
print(count_adjacent_mines(board, 0, 0)) # Output: 1
print(count_adjacent_mines(board, 0, 1)) # Output: 2
print(count_adjacent_mines(board, 1, 1)) # Output: 3
```
这个代码中,`board` 数组表示游戏的棋盘。数字 1 表示地雷,数字 0 表示空地。
`count_adjacent_mines` 函数用来计算某个位置周围(上下左右和四个角)的地雷数量。
`print_board` 函数用来输出游戏的棋盘。
这只是一个简单的例子,如果要实现一个完整的扫雷游戏,还需要添加更多的功能,比如玩家点击地图上的某个位置,如果这个位置上是地雷就结束游戏,如果不是就显示周围的地雷数量。