写一个扫雷游戏判断胜利的代码
时间: 2024-05-18 11:15:41 浏览: 15
以下是一个简单的扫雷游戏判断胜利的示例代码:
```python
def check_win(board, num_mines, num_flags):
"""
判断游戏是否胜利
:param board: 扫雷游戏的棋盘
:param num_mines: 地雷数量
:param num_flags: 玩家标记的地雷数量
:return: True表示游戏胜利,False表示游戏未结束或已失败
"""
for i in range(len(board)):
for j in range(len(board[0])):
if board[i][j] == -1 and num_flags != num_mines: # 未标记完全部地雷
return False
if board[i][j] != -1 and board[i][j] != -2: # 未翻开全部非地雷方块
return False
return True
```
其中,`board`表示扫雷游戏的棋盘,-1表示地雷,-2表示未翻开的方块,0~8表示周围地雷数量;`num_mines`表示地雷数量,`num_flags`表示玩家标记的地雷数量。如果所有地雷都被标记,且所有非地雷方块都被翻开,则游戏胜利。该函数返回True表示游戏胜利,False表示游戏未结束或已失败。
相关问题
帮我写一个扫雷游戏代码
好的,下面是一个简单的扫雷游戏代码,使用 Python 语言编写:
```python
import random
# 初始化地图
def init_map(row, col, num):
map = [[0 for i in range(col)] for j in range(row)]
count = 0
while count < num:
x = random.randint(0, row - 1)
y = random.randint(0, col - 1)
if map[x][y] == 0:
map[x][y] = 9
count += 1
for i in range(row):
for j in range(col):
if map[i][j] != 9:
count = 0
for x in range(max(0, i - 1), min(row, i + 2)):
for y in range(max(0, j - 1), min(col, j + 2)):
if map[x][y] == 9:
count += 1
map[i][j] = count
return map
# 显示地图
def show_map(map):
for i in range(len(map)):
for j in range(len(map[0])):
if map[i][j] == 9:
print('*', end=' ')
elif map[i][j] == 0:
print('.', end=' ')
else:
print(map[i][j], end=' ')
print()
# 挖开格子
def dig(map, x, y):
if map[x][y] == 9:
return False
elif map[x][y] == 0:
map[x][y] = -1
for i in range(max(0, x - 1), min(len(map), x + 2)):
for j in range(max(0, y - 1), min(len(map[0]), y + 2)):
if map[i][j] != -1:
dig(map, i, j)
else:
map[x][y] = -1
return True
# 判断是否胜利
def is_win(map):
for i in range(len(map)):
for j in range(len(map[0])):
if map[i][j] > 0:
return False
return True
# 主函数
def main():
row = 10
col = 10
num = 10
map = init_map(row, col, num)
while True:
show_map(map)
x = int(input('请输入行坐标(0-9): '))
y = int(input('请输入列坐标(0-9): '))
if dig(map, x, y):
if is_win(map):
print('你赢了!')
break
else:
print('你输了!')
show_map(map)
break
if __name__ == '__main__':
main()
```
这个扫雷游戏中,使用数字 0-8 表示周围雷的数量,数字 9 表示雷,符号 \* 表示未挖开的方格,符号 . 表示已挖开但周围没有雷的方格。玩家通过输入行列坐标来挖开方格,如果挖到雷就输了,如果所有没有雷的方格都被挖开就赢了。
写一个完整的扫雷游戏代码
好的,以下是一个完整的扫雷游戏代码,包括计算周围雷数、判断游戏胜利条件、点击雷时的游戏失败等功能:
```python
import pygame
import random
# 游戏窗口大小
WINDOW_WIDTH, WINDOW_HEIGHT = 400, 400
# 方块大小和行列数
BLOCK_SIZE = 20
ROWS, COLS = WINDOW_HEIGHT // BLOCK_SIZE, WINDOW_WIDTH // BLOCK_SIZE
# 雷的数量
NUM_MINES = 40
# 初始化Pygame
pygame.init()
# 创建游戏窗口
window = pygame.display.set_mode((WINDOW_WIDTH, WINDOW_HEIGHT))
# 设置窗口标题
pygame.display.set_caption("扫雷游戏")
# 定义颜色
BLACK = (0, 0, 0)
GRAY = (128, 128, 128)
WHITE = (255, 255, 255)
# 游戏结束标志
game_over = False
# 创建方块类
class Block:
def __init__(self, row, col):
self.row = row
self.col = col
self.x = col * BLOCK_SIZE
self.y = row * BLOCK_SIZE
self.is_mine = False
self.is_revealed = False
self.is_flagged = False
self.adjacent_mines = 0
def draw(self):
if self.is_revealed:
pygame.draw.rect(window, GRAY, (self.x, self.y, BLOCK_SIZE, BLOCK_SIZE))
if self.is_mine:
pygame.draw.circle(window, BLACK, (self.x + BLOCK_SIZE // 2, self.y + BLOCK_SIZE // 2), BLOCK_SIZE // 4)
elif self.adjacent_mines > 0:
font = pygame.font.Font(None, BLOCK_SIZE)
text = font.render(str(self.adjacent_mines), True, BLACK)
text_rect = text.get_rect(center=(self.x + BLOCK_SIZE // 2, self.y + BLOCK_SIZE // 2))
window.blit(text, text_rect)
else:
pygame.draw.rect(window, WHITE, (self.x, self.y, BLOCK_SIZE, BLOCK_SIZE))
pygame.draw.rect(window, GRAY, (self.x, self.y, BLOCK_SIZE, BLOCK_SIZE), 1)
if self.is_flagged:
pygame.draw.line(window, BLACK, (self.x + BLOCK_SIZE // 2, self.y + BLOCK_SIZE // 4),
(self.x + BLOCK_SIZE // 2, self.y + BLOCK_SIZE * 3 // 4), 2)
pygame.draw.line(window, BLACK, (self.x + BLOCK_SIZE // 4, self.y + BLOCK_SIZE // 2),
(self.x + BLOCK_SIZE * 3 // 4, self.y + BLOCK_SIZE // 2), 2)
def reveal(self):
self.is_revealed = True
# 创建方块列表
blocks = [[Block(row, col) for col in range(COLS)] for row in range(ROWS)]
# 随机生成雷的位置
mines = random.sample(range(ROWS * COLS), NUM_MINES)
for mine in mines:
row = mine // COLS
col = mine % COLS
blocks[row][col].is_mine = True
# 计算每个方块周围的雷数
for row in range(ROWS):
for col in range(COLS):
block = blocks[row][col]
if not block.is_mine:
count = 0
for i in range(max(0, row - 1), min(row + 2, ROWS)):
for j in range(max(0, col - 1), min(col + 2, COLS)):
if blocks[i][j].is_mine:
count += 1
block.adjacent_mines = count
# 游戏主循环
while not game_over:
# 处理事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
game_over = True
if event.type == pygame.MOUSEBUTTONDOWN:
x, y = pygame.mouse.get_pos()
row = y // BLOCK_SIZE
col = x // BLOCK_SIZE
if pygame.mouse.get_pressed()[0]: # 左键点击
if not blocks[row][col].is_flagged:
blocks[row][col].reveal()
if blocks[row][col].is_mine:
game_over = True
elif pygame.mouse.get_pressed()[2]: # 右键点击
blocks[row][col].is_flagged = not blocks[row][col].is_flagged
# 判断游戏胜利条件
def check_win():
for row in blocks:
for block in row:
if not block.is_revealed and not block.is_mine:
return False
return True
# 绘制方块
for row in blocks:
for block in row:
block.draw()
# 判断游戏胜利或失败
if game_over:
font = pygame.font.Font(None, 50)
if check_win():
text = font.render("You Win!", True, BLACK)
else:
text = font.render("Game Over!", True, BLACK)
text_rect = text.get_rect(center=(WINDOW_WIDTH // 2, WINDOW_HEIGHT // 2))
window.blit(text, text_rect)
# 更新屏幕
pygame.display.update()
# 退出游戏
pygame.quit()
```
这个代码实现了完整的扫雷游戏,包括生成随机雷的位置、计算周围雷数、左键点击方块进行揭示和右键点击进行标记等功能。当游戏结束时,会显示相应的胜利或失败信息。你可以尝试运行代码并进行游戏。希望对你有帮助!