微剋多資訊

 找回密碼
 註冊

Sign in with google

Google帳號登入

搜索
回覆 1則 瀏覽 16893篇
Line

踩地雷 RC1

該用戶從未簽到

升級   57.5%

跳轉到指定樓層
主題
發表於 2012-12-26 23:55 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. const int h=9,w=9,mt=10;//設定高,寬,地雷數量
  6. int mine[h+2][w+2],t=0,hi=0,wi=0;
  7. char state[h+2][w+2] ;
  8. void initialgame();
  9. void printgmaefield();
  10. void openspace(int hic,int wic);
  11. void checksp(int hic,int wic);
  12. void cler();
  13. void randm();
  14. void con();
  15. int main(){
  16.   srand((unsigned) time(NULL));
  17.   char k,re;
  18.   int f=0,one=0;
  19.   fflush(stdin);
  20.   for(int i=0;i<h+2;i++)
  21.     for(int j=0;j<w+2;j++)
  22.       state[i][j]='u';
  23.   printf("-----------------Program Name: Minesweeper-----------------n");
  24.   printf("--------------------Author:Chiayin Wang--------------------n");
  25.   initialgame();
  26.   while(1){
  27.     if(state[hi][wi]=='c'&&mine[hi][wi]==0){
  28.       checksp(hi-1,wi-1);
  29.     }
  30.     printgmaefield();
  31.     if(state[hi][wi]=='c'&&mine[hi][wi]==-1){//踩到地雷
  32.       printf("你踩到地雷了!!!n");
  33.       printf("-------------------------Game Over-------------------------n");
  34.       printf("--------------------Author:Chiayin Wang--------------------n");
  35.       printf("要重新開始嗎?(輸入Y/y重新開始 其他鍵結束!!!)n");
  36.       scanf("%c",&re);
  37.       if(re=='y'||re=='Y')main();

  38.       return 0;
  39.     }
  40.     f=0;
  41.     for(int p=1;p<h+1;p++)//檢查剩餘地雷
  42.       for(int q=1;q<w+1;q++)
  43.         if(state[p][q]=='c'&&mine[p][q]!=-1)f++;
  44.     if(((h*w)-f)==mt){
  45.       printf("恭喜獲勝!!!n");
  46.       printf("-------------------------Game Over-------------------------n");
  47.       printf("--------------------Author:Chiayin Wang--------------------n");
  48.       printf("要重新開始嗎?(輸入Y/y重新開始 其他鍵結束!!!)n");
  49.       fflush(stdin);
  50.       scanf("%c",&re);
  51.       if(re=='y'||re=='Y')main();
  52.       else return 0;
  53.     }

  54.     printf("n");

  55.     printf("請輸入欲操作之座標(如A1或B2):");
  56.     scanf("%c%d",&hi,&wi);
  57.     if(hi>='A'&&hi<='Z')hi-='A'-1;
  58.     else if(hi>='a'&&hi<='z') hi-='a'-1;
  59.     if(state[hi][wi]=='c'){
  60.       printf("資訊:操作無效!!!n該格已經被打開!n");
  61.     }
  62.     else{
  63.       printf("請輸入動作(C/c打開)(F/f插旗)(U/u解除插旗):");
  64.       scanf("n%c",&k);
  65.       printf("n");
  66.       if(k>='A'&&k<='Z')k+=32;
  67.       if(state[hi][wi]=='f'&&k=='c')printf("資訊:操作無效!!!n該格已經被插旗封印!n");

  68.       else state[hi][wi]=k;
  69.     }
  70.    
  71.     fflush(stdin);
  72.   }
  73.   return 0;
  74. }

  75. void play(){
  76. }

  77. void initialgame(){//前置作業
  78.   cler();
  79.   randm();
  80.   con();
  81. }
  82. void cler(){//清空地雷圖層
  83.   for(int i=0;i<h+2;i++){
  84.     for(int j=0;j<w+2;j++){
  85.       mine[i][j]=0;}
  86.   }
  87. }
  88. void randm(){//產生地雷
  89.   int hr=0,wr=0,t=0;
  90.   for(int i=1;i<(mt+1);i++){
  91.     hr=rand()%h+1;
  92.     wr=rand()%w+1;
  93.     mine[hr][wr]=-1;
  94.   }
  95.   for(int i=1;i<h+1;i++){
  96.     for(int j=1;j<w+1;j++){
  97.       if(mine[i][j]==-1){
  98.         t++;
  99.       }
  100.     }
  101.   }
  102.   if(t<mt){
  103.     cler();
  104.     randm();
  105.   }
  106. }
  107. void printgmaefield(){//印出表格
  108.   for(int i=1;i<w+1;i++)printf("   %d",i);
  109.   printf("n ┌");
  110.   for(int i=1;i<w;i++)printf("─┬");
  111.   printf("─┐n");
  112.   for(int i=0;i<h;i++){
  113.     printf("%c│",i+65);
  114.     for(int j=0;j<w;j++){
  115.       if(state[i+1][j+1]=='u'){
  116.         printf("■│");
  117.       }
  118.       if(state[i+1][j+1]=='f'){
  119.         printf("◤│");
  120.       }
  121.       if(state[i+1][j+1]=='c'&&mine[i+1][j+1]==0){
  122.         printf(" │");
  123.       }else if(state[i+1][j+1]=='c'&&mine[i+1][j+1]==-1){
  124.         printf("●│");
  125.       }else if(state[i+1][j+1]=='c')
  126.       {
  127.         printf("%2d│",mine[i+1][j+1]);
  128.       }
  129.     }
  130.     printf("n");
  131.     if(i<h-1){
  132.       printf(" ├");
  133.       for(int j=0;j<w-1;j++)
  134.       {
  135.         printf("─┼");
  136.       }
  137.       printf("─┤n");
  138.     }
  139.   }
  140.   printf(" └");
  141.   for(int i=1;i<w;i++)printf("─┴");
  142.   printf("─┘n");
  143. }
  144. void con(){//計算炸彈數量
  145.   for(int i=0;i<h+2;i++){
  146.     for(int j=0;j<w+2;j++){
  147.       if(mine[i][j]==-1){
  148.         if(mine[i+1][j]!=-1){
  149.           mine[i+1][j]++;
  150.         }
  151.         if(mine[i][j+1]!=-1){
  152.           mine[i][j+1]++;
  153.         }

  154.         if(mine[i-1][j]!=-1){
  155.           mine[i-1][j]++;
  156.         }
  157.         if(mine[i][j-1]!=-1){
  158.           mine[i][j-1]++;
  159.         }
  160.         if(mine[i+1][j+1]!=-1){
  161.           mine[i+1][j+1]++;
  162.         }
  163.         if(mine[i-1][j-1]!=-1){
  164.           mine[i-1][j-1]++;
  165.         }
  166.         if(mine[i+1][j-1]!=-1){
  167.           mine[i+1][j-1]++;
  168.         }
  169.         if(mine[i-1][j+1]!=-1){
  170.           mine[i-1][j+1]++;
  171.         }
  172.       }  
  173.     }
  174.   }
  175. }
  176. void checksp(int hic,int wic){//檢查空白
  177.   int tt=0;
  178.   openspace(hic,wic);
  179.   while(h*w){
  180.     for(int i=0;i<h+1;i++){
  181.       for(int j=0;j<w+1;j++){
  182.         if(state[i+1][j+1]=='c'&&mine[i+1][j+1]==0)openspace(i,j);
  183.         
  184.       }
  185.     }
  186.     tt++;
  187.   }

  188. }
  189. void openspace(int hic, int wic){//展開空白
  190.   state[hic+2][wic+1]='c';
  191.   state[hic][wic+1]='c';
  192.   state[hic+1][wic+2]='c';

  193.   state[hic+1][wic]='c';
  194.   state[hic+2][wic+2]='c';
  195.   state[hic][wic+2]='c';
  196.   state[hic+2][wic]='c';
  197.   state[hic][wic]='c';
  198. }
複製代碼

本帖子中包含更多資源

您需要 登入 才可以下載或查看,沒有帳號?註冊

x
樓主熱門主題

該用戶從未簽到

升級   0%

2F
發表於 2012-12-29 16:00 | 只看該作者
呵呵,真是有趣的小程式,才短短200行,還是命令視窗版的

使用道具

您需要登入後才可以回帖 登入 | 註冊

本版積分規則

小黑屋|Archiver|微剋多資訊(MicroDuo)

GMT+8, 2024-4-19 10:57

Discuz! X

© 2009-2023 Microduo

快速回覆 返回頂部 返回列表