⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 minesweeping.cpp

📁 简单的扫雷,用于VC6.0编译的
💻 CPP
字号:
/*20:20 2008-12-4 Hadrian
**play mine sweeping like the computer's
************/

#include<iostream.h>
#include<stdlib.h>
#include<time.h>
#include<iomanip.h>
#include<conio.h>

void defineMine(int a[][10],int &);
void play(int a[][10],int );
void print(int b[][10],int c[][10],int ,int );

void main(int [][10]){
	int a[10][10]={0},x=0;
    defineMine(a,x);//布置雷阵
    play(a,x);//开始游戏
}


void defineMine(int a[][10],int &x){
	char A;
	cout<<"If you want to define the number of the mines?Input Y or N."<<endl;
	cin>>A;//选择自己定义雷数还是系统定义
	if(A=='N'){
		char d;
	    cout<<"Chose difficulty including easy,medium and hard."<<endl;
	    cout<<"easy input E,medium input M,hard input H."<<endl<<"Difficulty:";
	    cin>>d;
		if(d=='E') x=10;
	    if(d=='M') x=5;
		if(d=='H') x=3;
		if(x==0) {cout<<"error!";return;}//系统控制雷数
	    x=100/x;
	}
	else{
		cout<<"The number of mines:";
		cin>>x;
	}
	int y,k=0;
	y=100/x+1;
	srand(time(0));
	for(int n=0;n<x;)
	    for(int i=0;i<10&&n<x;i++)
	        for(int j=0;j<10&&n<x;j++)
			    if(a[i][j]!=1){
			     	a[i][j]=rand()%(y);
		    		if(a[i][j]!=1) a[i][j]=0;
			    	   else n++;//a[i][j]==1 表示雷
				}  
}

void play(int a[][10],int x){
	int m,n,b[10][10]={0},c[10][10]={0},d[10][10]={0};
	char I;
	for(m=1;m<=10;m++)
		for(n=1;n<=10;n++){
			if(m>1&&n>1&&m<10&&n<10) b[m-1][n-1]=a[m-2][n-2]+a[m-2][n-1]+a[m-2][n]+a[m-1][n-2]+a[m-1][n]+a[m][n-2]+a[m][n-1]+a[m][n];
	        if(m==1&&n>1&&n<10) b[m-1][n-1]=a[m-1][n-2]+a[m-1][n]+a[m][n-2]+a[m][n-1]+a[m][n];
	    	if(m>1&&n==1&&m<10) b[m-1][n-1]=a[m-2][n-1]+a[m-2][n]+a[m-1][n]+a[m][n-1]+a[m][n];
	    	if(n>1&&m==10&&n<10) b[m-1][n-1]=a[m-2][n-2]+a[m-2][n-1]+a[m-2][n]+a[m-1][n-2]+a[m-1][n];
    		if(m>1&&m<10&&n==10) b[m-1][n-1]=a[m-2][n-2]+a[m-2][n-1]+a[m-1][n-2]+a[m][n-2]+a[m][n-1];
    		if(m==1&&n==1) b[m-1][n-1]=a[m-1][n]+a[m][n-1]+a[m][n];
	    	if(m==10&&n==10) b[m-1][n-1]=a[m-2][n-2]+a[m-2][n-1]+a[m-1][n-2];
    		if(m==1&&n==10) b[m-1][n-1]=a[m-1][n-2]+a[m][n-2]+a[m][n-1];
    		if(n==1&&m==10) b[m-1][n-1]=a[m-2][n-1]+a[m-2][n]+a[m-1][n];
		}
	for(int o;o<100-x;){
		o=0;
		cout<<"Which line do you want to dig?From 1 to 10."<<endl<<"line:";
	    cin>>m;
	    cout<<"Which row do you want to dig?from 1 to 10."<<endl<<"row:";
	    cin>>n;//选择挖地雷的地方
		system("cls");
		if(a[m-1][n-1]!=1){
			if(b[m-1][n-1]==0) print(b,c,m,n);
			else c[m-1][n-1]=9;
			for(int i=0;i<10;i++){
		        for(int j=0;j<10;j++){
	                if(c[i][j]==9) cout<<b[i][j]<<setw(3),o++;
			         else  cout<<"~"<<setw(3);	
				}
				cout<<endl;	
			}
			cout<<o<<endl;
			if(o>=100-x){
				cout<<"Great!You are win!"<<endl;
				for(int i=0;i<10;i++){
		            for(int j=0;j<10;j++){
			            if(a[i][j]==0) cout<<b[i][j]<<setw(3);
			            else cout<<"*"<<setw(3);
					}
					cout<<endl;
				}
			}
		}
        else {
			o=100-x;
			cout<<"Bomb!"<<endl<<"You are lost."<<endl;//输了
			for(int i=0;i<10;i++){
		        for(int j=0;j<10;j++){
			         if(a[i][j]==0) cout<<b[i][j]<<setw(3);
			        else cout<<"*"<<setw(3);
				}
                cout<<endl;
			}
		}
	}			
	cout<<"If you want to play again?Input Y or N."<<endl;
	cin>>I;
	if(I=='Y')  main(a);//是否继续玩?
	else return;
}

void print(int b[][10],int c[][10],int x,int y){
	c[x-1][y-1]=9;
	      for(int i=x-2;i<x+1;i++)
		     for(int j=y-2;j<y+1;j++){
				 if(i>=0&&i<10&&j>=0&&j<10){  
					 if(c[i][j]==9) {
						 continue;
					 }
				    if(b[i][j]==0) print(b,c,i+1,j+1),c[i][j]=9;
				    else  c[i][j]=9;
				 }
			 }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -