#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cstring>
#ifdef _WIN32
#include <conio.h>
#include <windows.h>
#else
#include <termios.h>
#include <unistd.h>
char getch() {
struct termios old_termios, new_termios;
char c;
tcgetattr(STDIN_FILENO, &old_termios);
new_termios = old_termios;
new_termios.c_lflag &= ~(ICANON | ECHO);
tcsetattr(STDIN_FILENO, TCSANOW, &new_termios);
c = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &old_termios);
return c;
}
#define _getch getch
#endif
using namespace std;
const int MAX_SIZE = 30;
const char MINE = '*';
class Minesweeper {
private:
int rows, cols, mines;
char board[MAX_SIZE][MAX_SIZE];
bool revealed[MAX_SIZE][MAX_SIZE];
bool flagged[MAX_SIZE][MAX_SIZE];
int cursorRow, cursorCol;
bool gameOver, gameWin;
bool firstDisplay; // 用于标记是否是第一次显示棋盘
public:
Minesweeper(int r, int c, int m) : rows(r), cols(c), mines(m), cursorRow(0), cursorCol(0),
gameOver(false), gameWin(false), firstDisplay(true) {
memset(board, '.', sizeof(board));
memset(revealed, false, sizeof(revealed));
memset(flagged, false, sizeof(flagged));
generateMines(cursorRow, cursorCol);
calculateNumbers();
}
void generateMines(int safeR, int safeC) {
srand((unsigned)time(NULL));
int placed = 0;
while (placed < mines) {
int r = rand() % rows;
int c = rand() % cols;
if ((r >= safeR - 1 && r <= safeR + 1 && c >= safeC - 1 && c <= safeC + 1) || board[r][c] == MINE)
continue;
board[r][c] = MINE;
placed++;
}
}
void calculateNumbers() {
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (board[i][j] == MINE) continue;
int count = 0;
for (int di = -1; di <= 1; ++di) {
for (int dj = -1; dj <= 1; ++dj) {
if (di == 0 && dj == 0) continue;
int ni = i + di, nj = j + dj;
if (ni >= 0 && ni < rows && nj >= 0 && nj < cols && board[ni][nj] == MINE)
count++;
}
}
// 修改点1:将'0'替换为'.'
if (count == 0) {
board[i][j] = '.';
} else {
board[i][j] = '0' + count;
}
}
}
}
void displayBoard(bool showAll = false) {
// 首次显示:输出标题、说明和列号
if (firstDisplay) {
// 清屏并输出静态信息
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
cout << "=== 扫雷游戏 ===\n";
cout << "操作说明: WASD 移动, F 标记/取消标记, Space 翻开格子\n";
cout << "目标: 揭开所有非雷格子即获胜!\n\n";
// 输出列号
cout << " ";
for (int j = 0; j < cols; ++j)
cout << (j < 10 ? " " : "") << j % 10 << " ";
cout << "\n";
firstDisplay = false;
} else {
// 非首次显示:将光标移动到棋盘开始位置(假设静态信息占4行)
// \033[5;1H 将光标移动到第5行第1列(行和列编号从1开始)
cout << "\033[5;1H";
}
// 绘制棋盘主体
for (int i = 0; i < rows; ++i) {
// 输出行号
cout << (i < 10 ? " " : "") << i << " ";
for (int j = 0; j < cols; ++j) {
bool highlight = (i == cursorRow && j == cursorCol);
if (highlight) cout << "[";
else cout << " ";
if (showAll || revealed[i][j])
cout << board[i][j];
else if (flagged[i][j])
cout << "F";
else
cout << "#";
if (highlight) cout << "]";
else cout << " ";
}
// 清除本行光标后的内容,并换行
cout << "\033[K\n";
}
// 确保输出立即显示
cout << flush;
}
void moveCursor(char key) {
switch (key) {
case 'w':
case 'W':
if (cursorRow > 0) cursorRow--;
break;
case 's':
case 'S':
if (cursorRow < rows - 1) cursorRow++;
break;
case 'a':
case 'A':
if (cursorCol > 0) cursorCol--;
break;
case 'd':
case 'D':
if (cursorCol < cols - 1) cursorCol++;
break;
}
}
void toggleFlag() {
flagged[cursorRow][cursorCol] = !flagged[cursorRow][cursorCol];
}
void reveal(int r, int c) {
if (r < 0 || r >= rows || c < 0 || c >= cols || revealed[r][c] || flagged[r][c])
return;
revealed[r][c] = true;
if (board[r][c] == MINE) {
gameOver = true;
return;
}
// 自动展开空白区域
// 修改点2:将'0'替换为'.'
if (board[r][c] == '.') {
for (int di = -1; di <= 1; ++di) {
for (int dj = -1; dj <= 1; ++dj) {
reveal(r + di, c + dj);
}
}
}
}
void checkWin() {
int unrevealedSafeCells = 0;
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < cols; ++j) {
if (!revealed[i][j] && board[i][j] != MINE)
unrevealedSafeCells++;
}
}
if (unrevealedSafeCells == 0) gameWin = true;
}
void play() {
char key;
do {
displayBoard();
key = _getch();
if (gameOver || gameWin) break;
if (key == 'f' || key == 'F')
toggleFlag();
else if (key == ' ')
reveal(cursorRow, cursorCol);
else
moveCursor(key);
checkWin();
} while (true);
// 游戏结束时,清屏并显示最终棋盘
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
displayBoard(true);
if (gameOver)
cout << "\nBOOM! 游戏结束!\n";
else if (gameWin)
cout << "\n恭喜你赢了!\n";
cout << "按任意键退出...";
_getch();
}
};
// 启用Windows控制台的ANSI转义序列支持
#ifdef _WIN32
void enableANSI() {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
}
#endif
int main() {
// 启用ANSI支持(主要针对Windows)
#ifdef _WIN32
enableANSI();
#endif
int rows, cols, mines;
cout << "欢迎来到扫雷游戏!\n请输入行数(最大10): ";
cin >> rows;
cout << "请输入列数(最大10): ";
cin >> cols;
cout << "请输入地雷数量: ";
cin >> mines;
if (rows <= 0 || rows > MAX_SIZE || cols <= 0 || cols > MAX_SIZE ||
mines < 0 || mines >= rows * cols) {
cout << "输入参数无效!\n";
return 1;
}
Minesweeper game(rows, cols, mines);
game.play();
return 0;
}
剪贴板
剪贴板 381uqd41
剪贴板信息
剪贴板信息
- 收录
- 更新