import random
# 设置地图大小和雷的数量
map_size = 10mine_count = 10
# 创建地图mine_map = [[0] * map_size for _ in range(map_size)]revealed_map = [[False] * map_size for _ in range(map_size)]
# 布置雷mines = random.sample(range(map_size * map_size), mine_count)for mine in mines: row, col = divmod(mine, map_size) mine_map[row][col] = -1
# 计算周围雷的数量for row in range(map_size): for col in range(map_size): if mine_map[row][col] != -1: count = 0 for dr in range(-1, 2): for dc in range(-1, 2): r = row dr c = col dc if 0 <= r < map_size and 0 <= c < map_size and mine_map[r][c] == -1: count = 1 mine_map[row][col] = count
# 游戏主循环game_over = Falsewhile not game_over: # 打印雷区 for row in range(map_size): for col in range(map_size): if revealed_map[row][col]: if mine_map[row][col] == -1: print("*", end=" ") else: print(mine_map[row][col], end=" ") else: print("-", end=" ") print()
# 获取玩家输入 row = int(input("请选择要翻开的行(0-9): ")) col = int(input("请选择要翻开的列(0-9): "))
# 检查玩家是否踩雷 if mine_map[row][col] == -1: print("游戏结束!您踩到了雷!") game_over = True else: revealed_map[row][col] = True
# 检查是否已经胜利 win = True for row in range(map_size): for col in range(map_size): if mine_map[row][col] != -1 and not revealed_map[row][col]: win = False break if not win: break
if win: print("恭喜!您成功扫雷了!") game_over = True