|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include <termios.h>
- #include <string.h>
- #define SIZE 10
- static struct termios stored_settings;
- void set_keypress(void)
- {
- struct termios new_settings;
- tcgetattr(0, &stored_settings);
- new_settings = stored_settings;
- /* Disable canonical mode, and set buffer size to 1 byte */
- new_settings.c_lflag &= (~ICANON);
- new_settings.c_cc[VTIME] = 0;
- new_settings.c_cc[VMIN] = 1;
- tcsetattr(0, TCSANOW, &new_settings);
- return;
- }
- void reset_keypress(void)
- {
- tcsetattr(0, TCSANOW, &stored_settings);
- return;
- }
- void initgame();
- void drawgame();
- void playgame(int* h,int* l,int* action);
- void end();
- int win();
- int a[SIZE+2][SIZE+2]={0};//防止溢出,1-10为游戏区,0,11为辅助区
- int main()
- {
- initgame();
- int h=1,l=1,action;
- a[h][l]+=30;
- printf("hjkl移动,空格选择,g插旗");
- set_keypress();
- while(1){
- system("clear");
- printf("当前位置:%d,%d\n",h,l);
- drawgame();
- action=getchar();
- if(action=='q')break;
- playgame(&h,&l,&action);
- if(win()==1)break;
- }
- reset_keypress();
- }
- void initgame()
- {
- srand(time(0));
- int i,j,s;
- for(s=0;s<10;)
- {
- i=rand()%10+1;
- j=rand()%10+1;
- if(a[i][j]==0)
- {
- a[i][j]=-1;//埋雷
- s++;
- }
- }
- for(i=1;i<=10;i++)
- {
- for(j=1;j<=10;j++)
- {
- int o,k;
- for(o=-1;o<2;o++)
- {
- for(k=-1;k<2;k++)
- if(a[i+o][j+k]==-1&&a[i][j]!=-1)//算周围几个雷
- a[i][j]++;
- }
- }
- }
-
- for(i=1;i<=10;i++)
- {
- for(j=1;j<=10;j++)
- {
- a[i][j]+=20;
- }
- }
- }
- void drawgame()
- {
- /* 雷 -1 *
- 正常数字 0-8
- -1 - 8
- 旗帜 +10 9 -18
- 加密 +20 19-28
- 用户操作 +30 29-38 39-48 49-58
- */
- int i,j;
- for(i=1;i<=10;i++)
- {
- for(j=1;j<=10;j++)
- {
- if(a[i][j]==-1){printf("* ");}
- else if (a[i][j]<=8){printf("%-3d",a[i][j]);}
- else if (a[i][j]<=18){printf("P ");}
- else if (a[i][j]<=28){printf("# ");}
- else if (a[i][j]<=38)
- {
- switch(a[i][j]-30)
- {
- case -1:printf("* ");end();break;
- case 0:printf("⊙ ");break;
- case 1:printf("① ");break;
- case 2:printf("② ");break;
- case 3:printf("③ ");break;
- case 4:printf("④ ");break;
- case 5:printf("⑤ ");break;
- case 6:printf("⑥ ");break;
- case 7:printf("⑦ ");break;
- case 8:printf("⑧ ");break;
- }
-
- }
- else if(a[i][j]<=48)
- {
- printf("⊕ ");
- }
- else if(a[i][j]<=58)
- {
- printf("⊙ ");
- }
-
- }
- printf("\n");
- }
- }
- void playgame(int* h,int* l,int* action)
- {
- /* 雷 -1 *
- 正常数字 0-8
- -1 - 8
- 旗帜 +10 9 -18
- 加密 +20 19-28
- 用户操作 +30 29-38 39-48 49-58
- */
-
- switch(*action)
- {
- case 'h':
- if(*l>1)
- {
- a[*h][*l]-=30;
- a[*h][*l-1]+=30;
- *l-=1;
- }
- break;
- case 'l':
- if(*l<10)
- {
- a[*h][*l]-=30;
- a[*h][*l+1]+=30;
- *l+=1;
- }
- break;
- case 'j':
- if(*h<10)
- {
- a[*h][*l]-=30;
- a[*h+1][*l]+=30;
- *h+=1;
- }
- break;
- case 'k':
- if(*h>1)
- {
- a[*h][*l]-=30;
- a[*h-1][*l]+=30;
- *h-=1;
- }
- break;
- case ' ':
- if(a[*h][*l]>=49&&a[*h][*l]<=58)
- {
- a[*h][*l]-=20;
- }
- case 'g':
- if(a[*h][*l]>=49&&a[*h][*l]<=58)
- {
- a[*h][*l]-=10;
- }
- }
- }
- void end()
- {
- printf("you died");
- exit(0);
- }
- int win()
- {
- int q=0;
- int i,j;
- for(i=0;i<12;i++)
- {
- for(j=0;j<12;j++)
- {
- if(a[i][j]==9||a[i][j]==39)q++;
- }
- }
- if(q==10)
- {
- printf("you win");
- return 1;
- }
- else
- {
- q=0;
- }
- return 0;
- }
复制代码 |
|