aab
2026-06-27 13:30:47
发布于:广东
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <iomanip>
using namespace std;
// 常量定义
const int ROWS = 9;
const int COLS = 9;
const int MINES = 10;
// 方向数组(八邻域)
const int DIRS[8][2] = {
{-1, -1}, {-1, 0}, {-1, 1},
{0, -1}, {0, 1},
{1, -1}, {1, 0}, {1, 1}
};
// 游戏状态
enum GameState { PLAYING, WIN, LOSE };
vector<vector<int> > mineMap; // 注意:> > 中间加了空格,防C++98报错
vector<vector<char> > display; // 同上
GameState gameState = PLAYING;
int remainingSafe = ROWS * COLS - MINES;
// 函数声明
void initGame();
void placeMines();
void calcNumbers();
void printBoard();
bool isValidPos(int r, int c);
void reveal(int r, int c);
void toggleFlag(int r, int c);
bool checkWin();
void handleInput();
int main() {
srand((unsigned)time(NULL));
initGame();
printBoard();
while (gameState == PLAYING) {
handleInput();
printBoard();
if (gameState == WIN) {
cout << "恭喜你赢了!" << endl;
} else if (gameState == LOSE) {
cout << "踩到雷了!游戏结束。" << endl;
}
}
return 0;
}
void initGame() {
mineMap.assign(ROWS, vector<int>(COLS, 0));
display.assign(ROWS, vector<char>(COLS, '#'));
remainingSafe = ROWS * COLS - MINES;
gameState = PLAYING;
placeMines();
calcNumbers();
}
void placeMines() {
int placed = 0;
while (placed < MINES) {
int r = rand() % ROWS;
int c = rand() % COLS;
if (mineMap[r][c] != -1) {
mineMap[r][c] = -1;
placed++;
}
}
}
void calcNumbers() {
for (int r = 0; r < ROWS; ++r) {
for (int c = 0; c < COLS; ++c) {
if (mineMap[r][c] == -1) continue;
int count = 0;
// 遍历八个方向(改用下标循环)
for (int i = 0; i < 8; i) {
int nr = r + DIRS[i][0];
int nc = c + DIRS[i][1];
if (isValidPos(nr, nc) && mineMap[nr][nc] == -1)
count;
}
mineMap[r][c] = count;
}
}
}
bool isValidPos(int r, int c) {
return r >= 0 && r < ROWS && c >= 0 && c < COLS;
}
void printBoard() {
system("cls"); // Windows清屏
cout << " ";
for (int c = 0; c < COLS; ++c)
cout << setw(2) << c << ' ';
cout << endl;
cout << " ";
for (int c = 0; c < COLS; ++c)
cout << "---";
cout << endl;
for (int r = 0; r < ROWS; ++r) {
cout << setw(2) << r << '|';
for (int c = 0; c < COLS; ++c) {
char ch = display[r][c];
if (ch == '#') cout << " . ";
else if (ch == 'F') cout << " F "; // 旗帜用F表示
else if (ch == ' ') cout << " ";
else cout << ' ' << ch << ' ';
}
cout << "|" << endl;
}
cout << " 剩余安全格数: " << remainingSafe << " 雷数: " << MINES << endl;
}
void reveal(int r, int c) {
if (!isValidPos(r, c)) return;
if (display[r][c] == 'F' || display[r][c] != '#') return;
if (mineMap[r][c] == -1) {
gameState = LOSE;
// 显示所有雷的位置(用*表示)
for (int i = 0; i < ROWS; ++i)
for (int j = 0; j < COLS; ++j)
if (mineMap[i][j] == -1)
display[i][j] = '*';
return;
}
int num = mineMap[r][c];
if (num == 0) {
display[r][c] = ' ';
} else {
display[r][c] = static_cast<char>('0' + num);
}
remainingSafe--;
// 如果是空格,递归展开(改用下标循环)
if (num == 0) {
for (int i = 0; i < 8; ++i) {
int nr = r + DIRS[i][0];
int nc = c + DIRS[i][1];
if (isValidPos(nr, nc) && display[nr][nc] == '#') {
reveal(nr, nc);
}
}
}
if (remainingSafe == 0)
gameState = WIN;
}
void toggleFlag(int r, int c) {
if (!isValidPos(r, c)) return;
if (display[r][c] == '#') {
display[r][c] = 'F';
} else if (display[r][c] == 'F') {
display[r][c] = '#';
}
}
void handleInput() {
int r, c;
char action;
cout << "请输入操作 (o 翻开, f 标记/取消标记) 和坐标,例如: o 3 5 或 f 2 4: ";
cin >> action >> r >> c;
if (action != 'o' && action != 'f') {
cout << "无效操作,请重新输入。" << endl;
return;
}
if (!isValidPos(r, c)) {
cout << "坐标越界,请重新输入。" << endl;
return;
}
if (action == 'o') {
if (display[r][c] == 'F') {
cout << "该格子已插旗,不能翻开。" << endl;
return;
}
reveal(r, c);
} else if (action == 'f') {
toggleFlag(r, c);
}
}
这里空空如也
















有帮助,赞一个