#include<bits/stdc++.h> #ifndef MINESWEEPER_H #define MINESWEEPER_H
class Minesweeper { private: static const int SIZE = 23; static const int MINES = 99;
std::vector<std::vector<char>> board; // 实际雷区
std::vector<std::vector<char>> display; // 显示给玩家的棋盘
std::vector<std::vector<bool>> visited; // 记录访问状态
int remainingCells;
bool gameOver;
public: Minesweeper(); void initializeGame(); void placeMines(); void calculateNumbers(); void displayBoard(); void reveal(int x, int y); void revealAdjacent(int x, int y); int countAdjacentMines(int x, int y); bool isValid(int x, int y); void playGame(); bool checkWin(); };
#endif
Minesweeper::Minesweeper() : board(SIZE, std::vector<char>(SIZE, ‘0’)), display(SIZE, std::vector<char>(SIZE, ’#’)), visited(SIZE, std::vector<bool>(SIZE, false)), remainingCells(SIZE * SIZE - MINES), gameOver(false) { }
void Minesweeper::initializeGame() { // 重置所有数组 for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { board[i][j] = ‘0’; display[i][j] = ’#’; visited[i][j] = false; } } remainingCells = SIZE * SIZE - MINES; gameOver = false;
placeMines();
calculateNumbers();
}
void Minesweeper::placeMines() { std::random_device rd; std::mt19937 gen(rd()); std::uniform_int_distribution<> dis(0, SIZE - 1);
int minesPlaced = 0;
while (minesPlaced < MINES) {
int x = dis(gen);
int y = dis(gen);
if (board[x][y] != 'X') {
board[x][y] = 'X';
minesPlaced++;
}
}
}
void Minesweeper::calculateNumbers() { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (board[i][j] != ‘X’) { int count = countAdjacentMines(i, j); if (count > 0) { board[i][j] = ‘0’ + count; } } } } }
int Minesweeper::countAdjacentMines(int x, int y) { int count = 0; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { int nx = x + dx; int ny = y + dy; if (isValid(nx, ny) && board[nx][ny] == ‘X’) { count++; } } } return count; }
bool Minesweeper::isValid(int x, int y) { return x >= 0 && x < SIZE && y >= 0 && y < SIZE; }
void Minesweeper::displayBoard() { std::cout << ” ”; for (int i = 0; i < SIZE; i++) { std::cout << std::setw(2) << i << ” ”; } std::cout << “\n”;
for (int i = 0; i < SIZE; i++) {
std::cout << std::setw(2) << i << " ";
for (int j = 0; j < SIZE; j++) {
std::cout << " " << display[i][j] << " ";
}
std::cout << "\n";
}
}
void Minesweeper::reveal(int x, int y) { if (!isValid(x, y) || visited[x][y]) { return; }
visited[x][y] = true;
if (board[x][y] == 'X') {
gameOver = true;
display[x][y] = 'X';
return;
}
if (board[x][y] != '0') {
display[x][y] = board[x][y];
remainingCells--;
return;
}
// 如果是0,递归揭示相邻格子
display[x][y] = ' ';
remainingCells--;
revealAdjacent(x, y);
}
void Minesweeper::revealAdjacent(int x, int y) { for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (dx == 0 && dy == 0) continue;
int nx = x + dx;
int ny = y + dy;
if (isValid(nx, ny) && !visited[nx][ny]) {
if (board[nx][ny] == '0') {
reveal(nx, ny);
} else if (board[nx][ny] != 'X') {
visited[nx][ny] = true;
display[nx][ny] = board[nx][ny];
remainingCells--;
}
}
}
}
}
bool Minesweeper::checkWin() { return remainingCells == 0 && !gameOver; }
void Minesweeper::playGame() { initializeGame();
std::cout << "游戏开始!棋盘大小: " << SIZE << "x" << SIZE << ", 地雷数量: " << MINES << "\n";
while (!gameOver && !checkWin()) {
displayBoard();
int x, y;
std::cout << "请输入要翻开的格子坐标 (行 列): ";
std::cin >> x >> y;
if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) {
std::cout << "坐标无效,请重新输入!\n";
continue;
}
reveal(x, y);
if (gameOver) {
displayBoard();
std::cout << "很遗憾,你踩到地雷了!游戏结束。\n";
break;
}
if (checkWin()) {
displayBoard();
std::cout << "恭喜你,扫雷成功!\n";
break;
}
}
}
void displayMenu() { std::cout << ”================================\n”; std::cout << ” 扫雷游戏 v1.0 \n”; std::cout << ”================================\n”; std::cout << ” 1. 开始游戏 \n”; std::cout << ” 0. 退出游戏 \n”; std::cout << ”================================\n”; std::cout << “请选择: ”; }
int main() { Minesweeper game; int choice;
do {
displayMenu();
std::cin >> choice;
switch (choice) {
case 1:
game.playGame();
break;
case 0:
std::cout << "再见,欢迎下次再来玩!\n";
break;
default:
std::cout << "选择错误,请重新选择!\n";
break;
}
} while (choice != 0);
return 0;
}