极简版扫雷
2025-09-11 20:58:17
发布于:广东
#include <bits/stdc++.h>
#include <windows.h>
using namespace std;
typedef long long ll;
const int dx[] = {0,0,1,-1,1,1,-1,-1};
const int dy[] = {1,-1,0,0,-1,1,1,-1};
bool gui = 0;//作弊
void back_hall();
void gof(int &x,int &y,int m,int step)
{
if(y+step > m) x++,y = 1;
else y++;
}
void game(int h,int l,int op)
{
//注意代码下标是1-based(作者不习惯0-based)
system("cls");
system("color 0F");
if(gui) cout<<"您已开启作弊模式\n";
else cout<<"您未开启作弊模式\n";
vector<vector<bool> > lei(h+5,vector<bool>(l+5,0)); //每个点是否是雷
vector<vector<int> > type(h+5,vector<int>(l+5,-1)); //每个点类型(-1是未探索,-2是插旗,-3是问号,其他数字是雷数)
//初始化
int yu = op;
int x=1,y=1,s = h*l;
while(yu)
{
int d = rand()%5;
if(d == 1)
{
lei[x][y] = 1;
yu--;
}
s--;
if(s == yu)
{
while(yu--)
{
lei[x][y] = 1;
gof(x,y,l,1);
}
break;
}
gof(x,y,l,1);
}
Sleep(1000);
while(1)
{
//break;
system("cls");
int cnt = 0;
if(gui)
{
for(int i=1;i<=h;i++)
{
for(int j=1;j<=l;j++)
{
cout<<lei[i][j]<<' ';
}
cout<<'\n';
}
cout<<'\n';
}
for(int i=1;i<=h;i++)
{
for(int j=1;j<=l;j++)
{
if(type[i][j] == -1) cout<<". ";
else if(type[i][j] == -2) cout<<"f ";
else if(type[i][j] == -3) cout<<"? ";
else
{
cnt++;
cout<<type[i][j]<<' ';
}
}
cout<<'\n';
}
if(cnt == h*l-op) break;
cout<<"选择您的操作\n";
cout<<"下文提到的坐标是行、列(不是平面直角坐标系)\n";
cout<<"如,第一行第三列输入1 3\n";
cout<<"输入1和坐标探索这个格子\n";
cout<<"输入2和坐标给这个格子插旗\n";
cout<<"输入3和坐标给这个格子标问号\n";
int czs,x,y;
cin>>czs>>x>>y;
if(x > h || x < 1 || y > l || y < 1 || czs > 3 || czs < 1)
{
cout<<"输入的什么玩意!";
Sleep(1000);
continue;
}
if(czs == 1)
{
if(lei[x][y])
{
cout<<"踩雷啦!(l是雷)";
system("color 0C");
Sleep(5000);
system("cls");
for(int i=1;i<=h;i++)
{
for(int j=1;j<=l;j++)
{
if(lei[i][j] == 1) cout<<"l ";
else if(type[i][j] == -1) cout<<". ";
else if(type[i][j] == -2) cout<<"f ";
else if(type[i][j] == -3) cout<<"? ";
else cout<<type[i][j]<<' ';
}
cout<<'\n';
}
Sleep(15000);
return;
}
else
{
type[x][y] = 0;
for(int i=0;i<8;i++)
{
int cx = x+dx[i],cy = y+dy[i];
if(cx < 1 || cx > h || cy < 1 || cy > l) continue;
type[x][y] += lei[cx][cy];
}
if(type[x][y] == 0)
{
//BFS
queue<pair<int,int>> q;
q.push({x,y});
while(!q.empty())
{
auto f = q.front();
q.pop();
int ax=f.first,ay=f.second;
if(type[ax][ay] > 0) continue;
for(int i=0;i<8;i++)
{
int tx=ax+dx[i];
int ty=ay+dy[i];
if(tx<1||tx>h||ty<1||ty>l) continue;
if(type[tx][ty] > -1) continue;
type[tx][ty] = 0;
for(int i=0;i<8;i++)
{
int cx = tx+dx[i],cy = ty+dy[i];
if(cx < 1 || cx > h || cy < 1 || cy > l) continue;
type[tx][ty] += lei[cx][cy];
}
q.push({tx,ty});
}
}
}
}
}
else if(czs == 2 || czs == 3) type[x][y] = -czs;
}
cout<<"恭喜您赢了\n";
system("color 0A"); // 0A
Sleep(8000);
}
void gameAI(int h,int l,int op)
{
//注意代码下标是1-based(作者不习惯0-based)
system("cls");
system("color 0F");
if(gui) cout<<"您已开启作弊模式\n";
else cout<<"您未开启作弊模式\n";
vector<vector<bool> > lei(h+5,vector<bool>(l+5,0)); //每个点是否是雷
vector<vector<int> > type(h+5,vector<int>(l+5,-1)); //每个点类型(-1是未探索,-2是插旗,-3是问号,其他数字是雷数)
//初始化
int yu = op;
int x=1,y=1,s = h*l;
bool pa = 0;
while(yu)
{
int d = rand()%5;
if(d == 1)
{
lei[x][y] = 1;
yu--;
}
s--;
if(s == yu)
{
while(yu--)
{
lei[x][y] = 1;
gof(x,y,l,1);
}
break;
}
gof(x,y,l,1);
}
Sleep(1000);
while(1)
{
pa = !pa;
//break;
system("cls");
int cnt = 0;
if(gui)
{
for(int i=1;i<=h;i++)
{
for(int j=1;j<=l;j++)
{
cout<<lei[i][j]<<' ';
}
cout<<'\n';
}
cout<<'\n';
}
vector<pair<int,int> > p;
for(int i=1;i<=h;i++)
{
for(int j=1;j<=l;j++)
{
if(!pa && type[i][j] <= -1) p.push_back({i,j});
if(type[i][j] == -1) cout<<". ";
else if(type[i][j] == -2) cout<<"f ";
else if(type[i][j] == -3) cout<<"? ";
else
{
cnt++;
cout<<type[i][j]<<' ';
}
}
cout<<'\n';
}
if(cnt == h*l-op)
{
pa = !pa;
if(pa == 1)
{
cout<<"恭喜您赢了\n";
system("color 0A"); // 0A
Sleep(8000);
}
else
{
cout<<"恭喜AI赢了\n";
system("color 0C"); // 0C
Sleep(8000);
}
return;
}
cout<<pa<<'\n';
if(pa == 1)
{
cout<<"选择您的操作\n";
cout<<"下文提到的坐标是行、列(不是平面直角坐标系)\n";
cout<<"如,第一行第三列输入1 3\n";
cout<<"输入1和坐标探索这个格子\n";
cout<<"输入2和坐标给这个格子插旗\n";
cout<<"输入3和坐标给这个格子标问号\n";
int czs,x,y;
cin>>czs>>x>>y;
if(x > h || x < 1 || y > l || y < 1 || czs > 3 || czs < 1)
{
cout<<"输入的什么玩意!";
Sleep(1000);
continue;
}
if(czs == 1)
{
if(lei[x][y])
{
cout<<"踩雷啦!AI赢了(l是雷)";
system("color 0C");
Sleep(5000);
system("cls");
for(int i=1;i<=h;i++)
{
for(int j=1;j<=l;j++)
{
if(lei[i][j] == 1) cout<<"l ";
else if(type[i][j] == -1) cout<<". ";
else if(type[i][j] == -2) cout<<"f ";
else if(type[i][j] == -3) cout<<"? ";
else cout<<type[i][j]<<' ';
}
cout<<'\n';
}
Sleep(15000);
return;
}
else
{
type[x][y] = 0;
for(int i=0;i<8;i++)
{
int cx = x+dx[i],cy = y+dy[i];
if(cx < 1 || cx > h || cy < 1 || cy > l) continue;
type[x][y] += lei[cx][cy];
}
if(type[x][y] == 0)
{
//BFS
queue<pair<int,int>> q;
q.push({x,y});
while(!q.empty())
{
auto f = q.front();
q.pop();
int ax=f.first,ay=f.second;
if(type[ax][ay] > 0 || lei[ax][ay]) continue;
for(int i=0;i<8;i++)
{
int tx=ax+dx[i];
int ty=ay+dy[i];
if(tx<1||tx>h||ty<1||ty>l) continue;
if(type[tx][ty] > -1) continue;
type[tx][ty] = 0;
for(int i=0;i<8;i++)
{
int cx = tx+dx[i],cy = ty+dy[i];
if(cx < 1 || cx > h || cy < 1 || cy > l) continue;
type[tx][ty] += lei[cx][cy];
}
q.push({tx,ty});
}
}
}
}
}
else if(czs == 2 || czs == 3) type[x][y] = -czs;
}
else
{
cout<<"现在是AI操作!\n";
int pos = rand()%(int)p.size();
//cout<<pos<<" "<<p.size()<<'\n';
//exit(0);
int x = p[pos].first,y = p[pos].second;
cout<<"AI选择了("<<x<<','<<y<<")!\n";
if(lei[x][y])
{
cout<<"踩雷啦!你赢了(l是雷)";
system("color 0A");
Sleep(5000);
system("cls");
for(int i=1;i<=h;i++)
{
for(int j=1;j<=l;j++)
{
if(lei[i][j] == 1) cout<<"l ";
else if(type[i][j] == -1) cout<<". ";
else if(type[i][j] == -2) cout<<"f ";
else if(type[i][j] == -3) cout<<"? ";
else cout<<type[i][j]<<' ';
}
cout<<'\n';
}
Sleep(15000);
return;
}
else
{
type[x][y] = 0;
for(int i=0;i<8;i++)
{
int cx = x+dx[i],cy = y+dy[i];
if(cx < 1 || cx > h || cy < 1 || cy > l) continue;
type[x][y] += lei[cx][cy];
}
if(type[x][y] == 0)
{
//BFS
queue<pair<int,int>> q;
q.push({x,y});
while(!q.empty())
{
auto f = q.front();
q.pop();
int ax=f.first,ay=f.second;
if(type[ax][ay] > 0 || lei[ax][ay]) continue;
for(int i=0;i<8;i++)
{
int tx=ax+dx[i];
int ty=ay+dy[i];
if(tx<1||tx>h||ty<1||ty>l) continue;
if(type[tx][ty] > -1) continue;
type[tx][ty] = 0;
for(int i=0;i<8;i++)
{
int cx = tx+dx[i],cy = ty+dy[i];
if(cx < 1 || cx > h || cy < 1 || cy > l) continue;
type[tx][ty] += lei[cx][cy];
}
q.push({tx,ty});
}
}
}
}
Sleep(2000);
}
}
}
int main()
{
srand(unsigned(time(0)));
back_hall();
return 0;
}
void back_hall()
{
system("cls");
system("color 0F");
cout<<"---欢迎来到扫雷---\n";
cout<<"输入1开始简单模式\n";
cout<<"输入2开始普通模式\n";
cout<<"输入3开始困难模式\n";
cout<<"输入4开关作弊\n";
cout<<"输入5自定义难度(建议,因为雷在地图下方出现的数量极少)\n";
cout<<"输入6人机对局&自定义难度\n";
int czs;
cin>>czs;
if(czs == 1) game(8,8,8);
else if(czs == 2) game(20,20,40);
else if(czs == 3) game(65,65,300);
else if(czs == 4) gui = !gui;
else if(czs == 5)
{
cout<<"\n\n输入行、列、雷数量\n";
cout<<"2<=行<=500,2<=列<=500,1<=雷<行*列\n";
ll h,l,lei;
cin>>h>>l>>lei;
if(!(h >= 2 && h <= 500) || !(l >= 2 && l <= 500) || !(lei >= 1 && lei <= h*l))
{
cout<<"输入无效!"<<endl;
back_hall();
}
game(h,l,lei);
}
else if(czs == 6)
{
cout<<"\n\n输入行、列、雷数量\n";
cout<<"2<=行<=500,2<=列<=500,1<=雷<行*列\n";
ll h,l,lei;
cin>>h>>l>>lei;
if(!(h >= 2 && h <= 500) || !(l >= 2 && l <= 500) || !(lei >= 1 && lei <= h*l))
{
cout<<"输入无效!"<<endl;
back_hall();
}
gameAI(h,l,lei);
}
else
{
cout<<"输入无效!"<<endl;
back_hall();
}
back_hall();
}
全部评论 2
运行不了
给我改啊啊啊17小时前 来自 北京
0用DEV
17小时前 来自 广东
0
d
昨天 来自 广东
0
有帮助,赞一个