- #include <iostream>
- #include <ctime>
- #include <cstdlib>
- using namespace std;
- short boom_map[9][9];
- void setRand(short n,short max); //设置随机地图
- void getRand(short n,short max); //输出随机地图
- void resetMap(short n);//重置地图
- void setRand(short n,short max)
- {
- srand((unsigned)time(NULL));
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<n;++j)
- {
- boom_map[i][j]=rand()%max;
- }
- }
- }
- void resetMap(short n)
- {
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<n;++j)
- {
- int aim=9; //不设置为0 0代表地雷
- while(boom_map[i][j]!=0)//求周围地雷
- {
- for(int m=-1;m<2;++m)
- {
- for(int n=-1;n<2;++n)
- {
- if(((i+m>=0)&&(i+m<9))&&((j+n>=0)&&(j+n<9)))
- {
- if(boom_map[i+m][j+n]==0)
- ++aim;
- }
- }
- }//已经得出该点周围的地雷aim-9
- boom_map[i][j]=(aim==9)?9:aim-9;
- break;
- }
- }
- }
- }
- void getRand(short n,short max)
- {
- for(int i=0;i<n;i++)
- {
- for(int j=0;j<n;++j)
- {
- cout<<boom_map[i][j]<<" ";
- if(j==8)
- cout<<endl;
- }
- }
- }
- int main()
- {
- cout<<"随机出来的地图:"<<endl;
- setRand(9,9);//9是矩阵维数 9是最大随机数
- getRand(9,9);
- cout<<"//////////////////"<<endl;
- cout<<"计算之后的地图:"<<endl;
- resetMap(9);
- getRand(9,9);
- return 0;
- }
- //该片段来自于http://www.codesnippet.cn/detail/281120137570.html
来源: http://www.codesnippet.cn/detail/281120137570.html