C++扫雷

转自http://zhidao.baidu.com/question/1495186785698581099.html

#include <iostream>
#include<fstream>
#include <ctime>
#include <cmath>
#include <stdlib.h>
#include<stdio.h> //时间 //文件
#include <string>
#define random(x)(rand()%x)
using namespace std;
void thunder(int Dif,int Row,int Column,char *USer)
{
	int r,c,alls[22][22],backstage[22][22]={0};
	srand((int)time(0));
	for(r=1;r<=Row;r++)                                      // 生成alls(0~1)1是雷
	{
		for(c=1;c<=Column;c++)
		{
			if(random(6)<1)  {alls[r][c]=1;} else{alls[r][c]=0;};
		}
	};
	for(r=0;r<=Row+1;r++)                                //生成 backstage(正确答案)
	{
		for(int c=0;c<=Column+1;c++)
		{
			if(alls[r][c]==1)  
			{
				(int)backstage[r][c]='*';             //将1变为 *  代表雷
			}
			else
			{
				for(int i=r-1;i<=r+1;i++)             //将0变为数字 (代表周围雷数)
					for(int j=c-1;j<=c+1;j++)
					{
						
						if(alls[i][j]!=alls[r][c]&&alls[i][j]==1){backstage[r][c]++;};
					}
			};  //else 结束 
		};    // for 结束
	};          // for 结束 
	cout<<"======================*********================================"<<endl;
	char surface[22][22];              //生成surface(用户界面)
	for(r=0;r<22;r++)                  //全部为零
		for(c=0;c<22;c++)
		{
			surface[r][c]='0';
		}
	for(r=1;r<=Row;r++)                 //中间化 #   形成0包围#的形式  (通过数  #-->(*||数字) 的个数  赢的时候停止循环)
		for(c=1;c<=Column;c++)
		{
			surface[r][c]='#';
		}
	for(r=1;r<=Row;r++)                      //输出  surface   界面  便于检查
	{
		for(c=1;c<=Column;c++) {cout<<"  "<<surface[r][c];}; 
		cout<<endl;
	};
	cout<<"请按格式输入"<<endl
		<<"前两个数字为坐标,最后一个数字“1”表示此位置为雷,“0”则表示不是。"<<endl
		<<"如:1 3 1  表示一行三列是雷;2 4 0 表示二行四列不是雷"<<endl
		<<"提示:当数字周围雷都被扫出时,可再次按要求输入此位置,可得到周围数字。"<<endl;
	long  i=10000000L;         //计算时间开始
	clock_t start,finish;
	double duration;
	start=clock();             
	while(i--);                //计算时间开始
	int num=Row*Column;        //计算#号个数
	while(num!=0)              //控制 是否点完所有位置
	{
		int x,y,judge;
	   cin>>x>>y>>judge; 
	   if(alls[x][y]!=judge)
		{
			cout<<"you lose!!!"<<endl;
			cout<<"The answer is:"<<endl;
			for(r=1;r<=Row;r++)                    //输了   输出backstage  显示正确答案
				{
		          for(int c=1;c<=Column;c++)
				  {
			         cout<<"  "<<(char)(backstage[r][c]==42?backstage[r][c]:backstage[r][c]+'0');  //输出backstage
				  }
		          cout<<endl;
				}
			break;
		}
	   else                                      
		{
			if(alls[x][y]==1)  {if(surface[x][y]=='#'){num--;}surface[x][y]='@'; }      // 雷 判断正确 显示“@”;数“#”
			else
			{
				if(backstage[x][y]!=0)                                                  //  数字 判断正确 显示数字
				{
					if(surface[x][y]=='#'){num--; surface[x][y]=backstage[x][y]+'0'; }  //   数“#”
				   else
					{
						int lei_num=0;
				       for(int i=x-1;i<=x+1;i++)                                         //数 数字周围 雷的个数                                     
							for(int j=y-1;j<=y+1;j++)
							{
								if(surface[i][j]=='@')
							   lei_num++;
							}
					   if(backstage[x][y]==lei_num)                                   // 看数字周围雷是否全部扫出  提示 显示数字周围
						{
							for(int i=x-1;i<=x+1;i++)
								for(int j=y-1;j<=y+1;j++)
										if(surface[i][j]=='#')                         //数“#”
										{
											surface[i][j]=backstage[i][j]+'0';
											num--;   
										}
						}
					}
				}
				else                                                                   // 数字为零时   显示零周围的零
				{
					if(surface[x][y]=='#'){num--;};                                    //数“#”
				   surface[x][y]=backstage[x][y]+'0';
					for(int i=x-1;i<=x+1;i++)                                          //  显示零周围的数字
						for(int j=y-1;j<=y+1;j++)
							if(surface[i][j]=='#')                                     // 避免 死循环
							{
								surface[i][j]=backstage[i][j]+'0';               
								num--;  				                               //数“#”
							}
							for(int k=0;k<20;k++)                                       //最多20层零 (点出最边上的零)
							{
								for (int R=1;R<=Row;R++)                                //检查所有零
									for(int C=1;C<=Column;C++)                          //再次显示零周围的数字
									{
										if(surface[R][C]=='0')
										{
											for(int i=R-1;i<=R+1;i++)
												for(int j=C-1;j<=C+1;j++)
												{
													if(surface[i][j]=='#')                         // 避免 死循环 数“#”
													{
														surface[i][j]=backstage[i][j]+'0';
														num--;
													}  
												}
										}
									} //匹配for 内 
							} //匹配 for 外
				}//匹配else
			}//匹配else
		}//匹配els
		cout<<endl;
		cout<<"======================*********================================"<<endl;
		for(r=1;r<=Row;r++)                                                                          //输出界面(已修改)
		{
			for(c=1;c<=Column;c++) {cout<<"  "<<surface[r][c];};
			cout<<endl;
		};
	}                                                                                               //匹配while
	finish=clock();                                                                                //计算时间结束
    duration=(double)(finish-start)/CLOCKS_PER_SEC;                                                //时间变量
    if(num==0)                                                                                      //所有
	{
		cout<<"              You win!  Congratulations!!                 "<<endl;
        cout<<"                Your time is: "<<duration<<endl;
		if(Dif==1)                                                                            //读取 简单扫雷 的存储文件
		{
			string Name;
	       string name[6];
	       double Time,rang;
	       double times[6];
	       int i=0;
	       ifstream inf("扫雷 简单.txt");
	       for(i=0;i<5;i++)                                                                    //文件中信息导入到数组里
			{
				inf>>Name;inf>>rang>>Time;
		       name[i]=Name;
		       times[i]=Time;
			}
			inf.close();
			name[5]=USer;                                                                   //本轮玩家信息
			times[5]=duration;
	       double t1=0;
	       string t2;
            for(int j=0;j<5;j++)                                                               //冒泡排序法
			{                    
				for(i=0;i<5-j;i++)
				{
					if(times[i]>times[i+1])
					{
						t1=times[i];
				       times[i]=times[i+1];
				       times[i+1]=t1;
				       t2=name[i];
				       name[i]=name[i+1];
				       name[i+1]=t2;
					}
				}
			}
			ofstream outf("扫雷 简单.txt");
	       for(i=0;i<5;i++)                                                                   //将前五名玩家信息存储到文件中
			{
				cout<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
		       outf<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
			}
	       outf.close();
		}
        if(Dif==2)                                                                            //读取 一般扫雷 的存储文件
		{
			string Name;
	       string name[6];
	       double Time,rang;
	       double times[6];
	       int i=0;
	       ifstream inf("扫雷 一般.txt");
	       for(i=0;i<5;i++)                                                                    //文件中信息导入到数组里
			{
				inf>>Name;inf>>rang>>Time;
		       name[i]=Name;
		       times[i]=Time;
			}
			inf.close();
			name[5]=USer;                                                                   //本轮玩家信息
			times[5]=duration;
	       double t1=0;
	       string t2;
            for(int j=0;j<5;j++)                                                               //冒泡排序法
			{                    
				for(i=0;i<5-j;i++)
				{
					if(times[i]>times[i+1])
					{
						t1=times[i];
				       times[i]=times[i+1];
				       times[i+1]=t1;
				       t2=name[i];
				       name[i]=name[i+1];
				       name[i+1]=t2;
					}
				}
			}
			ofstream outf("扫雷 一般.txt");
	       for(i=0;i<5;i++)                                                                   //将前五名玩家信息存储到文件中 并输出
			{
				cout<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
		       outf<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
			}
	       outf.close();
		}
		if(Dif==3)                                                                            //读取 困难扫雷 的存储文件
		{
			string Name;
	       string name[6];
	       double Time,rang;
	       double times[6];
	       int i=0;
	       ifstream inf("扫雷 困难.txt");
	       for(i=0;i<5;i++)                                                                    //文件中信息导入到数组里
			{
				inf>>Name;inf>>rang>>Time;
		       name[i]=Name;
		       times[i]=Time;
			}
			inf.close();
			name[5]=USer;                                                                   //本轮玩家信息
			times[5]=duration;
	       double t1=0;
	       string t2;
            for(int j=0;j<5;j++)                                                               //冒泡排序法
			{                    
				for(i=0;i<5-j;i++)
				{
					if(times[i]>times[i+1])
					{
						t1=times[i];
				       times[i]=times[i+1];
				       times[i+1]=t1;
				       t2=name[i];
				       name[i]=name[i+1];
				       name[i+1]=t2;
					}
				}
			}
			ofstream outf("扫雷 困难.txt");
	       for(i=0;i<5;i++)                                                                   //将前五名玩家信息存储到文件中
			{
				cout<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
		       outf<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
			}
	       outf.close();
		}
    }
}
void scale(int dif,char *User)    //选择难度
{
	int row,column;
	if(dif==1) {row=3;column=3;}
	if(dif==2) {row=7;column=7;}
	if(dif==3)  {row=10;column=10;}
	cout<<"The scale is: "<<row<<"*"<<column<<endl;
	thunder(dif,row,column,User);
};
int main()
{	
	int Continue=1;
	int difficulty;
	char user[10];
	cout<<"                       Welcom to the game!                  "<<endl
		<<"                         请输入用户名!                      "<<endl;
	cin>>user;
	while(Continue==1)
	{
	   cout<<"=======================*******************======================="<<endl
		   <<"                          请选择难度!                        "<<endl
		   <<"                          简单——1                           "<<endl
		   <<"                          一般——2                           "<<endl
		   <<"                          困难——3                           "<<endl;
	   cin>>difficulty;
	   scale(difficulty,user);
		cout<<"继续游戏——1     结束游戏——0"<<endl;
		cin>>Continue;
	}
	return 0;
}


下面是一个简单的 C++ 扫雷游戏教程,希望能帮到你: 首先,我们需要定义一个二维数组来表示扫雷棋盘,使用 0 表示空白,使用 -1 表示雷,使用其他数字表示周围雷的数量。 ```cpp const int MAXN = 105; int n, m; // 行数和列数 int a[MAXN][MAXN]; // 扫雷棋盘 ``` 接下来,我们需要生成雷的位置。可以使用 `rand()` 函数生成随机数,如果生成的随机数小于某个概率阈值,就将该格子设置为雷。 ```cpp const int P = 10; // 雷的概率为 10% void generateMines() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (rand() % 100 < P) a[i][j] = -1; } } } ``` 接着,我们需要计算每个格子周围的雷的数量。遍历整个棋盘,对于每个不是雷的格子,统计周围 8 个格子中的雷的数量,并将该数量保存在该格子的值中。 ```cpp void countMines() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] != -1) { int cnt = 0; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (dx == 0 && dy == 0) continue; // 当前格子不算 int x = i + dx, y = j + dy; if (x < 1 || x > n || y < 1 || y > m) continue; // 越界不算 if (a[x][y] == -1) cnt++; } } a[i][j] = cnt; } } } } ``` 现在,我们可以开始实现游戏的主循环了。每次循环,先输出当前棋盘的状态,然后让玩家输入一个坐标,根据该坐标的情况进行处理。如果该格子是雷,游戏结束;如果该格子是空白,扩展该格子周围的空白格子;如果该格子是数字,直接显示该数字。 ```cpp void printBoard(bool showMines = false) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (showMines && a[i][j] == -1) cout << '*'; else if (a[i][j] == 0) cout << '.'; else if (a[i][j] == -1) cout << '.'; else cout << a[i][j]; } cout << endl; } } bool isWin() { // 判断是否胜利 for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] >= 0 && a[i][j] <= 8 && !vis[i][j]) { return false; } } } return true; } void play() { memset(vis, 0, sizeof(vis)); int remain = n * m; while (true) { printBoard(); int x, y; cout << "Please enter the coordinate (x, y): "; cin >> x >> y; if (vis[x][y]) { cout << "This cell has already been uncovered." << endl; continue; } remain--; if (a[x][y] == -1) { // 点到雷了 printBoard(true); cout << "Game over!" << endl; break; } else if (a[x][y] == 0) { // 点到空白 queue<pair<int, int>> q; q.push(make_pair(x, y)); vis[x][y] = true; while (!q.empty()) { auto p = q.front(); q.pop(); int x = p.first, y = p.second; for (int dx = -1; dx <= 1; dx++) { for (int dy = -1; dy <= 1; dy++) { if (dx == 0 && dy == 0) continue; int nx = x + dx, ny = y + dy; if (nx < 1 || nx > n || ny < 1 || ny > m) continue; if (vis[nx][ny]) continue; vis[nx][ny] = true; remain--; if (a[nx][ny] == 0) { q.push(make_pair(nx, ny)); } } } } } else { // 点到数字 vis[x][y] = true; if (remain == 10) { // 剩余 10 个格子时,自动打开所有非雷格子并胜利 for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (a[i][j] != -1) { vis[i][j] = true; } } } printBoard(); cout << "You win!" << endl; break; } } if (isWin()) { printBoard(); cout << "You win!" << endl; break; } } } ``` 完整代码如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值