扫雷1.0
Hamburger999 · · 个人记录
扫雷1.0
自制垃圾扫雷小游戏(bug可能很多……)
代码如下:(注释不太多……)
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<mmsystem.h>
#define Setcolor(x,opt) SetConsoleTextAttribute(hout,((caise||!opt)?x:7)) //设置字体颜色
#define click if(yinxiao) PlaySound(TEXT("effect\\d.wav"),NULL,SND_FILENAME|SND_ASYNC); //点击声音效果
using namespace std;
int A[51][51],B[51][51],X[51][51],cnt,h,w,Lp,Bj,x,y,hongzha=0,yinxiao=1,caise=1,md;
HANDLE hout = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_CURSOR_INFO cursorinfo;
const int Dx[8]={1,1,1,0,0,-1,-1,-1},Dy[8]={1,0,-1,1,-1,1,0,-1},colorlist[8]={0,2,3,4,5,6,14,13};
char hd;long long score[4]; string name[4];
void readdata(){ //读取文档
ifstream fin("data\\score.txt"),Fin("data\\setting.txt");
for(int i=1;i<=3;i++) fin>>name[i]>>score[i];
Fin>>hongzha>>yinxiao>>caise;
fin.close(),Fin.close();
}
void writedata(){ //覆写文档
ofstream fout("data\\score.txt"),Fout("data\\setting.txt");
for(int i=1;i<=3;i++) fout<<name[i]<<' '<<score[i]<<'\n';
Fout<<hongzha<<' '<<yinxiao<<' '<<caise;
fout.close(),Fout.close();
}
void F(int x,int y){ //翻开方块,并dfs翻开其它的方块
X[x][y]=1;if(A[x][y]==0){
for(int i=0;i<8;i++){
int x2=x+Dx[i],y2=y+Dy[i];
if(A[x2][y2]!=-1&&B[x2][y2]==0&&X[x2][y2]==0&&x2>=1&&x2<=h&&y2>=1&&y2<=w) F(x2,y2);
}
}
}
void Draw(bool mode){ //绘制游戏画面
SetConsoleCursorPosition(hout,(COORD){0,0});
for(int i=1;i<=h;i++){
for(int j=1;j<=w;j++){
if(mode&&A[i][j]==-1){
Setcolor(8,1);
printf("* ");
continue;
}
if(B[i][j]){
Setcolor(12,1);
if(i==x&&j==y&&!mode) Setcolor(11,0);
printf(B[i][j]==1?"x":"?");
printf(" ");
}
else if(X[i][j]==1){
if(A[i][j]==0){
Setcolor(7,1);
if(i==x&&j==y&&!mode) Setcolor(11,0);
printf(". ");
}
else{
Setcolor(colorlist[A[i][j]],1);
if(i==x&&j==y&&!mode) Setcolor(11,0);
printf("%d ",A[i][j]);
}
}
else{
Setcolor(1,1);
if(i==x&&j==y&&!mode) Setcolor(11,0);
printf("■");
}
}
if(i==1) Setcolor(7,1),printf(" 剩余雷数:%d",Lp-Bj);
if(i==2) Setcolor(7,1),printf(" 剩余未翻开:%d",cnt);
printf(" ");
cout<<endl;
}
Setcolor(7,1);
}
struct init_list{int x,y;};
void Init(int L,int xx,int yy){ //初始化雷的数量
vector<init_list> ok,tmp;
for(int i=1;i<=h;i++) for(int j=1;j<=w;j++)
ok.push_back({i,j});
for(int i=1;i<=L;i++){
tmp.clear();
for(int j=0;j<(int)ok.size();j++){
int x2=ok[j].x,y2=ok[j].y;
if(!(A[x2][y2]==-1||(abs(xx-x2)<=1&&abs(yy-y2)<=1)))
tmp.push_back(ok[j]);
}
ok.swap(tmp);
int t=rand()%ok.size();
A[ok[t].x][ok[t].y]=-1;
}
for(int i=1;i<=h;i++) for(int j=1;j<=w;j++)
if(A[i][j]!=-1){
int cc=0;
for(int k=-1;k<=1;k++) for(int l=-1;l<=1;l++)
if(A[i+k][j+l]==-1) cc++;
A[i][j]=cc;
}
}
void Count(){cnt=0; for(int i=1;i<=h;i++) for(int j=1;j<=w;j++) if(X[i][j]==0) cnt++;} //统计未翻开的地块数
void Out(string t,int ms_){for(int i=0;i<(int)t.length();i++) putchar(t[i]),Sleep(ms_);} //输出字符串(每输出一个字符停止一下)
void help1(){ //说明书
system("cls");
Out("游戏说明:\n以最快的速度翻开空方块,避开地雷,翻开所有安全方块则获胜,触发地雷则输。\n\n",10);
Out("操作说明:\nc 翻开(第一次翻开总是安全的)\nv 标记\nw/a/s/d 移动\n\n",10);
Out("图案说明:\n",10),Setcolor(1,1),Out("■",10),Setcolor(7,1),Out(" 未翻开的地块\n",10);
Out(". 翻开,且周围无雷的地块\n",10),Setcolor(2,1),Out("1",10),Setcolor(7,1),Out("~",10);
Setcolor(13,1),Out("8",10),Setcolor(7,1),Out(" 翻开,且周围有对应数字雷数的地块\n",10);
Setcolor(12,1),Out("x",10),Setcolor(7,1),Out(" 被标记有雷的地块\n",10);
Setcolor(12,1),Out("?",10),Setcolor(7,1),Out(" 被标记不确定的地块\n",10);
Setcolor(8,1),Out("*",10),Setcolor(7,1),Out(" 游戏结束,有雷的地块\n\n",10);
Out("注意事项:\n本游戏在盘面较大时可能有些卡顿现象。\n\n",10);
Out("按键返回主页……",10);
getch();click;
}
void help2(){ //设置
system("cls");
FLAG: SetConsoleCursorPosition(hout,(COORD){0,0});
cout<<"失败后轰炸电脑:"<<(hongzha?"开":"关")<<" 按 1 切换\n";
cout<<"音效: "<<(yinxiao?"开":"关")<<" 按 2 切换\n";
cout<<"彩色: "<<(caise?"开":"关")<<" 按 3 切换\n";
cout<<"按 4 退出\n";
hd=getch();click;if(hd=='1'){hongzha^=1;goto FLAG;}if(hd=='2'){yinxiao^=1;goto FLAG;}
if(hd=='3'){caise^=1;goto FLAG;}if(hd=='4'){writedata();return;} else goto FLAG;
}
void help3(){ //最快纪录
system("cls");
Out((string)"简单模式最快纪录 "+name[1]+" "+to_string(score[1])+"秒 \n",10);
Out((string)"中等模式最快纪录 "+name[2]+" "+to_string(score[2])+"秒 \n",10);
Out((string)"专家模式最快纪录 "+name[3]+" "+to_string(score[3])+"秒 \n",10);
Out("按键返回主页……",10);getch();click;
}
void help4(){ //版本信息
system("cls");
Out("扫雷 1.0\n",10),Out("制作人员 Hamburger999\n",10);
Out("音频资源 网上搜集\n",10),Out("完工日期 2025/6/13\n",10);
Out("欢迎提出建议 请发送电子邮件至 [email protected]\n",10);
Out("感谢您的体验!\n\n",10),Out("按键返回主页……",10);getch();click;
}
void SetConsoleFont(int width,int height){ //设置字体大小
PCONSOLE_FONT_INFOEX Font=new CONSOLE_FONT_INFOEX();
Font->cbSize=sizeof(CONSOLE_FONT_INFOEX),GetCurrentConsoleFontEx(hout,FALSE,Font);
Font->dwFontSize.X=width,Font->dwFontSize.Y=height,SetCurrentConsoleFontEx(hout,FALSE,Font);
}
int main(){ //主函数
readdata(),ShowWindow(GetForegroundWindow(),SW_MAXIMIZE);
SetConsoleFont(24,24);
system("mode con cols=120 lines=120");
GetConsoleCursorInfo(hout, &cursorinfo);
cursorinfo.bVisible=false;
SetConsoleCursorInfo(hout, &cursorinfo);
SetConsoleTitle("扫雷1.0");
Menu:system("cls"),memset(A,0,sizeof(A));
memset(B,0,sizeof(B)),memset(X,0,sizeof(X));
x=1,y=1,Bj=0,srand(time(0));
cout<<"+--------------------------+\n",cout<<"| 扫雷 1.0 |\n";
cout<<"|==========================|\n",cout<<"|\033[4m1)简单模式·9×9·10个雷 \033[0m|\n";
cout<<"|\033[4m2)中等模式·16×16·40个雷\033[0m|\n",cout<<"|\033[4m3)专家模式·30×16·99个雷\033[0m|\n";
cout<<"|\033[4m4)自定义扫雷 \033[0m|\n",cout<<"|\033[4m5)游戏说明书 \033[0m|\n";
cout<<"|\033[4m6)游戏设置 \033[0m|\n",cout<<"|\033[4m7)游戏最快纪录 \033[0m|\n";
cout<<"|\033[4m8)版本信息 \033[0m|\n",cout<<"|9)退出游戏 |\n";
cout<<"+--------------------------+\n";
Menu2:hd=getch();click;
if(hd=='1') w=h=9,cnt=81,Lp=10,md=1;
else if(hd=='2') w=h=16,cnt=256,Lp=40,md=2;
else if(hd=='3') w=30,h=16,cnt=480,Lp=99,md=3;
else if(hd=='4'){md=4;
system("cls");
Out("请输入地图的长宽,雷数:",10),printf("\n长:");
while(cin>>w){
if(6<=w&&w<=40) break;
Out("长应在 6~40 之间!请重新输入:",10);
}
click;printf("宽:");
while(cin>>h){
if(6<=h&&h<=40) break;
Out("宽应在 6~40 之间!请重新输入:",10);
}
click;printf("雷数:"),cnt=w*h;
while(cin>>Lp){
if(6<=Lp&&Lp<=w*h-10) break;
Out("雷数应在 6~长×宽-10 之间!请重新输入:",10);
}
click;
}
else if(hd=='5'){help1();goto Menu;}
else if(hd=='6'){help2();goto Menu;}
else if(hd=='7'){help3();goto Menu;}
else if(hd=='8'){help4();goto Menu;}
else if(hd=='9') return 0;
else goto Menu2;
long long stt;bool win=1;system("cls");
while(cnt>Lp){
Draw(0),hd=getch();
if(hd=='c'&&B[x][y]==0){
click;
if(cnt==w*h) Init(Lp,x,y),stt=time(0);
if(A[x][y]==-1){win=0;break;}
else F(x,y);
}
else if(hd=='v'){
click;
if(X[x][y]==0){
if(B[x][y]==0) B[x][y]=1,Bj++;
else if(B[x][y]==1) B[x][y]=2,Bj--;
else if(B[x][y]==2) B[x][y]=0;
}
}
else if(hd=='w') x=max(x-1,1); else if(hd=='a') y=max(y-1,1);
else if(hd=='s') x=min(x+1,h); else if(hd=='d') y=min(y+1,w);
Count();
}
long long ti=time(0)-stt;
system("cls"),Draw(1);
if(win){
if(yinxiao) PlaySound(TEXT("effect//win.wav"),NULL,SND_FILENAME|SND_ASYNC);
Out((string)"你赢了!\n用时:"+to_string(ti)+"秒\n",10);
if(md<=3){
if(ti<score[md]){
Out((string)"\n你已经打破由 "+name[md]+" 创造的速度纪录 "+to_string(score[md])+" 秒!!!\n",10);
Out("在这里,写下你的名字:",10),cin>>name[md],score[md]=ti,writedata();
Out("你创造的纪录已被保存!\n\n",10);
}else Out((string)"\n该模式当前最快是 "+name[md]+" ,用时 "+to_string(score[md])+" 秒。\n\n",10);
}
else Out("\n由于你使用的是自定义模式,无法保存纪录。\n\n",10);
}else{
if(yinxiao) PlaySound(TEXT("effect//lose.wav"),NULL,SND_FILENAME|SND_ASYNC);
Out("恭喜你触雷了!\n",10);
if(hongzha){
readdata(),ShowWindow(GetForegroundWindow(),SW_HIDE);
Out("你的电脑即将爆炸!",10),Sleep(300);
system("rundll32.exe user32.dll LockWorkStation"),system("shutdown -s -t 100");
while(1) system("start cmd"),SetCursorPos(rand()%3000,rand()%1000);
}
}
Out("返回主页?(y/n)",10);
while(true){
hd=getch(); if(hd=='y'||hd=='n') {
click;
if(hd=='y') goto Menu;
if(hd=='n') break;
}
}
Out("\n886~ 按键退出!",10),getch();
return 0;
}
可以看到的是,这个扫雷游戏需要以下文件:
- 声音文件:
effect\d.wav,effect\win.wav,effect\lose.wav - 文本文件:
data\score.txt,data\setting.txt。
请自行配置,或在这里下载。
你可能会发现这个程序无法过编译。一些处理方法:
- 请把编译器版本提高到 C++14 及以上。
- 在“编译器选项”的“链接时加入以下选项”中加入
-lwinmm。
扫雷 1.1 更新:
文件地址:here