自编《扫雷》。
很刺激!
看代码:
#include<iostream>
#include<windows.h>
#include<stdlib.h>
#include<time.h>
#include<cmath>
#define maxn 11
using namespace std;
char a[maxn][maxn];
int b[maxn][maxn],m,n,k,c[maxn][maxn],num;
bool life=true;
void number(){
int i,j;
k=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if(b[i][j]==c[i][j])
k++;
}
void number1(){
int i,j;
num=0;
for(i=1;i<=m;i++)
for(j=1;j<=n;j++)
if(a[i][j]=='*')
num++;
}
void around(int x,int y){
int t=0;
if(a[x-1][y]=='*') t++;
if(a[x+1][y]=='*') t++;
if(a[x][y-1]=='*') t++;
if(a[x][y+1]=='*') t++;
if(a[x-1][y-1]=='*') t++;
if(a[x-1][y+1]=='*') t++;
if(a[x+1][y-1]=='*') t++;
if(a[x+1][y+1]=='*') t++;
b[x][y]=t;
c[x][y]=t;
}
void color(int a){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void doing(int x,int y){
system("cls");
int i,j;
if(x<=m&&y<=n&&b[x][y]!=c[x][y]){
if(a[x][y]=='*'){
life=false;
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
if(a[i][j]=='*'){
color(12);
cout<<'*';
}
else{
color(15);
cout<<'#';
}
}
cout<<endl;
}
color(11);
cout<<"你over了!";
color(15);
}
else{
around(x,y);
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
if(((i==x-1&&j==y)||(i==x+1&&j==y)||(i==x&&j==y-1)||(i==x&&j==y+1))&&a[i][j]!='*'){
around(i,j);
color(10);
cout<<b[i][j];
}
else if(b[i][j]==c[i][j]){
color(10);
cout<<b[i][j];
}
else{
color(15);
cout<<'#';
}
}
cout<<endl;
}
}
}
else{
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
if(((i==x-1&&j==y)||(i==x+1&&j==y)||(i==x&&j==y-1)||(i==x&&j==y+1))&&a[i][j]!='*'){
around(i,j);
color(10);
cout<<b[i][j];
}
else if(b[i][j]==c[i][j]){
color(10);
cout<<b[i][j];
}
else{
color(15);
cout<<'#';
}
}
cout<<endl;
}
}
}
int main(){
int x,y,i,j,x1,y1;
for(i=1;i<=maxn;i++)
for(j=1;j<=maxn;j++)
c[i][j]=1;
cout<<"请输入雷阵行数和列数:";
cin>>m>>n;
if(m>=maxn) m=maxn-1;
if(n>=maxn) n=maxn-1;
srand(time(0));
for(i=1;i<=m*n/3;i++){
x1=rand()%m+1;
y1=rand()%n+1;
a[x1][y1]='*';
}
for(i=1;i<=m;i++){
for(j=1;j<=n;j++){
color(15);
cout<<'#';
}
cout<<endl;
}
while(life==true){
number();
number1();
if(k==m*n-num){
color(11);
cout<<"你赢了!";
color(15);
return 0;
}
color(15);
cout<<"提示:翻开的格子的上下左右四个格子中没有地雷的格子会翻开。"<<endl;
cout<<"请输入你要翻开的格子的行列位置:";
cin>>x>>y;
doing(x,y);
}
return 0;
} 
京公网安备 11010502036488号