帮我用Python写一个扫雷
时间: 2024-03-28 16:14:40 浏览: 21
基于Python(Tkinter)实现的扫雷游戏
好的,我可以帮您写一个Python的扫雷游戏。
以下是一个简单的扫雷游戏实现:
```
import random
class MineSweeper:
def __init__(self, width=10, height=10, mines=10):
self.width = width
self.height = height
self.mines = mines
self.board = []
self.create_board()
def create_board(self):
# create a 2D array filled with 0s
self.board = [[0 for x in range(self.width)] for y in range(self.height)]
# place mines randomly on the board
mines_placed = 0
while mines_placed < self.mines:
x = random.randint(0, self.width - 1)
y = random.randint(0, self.height - 1)
if self.board[y][x] != '*':
self.board[y][x] = '*'
mines_placed += 1
def display_board(self):
# print the top row with column indices
print(' ', end=' ')
for i in range(self.width):
print(i, end=' ')
print()
# print each row with a row index
for i, row in enumerate(self.board):
print(i, end=' ')
for j in row:
print(j, end=' ')
print()
if __name__ == '__main__':
game = MineSweeper()
game.display_board()
```
这是一个可以运行的版本,你可以根据需要进行修改。希望这个代码对您有所帮助。
阅读全文