Initial commit: C++ courseware
This commit is contained in:
commit
4a4ee7c287
1789 files changed
+373489
No files matched your search
@@ -0,0 +1,122 @@
|
||||
//
|
||||
// Created by xiaolinquan on 2024/12/13.
|
||||
//
|
||||
#include <bits/stdc++.h>
|
||||
#include <windows.h>
|
||||
using namespace std;
|
||||
|
||||
|
||||
void setColor(int color) {
|
||||
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
|
||||
SetConsoleTextAttribute(hConsole, color);
|
||||
}
|
||||
|
||||
int main() {
|
||||
// 提示用户输入雷区大小
|
||||
cout << "请输入您想要的雷区大小: ";
|
||||
int n, mineCount = 0;
|
||||
cin >> n; // 读取用户输入的雷区大小
|
||||
|
||||
// 声明雷区数组并初始化
|
||||
char mineArea[n][n];
|
||||
|
||||
// 初始化随机数种子
|
||||
srand((unsigned int)time(0));
|
||||
|
||||
// 随机初始化雷区
|
||||
// 遍历每个方格,随机放置地雷,20%的概率放置地雷
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (rand() % 5 == 0) { // 1/5的概率放置地雷
|
||||
mineArea[i][j] = '*'; // 用 '*' 表示地雷
|
||||
mineCount++; // 统计地雷数量
|
||||
} else {
|
||||
mineArea[i][j] = '0'; // 用 '0' 表示无地雷
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化每个方格周围的地雷数
|
||||
|
||||
int dx[3] = {-1, 0, 1}; // x方向上的偏移量
|
||||
int dy[3] = {-1, 0, 1}; // y方向上的偏移量
|
||||
|
||||
// 遍历雷区,为每个不是地雷的方格计算周围地雷的数量
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (mineArea[i][j] == '*') {
|
||||
// 对于每一个地雷,更新其周围8个方格的地雷计数
|
||||
for (int a = 0; a < 3; a++) {
|
||||
for (int b = 0; b < 3; b++) {
|
||||
int x = dx[a] + i;
|
||||
int y = dy[b] + j;
|
||||
// 检查是否在边界内并不是地雷的方格
|
||||
if (x >= 0 && x < n && y >= 0 && y < n && mineArea[x][y] != '*') {
|
||||
mineArea[x][y]++; // 增加相邻地雷计数
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 打印初始化后的雷区
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
cout << setw(3) << mineArea[i][j]; // 格式化输出,保证对齐
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
// 初始化用户视图数组,所有方格初始为未揭示状态 '?'
|
||||
char userArea[n][n];
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
userArea[i][j] = '?';
|
||||
}
|
||||
}
|
||||
|
||||
int remainSquareCount = n * n; // 剩余未揭示方格数量
|
||||
while (mineCount != remainSquareCount) {
|
||||
// 提示用户输入要探查的方格位置
|
||||
cout << "请输入你想要探查方格的位置,横坐标在前,纵坐标在后,以空格隔开:";
|
||||
int x, y;
|
||||
cin >> x >> y;
|
||||
x--; y--; // 调整为0开始的索引
|
||||
|
||||
// 检查输入是否合法
|
||||
if (x < 0 || x >= n || y < 0 || y >= n) {
|
||||
cout << "输入无效,请输入有效坐标。" << endl;
|
||||
continue; // 跳过当前循环,重新输入
|
||||
}
|
||||
|
||||
// 检查是否踩到地雷
|
||||
if (mineArea[x][y] == '*') {
|
||||
cout << "踩到雷了!游戏结束!";
|
||||
|
||||
return 0; // 结束程序
|
||||
} else {
|
||||
userArea[x][y] = mineArea[x][y]; // 更新用户视图
|
||||
}
|
||||
|
||||
// 打印当前用户视图
|
||||
for (int i = 0; i < n; ++i) {
|
||||
for (int j = 0; j < n; ++j) {
|
||||
if (i == x && j == y) {
|
||||
setColor(4); // 红色显示用户最新选择
|
||||
} else if (userArea[i][j] != '?') {
|
||||
setColor(1); // 蓝色显示已揭示的安全区
|
||||
} else {
|
||||
setColor(7); // 白色显示未揭示区域
|
||||
}
|
||||
cout << setw(3) << userArea[i][j]; // 格式化输出
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
remainSquareCount--; // 减少剩余未揭示方格数量
|
||||
}
|
||||
|
||||
// 如果循环结束且所有安全区已揭示,用户胜利
|
||||
cout << "恭喜你!!游戏成功!!";
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user