c++扫雷程序
修改自这里QwQ
修复部分bug
1 #include <bits/stdc++.h> 2 #include <windows.h> 3 #include <conio.h> 4 using namespace std; 5 const int p[8][2]= {{0,1},{1,0},{-1,0},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}}; 6 char c; 7 char xmp[21][21]; 8 int mp[21][21],n,m,k,t1,t2; 9 bool mpb[21][21],bo[21][21]; 10 void dfs0(int x,int y) { 11 mpb[x][y]=1; 12 for(int i=0; i<8; i++) { 13 if(x+p[i][0]<1 || x+p[i][0]>n || y+p[i][1]<1 || y+p[i][1]>m) 14 continue; 15 if(mp[x+p[i][0]][y+p[i][1]]==0) { 16 xmp[x+p[i][0]][y+p[i][1]]='0'; 17 if(!mpb[x+p[i][0]][y+p[i][1]]) 18 dfs0(x+p[i][0],y+p[i][1]); 19 } 20 if(mp[x+p[i][0]][y+p[i][1]]>0) 21 xmp[x+p[i][0]][y+p[i][1]]=mp[x+p[i][0]][y+p[i][1]]+'0'; 22 } 23 return; 24 } 25 void bai(int x,int y) { 26 system("cls"); 27 if(bo[x][y]) 28 return; 29 bo[x][y]=1; 30 system("color C"); 31 for(int k=1; k<=19; k++) { 32 Sleep(260); 33 system("cls"); 34 for(int i=1; i<=n; i++) { 35 for(int j=1; j<=m; j++) { 36 cout<<xmp[i][j]<<" "; 37 if(xmp[i][j]=='*') 38 xmp[i][j]='_'; 39 } 40 cout<<endl<<endl; 41 } 42 int j=y+k; 43 for(int i=x-k; i<=x+k; i++) { 44 if(i<1 || i>n || j<1 || j>m) 45 continue; 46 if(mp[i][j]==-1) { 47 xmp[i][j]='*'; 48 for(int l=0; l<8; l++) 49 xmp[i+p[l][0]][j+p[l][1]]='_'; 50 } 51 } 52 j=y-k; 53 for(int i=x-k; i<=x+k; i++) { 54 if(i<1 || i>n || j<1 || j>m) 55 continue; 56 if(mp[i][j]==-1) { 57 xmp[i][j]='*'; 58 for(int l=0; l<8; l++) 59 xmp[i+p[l][0]][j+p[l][1]]='_'; 60 } 61 } 62 int i=x+k; 63 for(int j=x-k; j<=x+k; j++) { 64 if(i<1 || i>n || j<1 || j>m) 65 continue; 66 if(mp[i][j]==-1) { 67 xmp[i][j]='*'; 68 for(int l=0; l<8; l++) 69 xmp[i+p[l][0]][j+p[l][1]]='_'; 70 } 71 } 72 i=x-k; 73 for(int j=x-k; j<=x+k; j++) { 74 if(i<1 || i>n || j<1 || j>m) 75 continue; 76 if(mp[i][j]==-1) { 77 xmp[i][j]='*'; 78 for(int l=0; l<8; l++) 79 xmp[i+p[l][0]][j+p[l][1]]='_'; 80 } 81 } 82 } 83 system("pause"); 84 system("color 7"); 85 } 86 void win() { 87 t2=time(0); 88 for(int k=1; k<=n; k++) { 89 Sleep(150); 90 system("cls"); 91 for(int i=1; i<=m; i++) { 92 if(mp[k-1][i]>0) 93 xmp[k-1][i]=mp[k-1][i]+'0'; 94 else 95 xmp[k-1][i]='*'; 96 xmp[k][i]=45; 97 } 98 for(int i=1; i<=n; i++) { 99 for(int j=1; j<=m; j++) 100 cout<<xmp[i][j]<<" "; 101 cout<<endl<<endl; 102 } 103 } 104 system("cls"); 105 cout<<"游戏成功,总共用时:"<<t2-t1<<"秒"<<endl<<endl; 106 system("pause"); 107 system("cls"); 108 } 109 void game() { 110 memset(mp,0,sizeof mp); 111 memset(mpb,0,sizeof mpb); 112 while(1) { 113 system("cls"); 114 cout<<"请输入长: ( >=5,<=20 )"<<endl; 115 cin>>m; 116 if(m<5 || m>20) { 117 cout<<"您输入的数太大或太小了!重新输入"<<endl; 118 Sleep(1200); 119 } else 120 break; 121 } 122 while(1) { 123 system("cls"); 124 cout<<"请输入宽: ( >=5,<=12 )"<<endl; 125 cin>>n; 126 if(n<5 || n>12) { 127 cout<<"您输入的数太大或太小了!重新输入"<<endl; 128 Sleep(1200); 129 } else 130 break; 131 } 132 while(1) { 133 system("cls"); 134 cout<<"请输入雷数: ( >=5,<="<<n*m-5<<" )"<<endl; 135 cin>>k; 136 if(k<5 || k>n*m-5) { 137 cout<<"您输入的数太大或太小了!重新输入"; 138 Sleep(1200); 139 } else 140 break; 141 } 142 system("cls"); 143 for(int i=1; i<=3; i++) { 144 cout<<"正在初始化."; 145 Sleep(280); 146 cout<<"."; 147 Sleep(280); 148 cout<<"."; 149 Sleep(280); 150 system("cls"); 151 } 152 srand((unsigned)time(NULL)); 153 for(int i=1; i<=k; i++) { 154 int x=rand()%n+1,y=rand()%m+1; 155 if(mp[x][y]==-1) { 156 i--; 157 continue; 158 } else 159 mp[x][y]=-1; 160 } 161 for(int i=1; i<=n; i++) 162 for(int j=1; j<=m; j++) { 163 if(mp[i][j]==-1) 164 continue; 165 for(int k=0; k<8; k++) 166 if(mp[i+p[k][0]][j+p[k][1]]==-1) 167 mp[i][j]++; 168 } 169 char s[21][21]; 170 for(int i=1; i<=20; i++) 171 for(int j=1; j<=20; j++) { 172 s[i][j]=' '; 173 xmp[i][j]=22; 174 } 175 int x=1,y=1,sum=0; 176 bool o=0,f=0,fir=0; 177 while(1) { 178 sum=0; 179 system("cls"); 180 s[x][y]='<'; 181 for(int i=1; i<=n; i++) { 182 for(int j=1; j<=m; j++) { 183 if(xmp[i][j]==127) 184 sum++; 185 cout<<xmp[i][j]<<s[i][j]; 186 } 187 cout<<endl<<endl; 188 } 189 cout<<"剩余雷数: "<<k-sum; 190 if(o) { 191 Sleep(2000); 192 bai(x,y); 193 system("cls"); 194 cout<<"游戏失败!"; 195 Sleep(2500); 196 system("cls"); 197 return; 198 } 199 c=getch(); 200 switch(c) { 201 case 'w': { 202 s[x][y]=' '; 203 if(--x<1) 204 x=n; 205 break; 206 } 207 case 's': { 208 s[x][y]=' '; 209 if(++x>n) 210 x=1; 211 break; 212 } 213 case 'a': { 214 s[x][y]=' '; 215 if(--y<1) 216 y=m; 217 break; 218 } 219 case 'd': { 220 s[x][y]=' '; 221 if(++y>m) 222 y=1; 223 break; 224 } 225 case 'j': { 226 if(!fir) { 227 t1=time(0); 228 fir=1; 229 } 230 if(mp[x][y]==-1) { 231 if(!f) { 232 f=1; 233 cout<<endl<<"您的rp太低,这是个雷!(仅提醒一次)"; 234 Sleep(2000); 235 } else { 236 xmp[x][y]='*'; 237 o=1; 238 } 239 } 240 if(mp[x][y]==0) { 241 xmp[x][y]='0'; 242 dfs0(x,y); 243 } 244 if(mp[x][y]>0) 245 xmp[x][y]=mp[x][y]+'0'; 246 bool bo=0; 247 for(int i=1; i<=n; i++) { 248 for(int j=1; j<=m; j++) { 249 if(mp[i][j]==-1) 250 continue; 251 if(xmp[i][j]!=mp[i][j]+'0') 252 bo=1; 253 } 254 } 255 if(bo) 256 break; 257 win(); 258 return; 259 } 260 case 'k': { 261 if(xmp[x][y]==22) { 262 xmp[x][y]=127; 263 break; 264 } 265 if(xmp[x][y]==127) { 266 xmp[x][y]='?'; 267 break; 268 } 269 xmp[x][y]=22; 270 break; 271 } 272 } 273 } 274 } 275 int main() { 276 HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE); 277 CONSOLE_CURSOR_INFO CursorInfo; 278 GetConsoleCursorInfo(handle,&CursorInfo);//获取控制台光标信息 279 CursorInfo.bVisible = false; //隐藏控制台光标 280 SetConsoleCursorInfo(handle, &CursorInfo);//设置控制台光标状态 281 282 cout<<" ----欢迎您来到扫雷----"; 283 Sleep(2500); 284 system("cls"); 285 for(int i=1; i<=2; i++) { 286 cout<<"加载中."; 287 Sleep(300); 288 cout<<"."; 289 Sleep(300); 290 cout<<"."; 291 Sleep(300); 292 system("cls"); 293 } 294 string s[4]= {" "," "," "," "}; 295 int now=0; 296 while(1) { 297 s[now]='<'; 298 cout<<"自定义游戏 "<<s[0]<<endl; 299 cout<<"关于作者 "<<s[1]<<endl; 300 cout<<"帮助 "<<s[2]<<endl; 301 cout<<"退出 "<<s[3]<<endl<<endl; 302 cout<<"ws上下控制,j确定"; 303 c=getch(); 304 system("cls"); 305 switch(c) { 306 case 'w': { 307 s[now]=' '; 308 if(--now<0) 309 now=3; 310 break; 311 } 312 case 's': { 313 s[now]=' '; 314 if(++now>3) 315 now=0; 316 break; 317 } 318 case 'j': { 319 if(now==0) 320 game(); 321 if(now==1) { 322 system("cls"); 323 cout<<"作者"<<endl<<"gqz"<<endl; 324 cout<<"洛谷/steam ID: s5_gan"<<endl; 325 cout<<"qq: 540568193"<<endl; 326 cout<<"微信: s540568193"<<endl<<endl<<endl; 327 cout<<"调试及bug修复"<<endl<<"zcx"<<endl; 328 cout<<"洛谷 ID: zcxqiangwudi"<<endl; 329 cout<<"qq: 2631750980"<<endl<<endl<<endl; 330 system("pause"); 331 system("cls"); 332 } 333 if(now==2) { 334 system("cls"); 335 cout<<"wasd控制选中的单元格"<<endl; 336 cout<<"j翻开单元格,k标记单元格"<<endl; 337 cout<<"第一次翻到雷会给出一次提醒"<<endl<<endl; 338 cout<<"如果您运气实在太差"<<endl; 339 cout<<"翻出的是无法判断雷的图"<<endl; 340 cout<<"接下来就看脸 ^_^"<<endl<<endl; 341 system("pause"); 342 system("cls"); 343 } 344 if (now==3) 345 return 0; 346 break; 347 } 348 } 349 } 350 }