欧美一区二区三区,国内熟女精品熟女A片视频小说,日本av网,小鲜肉男男GAY做受XXX网站

python 掃雷源代碼

洪振霞2年前9瀏覽0評論

Python 掃雷游戲源代碼

import random
class Minesweeper():
def __init__(self, size, num_mines):
self.board = [[0] * size for _ in range(size)]
self.size = size
self.num_mines = num_mines
self.mines = set()
self.generate_board()
def generate_board(self):
while len(self.mines)< self.num_mines:
x, y = random.randint(0, self.size-1), random.randint(0, self.size-1)
if (x, y) not in self.mines:
self.mines.add((x, y))
self.board[x][y] = -1
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if 0<= i< self.size and 0<= j< self.size and self.board[i][j] != -1:
self.board[i][j] += 1
def print_board(self):
for row in self.board:
for cell in row:
if cell == -1:
print("*", end=" ")
else:
print(cell, end=" ")
print()
def reveal(self, x, y, visited):
if (x, y) in self.mines:
print("Game Over!")
return False
elif self.board[x][y] >0:
visited.add((x, y))
return True
else:
visited.add((x, y))
for i in range(x-1, x+2):
for j in range(y-1, y+2):
if (i, j) not in visited and 0<= i< self.size and 0<= j< self.size:
self.reveal(i, j, visited)
return True
def play(self):
self.print_board()
while True:
x = int(input("Enter x coordinate: "))
y = int(input("Enter y coordinate: "))
visited = set()
if not self.reveal(x, y, visited):
self.print_board()
return
game = Minesweeper(10, 20)
game.play()

以上是一個用 Python 編寫的掃雷游戲源代碼,它是一個 Minesweeper 類,包含了生成游戲盤面、打印游戲盤面、判斷游戲勝負等方法。玩家可以在 play 方法里輸入坐標來進行游戲。