鱼C论坛

 找回密码
 立即注册
查看: 2303|回复: 0

[技术交流] 扫雷 linux版

[复制链接]
发表于 2019-10-29 23:11:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <termios.h>
  5. #include <string.h>
  6. #define SIZE 10

  7. static struct termios stored_settings;

  8. void set_keypress(void)
  9. {
  10.         struct termios new_settings;

  11.         tcgetattr(0, &stored_settings);

  12.         new_settings = stored_settings;

  13.         /* Disable canonical mode, and set buffer size to 1 byte */
  14.         new_settings.c_lflag &= (~ICANON);
  15.         new_settings.c_cc[VTIME] = 0;
  16.         new_settings.c_cc[VMIN] = 1;

  17.         tcsetattr(0, TCSANOW, &new_settings);
  18.         return;
  19. }

  20. void reset_keypress(void)
  21. {
  22.         tcsetattr(0, TCSANOW, &stored_settings);
  23.         return;
  24. }
  25. void initgame();
  26. void drawgame();
  27. void playgame(int* h,int* l,int* action);
  28. void end();
  29. int win();

  30. int a[SIZE+2][SIZE+2]={0};//防止溢出,1-10为游戏区,0,11为辅助区

  31. int main()
  32. {
  33.         initgame();
  34.         int h=1,l=1,action;
  35.         a[h][l]+=30;
  36.         printf("hjkl移动,空格选择,g插旗");
  37.         set_keypress();
  38.         while(1){
  39.         system("clear");
  40.         printf("当前位置:%d,%d\n",h,l);
  41.         drawgame();
  42.         action=getchar();
  43.         if(action=='q')break;
  44.         playgame(&h,&l,&action);
  45.         if(win()==1)break;
  46.         }
  47.         reset_keypress();
  48. }

  49. void initgame()
  50. {
  51.         srand(time(0));
  52.         int i,j,s;
  53.         for(s=0;s<10;)
  54.         {
  55.                 i=rand()%10+1;
  56.                 j=rand()%10+1;
  57.                 if(a[i][j]==0)
  58.                 {
  59.                         a[i][j]=-1;//埋雷
  60.                         s++;
  61.                 }
  62.         }
  63.         for(i=1;i<=10;i++)
  64.         {
  65.                 for(j=1;j<=10;j++)
  66.                 {
  67.                         int o,k;
  68.                         for(o=-1;o<2;o++)
  69.                         {
  70.                          for(k=-1;k<2;k++)
  71.                                                         if(a[i+o][j+k]==-1&&a[i][j]!=-1)//算周围几个雷
  72.                             a[i][j]++;
  73.                          }
  74.                         }
  75.                 }
  76.         
  77.         for(i=1;i<=10;i++)
  78.         {
  79.                 for(j=1;j<=10;j++)
  80.                 {
  81.                         a[i][j]+=20;
  82.                 }
  83.         }
  84. }

  85. void drawgame()
  86. {
  87.         /* 雷 -1 *
  88.            正常数字 0-8
  89.            -1 - 8
  90.            旗帜 +10         9 -18
  91.            加密     +20 19-28
  92.            用户操作 +30 29-38      39-48  49-58
  93.         */
  94.         int i,j;
  95.         for(i=1;i<=10;i++)
  96.         {
  97.         for(j=1;j<=10;j++)
  98.         {
  99.         if(a[i][j]==-1){printf("*  ");}
  100.         else if (a[i][j]<=8){printf("%-3d",a[i][j]);}
  101.         else if (a[i][j]<=18){printf("P  ");}
  102.         else if (a[i][j]<=28){printf("#  ");}
  103.         else if (a[i][j]<=38)
  104.         {
  105.                 switch(a[i][j]-30)
  106.                 {       
  107.                         case -1:printf("*  ");end();break;
  108.                         case 0:printf("⊙ ");break;
  109.                         case 1:printf("① ");break;
  110.                         case 2:printf("② ");break;
  111.                         case 3:printf("③ ");break;
  112.                         case 4:printf("④ ");break;
  113.                         case 5:printf("⑤ ");break;
  114.                         case 6:printf("⑥ ");break;
  115.                         case 7:printf("⑦ ");break;
  116.                         case 8:printf("⑧ ");break;
  117.                                 }
  118.                        
  119.         }
  120.         else if(a[i][j]<=48)
  121.         {
  122.                 printf("⊕ ");
  123.                 }
  124.                 else if(a[i][j]<=58)
  125.                 {
  126.                         printf("⊙ ");
  127.                 }
  128.         
  129.                 }
  130.         printf("\n");
  131.         }

  132. }

  133. void playgame(int* h,int* l,int* action)
  134. {
  135.         /* 雷 -1 *
  136.            正常数字 0-8
  137.            -1 - 8
  138.            旗帜 +10         9 -18
  139.            加密     +20 19-28
  140.            用户操作 +30 29-38      39-48  49-58
  141.         */
  142.        
  143.         switch(*action)
  144.         {
  145.                 case 'h':
  146.                         if(*l>1)
  147.                         {
  148.                                 a[*h][*l]-=30;
  149.                                 a[*h][*l-1]+=30;
  150.                                 *l-=1;
  151.                         }
  152.                         break;
  153.                 case 'l':
  154.                         if(*l<10)
  155.                         {
  156.                                 a[*h][*l]-=30;
  157.                                 a[*h][*l+1]+=30;
  158.                                 *l+=1;
  159.                         }
  160.                         break;
  161.                 case 'j':
  162.                         if(*h<10)
  163.                         {
  164.                                 a[*h][*l]-=30;
  165.                                 a[*h+1][*l]+=30;
  166.                                 *h+=1;
  167.                         }
  168.                         break;
  169.                 case 'k':
  170.                         if(*h>1)
  171.                         {
  172.                                 a[*h][*l]-=30;
  173.                                 a[*h-1][*l]+=30;
  174.                                 *h-=1;
  175.                         }
  176.                         break;
  177.                 case ' ':
  178.                         if(a[*h][*l]>=49&&a[*h][*l]<=58)
  179.                         {
  180.                                 a[*h][*l]-=20;
  181.                         }
  182.                 case 'g':
  183.                         if(a[*h][*l]>=49&&a[*h][*l]<=58)
  184.                         {
  185.                                 a[*h][*l]-=10;
  186.                         }
  187.         }
  188. }

  189. void end()
  190. {
  191.         printf("you died");
  192.         exit(0);
  193. }

  194. int win()
  195. {
  196.         int q=0;
  197.         int i,j;
  198.         for(i=0;i<12;i++)
  199.         {
  200.                 for(j=0;j<12;j++)
  201.                         {
  202.                                 if(a[i][j]==9||a[i][j]==39)q++;
  203.                         }
  204.         }
  205.         if(q==10)
  206.         {
  207.                 printf("you win");
  208.                 return 1;
  209.         }
  210.         else
  211.         {
  212.                 q=0;
  213.         }
  214.         return 0;
  215. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-7-6 13:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表