#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
char c[10][10]/*可视地图*/, flag[10][10]/*初始化地图*/;
int go[8][2] = {-1, -1, -1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 0, 1, 1}/*方向定义*/, boompoint = 0/*炸弹数*/,
deep = 0/*步数*/;
bool f[10][10]/*剪枝*/, winf = false;
bool win() { //判断是否胜利
int cnt = 0;
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
if (c[i][j] == '*') {
cnt++;
}
}
}
if (boompoint == cnt) {
return 1;
}
return 0;
}
void dfs(int x, int y) { //当那个点为"0",他周围的点都要显示(windows7的扫雷规则)(DFS)
c[x][y] = flag[x][y];
if (flag[x][y] == '0') {
for (int k = 0; k < 8; k++) {
if (f[x + go[k][0]][y + go[k][1]] == 0) { //剪枝
f[x + go[k][0]][y + go[k][1]] = 1; //记录状态
dfs(x + go[k][0], y + go[k][1]); //递归
f[x + go[k][0]][y + go[k][1]] = 0; //记录状态
}
}
}
return ;
}
void begin() {
system("cls");
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\t\t\t\t\t\t扫雷大战--现在开始!\n");
for (int i = 1; i <= 3; i++) {
system("color 10");
system("color 20");
system("color 30");
system("color 40");
system("color 50");
system("color 60");
system("color 70");
system("color 80");
system("color 90");
system("color a0");
system("color b0");
system("color c0");
system("color d0");
system("color e0");
system("color f0");
system("color 0f");
}
system("color 8A");
Sleep(3000);//使用windows.h,指停止1000ms
system("cls");
MessageBox(0, "您可以输入作弊码(输入其他的跳过)", "作弊系统", MB_OK);
MessageBox(0, "作弊码只能输数字,输入其他的会使系统崩溃", "警告", MB_OK);
int password, bx, by; //bx,by为你提供第一步
cout << "作弊码:";
cin >> password;
if (password == 2147483647) { //作弊系统
bool haha = 1;
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 9; j++) {
if (flag[i][j] == '0') {
bx = i, by = j;
haha = 0;
break;
}
}
if (haha == 0)
break;
}
cout << endl << "第一步的行数为" << bx << ",列数为" << by << endl;
system("pause");
}
while (1) {
deep++;
system("cls");//使用windows.h,清屏
cout << endl;
int x, y;
for (int i = 1; i <= 8; i++) { //目前地图的状态
for (int j = 1; j <= 9; j++) {
cout << " " << c[i][j];
}
}
cout << "共1~10个雷,请输入行数与列数" << endl;
cout << "行数:", cin>>x;
cout << "列数:", cin>>y;
if (flag[x][y] == '#') {
system("cls");
cout << endl << "触雷!" << endl;
return ;
}
printf("\a");//蜂鸣器
memset(f, 0, sizeof(f));
dfs(x, y);
if (win()) {
winf = true;
cout << "恭喜通关!" << endl;
return;
}
}
}
int main() {
system("color 8A");//使用windows.h头文件,具体在最后几行
srand((int)time(NULL));//随机数种子
int x, y;
x = rand() % 8 + 1, y = rand() % 8 + 1, flag[x][y] = '#'; //产生随机数(生成雷)
x = rand() % 8 + 1, y = rand() % 8 + 1, flag[x][y] = '#';
x = rand() % 8 + 1, y = rand() % 8 + 1, flag[x][y] = '#';
x = rand() % 8 + 1, y = rand() % 8 + 1, flag[x][y] = '#';
x = rand() % 8 + 1, y = rand() % 8 + 1, flag[x][y] = '#';
x = rand() % 8 + 1, y = rand() % 8 + 1, flag[x][y] = '#';
x = rand() % 8 + 1, y = rand() % 8 + 1, flag[x][y] = '#';
x = rand() % 8 + 1, y = rand() % 8 + 1, flag[x][y] = '#';
x = rand() % 8 + 1, y = rand() % 8 + 1, flag[x][y] = '#';
x = rand() % 8 + 1, y = rand() % 8 + 1, flag[x][y] = '#';
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 8; j++) {
c[i][j] = '*';
}
c[i][9] = flag[i][9] = '\n';
}//初始化地图
for (int i = 1; i <= 8; i++) { //初始化地图
for (int j = 1; j <= 8; j++) {
if (flag[i][j] != '#') {
char cnt = '0';
for (int k = 0; k < 8; k++) {
if (flag[i + go[k][0]][j + go[k][1]] == '#') {
cnt++;
}
}
flag[i][j] = cnt; //计算周边的雷的数量
} else {
boompoint++;//记录雷的个数
}
}
}
/*for(int i=1;i<=8;i++){
for(int j=1;j<=9;j++){
cout<<" "<<flag[i][j];
}
}*/
// printf("\a");
cout << "扫雷(通关有惊喜)" << endl;
Sleep(1000);//使用windows.h,指停止1000ms
cout << "作者:Null_" << endl;
Sleep(1000);
begin();//函数
Sleep(1000);
cout << endl;
cout << "您共走了" << deep << "步" << endl;
system("pause");//使用windows.h,作用为"按任意键继续"
cout << endl;
cout << "#代表地雷" << endl;
for (int i = 1; i <= 8; i++) {
for (int j = 1; j <= 9; j++) {
cout << " " << flag[i][j];
}
}
cout << endl;
system("pause");
if (winf == true) {
for (int i = 1; i <= 3; i++) {
system("color 10");
system("color 20");
system("color 30");
system("color 40");
system("color 50");
system("color 60");
system("color 70");
system("color 80");
system("color 90");
system("color a0");
system("color b0");
system("color c0");
system("color d0");
system("color e0");
system("color f0");
system("color 0f");
}
system("color 8A");
cout << endl << "赠送作弊码:2147483647" << endl;
}
cout << endl << "byebye" << endl;
return 0;
}
扫雷代码!!!