- 编程
非常好玩的扫雷游戏
- 2025-9-6 22:22:18 @
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <limits>
using namespace std;
const int EASY_SIZE = 9;
const int MEDIUM_SIZE = 16;
const int HARD_SIZE = 24;
const int EASY_MINES = 10;
const int MEDIUM_MINES = 40;
const int HARD_MINES = 99;
class Minesweeper {
private:
int size;
int mineCount;
vector<vector<char>> board;
vector<vector<bool>> mines;
vector<vector<bool>> revealed;
vector<vector<bool>> flagged;
void placeMines() {
int placed = 0;
while (placed < mineCount) {
int x = rand() % size;
int y = rand() % size;
if (!mines[x][y]) {
mines[x][y] = true;
placed++;
}
}
}
int countAdjacentMines(int x, int y) {
int count = 0;
for (int i = max(0, x-1); i <= min(size-1, x+1); i++) {
for (int j = max(0, y-1); j <= min(size-1, y+1); j++) {
if (mines[i][j]) count++;
}
}
return count;
}
void revealEmptyCells(int x, int y) {
if (x < 0 || x >= size || y < 0 || y >= size || revealed[x][y]) return;
revealed[x][y] = true;
if (countAdjacentMines(x, y) > 0) return;
for (int i = max(0, x-1); i <= min(size-1, x+1); i++) {
for (int j = max(0, y-1); j <= min(size-1, y+1); j++) {
if (!(i == x && j == y)) {
revealEmptyCells(i, j);
}
}
}
}
public:
Minesweeper(int difficulty) {
switch(difficulty) {
case 1:
size = EASY_SIZE;
mineCount = EASY_MINES;
break;
case 2:
size = MEDIUM_SIZE;
mineCount = MEDIUM_MINES;
break;
case 3:
size = HARD_SIZE;
mineCount = HARD_MINES;
break;
default:
size = EASY_SIZE;
mineCount = EASY_MINES;
}
board.resize(size, vector<char>(size, '-'));
mines.resize(size, vector<bool>(size, false));
revealed.resize(size, vector<bool>(size, false));
flagged.resize(size, vector<bool>(size, false));
placeMines();
}
void printBoard(bool showMines = false) {
cout << " ";
for (int i = 0; i < size; i++) {
cout << i % 10 << " ";
}
cout << endl;
for (int i = 0; i < size; i++) {
cout << i % 10 << " ";
for (int j = 0; j < size; j++) {
if (flagged[i][j]) {
cout << "F ";
} else if (!revealed[i][j]) {
cout << "- ";
} else if (mines[i][j] && showMines) {
cout << "* ";
} else if (mines[i][j]) {
cout << "- ";
} else {
int count = countAdjacentMines(i, j);
if (count > 0) {
cout << count << " ";
} else {
cout << " ";
}
}
}
cout << endl;
}
}
bool makeMove(int x, int y, char action) {
if (x < 0 || x >= size || y < 0 || y >= size) return false;
if (action == 'f' || action == 'F') {
if (!revealed[x][y]) {
flagged[x][y] = !flagged[x][y];
}
return true;
} else if (action == 'r' || action == 'R') {
if (flagged[x][y]) return true;
if (mines[x][y]) return false;
revealEmptyCells(x, y);
return true;
}
return false;
}
bool checkWin() {
for (int i = 0; i < size; i++) {
for (int j = 0; j < size; j++) {
if (!mines[i][j] && !revealed[i][j]) {
return false;
}
}
}
return true;
}
};
void clearScreen() {
system("cls || clear");
}
int main() {
srand(time(0));
cout << "欢迎来到扫雷游戏!" << endl;
cout << "请选择难度:" << endl;
cout << "1. 简单 (9x9, 10个雷)" << endl;
cout << "2. 中等 (16x16, 40个雷)" << endl;
cout << "3. 困难 (24x24, 99个雷)" << endl;
cout << "输入选择 (1-3): ";
int difficulty;
while (!(cin >> difficulty) || difficulty < 1 || difficulty > 3) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "无效输入,请重新输入 (1-3): ";
}
Minesweeper game(difficulty);
bool gameOver = false;
bool won = false;
while (!gameOver && !won) {
clearScreen();
game.printBoard();
cout << "输入操作 (格式: x y action)" << endl;
cout << "action: r-翻开, f-标记/取消标记" << endl;
cout << "示例: 5 5 r 表示翻开(5,5)位置" << endl;
cout << "输入: ";
int x, y;
char action;
while (!(cin >> x >> y >> action) ||
(action != 'r' && action != 'R' && action != 'f' && action != 'F')) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "无效输入,请重新输入 (格式: x y action): ";
}
if (!game.makeMove(x, y, action)) {
gameOver = true;
}
won = game.checkWin();
}
clearScreen();
game.printBoard(true);
if (won) {
cout << "恭喜你赢了!" << endl;
} else {
cout << "很遗憾,你踩到地雷了!" << endl;
}
return 0;
}
0 条评论
目前还没有评论...