码迷,mamicode.com
首页 > 编程语言 > 详细

扫雷C++实现

时间:2017-03-20 22:10:08      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:under   surf   导入   文件   ==   tin   style   clock   cin   

  1 #include <iostream>
  2 #include<fstream>
  3 #include <ctime>
  4 #include <cmath>
  5 #include <stdlib.h>
  6 #include<stdio.h> //时间 //文件
  7 #include <string>
  8 #define random(x)(rand()%x)
  9 using namespace std;
 10 void thunder(int Dif,int Row,int Column,char *USer)
 11 {
 12     int r,c,alls[22][22],backstage[22][22]={0};
 13     srand((int)time(0));
 14     for(r=1;r<=Row;r++)                                      // 生成alls(0~1)1是雷
 15     {
 16         for(c=1;c<=Column;c++)
 17         {
 18             if(random(6)<1)  {alls[r][c]=1;} else{alls[r][c]=0;};
 19         }
 20     };
 21     for(r=0;r<=Row+1;r++)                                //生成 backstage(正确答案)
 22     {
 23         for(int c=0;c<=Column+1;c++)
 24         {
 25             if(alls[r][c]==1)  
 26             {
 27                 backstage[r][c]=*;             //将1变为 *  代表雷
 28             }
 29             else
 30             {
 31                 for(int i=r-1;i<=r+1;i++)             //将0变为数字 (代表周围雷数)
 32                     for(int j=c-1;j<=c+1;j++)
 33                     {
 34                         
 35                         if(alls[i][j]!=alls[r][c]&&alls[i][j]==1){backstage[r][c]++;};
 36                     }
 37             };  //else 结束
 38         };    // for 结束
 39     };          // for 结束
 40     cout<<"======================*********================================"<<endl;
 41     char surface[22][22];              //生成surface(用户界面)
 42     for(r=0;r<22;r++)                  //全部为零
 43         for(c=0;c<22;c++)
 44         {
 45             surface[r][c]=0;
 46         }
 47     for(r=1;r<=Row;r++)                 //中间化 #   形成0包围#的形式  (通过数  #-->(*||数字) 的个数  赢的时候停止循环)
 48         for(c=1;c<=Column;c++)
 49         {
 50             surface[r][c]=#;
 51         }
 52     for(r=1;r<=Row;r++)                      //输出  surface   界面  便于检查
 53     {
 54         for(c=1;c<=Column;c++) {cout<<"  "<<surface[r][c];};
 55         cout<<endl;
 56     };
 57     cout<<"请按格式输入"<<endl
 58         <<"前两个数字为坐标,最后一个数字“1”表示此位置为雷,“0”则表示不是。"<<endl
 59         <<"如:1 3 1  表示一行三列是雷;2 4 0 表示二行四列不是雷"<<endl
 60         <<"提示:当数字周围雷都被扫出时,可再次按要求输入此位置,可得到周围数字。"<<endl;
 61     long  i=10000000L;         //计算时间开始
 62     clock_t start,finish;
 63     double duration;
 64     start=clock();            
 65     while(i--);                //计算时间开始
 66     int num=Row*Column;        //计算#号个数
 67     while(num!=0)              //控制 是否点完所有位置
 68     {
 69         int x,y,judge;
 70        cin>>x>>y>>judge;
 71        if(alls[x][y]!=judge)
 72         {
 73             cout<<"you lose!!!"<<endl;
 74             cout<<"The answer is:"<<endl;
 75             for(r=1;r<=Row;r++)                    //输了   输出backstage  显示正确答案
 76                 {
 77                   for(int c=1;c<=Column;c++)
 78                   {
 79                      cout<<"  "<<(char)(backstage[r][c]==42?backstage[r][c]:backstage[r][c]+0);  //输出backstage
 80                   }
 81                   cout<<endl;
 82                 }
 83             break;
 84         }
 85        else                                      
 86         {
 87             if(alls[x][y]==1)  {if(surface[x][y]==#){num--;}surface[x][y]=@; }      // 雷 判断正确 显示“@”;数“#”
 88             else
 89             {
 90                 if(backstage[x][y]!=0)                                                  //  数字 判断正确 显示数字
 91                 {
 92                     if(surface[x][y]==#){num--; surface[x][y]=backstage[x][y]+0; }  //   数“#”
 93                    else
 94                     {
 95                         int lei_num=0;
 96                        for(int i=x-1;i<=x+1;i++)                                         //数 数字周围 雷的个数                                    
 97                             for(int j=y-1;j<=y+1;j++)
 98                             {
 99                                 if(surface[i][j]==@)
100                                lei_num++;
101                             }
102                        if(backstage[x][y]==lei_num)                                   // 看数字周围雷是否全部扫出  提示 显示数字周围
103                         {
104                             for(int i=x-1;i<=x+1;i++)
105                                 for(int j=y-1;j<=y+1;j++)
106                                         if(surface[i][j]==#)                         //数“#”
107                                         {
108                                             surface[i][j]=backstage[i][j]+0;
109                                             num--;  
110                                         }
111                         }
112                     }
113                 }
114                 else                                                                   // 数字为零时   显示零周围的零
115                 {
116                     if(surface[x][y]==#){num--;};                                    //数“#”
117                    surface[x][y]=backstage[x][y]+0;
118                     for(int i=x-1;i<=x+1;i++)                                          //  显示零周围的数字
119                         for(int j=y-1;j<=y+1;j++)
120                             if(surface[i][j]==#)                                     // 避免 死循环
121                             {
122                                 surface[i][j]=backstage[i][j]+0;              
123                                 num--;                                                 //数“#”
124                             }
125                             for(int k=0;k<20;k++)                                       //最多20层零 (点出最边上的零)
126                             {
127                                 for (int R=1;R<=Row;R++)                                //检查所有零
128                                     for(int C=1;C<=Column;C++)                          //再次显示零周围的数字
129                                     {
130                                         if(surface[R][C]==0)
131                                         {
132                                             for(int i=R-1;i<=R+1;i++)
133                                                 for(int j=C-1;j<=C+1;j++)
134                                                 {
135                                                     if(surface[i][j]==#)                         // 避免 死循环 数“#”
136                                                     {
137                                                         surface[i][j]=backstage[i][j]+0;
138                                                         num--;
139                                                     }  
140                                                 }
141                                         }
142                                     } //匹配for 内
143                             } //匹配 for 外
144                 }//匹配else
145             }//匹配else
146         }//匹配els
147         cout<<endl;
148         cout<<"======================*********================================"<<endl;
149         for(r=1;r<=Row;r++)                                                                          //输出界面(已修改)
150         {
151             for(c=1;c<=Column;c++) {cout<<"  "<<surface[r][c];};
152             cout<<endl;
153         };
154     }                                                                                               //匹配while
155     finish=clock();                                                                                //计算时间结束
156     duration=(double)(finish-start)/CLOCKS_PER_SEC;                                                //时间变量
157     if(num==0)                                                                                      //所有
158     {
159         cout<<"              You win!  Congratulations!!                 "<<endl;
160         cout<<"                Your time is: "<<duration<<endl;
161         if(Dif==1)                                                                            //读取 简单扫雷 的存储文件
162         {
163             string Name;
164            string name[6];
165            double Time,rang;
166            double times[6];
167            int i=0;
168            ifstream inf("扫雷 简单.txt");
169            for(i=0;i<5;i++)                                                                    //文件中信息导入到数组里
170             {
171                 inf>>Name;inf>>rang>>Time;
172                name[i]=Name;
173                times[i]=Time;
174             }
175             inf.close();
176             name[5]=USer;                                                                   //本轮玩家信息
177             times[5]=duration;
178            double t1=0;
179            string t2;
180             for(int j=0;j<5;j++)                                                               //冒泡排序法
181             {                    
182                 for(i=0;i<5-j;i++)
183                 {
184                     if(times[i]>times[i+1])
185                     {
186                         t1=times[i];
187                        times[i]=times[i+1];
188                        times[i+1]=t1;
189                        t2=name[i];
190                        name[i]=name[i+1];
191                        name[i+1]=t2;
192                     }
193                 }
194             }
195             ofstream outf("扫雷 简单.txt");
196            for(i=0;i<5;i++)                                                                   //将前五名玩家信息存储到文件中
197             {
198                 cout<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
199                outf<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
200             }
201            outf.close();
202         }
203         if(Dif==2)                                                                            //读取 一般扫雷 的存储文件
204         {
205             string Name;
206            string name[6];
207            double Time,rang;
208            double times[6];
209            int i=0;
210            ifstream inf("扫雷 一般.txt");
211            for(i=0;i<5;i++)                                                                    //文件中信息导入到数组里
212             {
213                 inf>>Name;inf>>rang>>Time;
214                name[i]=Name;
215                times[i]=Time;
216             }
217             inf.close();
218             name[5]=USer;                                                                   //本轮玩家信息
219             times[5]=duration;
220            double t1=0;
221            string t2;
222             for(int j=0;j<5;j++)                                                               //冒泡排序法
223             {                    
224                 for(i=0;i<5-j;i++)
225                 {
226                     if(times[i]>times[i+1])
227                     {
228                         t1=times[i];
229                        times[i]=times[i+1];
230                        times[i+1]=t1;
231                        t2=name[i];
232                        name[i]=name[i+1];
233                        name[i+1]=t2;
234                     }
235                 }
236             }
237             ofstream outf("扫雷 一般.txt");
238            for(i=0;i<5;i++)                                                                   //将前五名玩家信息存储到文件中 并输出
239             {
240                 cout<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
241                outf<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
242             }
243            outf.close();
244         }
245         if(Dif==3)                                                                            //读取 困难扫雷 的存储文件
246         {
247             string Name;
248            string name[6];
249            double Time,rang;
250            double times[6];
251            int i=0;
252            ifstream inf("扫雷 困难.txt");
253            for(i=0;i<5;i++)                                                                    //文件中信息导入到数组里
254             {
255                 inf>>Name;inf>>rang>>Time;
256                name[i]=Name;
257                times[i]=Time;
258             }
259             inf.close();
260             name[5]=USer;                                                                   //本轮玩家信息
261             times[5]=duration;
262            double t1=0;
263            string t2;
264             for(int j=0;j<5;j++)                                                               //冒泡排序法
265             {                    
266                 for(i=0;i<5-j;i++)
267                 {
268                     if(times[i]>times[i+1])
269                     {
270                         t1=times[i];
271                        times[i]=times[i+1];
272                        times[i+1]=t1;
273                        t2=name[i];
274                        name[i]=name[i+1];
275                        name[i+1]=t2;
276                     }
277                 }
278             }
279             ofstream outf("扫雷 困难.txt");
280            for(i=0;i<5;i++)                                                                   //将前五名玩家信息存储到文件中
281             {
282                 cout<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
283                outf<<name[i]<<"    "<<i+1<<"   "<<times[i]<<endl;
284             }
285            outf.close();
286         }
287     }
288 }
289 void scale(int dif,char *User)    //选择难度
290 {
291     int row,column;
292     if(dif==1) {row=3;column=3;}
293     if(dif==2) {row=7;column=7;}
294     if(dif==3)  {row=10;column=10;}
295     cout<<"The scale is: "<<row<<"*"<<column<<endl;
296     thunder(dif,row,column,User);
297 };
298 int main()
299 {    
300     int Continue=1;
301     int difficulty;
302     char user[10];
303     cout<<"                       Welcom to the game!                  "<<endl
304         <<"                         请输入用户名!                      "<<endl;
305     cin>>user;
306     while(Continue==1)
307     {
308        cout<<"=======================*******************======================="<<endl
309            <<"                          请选择难度!                        "<<endl
310            <<"                          简单——1                           "<<endl
311            <<"                          一般——2                           "<<endl
312            <<"                          困难——3                           "<<endl;
313        cin>>difficulty;
314        scale(difficulty,user);
315         cout<<"继续游戏——1     结束游戏——0"<<endl;
316         cin>>Continue;
317     }
318     return 0;
319 }

 

扫雷C++实现

标签:under   surf   导入   文件   ==   tin   style   clock   cin   

原文地址:http://www.cnblogs.com/yhyl/p/6591837.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!